温馨提示×

php-fpm在centos上如何配置SSL加密

小樊
37
2025-12-05 13:29:56
栏目: 云计算

在CentOS上为PHP-FPM配置SSL加密,你需要完成以下几个步骤:

  1. 安装Certbot和Nginx(如果你还没有安装的话):
sudo yum install epel-release
sudo yum install certbot python2-certbot-nginx
  1. 获取SSL证书:
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

按照提示操作,Certbot会自动配置Nginx并获取SSL证书。

  1. 修改Nginx配置文件: 打开Nginx配置文件,通常位于/etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf。找到以下部分:
server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    ...
}

将其修改为:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;
    return 301 https://$host$request_uri;
}

然后添加一个新的server块来处理HTTPS请求:

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/html;
    index index.php index.html index.htm;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

注意:请将yourdomain.com替换为你的域名,同时确保ssl_certificatessl_certificate_key指向正确的证书文件路径。

  1. 重启Nginx和PHP-FPM服务:
sudo systemctl restart nginx
sudo systemctl restart php-fpm

现在你的CentOS服务器上的PHP-FPM应该已经通过SSL加密提供服务了。访问你的域名,浏览器应该会显示一个安全的连接。

0