How to install Nginx on Ubuntu & Securing with Let’s Encrypt

What is Nginx

Nginx, pronounced “engine-x,” is a high-performance open-source web server and reverse proxy server. Renowned for its efficiency, scalability, and low resource consumption, Nginx serves as a popular choice for delivering static content, load balancing, and acting as a reverse proxy to enhance web application performance. With a modular architecture, it excels at handling concurrent connections and efficiently serving static files.

Beyond its web server capabilities, Nginx is widely used as a reverse proxy to distribute incoming web traffic across multiple servers, improving overall reliability and performance. It also supports SSL/TLS encryption, enhancing security for web applications. Nginx’s configuration syntax is flexible and allows for intricate setups, making it adaptable to various deployment scenarios. Frequently utilized in conjunction with other web technologies, such as PHP and Node.js, Nginx plays a crucial role in modern web infrastructure, catering to the demands of high-traffic websites and applications.

Installing Nginx

				
					sudo apt update && apt upgrade -y
sudo apt install nginx -y

#Adjusting the Firewall:
sudo ufw allow 'Nginx HTTP'
sudo ufw allow 'openSSH'

#Checking your Web Server:
systemctl status nginx
				
			

Setting Up Server Blocks:

				
					sudo mkdir -p /var/www/your_domain/html
 sudo chown -R $USER:$USER /var/www/your_domain/html
 sudo chmod -R 755 /var/www/your_domain
 nano /var/www/your_domain/html/index.html
 
 #paste
 <!DOCTYPE html>
<html lang="en">
<head>
    <title>Test Page</title>
</head>
<body>
    <h1>Hello</h1>
</body>
</html>

sudo nano /etc/nginx/sites-available/your_domain
#paste
paste
server {
        listen 80;
        listen [::]:80;

        root /var/www/your_domain/html;
        index index.html index.htm index.nginx-debian.html;

        server_name your_domain www.your_domain;

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

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

sudo nano /etc/nginx/nginx.conf
...
http {
    ...
    server_names_hash_bucket_size 64;
    ...
}
...

sudo nginx -t

sudo systemctl restart nginx

				
			

Visit
http://your_domain

Secure Nginx with Let's Encrypt

				
					sudo snap install --classic certbot

#create a symbolic link to the newly installed /snap/bin/certbot:
sudo ln -s /snap/bin/certbot /usr/bin/certbot

#Confirming Nginx’s Configuration:
sudo nano /etc/nginx/sites-available/your_domain
#if not then update and save it
sudo nginx -t
sudo systemctl reload nginx

Allowing HTTPS:
	sudo ufw status
	sudo ufw allow 'Nginx Full'
	sudo ufw delete allow 'Nginx HTTP'
	sudo ufw status

Obtaining an SSL Certificate:
	sudo certbot --nginx -d your_domain
				
			

Leave a Comment

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