Updated on 2023-12-19 GMT+08:00

Installing a Private Certificate on an Nginx Server

This topic describes how to install a private certificate on an Nginx 1.7.8 server running CentOS 7.

The installation procedure in this topic is for your reference only as the commands executed and configuration file modified during the installation may vary depending on OS types and server configurations.

Prerequisites

  • The certificate has been issued.
  • You have downloaded the private certificate in the format that is supported by Nginx. For details, see Downloading a Certificate.
  • You have used a system-generated CSR to apply for the certificate.

Constraints

  • Before installing the certificate, enable port 443 on the server where the private certificate is installed and add port 443 to the security group. Otherwise, HTTPS cannot be enabled after the installation.
  • A root CA must be added to the trusted client CA list so that all server certificates issued by the root CA can be trusted by the client. For details, see Trusting a Private Root CA.
  • If a domain name maps to multiple servers, deploy the certificate on each server.
  • A private certificate can only be installed on the server that maps to the domain name associated with the certificate. Otherwise, the web browser will display a message indicating that the domain name is insecure.

Procedure

The installation process is as follows (for Nginx 1.7.8 servers running CentOS 7):

Step 1: Obtaining FilesStep 2: Creating a DirectoryStep 3: Modifying Configuration FilesStep 4: Verifying the ConfigurationStep 5: Restarting NginxVerifying the Result

Step 1: Obtaining Files

Decompress the downloaded certificate file on your local PC.

You will obtain certificate file server.crt and private key file server.key.

  • The server.crt file contains two segments of certificate code: -----BEGIN CERTIFICATE----- and -----END CERTIFICATE-----, which are the server certificate and intermediate CA certificate respectively.
  • server.key contains one segment of private key code: -----BEGIN RSA PRIVATE KEY----- and -----END RSA PRIVATE KEY-----.

Step 2: Creating a Directory

Create a cert directory in the Nginx installation directory, and copy the server.key and server.crt files to the cert directory.

Step 3: Modifying Configuration Files

Before modifying the configuration file, back up the configuration file. You are advised to deploy the configuration file in the test environment and then configure it on the production environment to avoid service interruptions caused by incorrect configurations.

Configure the nginx.conf file in the conf directory of Nginx.

  1. Find the following configuration:
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
  2. Delete comment tags (#) at the beginning of the lines.
    server {  
                 listen              443 ssl;  
                 server_name         localhost;
                 ssl_certificate     cert.pem;  
                 ssl_certificate_key cert.key;
                 ssl_session_cache   shared:SSL:1m;  
                 ssl_session_timeout 5m;  
                 ssl_ciphers         HIGH:!aNULL:!MD5;         
                 ssl_prefer_server_ciphers  on;  
                 location / {  
                            root     html; 
                            index    index.html index.htm;  
                             }  
    }
  3. Modify the following parameters according to Table 1.
    ssl_certificate          cert/server.crt;    
    ssl_certificate_key      cert/server.key;   

    The complete configuration is as follows. Modify other parameters based on your needs.

    server {
            listen       443 ssl; # Set the default HTTPS port to 443. If the default HTTPS port is not configured, Nginx may fail to start.
            server_name  www.domain.com; #Replace www.domain.com with the domain name associated with your certificate.
            ssl_certificate      cert/server.crt; #Replace cert/server.crt with the path of the certificate file.
            ssl_certificate_key  cert/server.key; #Replace cert/server.key with the path of the private key.
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers  HIGH:!aNULL:!MD5; #Encryption suite
            ssl_prefer_server_ciphers  on;
            location / {
                root   html; #Site directory
                index  index.html index.htm; #Add attributes.
                       }  
    }

    Do not directly copy all configuration. Only attributes starting with ssl are directly related to the certificate configuration. Modify other parameters based on site requirements.

    Table 1 Parameters

    Parameter

    Description

    listen

    SSL access port number. Set the value to 443.

    Set the default HTTPS port to 443. If the default HTTPS port is not configured, Nginx may fail to start.

    server_name

    Domain name which the certificate is used for. Example: www.domain.com

    ssl_certificate

    Certificate file server.crt

    Set the value to the path of the server.crt file. An example of the path is cert/server.crt.

    ssl_certificate_key

    Private key file server.key

    Set the value to the path of the server.key file. An example of the path is cert/server.key.

  4. Save the configuration file.

Step 4: Verifying the Configuration

Go to the execution directory of Nginx and run the following command:

sbin/nginx -t

If the following information is displayed, the configuration is correct.
nginx.conf syntax is ok
nginx.conf test is successful

Step 5: Restarting Nginx

Run the following command to restart Nginx to make the configuration take effect:

cd /usr/local/nginx/sbin

./nginx -s reload

Verifying the Result

After the deployment succeeds, in the address bar of the browser, enter https://Domain name and press Enter.

If a security padlock is displayed in the address bar of the browser, the certificate has been installed successfully.