Tech News

How To Set Up Nginx Web Server on Ubuntu 19.04

Nginx is one of the most popular web servers in the world and is responsible for hosting some of the largest and highest-traffic 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 tutorial, we'll show you how to install Nginx web server on an Ubuntu 19.04 machine.

Prerequisites
Before you begin this tutorial, you should have a non-root user with sudo privileges configured on your Ubuntu 19.04 server.


Installing Nginx
Nginx is available in Ubuntu's default repositories, you ca install it from these repositories using the apt packaging system.

sudo apt update

sudo apt install nginx


Output
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common nginx-core
Suggested packages:
  libgd-tools fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
  fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0 libjpeg-turbo8 libjpeg8
  libnginx-mod-http-geoip libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter libnginx-mod-mail
  libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx nginx-common nginx-core
0 upgraded, 18 newly installed, 0 to remove and 95 not upgraded.
Need to get 2,432 kB of archives.
After this operation, 7,895 kB of additional disk space will be used.
Do you want to continue? [Y/n] y



Configuring 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:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

As you can see, 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. Since we haven't configured SSL for our server yet in this guide, we will only need to allow traffic on port 80.

You can enable this by typing:

sudo ufw allow 'Nginx HTTP'

You can verify the change by typing:

sudo ufw status

You should see HTTP traffic allowed in the displayed output:

Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere                 
Nginx HTTP                 ALLOW       Anywhere                 
OpenSSH (v6)               ALLOW       Anywhere (v6)            
Nginx HTTP (v6)            ALLOW       Anywhere (v6)



At the end of the installation process, Ubuntu 19.04 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 Tue 2019-04-16 12:33:13 UTC; 11min ago
     Docs: man:nginx(8)
 Main PID: 2666 (nginx)
    Tasks: 2 (limit: 2277)
   Memory: 4.7M
   CGroup: /system.slice/nginx.service
           ├─2666 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─2667 nginx: worker process

Apr 16 12:33:13 ubuntu1904 systemd[1]: Starting A high performance web server and a reverse proxy server...
Apr 16 12:33:13 ubuntu1904 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid ar
Apr 16 12:33:13 ubuntu1904 systemd[1]: Started A high performance web server and a reverse proxy server.


As you can see above, the service appears to have started successfully. However, the best way to test this is to actually request a page from Nginx.

You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server's IP address.

http://your_server_ip

You should see the default Nginx landing page:


This page is included with Nginx to show you that the server is running correctly.


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. We will set up a domain called sample.com, but you should replace this with your own domain name.

Create the directory for sample.com as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/sample.com/html

Next, assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/sample.com/html

The permissions of your web roots should be correct if you haven't modified your umask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/sample.com

Next, create a sample index.html page using nano or your favorite editor:

sudo nano /var/www/sample.com/html/index.html

Inside, add the following sample HTML:

<html>
    <head>
        <title>Welcome to sample.com!</title>
    </head>
    <body>
        <h1>Success!  The sample.com server block is working!</h1>
    </body>
</html>

Save and close the file when you are finished.

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 a new one at /etc/nginx/sites-available/example.com:

sudo nano /etc/nginx/sites-available/sample.com

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

server {
        listen 80;
        listen [::]:80;

        root /var/www/sample.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name sample.com www.sample.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

Notice that we’ve updated the root configuration to our new directory, and the server_name to our domain name.

Next, let's enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:

sudo ln -s /etc/nginx/sites-available/sample.com /etc/nginx/sites-enabled/

Two server blocks are now enabled and configured to respond to requests based on their listen and server_name directives

  • sample.com: Will respond to requests for sample.com and www.sample.com.
  • default: Will respond to any requests on port 80 that do not match the other two blocks.

To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary 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 when you are finished

Next, test to make sure that there are no syntax errors in any of your Nginx files:

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

If there aren't any problems, 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://sample.com, where you should see something like this:



Wrapping up
Now that you have your Nginx web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

No comments