在Ubuntu上为PHP-FPM配置SSL,你需要遵循以下步骤:
- 安装Certbot: Certbot是一个自动化的工具,用于获取和更新Let’s Encrypt SSL证书。在终端中运行以下命令来安装Certbot及其Nginx插件(如果你使用的是Nginx作为Web服务器):
sudo apt update
sudo apt install certbot python3-certbot-nginx
如果你使用的是Apache Web服务器,请安装python3-certbot-apache插件:
sudo apt install certbot python3-certbot-apache
- 获取SSL证书:
运行以下命令来获取SSL证书。请确保将
yourdomain.com替换为你的实际域名。
对于Nginx:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
对于Apache:
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Certbot将自动配置你的Web服务器以使用SSL,并将证书文件存储在/etc/letsencrypt/live/yourdomain.com/目录中。
- 配置PHP-FPM:
编辑PHP-FPM配置文件,通常位于
/etc/php/7.x/fpm/pool.d/www.conf(其中7.x是你的PHP版本)。找到listen指令并将其更改为监听Unix套接字,如下所示:
listen = /run/php/php7.x-fpm.sock
确保将7.x替换为你的实际PHP版本。
- 更新Nginx或Apache配置: 根据你使用的Web服务器,更新其配置文件以使用SSL证书,并将请求传递给PHP-FPM。
对于Nginx,在/etc/nginx/sites-available/yourdomain.com.conf文件中添加以下内容:
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 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/yourdomain.com;
index index.php index.html index.htm;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.x-fpm.sock;
}
}
对于Apache,在/etc/apache2/sites-available/yourdomain.com.conf文件中添加以下内容:
ServerName yourdomain.com www.yourdomain.com
Redirect permanent / https://yourdomain.com/
ServerName yourdomain.com www.yourdomain.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
DocumentRoot /var/www/yourdomain.com
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
SetHandler "proxy:unix:/run/php/php7.x-fpm.sock|fcgi://localhost"
确保将yourdomain.com替换为你的实际域名,并根据需要调整文件路径。
- 重启Web服务器和PHP-FPM服务:
对于Nginx:
sudo systemctl restart nginx
sudo systemctl restart php7.x-fpm
对于Apache:
sudo systemctl restart apache2
sudo systemctl restart php7.x-fpm
现在,你的PHP-FPM应用程序应该已经通过SSL提供服务了。请确保在浏览器中访问https://yourdomain.com以验证配置是否正确。
以上就是关于“php-fpm于ubuntu如何配置SSL”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm