当前位置: 首页 > 帮助中心

php-fpm于ubuntu如何配置SSL

时间:2026-01-29 15:00:39

在Ubuntu上为PHP-FPM配置SSL,你需要遵循以下步骤:

    安装Certbot:Certbot是一个自动化的工具,用于获取和更新Let’s Encrypt SSL证书。在终端中运行以下命令来安装Certbot及其Nginx插件(如果你使用的是Nginx作为Web服务器):
sudo apt updatesudo 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文件中添加以下内容:

<VirtualHost *:80>ServerName yourdomain.com www.yourdomain.comRedirect permanent / https://yourdomain.com/</VirtualHost><VirtualHost *:443>ServerName yourdomain.com www.yourdomain.comSSLEngine onSSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pemDocumentRoot /var/www/yourdomain.com<Directory /var/www/yourdomain.com>Options Indexes FollowSymLinksAllowOverride AllRequire all granted</Directory><FilesMatch \.php$>SetHandler "proxy:unix:/run/php/php7.x-fpm.sock|fcgi://localhost"</FilesMatch></VirtualHost>

确保将yourdomain.com替换为你的实际域名,并根据需要调整文件路径。

    重启Web服务器和PHP-FPM服务:

对于Nginx:

sudo systemctl restart nginxsudo systemctl restart php7.x-fpm

对于Apache:

sudo systemctl restart apache2sudo systemctl restart php7.x-fpm

现在,你的PHP-FPM应用程序应该已经通过SSL提供服务了。请确保在浏览器中访问https://yourdomain.com以验证配置是否正确。


上一篇:怎样调整HDFS的存储策略
下一篇:Linux MongoDB如何高效备份数据
Ubuntu
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器
  • 英特尔第五代 Xeon CPU 来了:详细信息和行业反应
  • 由于云计算放缓引发扩张担忧,甲骨文股价暴跌
  • Web开发状况报告详细介绍可组合架构的优点
  • 如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳
  • 美光在数据中心需求增长后给出了强有力的预测
  • 2027服务器市场价值将接近1960亿美元
  • 生成式人工智能的下一步是什么?
  • 分享在外部存储上安装Ubuntu的5种方法技巧
  • 全球数据中心发展的关键考虑因素
  • 英特尔与 Vertiv 合作开发液冷 AI 处理器

    英特尔第五代 Xeon CPU 来了:详细信息和行业反应

    由于云计算放缓引发扩张担忧,甲骨文股价暴跌

    Web开发状况报告详细介绍可组合架构的优点

    如何使用 PowerShell 的 Get-Date Cmdlet 创建时间戳

    美光在数据中心需求增长后给出了强有力的预测

    2027服务器市场价值将接近1960亿美元

    生成式人工智能的下一步是什么?

    分享在外部存储上安装Ubuntu的5种方法技巧

    全球数据中心发展的关键考虑因素