How to compile from source and install Nginx in a Debian 7 (Wheezy) VPS

The following article is all about how to compile and install the blazing fast HTTP server known as Nginx.

So what exactly is Nginx?

It is lightweight, free, open-source and high-performance HTTP server, which, unlike most other HTTP servers, does not rely on threaded handling of the requests but instead, uses a much more scalable and robust event driven (asynchronous) architecture.

This uses a very small and predictable amount of memory under heavy load.

Anyway, if you are looking for instructions on how to set-up and install Nginx, PHP-FPM and MySQL (LNMP Stack) in Debian 6 (Squeeze) than please read our article on How to install and configure LEMP (Nginx, MySQL and PHP) server on a Debian 6 (squeeze) VPS

For this tutorial we are using one of our Debian virtual servers. Ok, first thing to do is to make sure our Debian 7 (Wheezy) system is up-to-date:

# apt-get update
# apt-get upgrade
# apt-get dist-upgrade

Next, install some necessary packages so we can build our Nginx:

# apt-get install build-essential
# apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev

Proceed with downloading and extracting Nginx sources from http://nginx.org/en/download.html

# mkdir ~/sources
# cd ~/sources/
# wget http://nginx.org/download/nginx-1.4.0.tar.gz
# tar zxf nginx-1.4.0.tar.gz
# cd nginx-1.4.0

run the ‘configure’ script to configure the sources. see `./configure –help` for more information on what you can use.

# ./configure \
	--prefix=/usr \
	--conf-path=/etc/nginx/nginx.conf \
	--error-log-path=/var/log/nginx/error.log \
	--http-log-path=/var/log/nginx/access.log \
	--pid-path=/var/run/nginx.pid \
	--lock-path=/var/lock/nginx.lock \
	--with-http_ssl_module \
	--user=www-data \
	--group=www-data \
    --with-http_stub_status_module \
	--with-http_gzip_static_module \
	--without-mail_pop3_module \
	--without-mail_imap_module \

proceed with compiling and installing Nginx:

# make
# make install

once the installation is completed, add the following init script to /etc/init.d/nginx so you can manage your nginx installation:

#! /bin/sh

# 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


test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx

set -e

. /lib/lsb/init-functions

case "$1" in
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1

exit 0

make the init script executable and add Nginx to your system’s start-up applications:

# chmod +x /etc/init.d/nginx
# update-rc.d -f nginx defaults

set-up Nginx:

# mkdir /etc/nginx/{sites-available,sites-enabled}

in /etc/nginx/nginx.conf add the following:

user  www-data;
worker_processes  2;

events {
    worker_connections  1024;

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/sites-enabled/*;

add the server block in /etc/nginx/sites-available/example.com (make sure to change any occurrences of example.com with your desired domain).

server {
    listen       80;
    server_name  example.com;

    location / {
        root   /var/www/example.com;
        index  index.html index.htm;

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;


enable the server block you’ve just added:

# cd /etc/nginx/sites-enabled/
# ln -s /etc/nginx/sites-available/example.com

restart your Nginx by using:

# service nginx restart

add a sample page and test your Nginx by opening your domain you used in the server block.

# mkdir -p /var/www/example.com

# echo -e "<html>\n<head><title>NGINX</title></head>\n \
<body>\t<h1>Powered by RoseHosting.com</h1>\n</body>\n</html>" \
> /var/www/example.com/index.html

Of course you don’t have to do any of this if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert linux admins to install this for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.


Categories: Tutorials

View Comments

Leave a Comment