• windows nginx php配置


    传说中nginx要比apache的负载均衡好的多,堪称神器 。 奈何,本人一直对新鲜事物不感冒。不过服务器上面有很多都是用nginx来做http服务器,今天偷闲了解一下。上网上搜了一下,大多数都是介绍 linux下的nginx与php的配置。本人现在在windows下面想尝试一下,发现资料并不多,且并不是很准确。其间也遇到了很多问题,还好折腾了 几次总算搞定了。下面说一下windows下面nginx和php的配置,不对的地方大家多担待。

    准备资源:

    原来我在用APM,很好用,集成了Apache,PHP和MySQL。这次把它关掉需要重新下载这三个文件。资源一定要准备好,不然玩不转啊。呵呵。

    Nginx 下载地址:http://sysoev.ru/nginx/nginx-0.8.16.zip

    PHP 下载地址:http://cn.php.net/distributions/php-5.2.11-Win32.zip

    MySQL 下载地址:http://download.mysql.cn/download_file/zip/5.0/mysql-5.0.22-win32.zip

    配置PHP:

    把下载完的php解压到:D:\php,随便你找个地方,自己喜欢就好。备份php.ini-recommended,并将其重命名为php.ini。然后编辑php.ini。

    基本上和使用apache一样,php的配置文件改动的地方并不是很多,像打开扩展文件之类的就不详谈了,有几个重点需要大家注意一下:

    指定docroot——php文件的存放目录,即你的documentroot。doc_root = “e:\www”

    扩展存放目录 extension_dir——更改为真实的扩展存放地址。extension_dir = “./ext”

    默认时区更改——在[Date]里面增加:date.timezone = Asia/ChongQing

    因为nginx需要的是cgi方式的php,所以如下几个地方是重点,否则nginx无法打开php文件

    enable_dl = On
    cgi.force_redirect = 0
    cgi.fix_pathinfo=1
    fastcgi.impersonate = 1
    cgi.rfc2616_headers = 1

    如果你使用MySQL,那么和我一样拷贝php5ts.dll和libmysql.dll(开启MYSQL)至C:\WINDOWS\system32下面。并且拷贝php.ini至C:\WINDOWS目录下。

    配置Nginx:

    将下载下来的nginx解压到D:\nginx目录中。很明显conf目录里存放的应该是配置文件,那么还说啥?过去更改nginx.conf文件是必须的。

    不看不知道,一看还真是挺开心的,这配置文件很清楚,很简单。最起码比apache要简单的多。让人看着发自内心的欣喜。具体配置如下:

    打开错误日志:error_log logs/error.log;
    更改线程数,因为是本地开发使用,所以线程不开太多了:worker_connections 64;
    更改默认文件编码:charset gbk;

    具体配置还有一些,但是不适合在这里给出,稍候我给出我的配置文件全文及截图描述 。大家详细看一下即可。

    启动Nginx和PHP:

    启动Nginx很简单,使用nginx目录下面的nginx.exe双击即可 。可是php的启动因为要使用cgi方式,所以有些特殊。我参照网上的方法下载了RunHiddenConsole.exe ,使用该工具用其执行的cmd窗口会自动关闭,否则你看一个CMD窗口一直在那晃来晃去,头会晕的。

    创建start_php.bat,来启动php,文件内容如下:

    @echo off
    echo Starting PHP FastCGI...
    RunHiddenConsole.exe d:\php\php-cgi.exe -b 127.0.0.1:9000  -c d:\php\php.ini

    创建stop_nginx.bat,来关闭php和nginx进程,文件内容如下:(http://www.my400800.cn )

    @echo off
    echo Stopping nginx...
    taskkill /F /IM nginx.exe > nul
    echo Stopping PHP FastCGI...
    taskkill /F /IM php-cgi.exe > nul
    exit

    效果图:

    因为我用的就是默认的80端口,所以直接访问localhost即可。下面给出两个截图。



     

    welcome nginx



     

    nginx 目录结构

    配置文件图解:

    这里的图就是随便截了一下,文字也没有标红,列位对付着看吧。自己乱写的,把没用的地方都去掉了(不是没用,是我没有更改和扩展这些配置)。



     

    nginx conf desc

    nginx配置文件:

    php.ini就不给出来了,一会儿给一下下载地址得了,那玩意太长了。只放出nginx的配置文件得了,列位莫怪。

     

    Xml代码
    1. #user  nobody;  
    2. worker_processes  1;  
    3.   
    4. error_log  logs/error.log;  
    5. #error_log  logs/error.log  notice;  
    6. #error_log  logs/error.log  info;  
    7. #pid        logs/nginx.pid;  
    8.   
    9.   
    10. events {  
    11.     worker_connections  64;  
    12. }  
    13.   
    14.   
    15. http {  
    16.     include       mime.types;  
    17.     default_type  application/octet-stream;  
    18.   
    19.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
    20.     #                  '$status $body_bytes_sent "$http_referer" '  
    21.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
    22.   
    23.     #access_log  logs/access.log  main;  
    24.   
    25.     sendfile        on;  
    26.     #tcp_nopush     on;  
    27.   
    28.     #keepalive_timeout  0;  
    29.     keepalive_timeout  65;  
    30.   
    31.     #gzip  on;  
    32.   
    33.     server {  
    34.         listen       80;  
    35.         server_name  localhost;  
    36.   
    37.         charset gbk;  
    38.   
    39.         #access_log  logs/host.access.log  main;  
    40.   
    41.         location / {  
    42.             root   "C:\Program Files\VertrigoServ\www";  
    43.             index  index.html index.htm index.php;  
    44.         autoindex on;  
    45.         }  
    46.   
    47.         #error_page  404              /404.html;  
    48.   
    49.         # redirect server error pages to the static page /50x.html  
    50.         #  
    51.         error_page   500 502 503 504  /50x.html;  
    52.         location = /50x.html {  
    53.             root   html;  
    54.         }  
    55.   
    56.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
    57.         #  
    58.         #location ~ \.php$ {  
    59.         #    proxy_pass   http://127.0.0.1;  
    60.         #}  
    61.   
    62.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
    63.         #  
    64.         #location ~ \.php$ {  
    65.         #    root           html;  
    66.         #    fastcgi_pass   127.0.0.1:9000;  
    67.         #    fastcgi_index  index.php;  
    68.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    69.         #    include        fastcgi_params;  
    70.         #}  
    71.         location ~ \.php$ {  
    72.             root   "C:\Program Files\VertrigoServ\www";  
    73.             fastcgi_pass   127.0.0.1:9000;  
    74.             fastcgi_index  index.php;  
    75.             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  
    76.             include        fastcgi_params;  
    77.         }  
    78.         # deny access to .htaccess files, if Apache's document root  
    79.         # concurs with nginx's one  
    80.         #  
    81.         #location ~ /\.ht {  
    82.         #    deny  all;  
    83.         #}  
    84.     }  
    85.   
    86.   
    87.     # another virtual host using mix of IP-, name-, and port-based configuration  
    88.     #  
    89.     #server {  
    90.     #    listen       8000;  
    91.     #    listen       somename:8080;  
    92.     #    server_name  somename  alias  another.alias;  
    93.   
    94.     #    location / {  
    95.     #        root   html;  
    96.     #        index  index.html index.htm;  
    97.     #    }  
    98.     #}  
    99.   
    100.   
    101.     # HTTPS server  
    102.     #  
    103.     #server {  
    104.     #    listen       443;  
    105.     #    server_name  localhost;  
    106.   
    107.     #    ssl                  on;  
    108.     #    ssl_certificate      cert.pem;  
    109.     #    ssl_certificate_key  cert.key;  
    110.   
    111.     #    ssl_session_timeout  5m;  
    112.   
    113.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
    114.     #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;  
    115.     #    ssl_prefer_server_ciphers   on;  
    116.   
    117.     #    location / {  
    118.     #        root   html;  
    119.     #        index  index.html index.htm;  
    120.     #    }  
    121.     #}  
    122.   
    123. }  

    压力测试:

    下面是用100个并发连接分别去压生产环境中同一负载均衡器VIP下、提供相同服务的两台服务器,一台为Nginx,另一台为Apache,Nginx每秒处理的请求数是Apache的两倍多,Nginx服务器的系统负载、CPU使用率远低于Apache:

      你可以将连接数开到10000~30000,去压Nginx和Apache上的phpinfo.php,这是用浏览器访问Nginx上的 phpinfo.php一切正常,而访问Apache服务器的phpinfo.php,则是该页无法显示。4G内存的服务器,即使再优化,Apache也 很难在“webbench -c 30000 -t 60 http://xxx.xxx.xxx.xxx/phpinfo.php”的压力情况下正常访问,而调整参数优化后的Nginx可以。

      webbench 下载地址:http://blog.s135.com/post/288/

      注意:webbench 做压力测试时,该软件自身也会消耗CPU和内存资源,为了测试准确,请将 webbench 安装在别的服务器上。

    3种WEB服务器的比较:

    server

    Apache

    Nginx     

    Lighttpd

    Proxy 代理

    非常好

    非常好

    一般

    Rewriter

    非常好

    一般

    Fcgi

    不好

    非常好

    热部署

    不支持

    支持

    不支持

    系统压力比较

    很大

    很小

    比较小

    稳定性

    非常好

    不好

    安全性

    一般

    一般

    技术支持

    非常好

    很少

    一般

    静态文件处理

    一般

    非常好

    Vhosts 虚拟主机

    支持

    不支持

    支持

    反向代理

    一般

    非常好

    一般

    Session sticky

    支持

    不支持

    不支持

    注:在相对 比较 大的网站,节约下来的服务器成本无疑是客观的。而有些小型网站往往服务器不多,如果采用 Apache 这类传统 Web 服务器,似乎也还能撑过去。但有其很明显的弊端: Apache 在处理流量爆发的时候 ( 比如爬虫或者是 Digg 效应 ) 很容易过载,这样的情况下采用 Nginx 最为合适。

    建议方案:

    Apache 后台服务器(主要处理 php 及一些功能请求 如:中文 url

    Nginx  前端服务器(利用它占用系统资源少得优势来处理静态页面大量请求)

    Lighttpd 图片服务器

    总体来说,随着 nginx 功能得完善将使他成为今后 web server 得主流。

     

     

    性能测试:

    将分别测试 3 种软件在对动态页面和静态页面请求及并发时的响应时间

    l        静态页面 搜狐首页

    LIGHTTPD

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    64

    60

    462.75

    21.6

    100000/200

    67

    60

    312.07

    32.4

    100000/500

    83

    60

    137.24

    72.8

    100000/1000

    出现错误丢包

    94

    60

    126.6

    78.9

    NGINX

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    34.6

    140

    943.66

    10.597

    100000/200

    35.6

    110

    924.32

    10.818

    100000/500

    34.3

    110

    912.68

    10.956

    100000/1000

    37

    160

    832.59

    12.106

    APACHE

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    40.6

    170

    690.72

    14.47

    100000/200

    41.1

    180

    685.39

    14.59

    100000/500

    42.3

    190

    633.64

    15.78

    100000/1000

    43.1

    200

    547.53

    18.26

    l        动态页面 内部社区首页

    LIGHTTPD

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    1000/100

    50

    200

    33.54

    29.816

    1000/200

    52

    210

    30.43

    32.858

    1000/500

    54

    230

    25.79

    38.76

    1000/1000

    62

    250

    24.83

    40.28

    NGINX

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    1000/100

    53.8

    250

    83.12

    12.305

    1000/200

    55.8

    250

    74.05

    13.504

    1000/500

    56

    260

    58.99

    16.951

    1000/1000

    58

    260

    43.41

    23.347

    APACHE

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    60

    200

    27.37

    36.541

    100000/200

    61

    220

    23.82

    41.981

    100000/500

    73

    150

    20.59

    48.562

    100000/1000

    53

    200

    27.18

    36.796

    l        PHPINFO 函数页

    LIGHTTPD

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    45

    20

    168.06

    59.504

    100000/200

    47

    22

    140.64

    71.103

    100000/500

    49

    24

    52.80

    189.386

    100000/1000

    在请求到4840时测试测试程序死掉

    NGINX

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    70

    120

    143.46

    69.706

    100000/200

    72

    130

    140.57

    71.140

    100000/500

    73

    150

    135.87

    73.601

    100000/1000

    77

    160

    132.18

    75.657

    APACHE 出现丢包

    n/-c(ab 参数)

    cpu%

    Mem

    RequestsperSecond

    Time taken for tests

    100000/100

    70

    180

    245.73

    40.694

    100000/200

    72

    190

    245.79

    40.684

    100000/500

    75

    200

    241.29

    41.443

    100000/1000

    77

    220

    236.74

    42.239

    各大网站 WEB 服务器资源列表

    网站名    操作系统    web 服务器

    1. 门户网站类:

    搜狐      LINUX           apache 1.3.37

    新浪      LINUX           apache 2.0.54

    迅雷      LINUX           nginx 0.6.31

    163      LINUX           apache 2.2.6

    2. 搜索类

    百度       unknown        BWS 1.0

    Google   linux           gws

    Sougou   FreeBSD          apache 2.2.4

    Hao123   linux           apache 2.2.4

    4. 电子邮箱类

    126        linux         apache

    Hotmail    win2003       microsoft-IIS 6.0

    新浪邮箱     F5 Big-IP    apache 2.2.8

    263        linux         apache 2.2.6

    5. 博客类

    新浪博客     linux          nginx 0.5.35

    搜狐博客     linux          nginx

    迅雷博客     linux          nginx 0.6.32

    天涯博客     F5 Big-IP      Microsoft-IIS/5.0

    6 .视频类

    优酷          linux          apache

    土豆          linux          apache

    Ku6         linux            apache

    六间房        linux          nginx 0.6.14

     

  • 相关阅读:
    laravel使用redis报错
    PHP新特性W3Cschool
    【python】跳过验证直接登陆-cookie已经知道需要部分直接注入
    【python】显示等待
    【python】pymysql库的简单使用
    【python】UI自动化优化浏览器重复打开和跑脚本时电脑无法做其他工作的问题
    【python】seleniumwire和selenium的区别
    【python】UI自动化-新版
    【python】UI自动化获取输入框的值
    【python】UI自动化多窗口处理
  • 原文地址:https://www.cnblogs.com/jishu/p/2303580.html
Copyright © 2020-2023  润新知