Linux

How to Install Nginx and Get Free SSL with Certbot on Ubuntu

Learn how to install Nginx on Ubuntu and secure your website with a free SSL certificate from Let’s Encrypt using Certbot. Step-by-step commands and configuration examples included for beginners and DevOps users.

1. Update System Packages

sudo apt update && sudo apt upgrade -y

2. Install Nginx

sudo apt install nginx -y

Verify it’s running:

sudo systemctl status nginx

If it’s not active:

sudo systemctl start nginx
sudo systemctl enable nginx

Test in your browser:

http://your_domain_or_server_ip

3. Adjust Firewall (if using UFW):

sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

4. Add Your Website Configuration

sudo nano /etc/nginx/sites-available/yourdomain.com

Example config:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.html index.htm index.php;
    location / {
        try_files $uri $uri/ =404;
    }
}

Enable it:

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Create your web root:

sudo mkdir -p /var/www/yourdomain.com
sudo chown -R $USER:$USER /var/www/yourdomain.com

5. Install Certbot & SSL Plugin for Nginx

sudo apt install certbot python3-certbot-nginx -y

6. Obtain a Free SSL Certificate

Run the following:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
  • Enter your email for urgent renewal/security notices.

  • Agree to the terms.

  • Choose whether to redirect HTTP → HTTPS (recommended: “2” for redirect).

Once complete, Certbot will automatically:

  • Get and install the SSL certificate

  • Edit your Nginx config for HTTPS

  • Reload Nginx

7. Test SSL Auto-Renewal

sudo certbot renew --dry-run

8. Verify HTTPS

https://yourdomain.com

Quick Summary

Step

Command / Description

Update system

sudo apt update && sudo apt upgrade -y

Install Nginx

sudo apt install nginx -y

Allow firewall

sudo ufw allow 'Nginx Full'

Install Certbot

sudo apt install certbot python3-certbot-nginx -y

Get SSL

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Test renew

sudo certbot renew --dry-run

Thank for visit  my website