How to Install Nginx on Debian 9
3 min read
Nginx is an open-source, high-performance HTTP and reverse proxy server that powers some of the largest sites on the Internet.
Compared to Apache, Nginx can handle a much large number of concurrent connections and has a smaller memory footprint per connection.
This tutorial will outline the steps to install and manage Nginx on a Debian machine.
Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.
The installation is pretty straightforward. Follow the steps below to install Nginx on your Debian system:
Update the packages index:
sudo apt update
Install the Nginx package:
sudo apt install nginx
Nginx service will automatically start after the installation process is complete. You can verify it by running the following
curl -I 127.0.0.1
HTTP/1.1 200 OK Server: nginx/1.10.3 Date: Mon, 27 Aug 2018 22:29:02 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 27 Aug 2018 22:27:54 GMT Connection: keep-alive ETag: "5b847aea-264" Accept-Ranges: bytes
Adjust the Firewall
If you use iptables to filter connections to your system, you’ll need to open HTTP (
80) and HTTPS (
Open the necessary ports by issuing the following command:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
Manage the Nginx service with systemctl
You can use the
systemctl command to manage the Nginx service, same as any other systemd unit.
To stop the Nginx service, run:
sudo systemctl stop nginx
To start it again, type:
sudo systemctl start nginx
sudo systemctl restart nginx
Reload the Nginx service after you have made some configuration changes:
sudo systemctl reload nginx
Disable the Nginx service to start at boot:
sudo systemctl disable nginx
And to re-enable it again:
sudo systemctl enable nginx
Nginx Configuration File’s Structure and Best Practices
- Nginx configuration files are stored in the
- The main Nginx configuration file is
- Server block (vhost) configuration files are stored in
/etc/nginx/sites-availabledirectory. The configuration files found in this directory are not used by Nginx unless they are linked to the
- Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a
sites-availabledirectory to the
- To write more maintainable code, it’s a good idea to a follow a standard naming convention. For example if your domain name is
mydomain.comthen the configuration file should be named
/etc/nginx/snippetsdirectory contains configuration snippets that can be included in the server block files. If you use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks.
- Nginx log files (
error.log) are located in the
/var/log/nginx/directory. It is recommended to have a different
errorlog files for each server block.
- You can set your domain document root directory to any location you want. The most common locations for webroot include:
Congratulations, you have successfully installed Nginx on your Debian 9 server. You’re now ready to start deploying your applications and use Nginx as a web or proxy server.