在Linux服务器上为PHP部署SSL证书通常涉及以下步骤:
获取SSL证书:
安装SSL证书:
yourdomain.crt)yourdomain.key)ca_bundle.crt)配置Web服务器:
启用SSL模块:
sudo a2enmod ssl
重启Apache:
sudo systemctl restart apache2
配置SSL虚拟主机:
编辑你的SSL虚拟主机配置文件(通常位于/etc/apache2/sites-available/yourdomain.conf),添加以下内容:
<VirtualHost *:443>
ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /path/to/yourdomain.crt
SSLCertificateKeyFile /path/to/yourdomain.key
SSLCertificateChainFile /path/to/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 yourdomain.conf
重启Apache:
sudo systemctl restart apache2
配置SSL虚拟主机:
编辑你的SSL虚拟主机配置文件(通常位于/etc/nginx/sites-available/yourdomain),添加以下内容:
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /path/to/yourdomain.crt;
ssl_certificate_key /path/to/yourdomain.key;
ssl_trusted_certificate /path/to/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版本调整
}
location ~ /\.ht {
deny all;
}
error_log /var/log/nginx/yourdomain.error.log;
access_log /var/log/nginx/yourdomain.access.log;
}
启用虚拟主机:
sudo ln -s /etc/nginx/sites-available/yourdomain /etc/nginx/sites-enabled/
测试Nginx配置:
sudo nginx -t
重启Nginx:
sudo systemctl restart nginx
使用浏览器访问你的域名,确保浏览器显示安全锁标志,并且没有安全警告。你也可以使用在线工具如SSL Labs来检查你的SSL配置是否正确。
通过以上步骤,你应该能够在Linux服务器上成功部署SSL证书,并确保你的PHP应用通过HTTPS提供服务。