How to install SSL on Nginx Web Server

To install your premium SSL/TLS certificate on your domain running on Nginx Web Server, follow the steps below:

Prerequisites

  • You already generated a CSR and Private Key. If not, first use this guide
  • You sudo or root access to the server.
  • We will assume our domain is called truehosttestdomain.com

Procedure

1.Login to your server via SSH and navigate to the location you want to save your TLS files.

From our previous article on generating a CSR our certificate files were stored in our users cert directory inside home folder. So we will run the commands below

$ cd ~/certs

2. Create a file called truehosttestdomain.com.combined .

$ touch truehosttestdomain.com.combined

3. Open the file using your favorite editor, eg vim or nano

$ vim truehosttestdomain.com.combined

4. Copy the content of your CRT to truehosttestdomain.com.combined at the top. Then copy the content of the Intermediate Certificate just below that.

5.Navigate to your vhost location.

By default, this is /etc/nginx/sites-available/ for Debian based systems and /etc/nginx/conf.d/ for Redhat based systems

$ cd  /etc/nginx/sites-available #for Debian based systems e.g Ubuntu
$ cd  /etc/nginx/conf.d/  #for Redhat based systems e.g Centos

6. Open the vhost file with your favourite editor and search for the lines below

ssl on;
ssl_certificate /etc/ssl/your_domain_name.pem; (or bundle.crt)
ssl_certificate_key /etc/ssl/your_domain_name.key;

and replace them with

ssl on;
ssl_certificate /home/truehost/certs/truehosttestdomain.com.combined;
ssl_certificate_key //home/truehost/certs/truehosttestdomain.com.key;

7. Then, add the following lines at the top of your vhost file

server {
  listen 80;
   
    server_name truehosttestdomain.com;
    
    return 301 https://truehosttestdomain.com;
    
    }
}

8. Your final vhost file should look like this

server {
  listen 80;
   
    server_name truehosttestdomain.com;
    
    return 301 https://truehosttestdomain.com;
    
    }
}

server {
  listen443;
    ssl on;
    ssl_certificate /home/truehost/certs/truehosttestdomain.com.combined;
    ssl_certificate_key //home/truehost/certs/truehosttestdomain.com.key;
    
    server_name truehosttestdomain.com;
    
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    location / {
    root  /home/www/public_html/truehosttestdomain.com/public/;
    index  index.html;
    }
} 

8. Restart Nginx and access the site online

$ systemctl restart nginx

9. Test your SSL. We have prepared a guide on how to test your SSL Certificate validity. Click the link below

Was this article helpful?

Related Articles

Leave A Comment?