发布于2026-07-20 阅读(0)
扫一扫,手机访问
在CentOS 7上部署LNMP环境时,PHP的安装和配置是核心环节之一。这里整理了一份完整的操作指南,从安装到配置,再到与Nginx的整合,尽量做到一步到位。
首先,安装PHP及其相关扩展:
yum install php php-mysql php-fpm
安装过程中,你可能会遇到这样的问题:
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 有缺少的需求 libmysqlclient.so.18(libmysqlclient_18)(64bit)
这时候,最直接的解决办法是:把php-mysql换成php-mysqlnd。也就是说,执行:
yum install php php-mysqlnd php-fpm
接下来,需要调整PHP的配置文件。先打开/etc/php.ini:
vim /etc/php.ini
找到cgi.fix_pathinfo这一项,将;cgi.fix_pathinfo=1改为cgi.fix_pathinfo=0。
然后,编辑/etc/php-fpm.d/www.conf:
vim /etc/php-fpm.d/www.conf
将user = nobody和group = nobody改为:
user = nginx
group = nginx
当然,前提是系统里已经创建了nginx用户和nginx组。如果没有,可以这样操作:
# 添加nginx用户和用户组
groupadd -r nginx
useradd -r -g nginx nginx
配置完成后,启动PHP-FPM服务:
systemctl start php-fpm
为了让PHP-FPM在系统重启后自动运行,需要设置开机启动:
systemctl enable php-fpm
接下来,要让Nginx能够处理PHP请求。打开/etc/nginx/conf.d/default.conf(如果不存在,就手动创建),并写入以下配置:
server {
listen 80;
server_name www.twogether.cn;
# 注意:这几行来自原来的 "location /" 块
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
配置文件修改后,需要重启Nginx使改动生效:
systemctl restart nginx
最后,验证PHP是否正常工作。在/usr/share/nginx/html目录下创建一个测试文件:
vim /usr/share/nginx/html/info.php
输入以下内容:
然后,在浏览器中访问这个文件,比如http://your-server-ip/info.php。如果能看到PHP的信息页面,就说明配置成功了。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8