在Ubuntu系統中配置PHP以使用SSL證書,通常涉及以下幾個步驟:
安裝SSL證書:
安裝和配置Web服務器:
安裝Apache(如果尚未安裝):
sudo apt update
sudo apt install apache2
啟用SSL模塊:
sudo a2enmod ssl
重啟Apache:
sudo systemctl restart apache2
配置SSL證書:
sudo nano /etc/apache2/sites-available/default-ssl.conf
<VirtualHost *:443>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCACertificateFile /etc/letsencrypt/chain.pem
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
啟用SSL站點:
sudo a2ensite default-ssl
重啟Apache:
sudo systemctl restart apache2
安裝Nginx(如果尚未安裝):
sudo apt update
sudo apt install nginx
配置SSL證書:
sudo nano /etc/nginx/sites-available/default
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;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers on;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根據你的PHP版本調整
}
location ~ /\.ht {
deny all;
}
}
重啟Nginx:
sudo systemctl restart nginx
測試SSL配置:
確保PHP腳本能夠正確處理SSL請求。你可以通過創建一個簡單的PHP文件來測試:
<?php
if ($_SERVER['HTTPS'] != "on") {
header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
exit;
}
echo "SSL is working!";
?>
將這個文件放在你的Web服務器的根目錄下,然后通過瀏覽器訪問它。如果看到“SSL is working!”的消息,說明你的PHP配置正確。
通過以上步驟,你應該能夠在Ubuntu系統中成功配置PHP以使用SSL證書。