ubuntu

php-fpm于ubuntu如何配置SSL

小樊
41
2025-07-11 15:30:26
栏目: 云计算

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

  1. 安装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
  1. 获取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/目录中。

  1. 配置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版本。

  1. 更新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.com
    Redirect permanent / https://yourdomain.com/
</VirtualHost>

<VirtualHost *:443>
    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
    <Directory /var/www/yourdomain.com>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <FilesMatch \.php$>
        SetHandler "proxy:unix:/run/php/php7.x-fpm.sock|fcgi://localhost"
    </FilesMatch>
</VirtualHost>

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

  1. 重启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以验证配置是否正确。

0
看了该问题的人还看了