在Ubuntu上配置PHP的SSL證書,通常涉及以下幾個步驟:
安裝SSL證書:
.crt
或.pem
格式),你可以直接使用它們。配置Web服務器:
啟用SSL模塊:
sudo a2enmod ssl
重啟Apache:
sudo systemctl restart apache2
配置SSL:
編輯你的SSL站點配置文件,通常位于/etc/apache2/sites-available/your-site.conf
。
添加或修改以下內容:
<VirtualHost *:443>
ServerName yourdomain.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /path/to/your/certificate.crt
SSLCertificateKeyFile /path/to/your/private.key
SSLCertificateChainFile /path/to/your/ca_bundle.crt
<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>
啟用站點:
sudo a2ensite your-site.conf
重啟Apache:
sudo systemctl restart apache2
安裝Nginx(如果尚未安裝):
sudo apt update
sudo apt install nginx
配置SSL:
編輯你的Nginx站點配置文件,通常位于/etc/nginx/sites-available/your-site
。
添加或修改以下內容:
server {
listen 443 ssl;
server_name yourdomain.com;
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
ssl_trusted_certificate /path/to/your/ca_bundle.crt;
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版本調整
}
error_log /var/log/nginx/your-site-error.log;
access_log /var/log/nginx/your-site-access.log;
}
啟用站點:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
測試Nginx配置:
sudo nginx -t
重啟Nginx:
sudo systemctl restart nginx
確保你的PHP配置文件(通常是php.ini
)中沒有阻止SSL連接的設置。例如,檢查以下設置:
allow_url_fopen = On
openssl.cafile=/path/to/your/ca_bundle.crt
使用瀏覽器訪問你的網站,確保瀏覽器顯示安全連接(通常顯示一個鎖圖標)。你也可以使用在線工具如SSL Labs來檢查你的SSL配置。
通過以上步驟,你應該能夠在Ubuntu上成功配置PHP的SSL證書。