How to Set Up Nginx with Certbot for HTTPS on Your WordPress Website

Introduction

Set up Nginx with Certbot for HTTPS



Why You Should Use HTTPS on Your WordPress Website

  • Enhanced Security: HTTPS ensures all communication between your server and clients is encrypted, protecting sensitive data like passwords, credit card details, and personal information.
  • SEO Benefits: Google uses HTTPS as a ranking factor, which can give your website a boost in search results.
  • User Trust: Visitors are more likely to trust your website if they see the HTTPS padlock icon, increasing the likelihood of conversions.

Pre-Requisites

What You Need Before You Start

  • A WordPress Website: Make sure WordPress is already installed on your server.
  • A VPS or Dedicated Server: You’ll need a server running Nginx as your web server.
  • Root or Sudo Access: You’ll need to execute commands with root privileges.A Domain Name: Your site must have a fully qualified domain name (FQDN).
  • A Working DNS Setup: Your domain’s DNS should point to your server’s IP address.

Steps to Set up Nginx with Certbot for HTTPS



Step 1: Install Nginx on Your Server

  1. Update your server:
sudo apt update
sudo apt upgrade

2. Install Nginx:

sudo apt install nginx

3. Start and enable Nginx:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 2: Install Certbot

  1. Add Certbot’s repository:
sudo add-apt-repository ppa:certbot/certbot
sudo apt update

2. Install Certbot:

sudo apt install certbot python3-certbot-nginx

3. Verify the installation:

certbot --version

Step 3: Configure Nginx for Your WordPress Website

  1. Navigate to the Nginx configuration directory:
cd /etc/nginx/sites-available/

2. Create a new configuration file: Create a file named wordpress:

sudo nano wordpress

3. Add the following configuration: Replace yourdomain.com with your actual domain:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/wordpress;

    index index.php index.html index.htm index.nginx-debian.html;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

4. Create a symbolic link to enable the configuration:

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

5. Check Nginx configuration:

sudo nginx -t

6. Restart Nginx:

sudo systemctl restart nginx

Step 4: Obtain an SSL Certificate with Certbot

  1. Run Certbot for Nginx:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Step 5: Set Up Automatic SSL Renewal

  1. Edit the cron file:
sudo crontab -e

2. Add this line to the file:

0 0,12 * * * certbot renew --quiet

3. Save and exit.

Step 6: Test the SSL Setup

  1. Check your site’s HTTPS status: Use an online tool like SSL Labs’ SSL Test to check your website’s SSL configuration and security grade.
  2. Ensure your site redirects HTTP to HTTPS: Open your website on HTTP (http://yourdomain.com), and it should automatically redirect to HTTPS.

Conclusion

FAQs

  1. What is Certbot? Certbot is an automated tool that helps you obtain and install SSL certificates from Let’s Encrypt for securing your website with HTTPS.
  2. Can I use Certbot for other web servers? Yes, Certbot supports other web servers like Apache, but in this guide, we focus on Nginx.
  3. Is Certbot free? Yes, Certbot is free to use, and it provides SSL certificates from Let’s Encrypt, a nonprofit certificate authority.
  4. How often do I need to renew my SSL certificate? Let’s Encrypt SSL certificates are valid for 90 days, but Certbot makes it easy to renew them automatically.
  5. Why is HTTPS important for SEO? HTTPS is a ranking factor for Google, meaning it can give your site a boost in search results. It also enhances security and builds trust with visitors.



Previous Post
Next Post