Ubuntu 下配置 SSL 证书
一 准备与前提
sudo apt update && sudo apt install opensslsudo apt install nginxsudo apt install apache2sudo ufw allow 'Nginx Full'sudo ufw allow 'Apache Full'二 获取证书
openssl genrsa -out mydomain.key 2048openssl x509 -req -days 365 -in mydomain.csr -signkey mydomain.key -out mydomain.crtsudo apt install certbot python3-certbot-nginxsudo apt install certbot python3-certbot-apachesudo certbot --nginx -d example.com -d www.example.comsudo certbot --apache -d example.com -d www.example.com三 在 Nginx 中部署
listen 443 ssl http2;listen [::]:443 ssl http2;ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_prefer_server_ciphers on;ssl_ciphers HIGH:!aNULL:!MD5;server { listen 80; server_name example.com www.example.com; return 301 https://$host$request_uri; }sudo nginx -tsudo systemctl reload nginx四 在 Apache 中部署
sudo a2enmod sslsudo a2ensite default-ssl.conf(或自定义 SSL 配置)SSLEngine onSSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem<VirtualHost *:80> ... Redirect permanent / https://example.com/ </VirtualHost>sudo apache2ctl configtestsudo systemctl restart apache2五 验证与运维
openssl s_client -connect example.com:443sudo systemctl enable --now certbot.timersudo certbot renew --dry-run