How to Install Nginx on Ubuntu Linux

The post is an instructional guide on how to install, configure, and use the Nginx HTTP web server on Ubuntu Linux for beginners. It provides the steps necessary to install Nginx using the apt package management tool, check if Nginx is running, and how to allow the server through the Ubuntu firewall. It also explains…

This post shows students and new users steps to install, configure and use the 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 are mostly likely running the 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 a wide variety of modules can extend.

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

Follow the steps below to install the Nginx HTTP server on Ubuntu Linux.

How to use Nginx HTTP server on Ubuntu Linux

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

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 the Nginx HTTP server.

To find out if Nginx is 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 the Ubuntu firewall

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

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

sudo ufw allow 'Nginx Full'

That will allow full traffic to Nginx.

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

http://localhost

You should see Nginx default welcome page.

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 the Nginx home directory.

Nginx’s 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 the /etc/nginx/sites-available directory. This is the directory where individual websites are defined. Nginx does not use website configurations until they’re activated. Once activated, they are linked to the /etc/nginx/sites-enabled directory.

These are the command below to activate websites to link them to the /etc/nginx/sites-enable directory. (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 in the /etc/nginx/snippets/ directory.

Snippets only load with Nginx when 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 inclusive 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) in the /var/log/nginx/ directory. You can view access and error logs in these files on Ubuntu.

Other Nginx configuration files not listed above might be available in Ubuntu. For more Nginx configurations and how to use them, we’ll continue posting a valuable tutorial here.

Conclusion:

This post showed you how to install and use Nginx on Ubuntu Linux. Please use the comment form below if you find any errors above or have something to add.

Comments

52 responses to “How to Install Nginx on Ubuntu Linux”

  1. […] use and run CGI script on Nginx, you must first install and configure FastCGI. With Nginx installed, run the command below to install […]

  2. […] you already have a web server like Nginx or Apache installed and running a website, you can use this method to generate a free […]

Leave a Reply

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