配置Nginx以支持PHP

首先安装一堆 php 及相关组件

$ yum -y install php php-cli php-fpm php-pear php-mysql php-mssql php-odbc php-imap php-common php-devel lighttpd-fastcgi php-gd php-ldapphp-xml php-xmlrpc php-mbstring php-mcrypt php-snmp php-soap php-tidy

开启PHP服务

$ systemctl start php-fpm

查看是否启动成功

$ ps -ef | grep php-fpm
root      20153      1  0 21:26 ?        00:00:00 php-fpm: master process (/etc/php-fpm.conf)
apache    20154  20153  0 21:26 ?        00:00:00 php-fpm: pool www
apache    20155  20153  0 21:26 ?        00:00:00 php-fpm: pool www
apache    20156  20153  0 21:26 ?        00:00:00 php-fpm: pool www
apache    20157  20153  0 21:26 ?        00:00:00 php-fpm: pool www
apache    20158  20153  0 21:26 ?        00:00:00 php-fpm: pool www
root      20924  20650  0 21:38 pts/1    00:00:00 grep --color=auto php-fpm
$ netstat -ntlp | grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN

php-fpm 默认监听 9000

php-fpm 开机启动

$ chkconfig --levels 235 php-fpm on

配置 nginx.conf

首先最好备份 nginx.conf,以防出错

$ cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak

编辑 /etc/nginx/conf.d/default.conf

server {
  listen       80;
  server_name  localhost;
  location / {
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm; #增加index.php
  }
  #error_page  404              /404.html;
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
  location ~ \.php$ {
    root           /usr/share/nginx/html; #修改为nginx默认路径
    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;
  }
}

配置php,编辑 /etc/php.ini 文件,找到以下语句放开注释

$ vim /etc/php.ini
cgi.fix_pathinfo = 1

重启 nginx,php-fpm

$ systemctl restart nginx
$ systemctl restart php-fpm

建立info.php文件

$ vim /usr/share/nginx/html/info.php
<?php
  phpinfo();

此时,就可以在浏览器中访问 php 文件了。

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress