sudo yum install epel-release
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
sudo yum install certbot python2-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
(将yourdomain.com替换为您的实际域名)
sudo vi /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
sudo nginx -t
sudo systemctl restart nginx
sudo certbot renew --dry-run
sudo crontab -e
添加以下内容:
0 0,12 * * * /usr/bin/certbot renew --quiet
访问 https://yourdomain.com
,您应该能看到绿色锁标志,表示HTTPS配置成功。
如需更高级配置,如HTTP/2、OCSP Stapling等,可进一步优化Nginx SSL配置。