CentOS上如何把Web服务器从Apache换到Nginx

jopen 10年前

Nginx简介:

Nginx是一个高性能的HTTP服务器和反向代理服务器, 最大的优点是节省资源,适用于处理高并发的请求。

1. Nginx最初是按照反向代理设计的,和Apache不同, nginx关心如何处理url,而不是文件!

2. Apache 是个基于进程处理的web服务器,如果同时有多个请求,必须要启动多个进程来处理。 这样在高负载的情况下,资源的消耗和响应的速度都会有很大的问题。 而Nginx是个基于事件(event)的异步处理模式, 下面是Nginx的一个简单的示意图,有一个Master进程,Maste进程负责系统配置,管理socket,以及管理一个或是多个Worker进程。 而Worker进程接收和处理来自用户(浏览器)的请求。一般来讲,一个worker进程可以同时处理上千个用户的连接请求。每个worker进程采用异步的,基于event的方式来处理用户的请求。对于HTML的静态页面,Nginx会自行来处理,但对于PHP,JSP, Python等动态页面,Nginx是通过FastCGI(或者SCGI,UWSGI)来把动态页面的请求交给相应的处理程序来处理。

 

CentOS上如何把Web服务器从Apache换到Nginx

安装Nginx

需要注意的是,在CentOS的YUM的基础的容器中,并没有nginx和php-fpm的RPM包。这两个RMP包在epel的容器中, 虽然你可以从官网下载RPM包来安装,但我个人建议,如果你的CentOS/Redhat中没有加入YUM的epel容器,还是先把这个yum容器加上去比较好,以后可以省无数的折腾。epel具体的安装方法,我在 Redhat/CentOS 软件安装之RPM和YUM 这篇文章中有介绍。

yum的容器库中加入了epel容器后,在CentOS上安装Nginx就非常简单,运行下面的命令就可以了。

yum install nginx 

安装玩以后,会发现Nginx的配置文件放在 /etc/nginx目录下, 一般在缺省的情况下,web的root目录会在/usr/share/ngxin/html中。

安装完nginx以后,我们要测试一下是否安装成功了.

如果之前已经安装过Apache的话,先要把Apache的服务停掉。

 /etc/init.d/httpd stop            #停掉apache服务    chkconfig httpd off              #开机重启后,apache服务不再启动

这时候你在浏览器上输入 http://主机ip, 如果能出现nginx的测试页面 “Test page for the nginx http server on EPEL”就说明nginx已经正常运行了。

安装php-fpm

PHP-FPM (PHP-FastCGI Process Manager) 是目前最常用的一个PHP FastCGI的实现。通俗的讲,这个模块在Nginx和PHP之间桥梁,使之可以互相通信和交换。

安装及启动过程如下:

 yum install php-fpm    /etc/init.d/php-fpm start    chkconfig php-fpm on

下一步是确认一下,nginx和php-fpm是否已经正常运行. 执行 netstat -tunlp 命令,会看到大约如下的一个界面。

可以看到nginx在监听80端口,而php-fpm在监听9000端口。

CentOS上如何把Web服务器从Apache换到Nginx

 

设置Nginx 和 PHP-FPM

我们假定这个主机上有两个网站,一个是aaa.com, 普通的PHP站点, 一个是bbb.com,为wordpress的博客。 我们就讨论一下在这种情况下,如何设置nginx.

首先为站点建立相应的目录

 mkdir -p  /var/www/aaa/html    mkdir -p /var/www/bbb/html    mkdir -p /var/log/nginx/aaa    mkdir -p /var/log/nginx/bbb    chown -R nginx:nginx  /var/www/aaa/html    chown -R nginx:nginx  /var/wwww/bbb/html    chown -R nginx:nginx  /var/log/nginx/aaa    chown -R nginx:nginx  /var/log/nginx/bbb

为两个网站分别设置虚拟目录(virtual directory)

为了保证整个配置更加清晰,我们尽量不修改主配置文件/etc/nginx/nginx.conf , 而是在在/etc/nginx/conf.d目录下建立两个文件,一个是aaa.conf, 一个是bbb.conf

其中aaa.conf的内容如下 (aaa是一个普通的php网站):

server {   listen 80 default_server;                 #当输入ip时,会访问aaa.com   server_name www.aaa.com aaa.com *aaa.com;  #这个应该是最好的写法了              access_log /var/log/nginx/aaa/access.log;   #access_log属于ngx_http_log_module的设置, 缺省level为info   error_log /var/log/nginx/aaa/error.log;     #error_log属于core module, 缺省的level是error      location / {      root /var/www/aaa/html;      index index.php index.html index.htm;     #由于是PHP类型的动态页面为主,所以把index.php放在前面效率会更高些     # try_files $uri $uri/ /index.php?$args;   #普通php网站因为没有rewrite的话,这个不需要   }     error_page 404 /404.html;         #error_page errcode uri (也就是说出现了404错误,会请求/404.html)   location = /404.html {            #这是一个典型的location     root /var/www/aaa/html;   }     # redirect server error pages to the static page /50x.html   #   error_page 500 502 503 504 /50x.html;   location = /50x.html {   root /var/www/aaa/html;   }     # proxy the PHP scripts to Apache listening on 127.0.0.1:80   # 因为我们不用Nginx做Apache的反向代理,所以不需要这个   #location ~ \.php$ {   # proxy_pass http://127.0.0.1;   #}     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000   # 这种写法可以防止.jpg.php之类的攻击,应该是最佳写法了吧   location ~ \.php$ {   root /var/www/aaa/html;   try_files $uri =404;   fastcgi_split_path_info ^(.+\.php)(/.+)$;   fastcgi_pass 127.0.0.1:9000;   fastcgi_index index.php;   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   include fastcgi_params;   fastcgi_pass php;   }     # deny access to .htaccess files, if Apache's document root   # concurs with nginx's one   #   location ~ /\.ht {     deny all;   }

设置完aaa.com的环境后,还需要设置bbb.com的 nginx的配置,因为bbb.com是wordpress的站点, 除了和aaa.com相同的设置外,还有些特殊的设置,具体设置请参考 http://codex.wordpress.org/Nginx

至此,从Apache向Nginx的移植基本完成。

原文链接!  http://www.androiddev.net/webserver-apache-to-nginx/