How to Install Nginx on Debian 9

Install Nginx on Debian 9

In this tutorial we are going to learn how to install Nginx on Debian 9. We will also install and configure its prerequisites. Nginx is one of the most common open source web server software. It is widely used for load balancing, reverse proxying, media streaming and much more. The specialization of Nginx is its capability to operate on little resource and handle a high number of requests. Nginx works as an internet server, proxy, load balancer and Reverse Proxy. Today Nginx is installed on more afterward 450 million servers net, and is used by a few of the maximum traffic sites on the internet such as Dropbox and Netflix.

I recommend to use a minimal Ubuntu server setup as a basis for the tutorial, that can be a virtual or a root server image with an Debian 9 Stretch minimal install from a web hosting company or you use our minimal server tutorial to install a server from scratch.

Install Nginx on Debian 9

Step 1. First, before we install packages, it’s a good idea to update repository and software packages. Run the following command on your Debian 9 OS:

sudo apt update
sudo apt upgrade

Step 2. Installing Nginx on Debian 9 Stretch.

Nginx is available in the Debian package repositories simple. Follow this command to install Nginx:

sudo apt install nginx

Once the installation is completed, check the status and the version of the Nginx service, with:

systemctl start nginx
systemctl enable nginx

Step 3. Configure Firewall ports for Nginx.

If you use iptables to filter connections to your system, you’ll need to open HTTP (80) and HTTPS (443) ports:

sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Step 4. Verifying Apache Installation.

To verify that everything works correctly, open your browser, type your server IP address or doman name http://Your_IP_or_Your_Domain/

Step 5. Nginx Configuration File Structure.

  • 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 your 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.

Congratulation’s! You have successfully install and configured Nginx on your Debian Stretch. Thanks for using this tutorial installing Nginx web server on Debian 9 (Stretch) system.