温馨提示×

Ubuntu PHP-FPM如何配置SSL加密

小樊
45
2025-03-04 12:00:44
栏目: 云计算

在Ubuntu上配置PHP-FPM以使用SSL加密涉及几个步骤。以下是一个基本的指南,帮助你完成这个过程:

1. 安装必要的软件包

首先,确保你已经安装了Nginx和PHP-FPM。如果没有安装,可以使用以下命令进行安装:

sudo apt update sudo apt install nginx php-fpm 

2. 获取SSL证书

你可以从Let’s Encrypt免费获取SSL证书。使用Certbot来获取和配置证书:

sudo apt install certbot python3-certbot-nginx 

运行Certbot来获取证书:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com 

按照提示完成证书的获取和配置。Certbot会自动修改Nginx配置文件以启用SSL。

3. 配置Nginx

Certbot会自动修改Nginx配置文件,通常位于/etc/nginx/sites-available/yourdomain.com-le-ssl.conf。确保配置文件中有以下内容:

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; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; root /var/www/html; index index.php index.html index.htm; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; # 根据你的PHP版本调整 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location / { try_files $uri $uri/ =404; } } 

确保fastcgi_pass指向正确的PHP-FPM套接字文件。根据你的PHP版本,路径可能会有所不同。

4. 启用SSL配置

创建一个符号链接以启用SSL配置:

sudo ln -s /etc/nginx/sites-available/yourdomain.com-le-ssl.conf /etc/nginx/sites-enabled/ 

5. 重启Nginx

重启Nginx以应用更改:

sudo systemctl restart nginx 

6. 配置PHP-FPM

确保PHP-FPM配置正确。编辑/etc/php/7.4/fpm/pool.d/www.conf文件(根据你的PHP版本调整路径),确保以下行没有被注释掉:

listen = /run/php/php7.4-fpm.sock 

然后重启PHP-FPM服务:

sudo systemctl restart php7.4-fpm 

7. 测试配置

打开浏览器并访问https://yourdomain.com,确保SSL证书正确安装并且网站可以正常访问。

通过以上步骤,你应该能够在Ubuntu上成功配置PHP-FPM以使用SSL加密。如果有任何问题,请检查Nginx和PHP-FPM的日志文件以获取更多信息。

0