How to Install Nginx on Ubuntu 18.04

Install Nginx on Ubuntu

Nginx is a free and open-source high-performance web server. It is also well-known for its reverse proxying, caching, load balancing, and media streaming. Configuring it is made simple, and it has low memory usage. A common misconception is that Apache is easier to use than Nginx – however, Nginx is just as easy-to-use as Apache. In this tutorial, we will show you how to install Nginx on an Ubuntu 18.04 VPS.

1: Log in and Update Your Server:

Log in to your Ubuntu 18.04 VPS via SSH as the root user:

$ ssh root@IP_Address -p Port_number

Don’t forget to replace ‘IP_Address’ and ‘Port_number’ with the actual IP address of your server as well as the correct SSH service port.

Run the following commands to make sure that all installed packages on your Ubuntu 18.04 VPS are updated to the latest available version:

$ apt update && apt upgrade

2: Install Nginx on Your Ubuntu Server

Nginx is available in the pre-installed Ubuntu package repositories. You can install it with the following command:

$ apt install nginx

Once the installation is complete, Nginx will be automatically started.
We can make sure that the Nginx service is running with the following command:

$ systemctl status nginx

The output should look similar to the one found below:

● 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 Sat 2019-01-21 01:50:44 CDT; 19s ago
 Main PID: 619 (nginx)
   CGroup: /system.slice/nginx.service
           ├─619 nginx: master process /usr/sbin/nginx -g daemon on; master_process on
           ├─620 nginx: worker process
           ├─621 nginx: worker process
           ├─622 nginx: worker process
           └─623 nginx: worker process

3: Managing the Nginx Service

Enable the Nginx server at boot time using the systemctl command:

$ systemctl enable nginx

Start Nginx server using the systemctl command:

$ systemctl start nginx

Restart Nginx server using the systemctl command:

$ systemctl restart nginx

Stop Nginx server using the systemctl command:

$ systemctl stop nginx

Reload Nginx server using the systemctl command:

$ systemctl reload nginx

Get the status of the Nginx server using the systemctl command:

$ systemctl status nginx

4: Creating a New Server Block

The default Nginx installation will have one server block enabled with a document root set to /var/www/html.
In this guide, we will create a new server block for the domain and set the document root to /var/www/

First, create the domain’s document root directory with the following command:

$ mkdir -p /var/www/

and then create an index.html file with the following content:

$ nano /var/www/
<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <h1> server block</h1>

Next, create a new server block with the following content by creating a file in the directory /etc/nginx/sites-available:

$ nano /etc/nginx/sites-available/
server {
       listen 80;
       listen [::]:80;

       server_name; root /var/www/; index index.html; location / { try_files $uri $uri/ =404; } }

Once you are done, save the file and close it.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer

Now just $43 .99


Activate the server block by creating a symbolic link:

$ ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled/

Check if there are any syntax errors present in the Nginx configuration by using the command below:

$ nginx -t

If everything is OK with the configuration, the output should be similar to the one below:

$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Otherwise, the errors will be listed in the output so you easily find out what the problem is.

Once you are sure there are no problems with the configuration in Nginx, you can restart the service using the following command:

$ systemctl restart nginx.service

5. Important Nginx Files and Directories


  • /var/www/html: The default root folder of the Nginx server. You can change that to any folder you want by editing your nginx.conf file and changing the DocumentRoot attribute. Remember to restart your Nginx server in order for the changes to take effect.

Server Configuration

  • /etc/nginx: All Nginx configuration files are located in the /etc/nginx/ directory.
  • /etc/nginx/nginx.conf: The first file that Nginx reads when it starts is nginx.conf. This file is maintained by Nginx package maintainers and it is recommended that administrators avoid editing this file unless they also follow changes made by upstream. This can be modified to make changes to the global Nginx configuration.
  • /etc/nginx/sites-available/: The directory for storing all of your server block configurations, regardless of whether or not they’re currently enabled.
  • /etc/nginx/sites-enabled/: The directory contains symlinks to server blocks in the sites-available directory. These sites are enabled and will be accessible through the server.

Server Logs

  • /var/log/nginx/access.log: Nginx writes information about client requests in the access log right after the request is processed. All requests to the Nginx web server are recorded to this log file.
  • /var/log/nginx/error.log: Nginx error logs are used to log general error messages.

Of course, you don’t need to install Nginx on Ubuntu 18.04 yourself if you use one of our Nginx Hosting services, in which case you can simply ask our expert Linux admins to install and set this up for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post on how to install Nginx on Ubuntu 18.04, please share it with your friends on the social networks by using the share shortcuts below, or simply leave a comment in the comments section. Thanks.

Leave a Comment