溫馨提示×

如何在Ubuntu上配置PHP的SSL證書

小樊
38
2025-06-01 03:43:50
欄目: 云計算

在Ubuntu上配置PHP的SSL證書,通常需要以下幾個步驟:

1. 安裝Nginx或Apache

首先,你需要一個Web服務器來托管你的PHP應用。這里以Nginx為例。

sudo apt update
sudo apt install nginx

2. 獲取SSL證書

你可以從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。

3. 配置Nginx

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的路徑。

4. 啟用SSL配置

創建一個符號鏈接以啟用SSL配置:

sudo ln -s /etc/nginx/sites-available/yourdomain.com-le-ssl.conf /etc/nginx/sites-enabled/

5. 重啟Nginx

重啟Nginx以應用更改:

sudo systemctl restart nginx

6. 配置PHP

確保你的PHP配置文件(通常是/etc/php/7.4/fpm/php.ini)中啟用了必要的SSL選項。例如:

openssl.cafile=/etc/ssl/certs/ca-certificates.crt

7. 測試配置

打開瀏覽器并訪問https://yourdomain.com,你應該能夠看到你的網站,并且瀏覽器地址欄會顯示安全鎖圖標。

8. 自動續期證書

Certbot會自動設置一個cron任務來定期續期證書。你可以手動測試續期過程:

sudo certbot renew --dry-run

如果沒有問題,證書將會自動續期。

通過以上步驟,你就可以在Ubuntu上成功配置PHP的SSL證書了。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女