How to Set Up an SSL Certificate on Nginx ⋆ ALexHost SRL

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
30.10.2024

How to Set Up an SSL Certificate on Nginx

An SSL certificate is crucial for securing communication between your web server and users. By using SSL, you ensure that data is encrypted and that your site is trustworthy. In this article, we’ll guide you through setting up an SSL certificate on Nginx, one of the most popular web servers.

Step 1: Install Certbot

Certbot is a free, automated tool used to obtain and install SSL certificates from Let’s Encrypt. To install Certbot on your server, run the following commands:

  1. Update the package list:
    sudo apt update
  2. Install Certbot:
    sudo apt install certbot python3-certbot-nginx

Step 2: Obtain the SSL Certificate

Once Certbot is installed, you can request an SSL certificate by running the following command:

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

Certbot will automatically configure the SSL certificate for your domain. Follow the on-screen instructions to complete the setup.

Step 3: Configure Nginx for SSL

Certbot will automatically modify your Nginx configuration to use SSL. However, if you need to manually configure it, follow these steps:

  1. Open the Nginx configuration file for your site:
    sudo nano /etc/nginx/sites-available/default
  2. Update the configuration to use SSL:
    server { listen 443 ssl; server_name yourdomain.com www.yourdomain.com; ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem; # Additional settings… }
  3. Save the file and exit the editor.

Step 4: Test and Restart Nginx

After configuring SSL, test the Nginx configuration for errors:

sudo nginx -t

If no errors are found, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 5: Set Up Auto-Renewal for SSL Certificate

Let’s Encrypt certificates are valid for 90 days, so it’s important to set up auto-renewal to avoid expiration. Certbot automatically creates a cron job to renew the certificate, but you can manually test the renewal process:

sudo certbot renew –dry-run

This command will simulate a renewal to ensure everything is working correctly.

Conclusion

Setting up an SSL certificate on Nginx is an essential step for securing your website. By using Certbot and Let’s Encrypt, you can easily obtain and manage free SSL certificates, ensuring your users’ data is protected through encryption.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills