要配置Ubuntu上的PHP-FPM以支持HTTPS,你需要完成以下幾個步驟:
安裝SSL證書: 你可以使用Let’s Encrypt免費獲取SSL證書。首先,確保你已經安裝了Certbot和Nginx(或Apache)。
sudo apt update
sudo apt install certbot python3-certbot-nginx
如果你選擇使用Apache,可以安裝python3-certbot-apache
。
獲取并安裝SSL證書: 使用Certbot獲取并安裝SSL證書。假設你選擇使用Nginx:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot會自動配置Nginx以使用SSL證書,并重定向HTTP流量到HTTPS。
配置PHP-FPM: PHP-FPM通常與Nginx或Apache一起使用。確保你的PHP-FPM配置正確。
編輯PHP-FPM配置文件:
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
確保以下行沒有被注釋掉:
listen = /run/php/php7.4-fpm.sock
如果你使用的是TCP/IP而不是Unix套接字,可以改為:
listen = 127.0.0.1:9000
配置Nginx: 編輯Nginx服務器塊配置文件:
sudo nano /etc/nginx/sites-available/yourdomain.com
確保配置文件中有以下內容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
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:/run/php/php7.4-fpm.sock; # 或者使用127.0.0.1:9000
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
try_files $uri $uri/ =404;
}
}
確保將yourdomain.com
替換為你的實際域名。
重啟服務: 重啟Nginx和PHP-FPM服務以應用更改:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm
驗證配置: 打開瀏覽器并訪問你的域名,確保HTTPS連接正常工作,并且PHP頁面能夠正確解析和執行。
通過以上步驟,你應該能夠在Ubuntu上成功配置PHP-FPM以支持HTTPS。