在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证书。