Installing SSL in Nginx on Ubuntu

This guide will help you set up SSL in Nginx on an Ubuntu server.

Prerequisites

  • A server running Ubuntu (18.04 or later)
  • Nginx installed on your server
  • Domain name pointed to your server's IP address
  • Access to the terminal/command line

Step 1: Update Your Package List

Before installing any new packages, update your package list:

sudo apt update

Step 2: Install Certbot

Certbot is a tool to obtain SSL certificates from Let's Encrypt. To install Certbot, run:

sudo apt install certbot python3-certbot-nginx

Step 3: Obtain an SSL Certificate

Use Certbot to obtain an SSL certificate. Replace yourdomain.com with your actual domain name:

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

Follow the prompts to complete the installation. Certbot will automatically configure Nginx to use the SSL certificate.

Step 4: Test Nginx Configuration

After the installation, test your Nginx configuration to ensure there are no errors:

sudo nginx -t

If the test is successful, reload Nginx to apply the changes:

sudo systemctl reload nginx

Step 5: Set Up Auto-Renewal

Let's Encrypt certificates are valid for 90 days. You can set up a cron job to automatically renew your certificates. Open the crontab file:

sudo crontab -e

Add the following line to check for renewal twice a day:

0 0,12 * * * /usr/bin/certbot renew --quiet

Conclusion

Your Nginx server should now be configured to use SSL with a Let's Encrypt certificate. You can verify that SSL is working by visiting your site in a browser and checking for the padlock icon in the address bar.

For more information on SSL and Nginx, refer to the Nginx documentation (opens in a new tab).