This guide walks through the steps of setting up nginx on a test server for use with a WordPress site. This guide assumes you already have Ubuntu 12.04 installed.
For this guide we will be setting up nginx from source. First we need to install the pre-requisites.
1
| sudo apt-get install libpcre3-dev build-essential libssl-dev |
sudo apt-get install libpcre3-dev build-essential libssl-dev
Next you will need to download the source from the nginx download page For this article we will assume the current version is 1.2.8.
1
2
3
4
5
| cd /opt/
sudo wget http://nginx.org/download/nginx-1.2.8.tar.gz
sudo tar xvzf nginx-1.2.8.tar.gz
cd /opt/nginx-1.2.8/
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module |
cd /opt/
sudo wget http://nginx.org/download/nginx-1.2.8.tar.gz
sudo tar xvzf nginx-1.2.8.tar.gz
cd /opt/nginx-1.2.8/
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module
After the the configure step please review the location of the configuration files. It should be displayed in the output of the configure command. After that we can compile and install it.
1
2
| sudo make
sudo make install |
sudo make
sudo make install
Now we need to create the user for the nginx process.
1
| sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx |
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
Next we need to create a startup script in /etc/init.d/nginx
1
| sudo vi /etc/init.d/nginx |
sudo vi /etc/init.d/nginx
Then add the following to the bash script.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| #! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0 |
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/opt/nginx/sbin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/opt/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /opt/nginx/logs/$NAME.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac
exit 0
Next we’ll make the script executable and have it run at startup.
1
2
| sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults |
sudo chmod +x /etc/init.d/nginx
sudo /usr/sbin/update-rc.d -f nginx defaults
Now start the server
1
| sudo /etc/init.d/nginx start |
sudo /etc/init.d/nginx start
Now we need to configure PHP using FastCGI. Execute the following commands install FastCGI for PHP.
1
| sudo apt-get install php5-cli php5-cgi psmisc spawn-fcgi |
sudo apt-get install php5-cli php5-cgi psmisc spawn-fcgi
Create the following file using your favoriate linux/unix text editor at /usr/bin/php-fastcgi.
1
| sudo vi /usr/bin/php-fastcgi |
sudo vi /usr/bin/php-fastcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| #!/bin/sh
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then
FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then
FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then
FASTCGI_USER=http
else
# Set the FASTCGI_USER variable below to the user that
# you want to run the php-fastcgi processes as
FASTCGI_USER=
fi
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi
/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5 |
#!/bin/sh
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then
FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then
FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then
FASTCGI_USER=http
else
# Set the FASTCGI_USER variable below to the user that
# you want to run the php-fastcgi processes as
FASTCGI_USER=
fi
SOCKET=/var/run/php-fastcgi/php-fastcgi.socket
PIDFILE=/var/run/php-fastcgi/php-fastcgi.pid
CHILDREN=6
PHP5=/usr/bin/php5-cgi
/usr/bin/spawn-fcgi -s $SOCKET -P $PIDFILE -C $CHILDREN -u $FASTCGI_USER -g $FASTCGI_GROUP -f $PHP5
At this point you want to make sure you have a folder in /var/run/php-fastcgi. If not you will need to create it and give the nginx user permission to it.
1
2
| sudo mkdir /var/run/php-fastcgi
sudo chown nginx:root /var/run/php-fastcgi |
sudo mkdir /var/run/php-fastcgi
sudo chown nginx:root /var/run/php-fastcgi
Now we will create the init script for fastcgi. Create the file /etc/init.d/php-fastcgi.
1
| sudo vi /etc/init.d/php-fastcgi |
sudo vi /etc/init.d/php-fastcgi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| #!/bin/bash
### BEGIN INIT INFO
# Provides: php-fastcgi
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then
FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then
FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then
FASTCGI_USER=http
else
# Set the FASTCGI_USER variable below to the user that
# you want to run the php-fastcgi processes as
FASTCGI_USER=
fi
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
sudo -u $FASTCGI_USER $PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
sudo -u $FASTCGI_USER $PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL |
#!/bin/bash
### BEGIN INIT INFO
# Provides: php-fastcgi
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
if [ `grep -c "nginx" /etc/passwd` = "1" ]; then
FASTCGI_USER=nginx
elif [ `grep -c "www-data" /etc/passwd` = "1" ]; then
FASTCGI_USER=www-data
elif [ `grep -c "http" /etc/passwd` = "1" ]; then
FASTCGI_USER=http
else
# Set the FASTCGI_USER variable below to the user that
# you want to run the php-fastcgi processes as
FASTCGI_USER=
fi
PHP_SCRIPT=/usr/bin/php-fastcgi
RETVAL=0
case "$1" in
start)
sudo -u $FASTCGI_USER $PHP_SCRIPT
RETVAL=$?
;;
stop)
killall -9 php5-cgi
RETVAL=$?
;;
restart)
killall -9 php5-cgi
sudo -u $FASTCGI_USER $PHP_SCRIPT
RETVAL=$?
;;
*)
echo "Usage: php-fastcgi {start|stop|restart}"
exit 1
;;
esac
exit $RETVAL
Now we can start the new service
1
| sudo /etc/init.d/php-fastcgi start |
sudo /etc/init.d/php-fastcgi start
Then we need to add it to the init scripts so it starts up on reboot.
1
| sudo update-rc.d /etc/init.d/php-fastcgi defaults |
sudo update-rc.d /etc/init.d/php-fastcgi defaults
Now to finalize the nginx configuration. I prefer to use a separate config file per site so I modified the /opt/nginx/conf/nginx.conf file by adding this line.
1
2
3
4
5
6
7
8
9
10
11
12
| ...
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_types text/plain text/css
application/x-javascript text/xml
application/xml application/xml+rss
text/javascript;
include /opt/etc/nginx/sites-enabled/*;
server {
... |
...
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_types text/plain text/css
application/x-javascript text/xml
application/xml application/xml+rss
text/javascript;
include /opt/etc/nginx/sites-enabled/*;
server {
...
In addition I turned the gzip on, this is a test server so you may want to keep it off, but I prefer to keep my test server as close to production as I can. Next I create a config based on the site. Lets pretend it’s this site adamruggles.net.
1
| sudo vi /opt/etc/nginx/sites-available/adamruggles.net.conf |
sudo vi /opt/etc/nginx/sites-available/adamruggles.net.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| server {
listen 80;
server_name test.adamruggles.net www.adamruggles.net adamruggles.net;
access_log /srv/adamruggles.net/logs/access.log;
error_log /srv/adamruggles.net/logs/error.log;
root /srv/adamruggles.net/public;
location / {
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/adamruggles.net/public$fastcgi_script_name;
include /opt/nginx/conf/fastcgi_params;
} |
server {
listen 80;
server_name test.adamruggles.net www.adamruggles.net adamruggles.net;
access_log /srv/adamruggles.net/logs/access.log;
error_log /srv/adamruggles.net/logs/error.log;
root /srv/adamruggles.net/public;
location / {
try_files $uri $uri/ /index.php?$args;
index index.html index.htm index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/adamruggles.net/public$fastcgi_script_name;
include /opt/nginx/conf/fastcgi_params;
}
Some parts of this configuration are specifically for WordPress. If you’re not using WordPress the you can modify the rewrite rule and the location / try files. Review nginx’s documentation for your configuration.
Next we create a symlink (this is very similar to how the ubuntu config works and I like it).
1
| sudo ln -s /opt/etc/nginx/sites-available/adamruggles.net.conf /opt/etc/nginx/sites-enabled/adamruggles.net.conf |
sudo ln -s /opt/etc/nginx/sites-available/adamruggles.net.conf /opt/etc/nginx/sites-enabled/adamruggles.net.conf
You will obviously want to change all he references to adamruggles.net to whatever domain you’re building your website for. I usually use the ip address for testing servers since they’re virtualized and I haven’t setup DNS for my virtual machines.
At this point you can restart nginx so the new configuration takes affect.
1
| sudo /etc/init.d/nginx restart |
sudo /etc/init.d/nginx restart
If you encounter any issues following this guide please leave a comment and I’ll address your issue.
You must be logged in to post a comment.