在Ubuntu上配置PHP的SSL證書,通常需要以下幾個步驟:
首先,你需要一個Web服務器來托管你的PHP應用。這里以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配置文件以啟用HTTPS。
Certbot會自動創建一個Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com-le-ssl.conf
。確保這個文件中的配置如下:
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的路徑。
創建一個符號鏈接以啟用SSL配置:
sudo ln -s /etc/nginx/sites-available/yourdomain.com-le-ssl.conf /etc/nginx/sites-enabled/
重啟Nginx以應用更改:
sudo systemctl restart nginx
確保你的PHP配置文件(通常是/etc/php/7.4/fpm/php.ini
)中啟用了必要的SSL選項。例如:
openssl.cafile=/etc/ssl/certs/ca-certificates.crt
打開瀏覽器并訪問https://yourdomain.com
,你應該能夠看到你的網站,并且瀏覽器地址欄會顯示安全鎖圖標。
Certbot會自動設置一個cron任務來定期續期證書。你可以手動測試續期過程:
sudo certbot renew --dry-run
如果沒有問題,證書將會自動續期。
通過以上步驟,你就可以在Ubuntu上成功配置PHP的SSL證書了。