温馨提示×

Debian LNMP如何配置SSL

小樊
58
2025-02-28 17:32:51
栏目: 云计算

在Debian系统上配置LNMP(Linux, Nginx, MySQL, PHP)以使用SSL证书,可以按照以下步骤进行:

1. 安装Nginx

如果你还没有安装Nginx,可以使用以下命令进行安装:

sudo apt update sudo apt install nginx 

2. 获取SSL证书

你可以从Let’s Encrypt免费获取SSL证书。使用Certbot工具可以简化这个过程。

安装Certbot

sudo apt install certbot python3-certbot-nginx 

获取证书

运行Certbot来获取证书:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com 

按照提示完成证书的获取和配置。

3. 配置Nginx

Certbot会自动修改你的Nginx配置文件以启用SSL。通常,配置文件位于/etc/nginx/sites-available/yourdomain.com

你可以手动编辑这个文件来进一步自定义配置。以下是一个基本的SSL配置示例:

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; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; root /var/www/html; 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; # 根据你的PHP版本调整 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } 

4. 重启Nginx

保存配置文件后,重启Nginx以应用更改:

sudo systemctl restart nginx 

5. 验证SSL配置

你可以使用以下命令来验证你的SSL配置是否正确:

sudo nginx -t 

6. 设置自动续期

Certbot会自动设置一个cron任务来定期续期证书。你可以手动测试续期过程:

sudo certbot renew --dry-run 

如果一切正常,Certbot会自动续期证书并更新Nginx配置。

通过以上步骤,你就可以在Debian系统上成功配置LNMP以使用SSL证书了。

0