Nginx Install

In this post we will see how we can install the Nginx web server in our Ubuntu server, and how to configure it to serve some files.
If you have already a web server installed, like Apache, you either have to uninstall it or change the default port that is listening because it will conflict with Nginx.
Install Nginx
We install nginx
and any required dependencies:
Adjusting the Firewall
After you have followed the initial server setup and enabled the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can check that UFW has an application profile for Nginx like so:
You should get a listing of the application profiles:
The three profiles do the following:
Nginx HTTP
: Opensport 80
Nginx HTTPS
: Opensport 443
Nginx Full
: Opens bothport 80
andport 443
We will not set up any SSL certificate, so we will not use port 443
, so we can open only port 80
:
else to open both ports:
Verify the change:
Check Web Server status
We can check the status of Nginx, by running:
If everything is ok, we should see a green active (running)
status.
Also, we can check the local network sockets to see the port that Nginx is using:
output:
We can also visit http://server-ip
to check the default page that Nginx serves. The page is located at /var/www/html/index.nginx-debian.html
.
Setting Up Server Blocks
We will create a Server Block (similar logic to Apache’s Virtual Hosts) that can be used to configure a web server on our server with a domain name. The Server Blocks allows us to have more than one domains on our single server.
For this example, we will use the example.org
domain. You should replace it everywhere we use it with your own registered domain. You can follow the Duck DNS guide to see how to get a free *.duckdns.org
domain.
Nginx has a default html
server block enabled that is located under /var/www/html
. We will create a new directory under /var/www
with the domain name we have as the directory name. This way, if we want to add more domain names in the future it will be as simple as creating a new directory there. We will leave the default /var/www/html
directory to be served if the client request does not match any other sites.
Files to be served
Firstly, create the example.org
directory:
Next, it is best to assign the ownership of the directory to a user and not leave the ownership to root
as it can create some security problems.
In order to assign the ownership to your current user you can run:
In order to make sure that the permissions are correct run:
Finally, we can create a basic index.html
page to display at the website:
and add:
Nginx Configuration File
In order for Nginx to server the files we have created at /var/www/example.org
we need to create a new configuration file under /etc/nginx/sites-available
(instead of editing the default one at /etc/nginx/sites-available/default
):
And add the following configuration block, which is similar to the default one but configured for our domain name:
The directives we used are explained:
listen
: Tells Nginx to listen for connections for both IPv4 and IPv6.server_name
: The domain of the website that we are looking for.root
: The directory where the website files are located.index
: The default file in the website files that Nginx will serve first.location
: Tells Nginx how to look up files, and if not found throw a 404 error.
After saving we can make sure we don’t have syntax errors:
Now we need to create a soft link to the /etc/nginx/sites-enabled
directory in order for Nginx to read it during startup:
And finally we can reload Nginx to update the changes we made:
Now if you type example.org
in your browser you should the contents in the index.html
we wrote.
Nginx Service Commands
Some useful systemd
commands for managing the Nginx service.
To stop the web server:
To start the web server:
To stop and start again the web server:
To reload simple configuration changes without stopping the web server:
To disable the web server from starting automatically at boot time:
To enable the web server for starting automatically at boot time:
Nginx Files and Directories
Some useful locations for Nginx in order to find quickly what you need:
Content
/var/www/html
: The web content that will be served by default, by Nginx.
Configuration
/etc/nginx
: The Nginx configuration directory./etc/nginx/nginx.conf
: The main Nginx configuration file for global settings./etc/nginx/sites-available
: The directory where server blocks are stored per-site. These configuration files will not be used by Nginx if they are not linked tosites-enabled
./etc/nginx/sites-enabled
: The directory where enabled server blocks are stored per-site. Usually, these configuration files are created by linking the ones fromsites-available
.
Logs
/var/log/nginx/access.log
: Every request to the Nginx web server is logged in this file./var/log/nginx/error.log
: Every Nginx error is logged in this file.