How to Install an SSL Certificate on NGINX

Introduction

Securing your website with SSL is crucial for protecting user data and ensuring trust. This guide explains how to install SSL certificates on NGINX for both Debian-based (e.g., Ubuntu) and RPM-based (e.g., CentOS, RHEL) systems.

Prerequisites

  • NGINX installed and running.
  • SSL certificate files: certificate.crt, private.key, and intermediate.crt.

Step 1: Install Required Packages

# Debian-Based Systems
sudo apt update
sudo apt install nginx

# RPM-Based Systems
sudo yum install epel-release
sudo yum install nginx

Step 2: Configure NGINX for SSL

Edit the NGINX configuration file:

  • Debian-Based: /etc/nginx/sites-available/yourdomain.conf
  • RPM-Based: /etc/nginx/conf.d/yourdomain.conf

Add the following configuration:

server {
    listen 443 ssl;
    server_name www.example.com;

    ssl_certificate /path/to/certificate.crt;
    ssl_certificate_key /path/to/private.key;
    ssl_trusted_certificate /path/to/intermediate.crt;

    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

Step 3: Redirect HTTP to HTTPS

Modify the configuration to redirect HTTP traffic to HTTPS:

server {
    listen 80;
    server_name www.example.com;
    return 301 https://$host$request_uri;
}

Step 4: Test and Reload NGINX

Verify the NGINX configuration:

sudo nginx -t

If the test is successful, reload NGINX:

sudo systemctl reload nginx

Step 5: Verify the Installation

Check your SSL installation using:

openssl s_client -connect www.example.com:443

Or use online tools like SSL Labs.

SSL

Leave a Reply

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