How to Install Nginx on Ubuntu Linux

This post shows students and new users steps to install, configure and use Nginx HTTP web server on Ubuntu Linux. Nginx is probably the second most popular open source web server in the world. Chances are many of the websites you visit today mostly likely are running Nginx HTTP server.

If you’re thinking of running a website, you’re more likely to go with Nginx or have support for Nginx on web hosting companies than other web servers. Nginx provides powerful features which can be extended by a wide variety of modules.

If you’re a student or new user learning Linux, the easiest place to start learning is on Ubuntu Linux. Ubuntu is the modern, open source Linux operating system for desktop, servers and other devices.

To get started with installing Nginx HTTP server on Ubuntu Linux, follow the steps below.

How to use Nginx HTTP server on Ubuntu Linux

As mentioned above, Nginx is widely used across the internet. If you want to learn how to install and use it on Ubuntu Linux, then continue below.

Nginx is available in Ubuntu repositories so we can easily install it using the apt package management tool.

To install Nginx, run the commands below:

sudo apt update
sudo apt install nginx

The commands above will install Nginx HTTP server.

Now, to find out if Nginx is actually installed and running, use the status check command below.

sudo systemctl status nginx

The command will output similar lines below when Nginx is running.

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 Wed 2021-09-15 12:27:58 CDT; 9s ago
       Docs: man:nginx(8)
   Main PID: 2711 (nginx)
      Tasks: 2 (limit: 4651)
     Memory: 3.0M
     CGroup: /system.slice/nginx.service
             ├─2711 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─2712 nginx: worker process

Sep 15 12:27:58 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse pro>
Sep 15 12:27:58 ubuntu2004 systemd[1]: Started A high performance web server and a reverse prox>

How to allow Nginx through Ubuntu firewall

If you’re running Ubuntu in protected mode with firewall enabled, then you’ll have to allows HTTP (80) and HTTPS (443) to Nginx web server. In most cases, Ubuntu server are running without firewall enabled. However, run the commands below if you’re not sure.

If you’re using UFW to manage Ubuntu firewall, then run the commands below to allow traffic.

sudo ufw allow 'Nginx Full'

That will allow full traffic to Nginx.

With the firewall opened, simply browse to the server hostname or IP address to see if Nginx default page is up.

http://localhost

You should see Nginx default welcome page.

nginx default home page test

How to configure Nginx on Ubuntu Linux

Now that Nginx is installed, there are important folders and locations that you should be aware of. Other Linux systems might have different folder structures and configuration files.

On Ubuntu Linux, these are Nginx directory structures and configuration files.

All Nginx configuration files are located in the /etc/nginx directory. This is considered Nginx home directory.

Nginx main configuration file is /etc/nginx/nginx.conf. Global configuration settings are done in the file, but this file is rarely ever touched.

Nginx Virtual Hosts files are stored in /etc/nginx/sites-available directory. This is the directory where individual website are defined. Website configurations are not used by Nginx until they’re activated. Once activated, they are then linked to the /etc/nginx/sites-enabled directory.

To activate websites so they’re linked to the /etc/nginx/sites-enable directory, these the command below. (replace example.com.conf with your VirtualHost file)

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

The command above will create a symlink of the website configuration files found in the sites-available directory to the sites-enabled directory.

To deactivate a virtual host use the command below. (replace example.com.conf with your website VirtualHost file).

sudo rm /etc/nginx/sites-enabled/example.com.conf

Nginx uses snippets to enhance and add additional functionalities and they are located in the /etc/nginx/snippets/ directory.

Snippets are only available to load with Nginx when they’re included within a server block.

You can create snippets and store them in the /etc/nginx/snippets directory. To use the snippets within a server block, use the include definition. Example below:

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

        root /var/www/html;

 include snippets/mycustomsnippets.conf;

........

Nginx also has log files (access.log and error.log) are located in the /var/log/nginx/ directory. You can view access and error logs in these files on Ubuntu.

There are other Nginx configuration files that might be available in Ubuntu that are not listed above. For more Nginx configurations and how to use it, we’ll continue posting valuable tutorial here.

Conclusion:

This post showed you how to install and use Nginx on Ubuntu Linux. If you find any error above or have something to add, please use the comment form below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.