在Ubuntu系统中配置PHP-FPM以使用SSL,通常涉及两个主要步骤:配置Web服务器(如Nginx或Apache)以处理HTTPS请求,并确保PHP-FPM能够通过SSL与Web服务器通信。以下是使用Nginx作为Web服务器的示例配置:
首先,确保你已经安装了Nginx和PHP-FPM。如果没有安装,可以使用以下命令进行安装:
sudo apt update
sudo apt install nginx php-fpm
你需要一个SSL证书和私钥。你可以从Let’s Encrypt免费获取,或者使用你自己的证书。
如果你使用Let’s Encrypt,可以使用Certbot来获取证书:
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的获取和配置。
编辑Nginx的默认服务器块文件或创建一个新的服务器块文件(例如/etc/nginx/sites-available/yourdomain.com):
sudo nano /etc/nginx/sites-available/yourdomain.com
添加以下内容:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
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/yourdomain.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
保存并关闭文件。
创建一个符号链接以启用新的服务器块:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
测试Nginx配置是否正确:
sudo nginx -t
如果没有错误,重新加载Nginx:
sudo systemctl reload nginx
确保PHP-FPM配置正确。编辑PHP-FPM配置文件(例如/etc/php/7.4/fpm/pool.d/www.conf):
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
确保以下行存在并且正确:
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
保存并关闭文件。
重启PHP-FPM服务:
sudo systemctl restart php7.4-fpm
现在,你应该能够通过HTTPS访问你的网站,并且PHP-FPM将通过SSL与Nginx通信。
通过以上步骤,你已经成功配置了Ubuntu系统中的PHP-FPM以使用SSL。