I use Nginx on some high traffic sites as it is super light weight and incredibly fast. Just recently I hosted a site for a motorsport event on a 128MB ram, ~500Mhz virtual server. At peak the server was handling 110 requests per second. The load barely went over 0.1 and the ram usage was around 20MB!
Over the 3 days the server served 120GB from a single page which was ~300KB in size.
So in my book Nginx is great!
On the downside it is not exactly friendly to use, but once you get into it its not too bad.
This is my set of notes (expanded to make a bit more readable) on how to set up Nginx on CentOS…
All this is done as root unless otherwise noted. Make your own backups, Im not responsible for any damage, Do it under adult supervision, yada yada yada…
Depending on how you installed your system (or how it was provisioned) you may have to add a few more basics (like my image doesn’t have installed but i’m ignoring that here) or not remove some programs (like sendmail if you plan to use it for mail).
Clean up:
yum update yum install gcc yum remove cups-libs httpd sendmail
Install some needed goodies:
yum install pcre-devel zlib-devel openssl-devel
Move to (or make sure you are at) /root and create a work area:
cd /root;mkdir source;cd source
Get the source (im using a slightly outdated version here as im copy pasta’ing from my internal wiki. Feel free to use the latest and greatest).
Then extract and enter the directory:
wget http://nginx.org/download/nginx-0.7.67.tar.gz tar -zxvf nginx-0.7.67.tar.gz cd nginx-0.7.67
Ok next is a complicated one. We are going to run the configure script with the options we need. There are many more but these do well for most of my stuff including flv streaming. You can tune this to your desire if you wish.
Ill go through each option listed after the command so read on. Dont just copy paste here (plus you need to make that all one line)!
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
Ok, here comes the long explanation of that command. Most of it will be copy paste from the Nginx Wiki but you can see where I have changed the defaults to a more sensible (to me) path:
–prefix=/usr
The path relative to which all other Nginx paths will resolve. If not specified, defaults to /usr/local/nginx
–sbin-path=/usr/sbin/nginx
The path to the nginx executable. Only used for installation. If not specified defaults to <prefix>/sbin/nginx
–conf-path=/etc/nginx/nginx.conf
The default location of nginx.conf if no -c parameter is provided. If not provided, defaults to <prefix>/conf/nginx.conf
–error-log-path=/var/log/nginx/error.log
The location of the error log if not set via the “error_log” in nginx.conf. If not set, defaults to <prefix>/logs/error.log
–pid-path=/var/run/nginx/nginx.pid
The path to nginx.pid, if not set via the “pid” directive in nginx.conf. If not provided, defaults to <prefix>/logs/nginx.pid
–lock-path=/var/lock/nginx.lock
The path to the nginx.lock file. If not provided, defaults to <prefix>/logs/nginx.lock.
–with-http_ssl_module
Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL.
–with-http_flv_module
Enable ngx_http_flv_module
–with-http_gzip_static_module
Enables http_gzip_static_module
–http-log-path=/var/log/nginx/access.log
The location of the access log if not set via the “access_log” directive in nginx.conf. If not set, defaults to <prefix>/logs/access.log.
–http-client-body-temp-path=/var/tmp/nginx/client/
Set path to the http client request body temporary files. If not set, defaults to <prefix>/client_body_temp
–http-proxy-temp-path=/var/tmp/nginx/proxy/
Set path to the http proxy temporary files. If not set, defaults to <prefix>/proxy_temp
–http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
Set path to the http fastcgi temporary files. If not set, defaults to <prefix>/fastcgi_temp
Ok. If you read all of that well done! If not, meh, just use some common sense if something trips you up later on and remember that command could be why!
Onwards!
Lets make and install that bad boy!
make && make install
Woo! We now have Nginx installed. We could leave it at that but lets not. Lets create a shiny init.d script eh?
vi /etc/init.d/nginx
This is the script I use:
Fairly sure I based it on one from somewhere else on the internet but I cant find any links to it, so yeah, not 100% original work this, and ive edited my company support info out.
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
#sbin-path: /usr/sbin/nginx
#conf-path=/etc/nginx/nginx.conf
#...log-path:/var/log/nginx/
#pid-path: /var/run/nginx/nginx.pid
#lock-path: /var/lock/nginx.lock
#
#Installed and maintained by
#MY TRADING NAME
#for support please contact us at:
#SUPPORT WEB ADDRESS
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
Make the init.d script executable:
chmod +x /etc/init.d/nginx
Ok. We are good to go!
Except we should probably create a config shouldn’t we?
vi /etc/nginx/nginx.conf
This is a very basic config to get you running.
Edit at will to suit what you are doing:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80 default;
server_name _;
location / {
root /var/www/vhosts/default;
index index.htm;
}
}
}
Quick bit of path trickery:
Creates the temp location, gives it to the user nobody and created the default location for files to be served from.
mkdir /var/tmp/nginx chown nobody:nobody /var/tmp/nginx mkdir /var/www/vhosts/default -p chmod 644 /var/www/vhosts/default
You will want to drop out of root to create your pages in that location.
Finally make Nginx start on its own!
chkconfig --add nginx chkconfig nginx on
Thats it!
You should be able to control Nginx with the init.d script – for example:
/etc/init.d/nginx start /etc/init.d/nginx stop

Discussion
No comments yet.