How to Install Nginx on Ubuntu Server & Setting Up Server Blocks.
Step 1 – Installing Nginx
Nginx is available in Ubuntu’s default repositories, let us install it from these repositories using the apt
packaging system. First, we will update our local package index so that we have access to the most recent package listings and install nginx
right after:
sudo apt update && sudo apt install nginx
Step 2 – Checking your Web Server
At the end of the installation process, Ubuntu 20.04 should starts Nginx. The web server should already be up and running.
We can check with the systemd
init system to make sure the service is running by typing:
systemctl status nginx
output
● 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 Wed 2021-01-20 15:17:29 UTC; 1 day 17h ago
Docs: man:nginx(8)
Process: 521332 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload>
Main PID: 5205 (nginx)
Tasks: 6 (limit: 2102)
CGroup: /system.slice/nginx.service
├─ 5205 nginx: master process /usr/sbin/nginx -g daemon on; master_process >
├─521344 nginx: worker process
├─521345 nginx: worker process
├─521346 nginx: worker process
├─521347 nginx: worker process
└─521348 nginx: cache manager process
Press q
to exit the service status.
Step 3 – Adjusting the Firewall
Before testing Nginx, the firewall software needs to be adjusted to allow access to the service. Nginx registers itself as a service with ufw
upon installation, making it straightforward to allow Nginx access.
List the application configurations that ufw
knows how to work with by typing:
sudo ufw app list
You should get a listing of the application profiles:
output
Available applications:
Apache
Apache Full
Apache Secure
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH
Postfix
Postfix SMTPS
Postfix Submission
As you can see by the output, there are three profiles available for Nginx:
- Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
- Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
- Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)
It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. For now, since we are not on an encrypted server, we will only allow the Nginx HTTP profile that will allow traffic on port 80.
We enable this by typing:
sudo ufw allow 'Nginx HTTP'
reload the firewall for the changes to persist.
sudo ufw reload
check the status of the firewall to verify the profiles that have been allowed.
output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Step 4 – Testing the Nginx on Ubuntu 20.04
On your browser’s URL field, enter your server’s IP address or domain name and hit ENTER.
http://server-IP or domain-name
You should get a default Nginx welcome page as below, your server is running correctly and is ready to be managed.
Step 5 – Managing the Nginx Process
To stop your web server, type:
sudo systemctl stop nginx
To start the webserver when it is stopped, type:
sudo systemctl stop nginx
To stop and then start the service again, type:
sudo systemctl restart nginx
If you are only making configuration changes, Nginx can reload without dropping connections, type:
sudo systemctl reload nginx
By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:
sudo systemctl disable nginx
To re-enable the service to start up at boot, type:
sudo systemctl enable nginx
Step 6 – Setting Up Server Blocks
When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. For the purposes of this guide, we will make a Server Block for domain_test1.com and another for domain_test2.com
Step 6.1 - Create Directories and Set Permissions
Let’s create two new directories in the /var/www/
directory for our two domains.
sudo mkdir -p /var/www/domain_test1.com/public_html
sudo mkdir -p /var/www/domain_test2.com/public_html
Next, assign ownership of the directory with the $USER
environment variable:
sudo chown -R $USER:$USER /var/www/domain_test1.com/public_html
sudo chown -R $USER:$USER /var/www/domain_test2.com/public_html
The $USER
variable will take the value of the user you are currently logged in as.
To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
sudo chmod -R 755 /var/www
Step 6.2 - Create Test Web Pages
Let us now create a very simple index.html
web page for each domain using using nano
or your favorite editor:
test1
sudo nano /var/www/domain_test1.com/public_html/index.html
test2
sudo nano /var/www/domain_test2.com/public_html/index.html
Inside, add the following sample HTML to both:
<html>
<head>
<title>Welcome to domain_test1!</title>
</head>
<body>
<h1>Success! The domain_test1 server block is working!</h1>
</body>
</html>
Save and close the file by typing CTRL
and X
then Y
and ENTER
when you are done.
Step 6.3 - Create Server Blocks
In order for Nginx to serve this content, it’s necessary to create a server block with the correct directives. Instead of modifying the default configuration file directly, let’s make two new one.
sudo nano /etc/nginx/sites-available/domain_test1
AND
sudo nano /etc/nginx/sites-available/domain_test2
Paste in the following configuration block, updated for our new directory and domain name:
server {
listen 80;
listen [::]:80;
root /var/www/domain_test1.com/public_html/index.html;
index index.html index.htm index.nginx-debian.html;
server_name domain_test1.com www.domain_test1.com;
location / {
try_files $uri $uri/ =404;
}
}
Save and exit (press CTRL
+ X
, press Y
and then press ENTER
)
Ensure the Nginx config file syntax is valid before continuing to the next step.
sudo nginx -t
OUTPUT
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Step 7 - Create Symbolic Links
Now, let’s enable symbolic links from the sites-available
directory to the sites-enabled
directory, which Nginx reads from during startup:
sudo ln -s /etc/nginx/sites-available/domain_test1.com /etc/nginx/sites-enabled/
AND
sudo ln -s /etc/nginx/sites-available/domain_test2.com /etc/nginx/sites-enabled/
We can also remove the symbolic link for the default server block.
sudo rm /etc/nginx/sites-enabled/default
Restart Nginx.
sudo systemctl nginx restart
Nginx should now be serving your domain name. You can test this by navigating to the domain_test1.com
or domain_test2.com
Step 7 - Important Nginx Files and Directories
Now that you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.
Content
/var/www/public_html
: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the/var/www/public_html
directory. This can be changed by altering Nginx configuration files.
Server Configuration
/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 the Nginx global configuration./etc/nginx/sites-available/
: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to thesites-enabled
directory. Basically, all server block configuration is done in this directory and then enabled by linking to the other directory./etc/nginx/sites-enabled/
: The directory where enabled per-site server blocks are stored. Basically, these are created by linking to configuration files found in thesites-available
directory./etc/nginx/snippets
: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.
Server Logs
/var/log/nginx/access.log
: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise./var/log/nginx/error.log
: Any Nginx errors will be recorded in this log.
Thanks for reading.
And have yourself a great day!