Skip to main content

[Ubuntu] How to install Nginx

Updated over 2 weeks ago

One of the most popular web servers in the world is Nginx. It uses an event-driven architecture to handle multiple requests within one thread, which makes it more resource-friendly than Apache. You can also use it as a reverse Proxy. In this guide, we will show how to set up Nginx on Ubuntu and its primary management features.

Installation

You can install Nginx from the default Ubuntu repositories using the apt packaging system. First, check and update local packages on your server:

sudo apt update
sudo apt upgrade

Installing Nginx:

sudo apt install nginx

Checking web server status

To make sure the Nginx service is running, use the init system check:

systemctl status nginx

Output:

Another way to verify if Nginx is up is to visit your server’s IP/hostname via browser:

http://your_server_ip

Commands to manage Nginx

To stop Nginx, use:

sudo systemctl stop nginx

To start Nginx, use:

sudo systemctl start nginx

To stop and start (restart) Nginx:

sudo systemctl restart nginx

To reload Nginx when adjusting configuration without dropping connections, execute:

sudo systemctl reload nginx

Nginx will start automatically with boot. To disable such behavior, use:

sudo systemctl disable nginx

To enable Nginx to start up at boot, use:

sudo systemctl enable nginx

Nginx files and directories

/var/www/html: web content, which only consists of the default Nginx page

/etc/nginx: this is a default Nginx configuration directory

/etc/nginx/nginx.conf: default configuration file. Modified to make changes to the global configuration of Nginx

/etc/nginx/sites-available/: this folder is for storing all of your server block configurations, whether or not they're currently enabled

/etc/nginx/sites-enabled/: this folder contains symlinks to files in the sites-available folder. This allows you to selectively disable server blocks by removing the symlink

/var/log/nginx/access.log: requests to your web server are recorded in this log. You can change the location by changing the Nginx configuration.

/var/log/nginx/error.log: records Nginx errors

Setting up Server Blocks (recommended)

Server Blocks in Nginx are equivalent to Virtual Hosts in Apache. It allows hosting multiple domains (websites) on one server.

By default, Nginx has one server block at /var/www/html. To host multiple domains, you would need to create a directory structure within /var/www for your your_domain_name site, leaving /var/www/html in place as the default directory.

Create a directory for your_domain_name, using -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain_name/html

Set permissions:

sudo chmod -R 755 /var/www/your_domain_name

Create an index.html file. In this case, we use nano, but you can definitely use any other text editor of your preference:

nano /var/www/your_domain_name/html/index.html

Add some content, for example:

<html> 
<head>
<title>Welcome!</title>
</head>
<body>
<h1>Success! your_domain_name server block is working!</h1>
</body>
</html>

Save and close the file by typing Ctrl+X.

For Nginx to serve this content, you need to create a server block with the correct directives. Make a new configuration file at /etc/nginx/sites-available/your_domain_name instead of modifying the default one.

Paste in 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_name/html;
index index.html index.htm index.nginx-debian.html;

server_name your_domain_name www.your_domain_name;

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

root configuration was updated to the new directory, and the server_name to your domain name.

Nginx uses symbolic links (symlinks) to track which of your server blocks are enabled. You can later delete the symlink from the sites-enabled directory while keeping the server block in sites-available if you want to allow it.

So we need to 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/your_domain_name /etc/nginx/sites-enabled/

Visit your domain via browser to verify it’s working:

To check all server blocks configured on your VPS, run the following command:

nginx -T | grep "server_name "

Be sure to include the whitespace to exclude irrelevant results.

Recommendation: To prevent hash bucket memory problem that can arise from adding additional server names, adjust the value server_names_hash_bucket_size in /etc/nginx/nginx.conf file:

sudo nano /etc/nginx/nginx.conf

Comment on the line server_names_hash_bucket_size. Output example:

If there are any issues, restart Nginx to enable your changes:

sudo systemctl restart nginx
Did this answer your question?