0%

Centos安装PHP并配置nginx支持PHP应用

首先使用yum命令安装php及相关扩展,命令如下:

1
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm 

安装过程很简单,安装过程中遇到提示输入y回车即可。
安装完PHP后接下来我们就要配置nginx支持php应用。

使用vi /etc/php-fpm.conf打开php-fpm的配置文件:

1
include=/etc/php-fpm.d/*.conf

看到上面这一句得知php-fpm的配置文件包含php-fpm.d文件下的所有conf文件,使用ls /etc/php-fpm.d查看一下该目录下有哪些文件:

1
www.conf  www.conf.rpmsave

使用vi /etc/php-fpm.d/www.conf:

1
2
3
4
; RPM: apache Choosed to be able to access some dir as httpd
user = apache
; RPM: Keep a group allowed to write in log dir.
group = apache

找到上面几行代码,将user跟group修改成nginx如下:

1
2
3
4
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

修改完成保存退出,然后重启php-fpm服务:

1
service php-fpm restart

接下来配置nginx支持php应用,根据Centos安装nginx与简单配置我们知道nginx的配置文件在/etc/nginx/conf.d/目录下,我们打开默认的配置或者你自己创建的网站配置文件vi /etc/nginx/conf.d/default.conf:
首先将

1
2
3
4
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

下面添加index.php

1
2
3
4
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}

然后接着往下面找到:

1
2
3
4
5
6
7
8
9
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

将location代码块前面的#号去掉,然后将/scripts改成你自己的网站目录路径(默认的是/usr/share/nginx/html目录):

1
2
3
4
5
6
7
8
9
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

保存退出然后重启nginx:

1
/etc/init.d/nginx restart

OK,配置完了,现在我们在网站根目录下创建一个index.php写入一下内容测试一下是否可以支持php了:
index.php:

1
2
3
<?php
phpinfo();
?>

然后在浏览器里输入你的网址访问一下,当看到php信息的页面就说明已经配置成功了。

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!