Nginx is a solid alternative to the popular Apache2 web server for Ubuntu servers. It is lightweight and lightning quick. Thankfully, so is the installation process.
First, you'll want to update apt repos to ensure they are up to date.
sudo apt update
Next, simply install Nginx.
sudo apt install nginx
After Nginx has been installed you will need to ensure your server's firewall (ufw) has been configured to allow traffic through to access Nginx. One neat feature of Nginx is that it automatically registers itself with ufw upon installation.
You can see a list of applications that ufw knows how to work with by using the below command.
sudo ufw app list
A list of applications should then be displayed like below.
There are three profiles available for Nginx:
- Nginx Full: Opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Nginx HTTP:Opens only port 80 (normal, unencrypted web traffic)
- Nginx HTTPS: Opens only port 443 (TLS/SSL encrypted traffic)
It is recommended to allow the most restrictive profile that allows you to still move forward with your project. For now, we will just allow basic HTTP access over port 80. You can allow this with the below command.
sudo ufw allow 'Nginx HTTP'
You can verify this by using the built-in status checker.
sudo ufw status
You should see that HTTP is allowed if your output looks as below with Nginx HTTP listed:
To Action From
-- -------- ----
Nginx HTTP ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere
Checking Status of Nginx Webserver
Once Nginx has been installed (after the apt install command has finished running) the Nginx service is automatically started. Normally, there shouldn't be any issues. However, if there is a problem after installation such as a conflicted port Nginx may fail to start.
You can check if your Nginx service is running properly by checking its status.
sudo systemctl status nginx
You should see an output like below if your Nginx service is running properly.
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2021-09-13 16:08:19 UTC; 3 days ago
Main PID: 2370 (nginx)
Tasks: 2 (limit: 1153)
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
At this point your web server is configured and ready to be utilized!
Managing the Nginx Service
Now that your web server is up and running and will be used to serve your website or web app, you may need to start, stop, reload, or restart Nginx. Let's go through some basic management commands that will be your best friend.
To stop Nginx, you would type:
sudo systemctl stop nginx
To start Nginx, you would type:
sudo systemctl start nginx
To restart Nginx, you would type:
sudo systemctl restart nginx
To reload (useful for configuration only changes that will not drop connections) Nginx, you would type:
sudo systemctl reload nginx
To disable Nginx entirely (i.e. to not have Nginx start automatically when your server starts - by default Nginx will start automatically), you would type:
sudo systemctl disable nginx
To enable Nginx after disabling it, you would type:
sudo systemctl enable nginx
Important Files and Locations
Now that you know how to install and manage the Nginx service itself, let's go over the important files and file locations used by the Nginx webserver.
- /var/www/html: The actual web content served, which by default only consists of the default Nginx page, is served out of the /var/www/html directory. This can be changed by altering the Nginx configuration files should you choose.
- /etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.
- /etc/nginx/nginx.conf: The main Nginx configuration file. This can be modified to make changes to your web server's global configuration.
- /etc/nginx/sites-available/: The directory where site-per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled directory located in the same parent direcotry. Typically, all server block configuration is done in this directory, and upon completion is enabled by linking to the other directory.
- /etc/nginx/sites-enabled/: This directory is where enabled site-per-site server blocks are stored. Typically, these are created by linking configuration files found in the sites-available directory to this directory for Nginx to serve.
- /etc/nginx/snippets: This directory contains configuration snippets that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are popular choices for snippet creation.
- /var/log/nginx/access.log: Every request to your Nginx web server is recorded in this log file. This location can be configured so that your access log file is saved elsewhere.
- /var/log/nginx/error.log: Any Nginx errors you encounter will be recorded in this log.
There you have it! Your Nginx web server is now installed and configured on your Ubuntu server. You will now be able to serve your website or web application for users.
There are plenty of other configuration options and configurations for Nginx so be sure to stay tuned for future tutorials to cover these features in more detail.
Should you have any trouble, send me a message and I'll do my best to help out.