Install Nginx Web Server on Debian 8
Updated by Phil Zona
Nginx is a lightweight, high-performance web server designed for 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.
NoteThis guide is written for a non-root user. Commands that require elevated privileges are prefixed with
sudo. If you’re not familiar with the
sudocommand, you can check our Users and Groups guide.
Before You Begin
To check your hostname run:
hostname hostname -f
The first command should show the short hostname, and the second should show the fully qualified domain name (FQDN).
Update your system:
sudo apt-get update && sudo apt-get upgrade
From Debian Repositories
The simplest way to install nginx on a server is to download it from Debian’s repositories.
Install nginx and start the daemon:
sudo apt-get install nginx
Installing nginx from the Debian repositories ensures that nginx has been tested and will optimally run on Debian. However, Debian repositories are often a few versions behind the latest nginx release.
Nginx can be tested by navigating to your FQDN in your browser. The default nginx page should be present.
From Nginx Package Repository
This method differs from the one above in that it installs from the official nginx repository rather than use the package provided by Debian. Follow these steps if you would like to install the latest stable version of nginx.
/etc/apt/sources.list.d/nginx.listfile, which instructs the package manager to download packages from the nginx repositories:
Add the following lines to the file:
deb http://nginx.org/packages/debian/ jessie nginx deb-src http://nginx.org/packages/debian/ jessie nginx
Download the PGP key used to sign the packages in the nginx repository and import it into your keyring:
curl http://nginx.org/keys/nginx_signing.key | apt-key add -
Update your list of available packages:
Instruct the package manager to install the nginx package:
apt-get install nginx
The Debian project does not track the latest development of the nginx server. Consequently, nginx can be downloaded and installed from a source distribution if you require a newer version.
Install nginx dependencies:
sudo apt-get install libpcre3-dev build-essential libssl-dev
The source files and binaries will be downloaded to the
/opt/directory. Navigate to
Download the latest stable version of nginx, which can be found on its website. At the time of this publication, nginx 1.11.2 is the latest stable version:
sudo wget http://nginx.org/download/nginx-1.11.2.tar.gz
Extract the file, then navigate to the new directory:
sudo tar -zxvf nginx-1.*.tar.gz cd /nginx-1.*
Configure the build options. You may also wish to install additional modules and specify additional settings in this step, depending on your needs:
sudo ./configure --prefix=/opt/nginx --user=nginx --group=nginx --with-http_ssl_module --with-ipv6
When this finishes running, it will output configuration information:
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"
Build and install nginx with the above configuration:
sudo make sudo make install
Nginx is now installed in
As the root user, create a user and group for nginx:
sudo adduser --system --no-create-home --disabled-login --disabled-password --group nginx
Create a systemd service script to run nginx:
1 2 3 4 5 6 7 8 910111213141516
[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
NoteThis script assumes that you used the build configuration options specified in Step 5. If your script is not working correctly, be sure that the path in the line beginning with
PIDFilematches your PID file, and the path in lines beginning with
Execmatch your binary file. These file paths can be found in the output when you configured your build options.
Change the ownership of the script:
sudo chmod +x /lib/systemd/system/nginx.service
sudo systemctl start nginx
Optionally, you can enable it to start automatically on boot:
sudo systemctl enable nginx
Continue reading our introduction to Basic nginx Configuration for more helpful information about using and setting up a web server.
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.
- How to Install Nginx on Debian 7 (Wheezy)
- Websites with nginx on Debian 6 (Squeeze) - Deprecated
- Websites with nginx on Debian 5 (Lenny) - Deprecated
- Installing McMyAdmin for Minecraft on Debian
- Installing Nginx on Ubuntu 12.04 LTS (Precise Pangolin) - Deprecated
This guide is published under a CC BY-ND 3.0 license.