install nginx in ubuntu

How to Install NGINX in Ubuntu

NGINX is a high-performance web server used by millions of websites around the world. It can be used as an HTTP server, load balancer and reverse proxy. In this article, we will look at how to install NGINX in Ubuntu. You can use these steps on any Debian flavored Linux.

How to Install NGINX in Ubuntu

Here are the steps to install NGINX in Ubuntu / Debian Linux.

1. Install NGINX

Open terminal and run the following commands to install NGINX in Ubuntu / Debian Linux.

$ sudo apt update
$ sudo apt install nginx

Bonus Read : How to Enable TLS 1.3 in Apache, NGINX

2. Verify NGINX installation

After installation, NGINX will automatically start running on port 80.  To verify NGINX installation, open a browser and visit http://localhost or http://127.0.0.1. You will see the following welcome screen that indicates that NGINX has been installed and is running successfully.

verify nginx installation

Bonus Read : How to Fix 503 Service Temporarily Available

The following steps are optional and just an example to show how to setup a website to run on NGINX.

3. Create Website (Optional)

By default, static web pages need to placed at /var/www/html for NGINX. If your pages are located at different location, you can use Virtual hosts to point NGINX to that location.

Virtual Hosts also allow you to run multiple websites on same server, and also make changes without accessing main server configuration file.

We will create a new folder /var/www/html/website and place index.html file in it.

cd /var/www
sudo mkdir website
cd website
sudo vi index.html

Your text editor will open index.html file. Copy-paste the following HTML code in it.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Hello, Nginx!</title>
</head>
<body>
    <h1>Hello, Nginx!</h1>
    <p>We have just configured our Nginx web server on Ubuntu Server!</p>
</body>
</html>

Save and close the file.

Bonus Read : How to Fix 500 Internal Server Error in NGINX

4. Set up Virtual Host

We will add virtual host configuration file specific to our website at /etc/nginx/sites-enabled

$ sudo /etc/nginx/sites-enabled

$ sudo vi website.conf

Copy-paste the following code in website.conf. We will make out website run on port 81, instead of port 80.

server {
       listen 81;
       listen [::]:81;

       server_name example.com;

       root /var/www/website;
       index index.html;

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

Bonus Read : How to Fix 504 Gateway Timeout Error in NGINX

5. Restart NGINX Server

Restart NGINX server to activate our virtual host running on port 81.

$ sudo service nginx restart

Open a browser and visit http://localhost:81 or http://127.0.0.1:81

You will see the following screen indicating that you have setup website correctly.

install nginx ubuntu

Hopefully, the above commands will help you install NGINX in Ubuntu / Debian Linux.

Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!