How to install SSL certificate in Nginx

Updated on 2026-10-27 12:01:26 84 visits

Before starting the tutorial, don't you have a Cloud Linux server where you can install your nginx web server? Don't worry! With just one click, you can easily deploy it with SW Hosting.

To install an SSL certificate in Nginx, we will need the following files:

  • Certificate (.crt)
  • Private key (.key)
  • Certification Authority (.ca)

Do you have them? Let's begin:

  1. Copy the three files to the server. In this manual, we assume that you have uploaded the three files via FTP, but you can do it in any other way.

  2. Connect to the server via SSH and access the directory where the certificates are located. If you have uploaded them to the 'web' directory: cd /var/www/minuevaweb.es/datos/web

  3. Combine the .crt file with the .ca file, and save it in the Nginx certificates directory: cat /var/www/minuevaweb.es/datos/web/certificado.crt /var/www/minuevaweb.es/datos/web/certificado.ca >> /etc/nginx/swpanel/ssl/certificados.crt

  4. Move the .key file to the Nginx certificates directory: mv /var/www/minuevaweb.es/datos/web/certificado.key /etc/nginx/ssl/

  5. Delete the certificate files from the web folder to ensure they are not public. rm /var/www/minuevaweb.es/datos/web/certificado.crt rm /var/www/minuevaweb.es/datos/web/certificado.ca

  6. Access the certificates directory and change the owner and group of the files to root: cd /etc/nginx/ssl chown root:root certificados.crt certificado.key

We have now copied the certificates to the corresponding directory.

Next, we proceed to modify the configuration file corresponding to our website:

  • Access the vhosts configuration files directory: cd /etc/nginx/sites-enabled/

  • Edit the configuration file corresponding to the website where you are installing the SSL certificate. In this example, we use the nano text editor, but you can use any other: nano minuevaweb.es.conf

  • Make a copy of the first existing block underneath it; it will be duplicated:

# nginx virtual host for domain 'minuevaweb.es'
server {
        listen 80;
        root '/var/www/minuevaweb.es/datos/web';
        index index.html index.php;
        server_name minuevaweb.es www.minuevaweb.es;
        access_log /var/www/minuevaweb.es/logs/20190307.log;
        error_log /var/www/minuevaweb.es/logs/error_20190307.log;
        include /etc/nginx/php7.2.conf;
}

(the original file)

server {
        listen 80;
        root '/var/www/minuevaweb.es/datos/web';
        index index.html index.php;
        server_name minuevaweb.es www.minuevaweb.es;
        access_log /var/www/minuevaweb.es/logs/20190307.log;
        error_log /var/www/minuevaweb.es/logs/error_20190307.log;
        include /etc/nginx/php7.2.conf;
}
server {
        listen 80;
        root '/var/www/minuevaweb.es/datos/web';
        index index.html index.php;
        server_name minuevaweb.es www.minuevaweb.es;
        access_log /var/www/minuevaweb.es/logs/20190307.log;
        error_log /var/www/minuevaweb.es/logs/error_20190307.log;
        include /etc/nginx/php7.2.conf;
}

(the file with the original copy)

  • In the second block, we change the port from 80 to 443:
server {
        listen 443;
        ...
  • In the second block, after the port (listen parameter), we add the following lines with the corresponding paths to our certificates:
        ...
        ssl on;
        ssl_certificate /etc/nginx/ssl/certificados.crt;
        ssl_certificate_key /etc/nginx/ssl/certificado.key;
        ...
  • It will look like this:
# nginx virtual host for domain 'minuevaweb.es'
server {
        listen 80;
        root '/var/www/minuevaweb.es/datos/web';
        index index.html index.php;
        server_name minuevaweb.es www.minuevaweb.es;
        access_log /var/www/minuevaweb.es/logs/20190307.log;
        error_log /var/www/minuevaweb.es/logs/error_20190307.log;
        include /etc/nginx/php7.2.conf;
}
server {
        listen 443;
        ssl on;
        ssl_certificate /etc/nginx/ssl/certificados.crt;
        ssl_certificate_key /etc/nginx/ssl/certificado.key;
        root '/var/www/minuevaweb.es/datos/web';
        index index.html index.php;
        server_name minuevaweb.es www.minuevaweb.es;
        access_log /var/www/minuevaweb.es/logs/20190307.log;
        error_log /var/www/minuevaweb.es/logs/error_20190307.log;
        include /etc/nginx/php7.2.conf;
}
  • Save the changes made. In the nano text editor, you must press the following key combination: Ctrl + X to save and exit Y key to confirm that you want to overwrite Enter key

  • Check that the configuration is correct: nginx -t If any error appears, review the previous steps.

  • Reload the Nginx configuration so that the changes are applied: /etc/init.d/nginx reload

Done! Access your website with the https:// prefix to verify that it works correctly.

Remember that if you do not yet have a Cloud server with a Linux operating system, you can easily deploy it with SW Hosting.