在Ubuntu上配置LNMP(Linux, Nginx, MySQL, PHP)環境的SSL證書,通常使用Let’s Encrypt提供的免費SSL/TLS證書。以下是詳細步驟:
Certbot是一個自動化工具,用于獲取和續訂Let’s Encrypt證書。
sudo apt update
sudo apt install certbot python3-certbot-nginx
使用Certbot獲取SSL證書。假設你的域名是example.com。
sudo certbot --nginx -d example.com -d www.example.com
Certbot會自動檢測Nginx配置文件,并提示你進行一些配置更改。按照提示操作即可。
Certbot會自動修改你的Nginx配置文件,通常位于/etc/nginx/sites-available/example.com-le-ssl.conf。確保以下內容正確:
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
root /var/www/html;
index index.php index.html index.htm;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
}
location ~ /\.ht {
deny all;
}
}
確保你的Nginx配置文件鏈接到sites-enabled目錄。
sudo ln -s /etc/nginx/sites-available/example.com-le-ssl.conf /etc/nginx/sites-enabled/
測試Nginx配置是否正確。
sudo nginx -t
如果沒有錯誤,重新加載Nginx。
sudo systemctl reload nginx
Certbot會自動設置一個cron任務或systemd定時任務來定期續訂證書。你可以手動測試續訂過程:
sudo certbot renew --dry-run
如果沒有錯誤,證書將自動續訂并更新到/etc/letsencrypt/live/example.com/目錄。
確保你的防火墻允許443端口的流量。
sudo ufw allow 'Nginx Full'
使用瀏覽器訪問https://example.com,確保SSL證書正確安裝并且網站可以正常訪問。
通過以上步驟,你就可以在Ubuntu上成功配置LNMP環境的SSL證書。