Introduction Nginx is one of the most popular web hosts in the world and is responsible for hosting some of the largest and most trafficked sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.
In this guide, you will learn how to install Nginx on your Ubuntu 18.04 server and about important Nginx files and directories.
Before
you
begin this guide, you must have a normal, non-root, user with sudo privileges and a basic firewall configured on the server. You can learn how to set up a regular user account by following our initial server setup guide for Ubuntu 18.04.
When you have an account available, log in as your non-root user to get started.
Step 1 – Installing
Nginx Since
Nginx is available in Ubuntu’s default repositories, it is possible to install it from these repositories using
the apt packaging system. Because this
may be your first interaction with the apt packaging system in this session, update the local package index so that you have access to the latest package listings. Afterward, you can
install nginx: sudo apt
- update sudo
- install Nginx
apt install nginx After agreeing to the procedure, apt will
and any required dependencies on your server
.
Step 2 – Firewall Adjustment
Before trying Nginx, the firewall software must be adjusted to allow access to the service. Nginx registers as a service with ufw upon installation, so it’s easy to allow access to Nginx.
List the
application configurations that ufw knows how to work with by typing the following
:
- List of applications sudo ufw
The result should be a list of application profiles
: OutputAvailable applications: Nginx Full Nginx HTTP Nginx HTTPS OpenSSH
This list shows three profiles available for
Nginx:
- Nginx Full
- 80 (normal, unencrypted web traffic) Nginx HTTPS: This profile opens only
- port 443 (TLS/SSL encrypted traffic)
: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic) Nginx HTTP: This profile opens only port
It is recommended that you enable the most restrictive profile that will still allow the traffic you have configured. Since you have not yet configured SSL for your server in this guide, you will only need to allow traffic on port 80.
You
can enable this by typing the following
: sudo ufw allow ‘Nginx HTTP’
Then, check the change
:
- sudo ufw status
You should receive a list of HTTP traffic allowed on the output
: OutputStatus: active To action From – – – OpenSSH ALLOW Anywhere Nginx HTTP ALLOW Anywhere OpenSSH (v6) ALLOW Anywhere (v6) Nginx HTTP (v6) ALLOW Anywhere (v6)
Now that you have added the appropriate firewall rule, you can verify that your web server is running and can serve content correctly.
Step 3 – Checking Your Web Server
At the end of the installation process, Ubuntu 18.04 launches Nginx. The web server should already be up and running.
Check with the systemd
init system to make sure the service is
running:
- systemctl status
nginx Output● nginx.service – A high-performance web server and reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset by provider: in Active: active (running) since Fri 2021-10-01 21:36:15 UTC; 35s ago Documents: man: nginx(8) Main PID: 9039 (nginx) Tasks: 2 (limit: 1151) CGroup: /system.slice/nginx.service ├─9039 nginx: master process /usr/sbin/nginx -g daemon on; master_pro └─9041 nginx: worker process
This output shows that the service started successfully. However, the best way to test this is to request an Nginx page.
You can access the default Nginx homepage to confirm that the software is running properly by navigating to your server’s IP address. If you don’t know the IP address of your server, you can get it in different ways.
Try typing the following at your server’s command prompt
:
- ip addr show eth0 | grep inet | awk ‘{ print $2; } ‘ | sed ‘s/\/.*$//’
You will receive a few lines. You can test each one in your web browser to confirm if they work.
An alternative is to run the following command, which should generate your identified public IP address from another location on the Internet
:
- curl -4 icanhazip.com
When you have the IP address of your server, enter it in the address bar of your browser
: http://your_server_ip
You should receive the default Nginx landing page:
<img src
=”https://assets.digitalocean.com/articles/nginx_1604/default_page.png” alt=”Nginx default page” />
This page is included with
Nginx to verify that the server is running correctly.
Step 4 – Managing the Nginx Process
Now that you have your web server up and running, let’s review some basic administration commands.
To stop the web server, type the following: sudo systemctl stop nginx To start the
web server when it is stopped
, type the following: sudo systemctl start nginx
To stop
and restart the service, type the following:
- sudo systemctl restart nginx
If you’re simply making configuration changes, you can often reload Nginx without dropping connections instead of restarting it. To do this, type the following:
- sudo systemctl reload nginx
By default, Nginx is set to start automatically when the server starts. If this is not what you want, you can disable this behavior by typing
the following: sudo systemctl disable nginx
To re-enable the service to start at boot, you can type the following:
- sudo systemctl enable nginx Nginx
should now start automatically when the server starts again.
Step 5 – Configuring Server Blocks (recommended)
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. We’ll set up a domain called your_domain, but you need to replace it with your own domain name. For more information on setting up a domain name with DigitalOcean, see our Introduction to DigitalOcean DNS.
Nginx in Ubuntu 18.04 has a server block enabled by default that is configured to serve documents from a directory in /var/www/html. While this works well for a single site, it can become unwieldy if you host multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for our your_domain site, leaving /var/www/html instead as the default directory to be served if a client request doesn’t match any other site.
Create the directory for your_domain as follows, using the -p
flag to create the required home directories
: sudo
- mkdir -p
/var/www/your_domain/html
Next, assign the directory property with the environment variable
$USER:
- sudo chown -R $USER:$USER /var/www/your_domain/html
The permissions of your web roots should be correct if you have not modified your umask value, but you can make sure by typing
the following:
- sudo chmod -R 755 /var/www
/your_domain Next,
create a sample index.html page using nano or your favorite editor: nano
/var/www/your_domain/html/index.html
Inside, add the following sample HTML:
<html> <head> <title>Welcome to your_domain!</title> <
- /
head> <body> <h1>Success! The server your_domain block is working!</h1> </body> </html>
Save and close the file when you are done. If you used nano, you can exit by pressing CTRL+X, then Y and ENTER.
For Nginx to serve this content, it is necessary to create a server block with the correct directives. Instead of modifying
the default configuration file directly, create a new one in /etc/nginx/sites-available/your_domain: sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration block, which is similar to the default, but updated for your new directory and domain name
: server { listen 80; listen [::]:80; root /var/www/your_domain/html; index index.html index.htm index.nginx-debian.html; server_name your_domain.com www. your_domain; location / { try_files $uri $uri
- /
=404; } }
Note that we have updated the root settings to the new directory and the server_name to the domain name. Save and close the file when you are finished.
Next, enable the file by creating a link from it to the site-enabled directory, which Nginx reads during startup:
sudo ln -s /etc/nginx/
- sites-available/your_domain /etc/nginx/sites-enabled/
Two server blocks are now enabled and configured to respond to requests based on your listener and server_name policies (you can read more about how Nginx processes these directives here):
your_domain: It will respond to
- your_domain and www.your_domain requests. default: It will respond to
- any requests on port 80 that do not match the other two blocks
.
To avoid a potential hash bucket memory issue that can arise when adding additional server names, you need to adjust a single value in the /etc/nginx/nginx.conf file. Open the file
: sudo nano /etc/nginx
- /nginx.conf
Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line:
… http { … server_names_hash_bucket_size 64; … } …
Save and close the file when you are finished.
Next, test to make sure there are no syntax errors in any of
your Nginx files: sudo nginx -t
If there is no problem, restart Nginx to enable your changes:
- sudo systemctl restart nginx Nginx
should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like the following:
Step 6 – Familiarize yourself with 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 some important directories and files.
Contents
- /var/www/html: The actual web content, which by default only consists of the default Nginx page you viewed earlier, is served from the /var/www/html directory. This can be changed by altering the Nginx configuration files.
/
- etc/nginx server configuration: The Nginx configuration directory. All Nginx configuration files reside here.
- /nginx.conf: The main Nginx configuration file. This can be modified to make changes to the Nginx global settings.
- etc/nginx/sites-available/: The directory where server blocks can be stored per site. Nginx will not use the configuration files found in this directory unless they are linked to the site-enabled directory. Typically, all server block configuration is done in this directory and then enabled by binding to the other directory.
- etc/nginx/sites-enabled/: The directory where the enabled per-site server blocks are stored. Typically, these are created by using links to configuration files that are located in the sites-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 chunks.
/etc/nginx
/
/
/
Server
logs
/var/log/nginx/access.log: Every request to your web server is logged in this log file unless Nginx is configured to do otherwise. /var/log/
- nginx/error.log: Any Nginx errors will be logged in this log.
Conclusion
Now that you have your web server installed, you have many options for the type of content you want to serve and the technologies you want to use to serve
Create a richer experience. If you want to create a
more complete application stack, check out this article on how to set up a LEMP stack in Ubuntu 18.04.