在LNMP(Linux, Nginx, MySQL, PHP)环境中配置HTTPS,你需要一个SSL证书。你可以从证书颁发机构(CA)购买一个,或者使用Let’s Encrypt免费获取。以下是配置HTTPS的基本步骤:
sudo apt-get update
sudo apt-get install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
将yourdomain.com
和www.yourdomain.com
替换为你的域名。Certbot会自动检测Nginx配置文件的位置,并在其中添加HTTPS相关的配置。
Certbot会提示你输入电子邮件地址、同意服务条款并设置自动续期。完成这些步骤后,Certbot会生成SSL证书并将其存储在/etc/letsencrypt/live/yourdomain.com/
目录下。
Certbot会自动修改Nginx配置文件,使其支持HTTPS。以下是一个典型的Nginx HTTPS配置示例:
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 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/yourdomain.com;
index index.php index.html index.htm;
location / {
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;
}
}
请确保将yourdomain.com
替换为你的域名,并根据需要调整其他设置。
sudo nginx -t
sudo systemctl reload nginx
现在,你的网站应该已经通过HTTPS提供服务了。你可以通过访问https://yourdomain.com
来验证。