在LNMP(Linux, Nginx, MySQL, PHP)服务器上配置SSL证书是确保网站安全通信的重要步骤。以下是详细的配置步骤:
可以使用LNMP一键安装包进行安装:
wget http://soft.vpser.net/lnmp/lnmp1.7.tar.gz
tar zxf lnmp1.7.tar.gz && cd lnmp1.7
./install.sh
以Let’s Encrypt为例:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
以ZeroSSL为例:
/usr/local/acme.sh -f --server zerossl --issue -d yourdomain.com -d www.yourdomain.com --keylength ec-256 -w /var/www/html
找到Nginx的虚拟主机配置文件,通常位于 /usr/local/nginx/conf/vhost/yourdomain.com.conf
。在配置文件中添加或修改以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
location / {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
执行以下命令以重新加载Nginx配置:
sudo systemctl reload nginx
在浏览器中访问 https://yourdomain.com
。如果地址栏显示绿色安全锁图标,则表示SSL证书安装成功。
为增强安全性,建议配置HTTP到HTTPS重定向。这通常需要在Nginx配置文件中添加重写规则。
以上步骤为在LNMP环境中安装SSL证书的基本流程,具体操作可能会因不同的Linux发行版和Nginx版本而略有差异。