安装PHP : Linux 6 下编译安装 PHP 5.6
二、演示环境###当前环境为CentOS 6.7,本演示配置适用于CentOS 7下配置nginx连接PHP。# cat /etc/redhat-release CentOS release 6.7 (Final)# nginx -vnginx version: nginx/1.8.1# service nginx startStarting nginx: [ OK ]# ss -nltp|grep nginxLISTEN 0 128 :8001 : users:((\"大众nginx\"大众,2168,6),(\"大众nginx\"大众,2171,6))# service php-fpm start Starting php-fpm done# ss -nltp|grep phpLISTEN 0 128 127.0.0.1:9000 : users:((\"大众php-fpm\公众,2255,9),(\公众php-fpm\"大众,2256,0),(\"大众php-fpm\公众,2257,0))# php -vPHP 5.6.9 (cli) (built: Jul 17 2016 18:45:50) Copyright (c) 1997-2015 The PHP GroupZend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies with XCache v3.2.0, Copyright (c) 2005-2014, by mOo with XCache Optimizer v3.2.0, Copyright (c) 2005-2014, by mOo with XCache Cacher v3.2.0, Copyright (c) 2005-2014, by mOo1234567891011121314151617181920212223242526三、配置nginx
1、编辑/etc/nginx/nginx.conf,启用如下选项: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; }在所支持的主页面格式中添加php格式的主页,类似如下:location / { root html; index index.php index.html index.htm; }2、编辑/etc/nginx/fastcgi_params,将其内容变动为如下内容:# cp /etc/nginx/fastcgi_params /etc/nginx/fastcgi_params.bk# cat /dev/null>/etc/nginx/fastcgi_params# vim /etc/nginx/fastcgi_paramsfastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;3、重新载入nginx的配置文件:# nginx -t # service nginx reload 12345678910111213141516171819202122232425262728293031323334353637383940414243四、验证
1、在/etc/nginx/html新建index.php的测试页面,测试php是否能正常事情:# vi /etc/nginx/html/index.php <?php ### Author : Leshamiphpinfo(); ### Blog : http://blog.csdn.net/leshami?>2、通过浏览测试php页面浏览器打开 http://192.168.1.245:8001也可以利用如下办法测试# curl -I http://192.168.1.245:8001HTTP/1.1 200 OK Server: nginx/1.8.1Date: Tue, 19 Jul 2016 03:29:44 GMTContent-Type: text/html; charset=UTF-8Connection: keep-aliveX-Powered-By: PHP/5.6.93、访问mysql数据库#yum install php-mysql(安装php的mysql驱动)vim /etc/nginx/html/connmysql.php<?php $conn = mysql_connect('192.168.1.242','robin','xxx'); if ($conn) echo \"大众succ\"大众; else echo \公众failute\"大众; mysql_close();?># curl http://192.168.1.245:8001/connmysql.phpsucc1234567891011121314151617181920212223242526272829303132333435363738五、Nginx与PHP分离的环境
Nginx Web做事器与PHP运用做事器可能位于不同的做事器,则做如下调度: Nginx配置通过fastcgi反向代理时应指定PHP做事器监听的IP地址及端口号。 PHP做事器监听器监听地址应修正为对外供应做事的IP地址及端口号。 可利用NFS将PHP运用做事器目录挂载确当地,配置为Nginx location 路径。如下示例: Nginx做事器:192.168.1.245:8001 PHP 做事器:192.168.1.242Nginx做事器nginx.conf配置 location / { # root html; root /data; index index.php index.html index.htm; location ~ \.php$ { # root html; root /data; fastcgi_pass 192.168.1.242:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; }PHP做事器php-fpm.conf配置 # grep listen /usr/local/php/etc/php-fpm.conf|grep -v \公众;\"大众 listen = 192.168.1.242:9000 # mkdir -pv /data ###存放php代码 # echo \"大众/data 192.168.1.0/24(rw)\"大众 >/etc/exports # /etc/init.d/rpcbind start # /etc/init.d/nfslock start # /etc/init.d/nfs startNginx做事器mount NFS # mount -t nfs 192.168.1.242:/data /data 验证略