I have been using nginx as a caching proxy for quite some time now, and have been very happy with the performance. I find it much easier to administer and performant. Now I’m also adding Nginx as the backend server. Here is how I manager to compile the latest version Nginx, php-fpm and a few modules.
Compiling Nginx
Download latest stable version of Nginx from http://nginx.org/en/download.html
wget http://nginx.org/download/nginx-1.6.1.tar.gz
Extract the source and make a folder called modules inside it
tar -xzf nginx-1.6.1.tar.gz cd nginx-1.6.1 mkdir modules
Download extra modules you want and store it into the newly created modules dir.
- ngx_cache_purge: I use this module to clear a single page from the caching proxy. Get it from https://github.com/FRiCKLE/ngx_cache_purge.
- nginx-ey-balancer: Allows to load balance requests across multiple servers and limit the number of concurrent connections sent to the backend. Since version 1.2 of Nginx you need to use this forked version https://github.com/msva/nginx-ey-balancer.git if you plan on using a previous version you can get it from https://github.com/ry/nginx-ey-balancer. Either versions require a patch to be applied
cd modules # get the cache purge module git clone https://github.com/FRiCKLE/ngx_cache_purge.git # the the load balancer module git clone https://github.com/msva/nginx-ey-balancer.git cd ../ # edit first line of load balancer module's Makefile to point to your build dir vim modules/nginx-ey-balancer/Makefile # apply a patch from load balancer module patch -p0 < modules/nginx-ey-balancer/patches/nginx-0.8.32.patch
Next I like to create a script called configure.sh which can be reused to recompile with updates later on
#!/bin/bash ######## # Nginx Configure Script ######## ./configure \ --sbin-path=/usr/sbin \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --http-log-path=/var/log/nginx/access.log \ --http-client-body-temp-path=/var/lib/nginx/body \ --http-proxy-temp-path=/var/lib/nginx/proxy \ --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \ --with-debug \ --with-http_stub_status_module \ --with-http_flv_module \ --with-http_ssl_module \ --with-http_dav_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-mail \ --with-mail_ssl_module \ --with-ipv6 \ --add-module=./modules/nginx-ey-balancer \ --add-module=./modules/ngx_cache_purge
Before compiling we make sure we have the needed dependencies.
sudo apt-get install build-essential \ libpcre3 libpcre3-dev libssl0.9.8 libssl-dev
We are now ready to configure and compile.
chmod +x ./configure.sh ./configure.sh make sudo make install # make some required directories sudo mkdir -p /var/lib/nginx/body sudo mkdir -p /var/lib/nginx/proxy sudo mkdir -p /var/lib/nginx/fastcgi sudo mkdir -p /var/lib/nginx/uwsgi sudo mkdir -p /var/lib/nginx/sc
Nginx is now installed you can test by issuing the following command
/usr/sbin/nginx -v # Outputs: nginx version: nginx/1.6.1
Lastly we create an init.d script as /etc/init.d/nginx
#! /bin/sh ### BEGIN INIT INFO # Provides: nginx # Required-Start: $local_fs $remote_fs $network $syslog # Required-Stop: $local_fs $remote_fs $network $syslog # 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=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/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 . /lib/lsb/init-functions test_nginx_config() { if nginx -t $DAEMON_OPTS then return 0 else return $? fi } case "$1" in start) echo -n "Starting $DESC: " test_nginx_config start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; stop) echo -n "Stopping $DESC: " start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || true echo "$NAME." ;; restart|force-reload) echo -n "Restarting $DESC: " start-stop-daemon --stop --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON || true sleep 1 test_nginx_config start-stop-daemon --start --quiet --pidfile \ /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true echo "$NAME." ;; reload) echo -n "Reloading $DESC configuration: " test_nginx_config start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \ --exec $DAEMON || true echo "$NAME." ;; configtest) echo -n "Testing $DESC configuration: " if test_nginx_config then echo "$NAME." else exit $? fi ;; status) status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $? ;; *) echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2 exit 1 ;; esac exit 0
We can now test it and register it so it’s called at the right moments to start at startup.
# make sure it's executable sudo chmod +x /etc/init.d/nginx # start nginx sudo /etc/init.d/nginx start # check that it's running and listenning sudo netstat -tlnp |grep nginx # register starup script sudo update-rc.d nginx defaults
That’s it we now have a working nginx server. Now adding php-fpm into the mix.
what if it is not a new nginx install but upgrading nginx. how to do it?
@kathoey if you’re on ubuntu/debian machine first backup your config files in /etc/nginx and then do the following commands just before doing the “make install”:
sudo /etc/init.d/nginx stop
sudo apt-get purge nginx
sudo mv /var/log/nginx /var/log/nginx.bak
once nginx has been removed you can perform the steps after the “sudo make install”