When I first looked at how old the nginx package in the debian repo was I thought that I should compile nginx. However, I eventually managed to find a more recent repo which I will post later.
Download nginx and install dependencies
We have to download the nginx source and perhaps a few modules. But before we can install this the system will need a few dependencies installed.
Download nginx from the website (http://wiki.nginx.org/Main)
wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz tar -C /tmp nginx-0.7.64.tar.gz
Download addition modules (http://wiki.nginx.org/Nginx3rdPartyModules)
i.e. Headers more module:
wget http://github.com/agentzh/headers-more-nginx-module/tarball/v0.01 tar -C /tmp agentzh-headers-more-nginx-mdule-c131b08.tar.gz
We need to check for any dependencies and install them if needed. Therefore run:
aptitude show nginx
This will show you the list of dependencies. Next is to install them:
apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-dev lsb-base
Configure and compiling nginx of debian
We can now go ahead and compile nginx on debian. The first thing to do is to configure the installation. Below I have set most of the options that will place nginx and its config/log files in similar locations to what your used to.
cd /tmp/nginx-0.7.64 ./configure \ --sbin-path=/usr/sbin \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --user=www-data \ --group=www-data \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gzip_static_module \ --with-http_realip_module \ --with-http_stub_status_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --add-module=/tmp/agentzh-headers-more-nginx-module-c131b08
Once the configure script has finished we can run make and install nginx
make && make install
Create the nginx init script
Just like all other services that you installed, we can create a nginx startup script. Create the file /etc/init.d/nginx and add the following:
#! /bin/sh
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'sudo update-rc.d nginx defaults', or use the appropriate command on your
# distro.
#
# Author: Ryan Norbauer
# Modified: Geoffrey Grosenbach http://topfunky.com
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/sbin/$NAME
CONFIGFILE=/etc/nginx/nginx.conf
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
$DAEMON -c $CONFIGFILE || echo -n " already running"
}
d_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
}
d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# One second might not be time enough for a daemon to stop,
# if this happens, d_start will fail (and dpkg will break if
# the package is being upgraded). Change the timeout if needed
# be, or change d_stop to have start-stop-daemon use --retry.
# Notice that using --retry slows down the shutdown process somewhat.
sleep 5
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
exit 0
Make the nginx startup script executable so that we can run it
chmod +x /etc/init.d/nginx
Finally we can make this script start on system startup.
/usr/sbin/update-rc.d -f nginx defaults
Configuring virtual hosts on nginx
We have to create a few directories to hold each virtual host and log files:
mkdir /etc/nginx/sites-available/ mkdir /etc/nginx/sites-enabled/
Configure the nginx server by opening the config file
nano /etc/nginx/nginx.conf
Replace the text with the following:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
I like to store my virtual hosts in /var/www/vhosts/host.com/httpdocs. Therefore create the directories:
mkdir /var/www/vhosts/host.com/httpdocs mkdir /var/www/vhosts/host.com/logs
We can now create a virtual host template. Therefore create the following file /etc/nginx/sites-available/test.com and insert the content below making sure you change host.com to your domain name
# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts
server {
listen 80;
server_name host.com;
access_log /var/www/vhosts/host.com/logs/access.log;
error_log /var/www/vhosts/host.com/logs/error.log;
location / {
root /var/www/vhosts/host.com/httpdocs;
index index.html;
}
location /doc {
root /usr/share;
autoindex on;
allow 127.0.0.1;
deny all;
}
location /images {
root /usr/share;
autoindex on;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#if (!-e $request_filename) {
# rewrite ^(.*)$ /index.php?q=$1 last;
# break;
#}
# try_files $uri $uri/ /wordpress/index.php?q=$uri;
#location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /var/www/vhosts/test/httpdocs$fastcgi_script_name;
#include /etc/nginx/fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#listen 8000;
#listen somename:8080;
#server_name somename alias another.alias;
#location / {
#root html;
#index index.html index.htm;
#}
#}
# HTTPS server
#
#server {
#listen 443;
#server_name localhost;
#ssl on;
#ssl_certificate cert.pem;
#ssl_certificate_key cert.key;
#ssl_session_timeout 5m;
#ssl_protocols SSLv2 SSLv3 TLSv1;
#ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
#ssl_prefer_server_ciphers on;
#location / {
#root html;
#index index.html index.htm;
#}
#}
Create a symlink which will make our virtual host config live:
ln -s /etc/nginx/sites-available/test /etc/nginx/sites-enabled/test
Finally we can now start the nginx server
/etc/init.d/nginx start
Now that we have nginx running, we need to make sure this service is always started. Therefore lets add it to our default run levels
update-rc.d -f nginx defualts
Hopefully you will now have the nginx server up and running. The next few blog posts will show how to incorporate PHP with nginx and to get both wordpress and symfony projects working.
