在Ubuntu上配置PHP以使用SSL證書,通常涉及幾個步驟。以下是一個基本的指南,幫助你設置SSL證書:
首先,你需要一個Web服務器。這里我們以Nginx為例。
sudo apt update
sudo apt install nginx
你可以從Let’s Encrypt免費獲取SSL證書。使用Certbot來簡化這個過程。
sudo apt install certbot python3-certbot-nginx
然后運行Certbot來獲取并安裝證書:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成證書的安裝。Certbot會自動配置Nginx以使用SSL證書。
確保你的PHP-FPM或PHP進程正在運行,并且Nginx可以正確地與它通信。
sudo apt install php-fpm
編輯Nginx配置文件(通常位于/etc/nginx/sites-available/yourdomain.com
),確保有以下配置:
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 ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
}
location / {
try_files $uri $uri/ =404;
}
}
確保將yourdomain.com
替換為你的實際域名,并根據需要調整PHP-FPM的路徑。
保存配置文件后,重啟Nginx以應用更改:
sudo systemctl restart nginx
打開瀏覽器,訪問https://yourdomain.com
,你應該看到一個安全的連接(鎖形圖標)。你也可以使用在線工具如SSL Labs來檢查你的SSL配置。
Certbot會自動設置一個cron任務來定期續期證書。你可以手動測試續期過程:
sudo certbot renew --dry-run
如果沒有錯誤,證書應該會自動續期。
通過以上步驟,你應該能夠在Ubuntu上成功配置PHP以使用SSL證書。