nginx配置详解php(nginx菜鸟教程)
作者
在中小企业中,LAMP架构被广泛使用。而对于高并发请求nginx配置详解php的情形,其Web服务器通常以Nginx居多,因此LNMP组合架构也非常普遍。在LAMP架构中,Apache可以使用三种方式与PHP连接通信,即以模块化的方式,CGI(已不再使用)以及FastCGI。那对于Nginx而言,则可以使用FastCGI方式,即反向代理方式,将PHP代码有FastCGI转交给PHP解释器进行处理。本文描述配置Nginx到PHP的连接。
一、环境准备安装Nginxnginx配置详解php:Linux 6 下安装编译安装Nginx
安装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三、配置nginx1、编辑/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 验证略目录
推荐阅读
0 条评论
本站已关闭游客评论,请登录或者注册后再评论吧~