在Ubuntu上配置PHP-FPM以使用SSL涉及几个步骤,包括安装SSL证书、配置Nginx或Apache以使用SSL,并确保PHP-FPM能够处理HTTPS请求。以下是一个基本的指南,假设你使用的是Nginx作为Web服务器。
你可以从Let’s Encrypt免费获取SSL证书。以下是使用Certbot的步骤:
sudo apt update
sudo apt install certbot python3-certbot-nginx
运行Certbot来获取并安装证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
按照提示完成证书的安装。Certbot会自动配置Nginx以使用SSL。
Certbot会自动创建一个Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com-le-ssl.conf
。确保这个文件中的配置正确,特别是listen 443 ssl;
和server_name yourdomain.com www.yourdomain.com;
部分。
PHP-FPM本身不需要特别的SSL配置,但它需要知道如何处理HTTPS请求。通常,Nginx会将PHP请求转发给PHP-FPM。确保Nginx配置文件中有类似以下的配置:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
确保所有配置都正确后,重启Nginx和PHP-FPM服务:
sudo systemctl restart nginx
sudo systemctl restart php7.4-fpm # 根据你的PHP版本调整
打开浏览器,访问https://yourdomain.com
,确保SSL证书已正确安装并且页面能够正常显示。
Certbot会自动设置一个cron任务来定期续期证书。你可以手动测试续期过程:
sudo certbot renew --dry-run
如果没有错误,证书应该会自动续期。
通过以上步骤,你应该能够在Ubuntu上成功配置PHP-FPM以使用SSL。如果有任何问题,请检查Nginx和PHP-FPM的日志文件以获取更多信息。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: ubuntu配置ssh连接的方法是什么