Nginx Web Server on Debian 8

Updated by Elle Krout

Nginx is a lightweight, high performance web server designed with the purpose of delivering large amounts of static content quickly and with efficient use of system resources. In contrast to the Apache server, Nginx uses an asynchronous event-driven model which provides more predictable performance under load.

This guide is written for a non-root user. Commands that require elevated privileges are prefixed with sudo. If you’re not familiar with the sudo command, you can check our Users and Groups guide.

Before You Begin

  1. Ensure that you have followed the Getting Started and Securing Your Server guides, and the Linode’s hostname is set.

    To check your hostname run:

    1
    2
    hostname
    hostname -f
    

    The first command should show the short hostname, and the second should show the fully qualified domain name (FQDN).

  2. Update your system:

    1
    sudo apt-get update && sudo apt-get upgrade
    

Installing Nginx

Install Nginx from Repositories

The simplest way to install Nginx on a server is by downloading it from Debian’s repositories:

  1. Install Nginx and start the daemon:

    1
    sudo apt-get install nginx
    

    Installing Nginx from the Debian repositories ensures that Nginx has been tested and successfully runs at its best on Debian. However, the Debian repositories are often a few versions behind the latest Nginx release.

  2. Nginx can be tested by navigating to your FQDN in your browser. The default Nginx page should be present.

Install Nginx from a Source Distribution

The Debian project does not track the latest development of Nginx server. If you require a newer version, Nginx can be downloaded and installed from a source distribution.

  1. Install Nginx’s dependencies:

    1
    sudo apt-get install libpcre3-dev build-essential libssl-dev
    
  2. The source files and binaries will be downloaded in the /opt/ directory. Navigate to /opt/:

    1
    cd /opt/
    
  3. Download the latest version of Nginx, which can be found on their website. At the time of publication, Nginx 1.9.2 is the mainline version:

    1
    sudo wget http://nginx.org/download/nginx-1.9.2.tar.gz
    
  4. Expand the file, then navigate to the new directory:

    1
    2
    sudo tar -zxvf nginx*.tar.gz
    cd /opt/nginx-*
    
  5. Configure the build options:

    1
    sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
    

    When this finishes running, it will output configuration information:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    nginx path prefix: "/opt/nginx"
    nginx binary file: "/opt/nginx/sbin/nginx"
    nginx configuration prefix: "/opt/nginx/conf"
    nginx configuration file: "/opt/nginx/conf/nginx.conf"
    nginx pid file: "/opt/nginx/logs/nginx.pid"
    nginx error log file: "/opt/nginx/logs/error.log"
    nginx http access log file: "/opt/nginx/logs/access.log"
    nginx http client request body temporary files: "client_body_temp"
    nginx http proxy temporary files: "proxy_temp"
    nginx http fastcgi temporary files: "fastcgi_temp"
    nginx http uwsgi temporary files: "uwsgi_temp"
    nginx http scgi temporary files: "scgi_temp"
    
  6. Build and install Nginx with the above configuration:

    1
    2
    sudo make
    sudo make install
    
  7. As the root user create a user and group for Nginx:

    1
    sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
    

    Nginx is now installed in /opt/nginx.

  8. Create a script to run Nginx:

    /lib/systemd/system/nginx.service
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [Unit]
    Description=A high performance web server and a reverse proxy server
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/opt/nginx/logs/nginx.pid
    ExecStartPre=/opt/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
    ExecStart=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;'
    ExecReload=/opt/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
    ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /opt/nginx/logs/nginx.pid
    TimeoutStopSec=5
    KillMode=mixed
    
    [Install]
    WantedBy=multi-user.target
    
  9. Change the ownership of the script:

    1
    sudo chmod +x /lib/systemd/system/nginx.service
    
  10. Start Nginx:

    1
    sudo systemctl start nginx
    

Continue reading our introduction to Basic Nginx Configuration for more information about using and setting up a web server.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

This guide is published under a CC BY-ND 3.0 license.