How to Install Nginx on Debian 9

Updated on

3 min read

Install Nginx on Debian 9

Nginx is an open-source, high-performance HTTP and reverse proxy server that powers some of the largest sites on the Internet.

Nginx can be used as a standalone web server, and as a reverse proxy for Apache and other web servers.

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.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges .

Install Nginx

The installation is pretty straightforward. Follow the steps below to install Nginx on your Debian system:

  1. Update the packages index:

    sudo apt update
  2. Install the Nginx package:

    sudo apt install nginx
  3. Nginx service will automatically start after the installation process is complete. You can verify it by running the following curl command:

    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 (443) ports.

Open the necessary ports by issuing the following command:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPTsudo 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

To restart the Nginx service :

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 /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • Server block (vhost) configuration files are stored in /etc/nginx/sites-available directory. The configuration files found in this directory are not used by Nginx unless they are linked to the /etc/nginx/sites-enabled directory.
  • Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a sites-available directory to the sites-enabled directory.
  • 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.com then the configuration file should be named /etc/nginx/sites-available/mydomain.com.conf.
  • The /etc/nginx/snippets directory 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 (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log 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:
    • /home/<user_name>/<site_name>
    • /var/www/<site_name>
    • /var/www/html/<site_name>
    • /opt/<site_name>

Conclusion

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.

This post is a part of the How to Install LEMP Stack on Debian 9 series.
Other posts in this series: