测试PHP解析

首先配置Nginx 配置文件,使其能够支持PHP

在浏览器中输入虚拟机IP,正常时跳转到Nginx启动之后的欢迎页面:

wKioL1lBBGzQ4P-nAABRIvsbNHE858.jpg

如果是第一次使用,没有正常跳转,则先运行此命令:[root@LHQ nginx-1.4.4]# iptables -F

[root@LHQ nginx-1.4.4]# cat /usr/local/nginx/html/index.html

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

以上内容对应Nginx欢迎页面。

在/usr/local/nginx/html/下写一个PHP测试文件,将PHP info打印出来:

[root@LHQ nginx-1.4.4]# vi /usr/local/nginx/html/info.php

写入内容:

<?php

phpinfo();

?>

保存退出之后再浏览器访问http://192.168.1.198/info.php,显示如下内容:

wKioL1lBBHuR6I09AACm0nGXGJE321.jpg

(上图为一部分)

[root@LHQ nginx-1.4.4]# curl localhost/info.php

如果正常解析PHP,则显示如下内容,如不能正常解析,则显示源代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"><head>

<style type="text/css">

body {background-color: #ffffff; color: #000000;}

body, td, th, h1, h2 {font-family: sans-serif;}

pre {margin: 0px; font-family: monospace;}

a:link {color: #000099; text-decoration: none; background-color: #ffffff;}

a:hover {text-decoration: underline;}

table {border-collapse: collapse;}

.center {text-align: center;}

.center table { margin-left: auto; margin-right: auto; text-align: left;}

.center th { text-align: center !important; }

td, th { border: 1px solid #000000; font-size: 75%; vertical-align: baseline;}

h1 {font-size: 150%;}

h2 {font-size: 125%;}

.p {text-align: left;}

.e {background-color: #ccccff; font-weight: bold; color: #000000;}

.h {background-color: #9999cc; font-weight: bold; color: #000000;}

.v {background-color: #cccccc; color: #000000;}

.vr {background-color: #cccccc; text-align: right; color: #000000;}

img {float: right; border: 0px;}

hr {width: 600px; background-color: #cccccc; border: 0px; height: 1px; color: #000000;}

</style>

<title>phpinfo()</title><meta name="ROBOTS" content="NOINDEX,NOFOLLOW,NOARCHIVE" /></head>

<body><div class="center">

<table border="0" cellpadding="3" width="600">

<tr class="h"><td>

<a href="http://www.php.net/"><img border="0" src="/info.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42" alt="PHP Logo" /></a><h1 class="p">PHP Version 5.4.45</h1>

</td></tr>

</table><br />

<table border="0" cellpadding="3" width="600">

<tr><td class="e">System </td><td class="v">Linux LHQ 2.6.32-431.el6.i686 #1 SMP Fri Nov 22 00:26:36 UTC 2013 i686 </td></tr>

<tr><td class="e">Build Date </td><td class="v">Apr 10 2017 18:47:15 </td></tr>

<tr><td class="e">Configure Command </td><td class="v"> &#039;./configure&#039;  &#039;--prefix=/usr/local/php&#039; &#039;--with-config-file-path=/usr/local/php/etc&#039; &#039;--enable-fpm&#039; &#039;--with-fpm-user=php-fpm&#039; &#039;--with-fpm-group=php-fpm&#039; &#039;--with-mysql=/usr/local/mysql&#039; &#039;--with-mysql-sock=/tmp/mysql.sock&#039; &#039;--with-libxml-dir&#039; &#039;--with-gd&#039; &#039;--with-jpeg-dir&#039; &#039;--with-png-dir&#039; &#039;--with-freetype-dir&#039; &#039;--with-iconv-dir&#039; &#039;--with-zlib-dir&#039; &#039;--with-mcrypt&#039; &#039;--enable-soap&#039; &#039;--enable-gd-native-ttf&#039; &#039;--enable-ftp&#039; &#039;--enable-mbstring&#039; &#039;--enable-exif&#039; &#039;--disable-ipv6&#039; &#039;--with-pear&#039; &#039;--with-curl&#039; &#039;--with-openssl&#039; </td></tr>

<tr><td class="e">Server API </td><td class="v">FPM/FastCGI </td></tr>

<tr><td class="e">Virtual Directory Support </td><td class="v">disabled </td></tr>

<tr><td class="e">Configuration File (php.ini) Path </td><td class="v">/usr/local/php/etc </td></tr>

<tr><td class="e">Loaded Configuration File </td><td class="v">/usr/local/php/etc/php.ini </td></tr>

<tr><td class="e">Scan this dir for additional .ini files </td><td class="v">(none) </td></tr>

<tr><td class="e">Additional .ini files parsed </td><td class="v">(none) </td></tr>

<tr><td class="e">PHP API </td><td class="v">20100412 </td></tr>

<tr><td class="e">PHP Extension </td><td class="v">20100525 </td></tr>

<tr><td class="e">Zend Extension </td><td class="v">220100525 </td></tr>

<tr><td class="e">Zend Extension Build </td><td class="v">API220100525,NTS </td></tr>

<tr><td class="e">PHP Extension Build </td><td class="v">API20100525,NTS </td></tr>

<tr><td class="e">Debug Build </td><td class="v">no </td></tr>

<tr><td class="e">Thread Safety </td><td class="v">disabled </td></tr>

<tr><td class="e">Zend Signal Handling </td><td class="v">disabled </td></tr>

(以上内容为部分解析内容)

Nginx配置文件详解:

#运行用户

user www-data;    

#启动进程,通常设置成和cpu的数量相等

worker_processes  1;

#全局错误日志及PID文件

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

#工作模式及连接数上限

events {

    use   epoll;             #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能

    worker_connections  1024;#单个后台worker process进程的最大并发链接数

    # multi_accept on; 

}

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    #设定日志格式

    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,

    #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.

    sendfile        on;

    #tcp_nopush     on;

    #连接超时时间

    #keepalive_timeout  0;

    keepalive_timeout  65;

    tcp_nodelay        on;

    

    #开启gzip压缩

    gzip  on;

    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #设定请求缓冲

    client_header_buffer_size    1k;

    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;

    include /etc/nginx/sites-enabled/*;

    #设定负载均衡的服务器列表

     upstream mysvr {

    #weigth参数表示权值,权值越高被分配到的几率越大

    #本机上的Squid开启3128端口

    server 192.168.8.1:3128 weight=5;

    server 192.168.8.2:80  weight=1;

    server 192.168.8.3:80  weight=6;

    }

   server {

    #侦听80端口

        listen       80;

        #定义使用www.xx.com访问

        server_name  www.xx.com;

        #设定本虚拟主机的访问日志

        access_log  logs/www.xx.com.access.log  main;

    #默认请求

    location / {

          root   /root;      #定义服务器的默认网站根目录位置

          index index.php index.html index.htm;   #定义首页索引文件的名称

          fastcgi_pass  www.xx.com;

         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 

          include /etc/nginx/fastcgi_params;

        }

    # 定义错误提示页面

    error_page   500 502 503 504 /50x.html;  

        location = /50x.html {

        root   /root;

    }

    #静态文件,nginx自己处理

    location ~ ^/(p_w_picpaths|javascript|js|css|flash|media|static)/ {

        root /var/www/virtual/htdocs;

        #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。

        expires 30d;

    }

    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.

    location ~ \.php$ {

        root /root;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;

        include fastcgi_params;

    }

    #设定查看Nginx状态的地址

    location /NginxStatus {

        stub_status            on;

        access_log              on;

        auth_basic              "NginxStatus";

        auth_basic_user_file  conf/htpasswd;

    }

    #禁止访问 .htxxx 文件

    location ~ /\.ht {

        deny all;

    }

     

     }

}

以上是一些基本的配置,使用Nginx最大的好处就是负载均衡

如果要使用负载均衡的话,可以修改配置http节点如下:

#设定http服务器,利用它的反向代理功能提供负载均衡支持

http {

     #设定mime类型,类型由mime.type文件定义

    include       /etc/nginx/mime.types;

    default_type  application/octet-stream;

    #设定日志格式

    access_log    /var/log/nginx/access.log;

    #省略上文有的一些配置节点

    #。。。。。。。。。。

    #设定负载均衡的服务器列表

     upstream mysvr {

    #weigth参数表示权值,权值越高被分配到的几率越大

    server 192.168.8.1x:3128 weight=5;#本机上的Squid开启3128端口

    server 192.168.8.2x:80  weight=1;

    server 192.168.8.3x:80  weight=6;

    }

   upstream mysvr2 {

    #weigth参数表示权值,权值越高被分配到的几率越大

    server 192.168.8.x:80  weight=1;

    server 192.168.8.x:80  weight=6;

    }

   #第一个虚拟服务器

   server {

    #侦听192.168.8.x的80端口

        listen       80;

        server_name  192.168.8.x;

      #对aspx后缀的进行负载均衡请求

    location ~ .*\.aspx$ {

         root   /root;      #定义服务器的默认网站根目录位置

          index index.php index.html index.htm;   #定义首页索引文件的名称

          proxy_pass  http://mysvr ;#请求转向mysvr 定义的服务器列表

          #以下是一些反向代理的配置可删除.

          proxy_redirect off;

          #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP

          proxy_set_header Host $host;

          proxy_set_header X-Real-IP $remote_addr;

          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          client_max_body_size 10m;    #允许客户端请求的最大单文件字节数

          client_body_buffer_size 128k;  #缓冲区代理缓冲用户端请求的最大字节数,

          proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间(代理连接超时)

          proxy_send_timeout 90;        #后端服务器数据回传时间(代理发送超时)

          proxy_read_timeout 90;         #连接成功后,后端服务器响应时间(代理接收超时)

          proxy_buffer_size 4k;             #设置代理服务器(nginx)保存用户头信息的缓冲区大小

          proxy_buffers 4 32k;               #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置

          proxy_busy_buffers_size 64k;    #高负荷下缓冲大小(proxy_buffers*2)

          proxy_temp_file_write_size 64k;  #设定缓存文件夹大小,大于这个值,将从upstream服务器传

       }

     }

}