`
ljl_xyf
  • 浏览: 617811 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

windows nginx php配置

    博客分类:
  • 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的配置文件得了,列位莫怪。

 

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;


events {
    worker_connections  64;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        charset gbk;

        #access_log  logs/host.access.log  main;

        location / {
            root   "C:\Program Files\VertrigoServ\www";
            index  index.html index.htm index.php;
        autoindex on;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #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;
        #}
        location ~ \.php$ {
            root   "C:\Program Files\VertrigoServ\www";
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

 

 

压力测试:

下面是用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

 

  • 大小: 26.6 KB
  • 大小: 33.5 KB
  • 大小: 47.1 KB
分享到:
评论

相关推荐

    windows下 php+nginx配置详解

    windows下 php+nginx配置详解,经整理修改测试通过,希望和大家一起分享!

    windows下配置nginx+php环境

    windows下配置nginx+php环境

    Windows+Nginx+PHP7安装配置

    完美安装PHP + Nginx +Windows 配置 ,一起安装只要十分钟,让你少钻几个坑 快速解决对应问题

    windows平台中配置nginx+php环境

    刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是”...那么上正题了,nginx有这么多优势,那在windows下如何来配置nginx+php环境?网上看到还是那么多转载来转载去的文章。这里就我配置的过程,来介绍一下

    windows nginx +php 配置

    NULL 博文链接:https://niweiwei.iteye.com/blog/1010873

    nginx windows 1.13.9 php

    nginx 1.13.9 配置了php, 包含nginx的启动,关闭,重载的bat文件和php的启动,重新加载的bat,vbs文件。php进程用的端口号是9900.默认php7的安装是c:\php7.要修改php_start.vbs中php的路径。 createobject("wscript....

    nginx搭建配置详细说明

    3. Nginx的配置文件详解 3.1. Nginx的主配置文件概述 3.1.1. 认识配置文件 3.1.2. nginx的配置文件结构 3.1.3. nginx的全局配置 3.2. events配置 3.3. http的配置 3.4. nginx重要指令之location 4. nginx中的...

    nginx多域名配置示例

    nginx多域名配置示例 php多级域名配置 二级域名配置示例 nginx伪静态示例

    windows_nginx1.15_php7

    windows 版的nginx1.15+php7.2 nginx 目录下集成了winsw-1.9-bin.exe ,这个软件可以很方便的将nginx安装成windows服务以便使用,可根据需要修改winsw-1.9-bin.xml 用时nginx 配置文件要检查一下,或重新下载一个新的...

    windows 下nginx和php 安装配置

    windows 下nginx和php 安装配置,可以学习安装步骤和配置信息

    nginx for windows下载以及详细安装与配置

    nginx for windows下载以及详细安装与配置 1.目前官方 Nginx 并不支持Windows,您只能在包括Linux,UNIX,BSD系统下安装和使用,现在提供nginx for windows下载以及详细安装与配置,供windows下的nginx应用。 2.Nginx ...

    Windows Nginx Mysql PHP 集成开发环境

    WNMPServer 是一款适用于 windows下开发PHP站点的完全绿色服务器端集成环境, 可以快速安装配置Web服务器,一般说来,大家都习惯于将 Nginx、MySQL、PHP架设在Linux系统下, 但是,不可否认的是Windows也有其优点,...

    Windows 下 Nginx+PHP+MySQL 服务器环境安装配置包

    包含 Nginx+PHP+MySQL 安装包,还有 OpenSSL 安装包和几个实用 bat 小脚本,涵盖了从搭建 Nginx,自签署 SSL 证书,配置 HTTPS ,部署和配置 Windows PHP 服务器环境,连接 MySQL 的全部所需软件。Linux 可能是搭建...

    nginx+php配置环境+源软件包

    windows下配置nginx+php环境 nginx+php配置信息和源软件包

    Nginx端口映射配置方法

    搭建一个网站传统的方法是使用一个web服务器去解析文件入口文件,如使用Nginx,Apache解析到对应的入口文件,但是随着技术的发展,一些语言可以自己起一个web服务,如Node,PHP,使用开发语言启动一个web服务默认只能在...

    windows server 2012 R2服务器下配置php7+nginx1.1环境

    这是本人亲自在腾讯云服务器上windows server 2012 R2 64位系统上搭建的php7+nginx1.1的环境。全程截图,详细记录下来了。希望对大家有帮助。

    windows下nginx多php版本配置攻略

    好用,不必折腾好用,不必折腾好用,不必折腾好用,不必折腾

    windows NGINX配置 php环境以及thinkphp5(tp5)配置-附件资源

    windows NGINX配置 php环境以及thinkphp5(tp5)配置-附件资源

    Nginx Windows下安装PHP的MySQL

    将Nginx配置成WINDOWS服务: 下载WINSW, 建立 winsw.xml,并输入内容如下: <service> 02 <id>nginx</id> 03 <name>nginx</name> 04 <description>nginx</description> 05 <executable>c:\nginx\nginx.exe...

    websocket消息推送_windows+nginx+wss+https.rar

    linux下为nginx+wss+https模式,重点是https和wss以及vue打包的nginx兼容配置。另外还包含在linux+php.exe下永久运行php文件的方法和杀死永久运行进程的方法以及开启防火墙端口的方法,都写在了说明里!

Global site tag (gtag.js) - Google Analytics