How to Install Nginx on Debian 10 Linux

Published on

3 min read

Install Nginx on Debian 10

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.

Nginx can be used as a standalone web server, and as a reverse proxy for HTTP and non-HTTP servers.

In this tutorial, we’ll explain how to install and manage Nginx on Debian 10 Buster.

Install Nginx

The Nginx package is included in the default Debian Buster repositories. The installation is pretty straightforward, just run the following commands as root or user with sudo privileges :

sudo apt updatesudo apt install nginx

Nginx service will automatically start after the installation process is complete. You can verify it with curl as shown below:

curl -I 127.0.0.1

The output will look similar to this:

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 16 Jul 2019 16:50:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Jul 2019 16:50:26 GMT
Connection: keep-alive
ETag: "5d2e0052-264"
Accept-Ranges: bytes

You can use the systemctl command to manage the Nginx service , same as any other systemd unit.

Adjust the Firewall

UFW users can open HTTP (80) and HTTPS (443) ports by enabling the ‘Nginx Full’ profile:

sudo ufw allow 'Nginx Full'

If you are using nftables to filter connections to your system, open the necessary ports by issuing the following command:

nft add rule inet filter input tcp dport {80, 443} ct state new,established counter accept

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 used by Nginx only when linked to the /etc/nginx/sites-enabled directory.
  • To activate a server blocks create a symlink (a pointer) from the configuration file in a sites-available directory to the sites-enabled directory.
  • To write more maintainable code, it’s a good idea to 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

Installing Nginx on Debian 10 is a matter of running a single command.

Now that you have installed Nginx on your Debian 10 Linux you can to start deploying your applications and use Nginx as a web or proxy server.

If you have any questions or feedback, feel free to leave a comment.

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