在Ubuntu上為PHP配置SSL證書,通常需要以下幾個步驟:
sudo apt update
sudo apt install certbot python3-certbot-apache
這里我們以Apache為例,如果你使用的是Nginx,請安裝python3-certbot-nginx
。
yourdomain.com
替換為你的實際域名。sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Certbot會自動配置Apache并啟用SSL。在安裝過程中,它會要求你輸入電子郵件地址、同意服務條款等。
.htaccess
文件或nginx.conf
文件來實現。對于Apache,在你的網站根目錄下找到.htaccess
文件(如果沒有這個文件,請創建一個),然后添加以下內容:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
對于Nginx,在nginx.conf
文件中找到server
塊,然后添加以下內容:
server {
...
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
...
}
對于Apache:
sudo systemctl restart apache2
對于Nginx:
sudo systemctl restart nginx
現在,你的PHP應用程序應該已經通過SSL證書提供內容了。你可以訪問https://yourdomain.com
來驗證SSL證書是否已正確安裝。