How to install Nginx on Ubuntu


Thu Feb 27, 2025

Step-by-Step Guide to Installing Nginx on Ubuntu

Nginx:

Nginx is a high-performance web server and reverse proxy server. It's known for its speed, reliability, and scalability, making it popular for serving web content and handling high-traffic websites. When used as a reverse proxy, Nginx sits between clients and backend servers, forwarding client requests to the appropriate backend servers and returning the responses to clients.

Step 1: Launch and Log In to Your Ubuntu Server:

    Start your Ubuntu server and log in to it.


Step 2: Update the Package Repositories:

 Before installing Nginx, update the package repositories by running the following command:

sudo apt-get update

Step 3: Install Nginx: Install Nginx using the following command:
sudo apt-get install nginx

Step 4: Check If Nginx Is Enabled: Run the following command to check if the Nginx service is enabled:
sudo systemctl is-enabled nginx
Check the status of the Nginx service using:
sudo systemctl status nginx

Step 5: Create a Server Block Configuration:

 Create a server block configuration for your backend target by running the following command.
In this example, we are creating a proxy for our backend service SonarQube, which is running on port 9000.

sudo vim /etc/nginx/sites-available/sonarqube.conf
This will open a blank file. Now, add the following content to the file, then save and exit:
server { listen 80;
server_name sonar.your-dmoan.io;
access_log /var/log/nginx/sonar.access.log;
error_log /var/log/nginx/sonar.error.log;
proxy_buffers 16 64k;
proxy_buffer_size 128k;

location / {
proxy_pass http://127.0.0.1:9000;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
} }

Step 6: Activate the Server Block Configuration: 

Activate the sonarqube.conf configuration by creating a symbolic link to the /etc/nginx/sites-enabled/ directory:

sudo ln -s /etc/nginx/sites-available/sonarqube.conf /etc/nginx/sites-enabled/
Verify the Nginx configuration files by running:
sudo nginx -t
If the syntax is correct, you will see an output similar to:
nginx: configuration file /etc/nginx/nginx.conf test is successful

Step 7: Enable and Reload Nginx: Enable the Nginx service so it starts on boot:
sudo systemctl enable nginx.service
Reload Nginx to apply the changes:
sudo systemctl reload nginx

Step 8: Configure the Firewall: 

Allow incoming TCP traffic on ports 80, 9000, and 9001 using Uncomplicated Firewall (UFW):

sudo ufw allow 80,9000,9001/tcp

Step 9: Test the Nginx Web Server:

Open your favorite browser and type:

http://your-ip
If everything is configured correctly, you should see the Nginx welcome page, confirming that your Nginx web server is up and running.



Chinmay Biswal
Solution Architect