26.11.2026
Installing and Configuring an SSL Certificate (Let's Encrypt and Self-Signed SSL)
SSL/TLS certificates are used to secure a website or internal service over HTTPS. Choosing the right issuance method depends on port availability and whether you have a public domain name:
- Option 1 (Certbot) — when your server has a domain name and both ports 80 and 443 are open.
- Option 2 (acme.sh via Port 443) — when your residential ISP blocks incoming port 80, but port 443 is open.
- Option 3 (Self-Signed SSL) — when the service runs only on a local network or directly via an IP address.
Option 1. Let's Encrypt via Certbot (Ports 80 and 443 Open)
The Certbot utility automatically issues Let's Encrypt certificates, configures Nginx to use them, and sets up automatic renewal.
Prerequisites
- Your domain's DNS settings (for example,
site.ru) contain an A record pointing to your server's public IP address. - The
nginxweb server is running and accepting connections on port 80.
Installing and Issuing the Certificate
Install certbot and the Nginx plugin:
user@COMPXX:~\$ sudo apt-get update && sudo apt-get install -y certbot python3-certbot-nginx
If the UFW firewall is enabled, open ports 80 and 443:
user@COMPXX:~\$ sudo ufw allow 'Nginx Full'
user@COMPXX:~\$ sudo ufw delete allow 'Nginx HTTP'
Run Certbot, specifying your domain and the www subdomain:
user@COMPXX:~\$ sudo certbot --nginx -d site.ru -d www.site.ru
During the first run, enter your email address for renewal notifications and accept the Terms of Service (Y). Certbot will automatically update your Nginx configuration and set up HTTP-to-HTTPS redirection.
Verifying Automatic Renewal
user@COMPXX:~\$ sudo systemctl status certbot.timer
user@COMPXX:~\$ sudo certbot renew --dry-run
Option 2. Let's Encrypt via acme.sh (When Your ISP Blocks Port 80)
Many residential ISPs block incoming traffic on port 80, causing standard certbot validation to fail with a connection timeout. In this case, you can use the acme.sh client with TLS-ALPN-01 validation directly over the open port 443.
Installing acme.sh
Install the script as root and reload your shell environment variables:
root@COMPXX:~# curl https://acme.sh | sh -s email=your@email.com
root@COMPXX:~# source ~/.bashrc
Issuing and Installing the Certificate
Create a directory to store the certificates:
root@COMPXX:~# mkdir -p /etc/nginx/ssl
Issue the certificate over port 443 (the --server letsencrypt flag is required for .ru domains, while --pre-hook and --post-hook temporarily stop Nginx for a few seconds to free port 443 during validation):
root@COMPXX:~# acme.sh --issue --server letsencrypt --alpn \
-d site.ru -d www.site.ru \
--pre-hook "systemctl stop nginx" \
--post-hook "systemctl start nginx"
Once you see the Cert success message, install the certificate files into /etc/nginx/ssl/ and configure the Nginx reload command for future automatic renewals:
root@COMPXX:~# acme.sh --install-cert -d site.ru \
--key-file /etc/nginx/ssl/site.ru.key \
--fullchain-file /etc/nginx/ssl/site.ru.crt \
--reloadcmd "nginx -t && systemctl reload nginx"
Configuring Nginx and Verifying Auto-Renewal
Point your site's Nginx configuration (/etc/nginx/sites-available/site.ru.conf) to the newly installed certificate files:
server {
listen 443 ssl;
server_name site.ru www.site.ru;
ssl_certificate /etc/nginx/ssl/site.ru.crt;
ssl_certificate_key /etc/nginx/ssl/site.ru.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "HIGH:!aNULL:!MD5";
# Your remaining site settings (root, location, etc.)
}
Test the configuration syntax and reload Nginx:
root@COMPXX:~# nginx -t && systemctl reload nginx
During installation, acme.sh automatically adds a crontab entry for background certificate renewal. You can verify it with:
root@COMPXX:~# crontab -l | grep acme.sh
Option 3. Self-Signed SSL Certificate (for Local Networks and IP Addresses)
If your server does not have a public domain name (for instance, it operates only on a local network, via an IP address, or is used to encrypt FTP/FTPS traffic), you can generate a self-signed certificate using openssl.
Note: A self-signed certificate securely encrypts traffic, but browsers will display an untrusted issuer warning upon first visit because it is not signed by a public Certificate Authority.
Generating the Key and Certificate
Create a directory and generate a private key along with a certificate valid for 365 days (replace 192.168.1.50 with your server's IP address or local hostname):
user@COMPXX:~\$ sudo mkdir -p /etc/nginx/ssl
user@COMPXX:~\$ sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/selfsigned.key \
-out /etc/nginx/ssl/selfsigned.crt \
-subj "/C=RU/ST=Region/L=City/O=Organization/CN=192.168.1.50" \
-addext "subjectAltName=IP:192.168.1.50"
Restrict access to the private key so other system users cannot read it:
user@COMPXX:~\$ sudo chmod 600 /etc/nginx/ssl/selfsigned.key
Configuring the Certificate in Nginx
Specify the paths to the generated files in your site configuration:
server {
listen 80;
server_name 192.168.1.50;
return 301 https://hostrequest_uri;
}
server {
listen 443 ssl;
server_name 192.168.1.50;
ssl_certificate /etc/nginx/ssl/selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/selfsigned.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "HIGH:!aNULL:!MD5";
root /SITE;
index index.php index.html index.htm;
location / {
try_files \$uri uri/ /index.php?query_string;
}
location ~* \.php\$ {
try_files \$uri =404;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \(document_root\)fastcgi_script_name;
include fastcgi_params;
}
}
Test the Nginx configuration and restart the service:
user@COMPXX:~\$ sudo nginx -t && sudo systemctl restart nginx
Tip: You can also use these exact files (
selfsigned.crtandselfsigned.key) to enable FTPS encryption invsftpdby settingrsa_cert_fileandrsa_private_key_filein/etc/vsftpd.confand enablingssl_enable=YES.