阿里云服务器上, 1G内存的配置,但用了一段时间,感觉Apache有些撑不出,想了想,还是换到目前最热门的Nginx吧。 这里把整个过程记录下来,与大家共享。
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)来把动态页面的请求交给相应的处理程序来处理。
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就非常简单,运行下面的命令就可以了。
1
|
1
| yum install nginx |
|
安装玩以后,会发现Nginx的配置文件放在 /etc/nginx目录下, 一般在缺省的情况下,web的root目录会在/usr/share/ngxin/html中。
安装完nginx以后,我们要测试一下是否安装成功了.
如果之前已经安装过Apache的话,先要把Apache的服务停掉。
1
2
|
1
| /etc/init.d/httpd stop #停掉apache服务 |
1
| 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之间桥梁,使之可以互相通信和交换。
安装及启动过程如下:
1
2
3
4
5
|
1
| yum install php-fpm |
1
| /etc/init.d/php-fpm start |
1
| chkconfig php-fpm on |
|
下一步是确认一下,nginx和php-fpm是否已经正常运行. 执行 netstat -tunlp 命令,会看到大约如下的一个界面。
可以看到nginx在监听80端口,而php-fpm在监听9000端口。
Screenshot
设置Nginx 和 PHP-FPM
我们假定这个主机上有两个网站,一个是aaa.com, 普通的PHP站点, 一个是bbb.com,为wordpress的博客。 我们就讨论一下在这种情况下,如何设置nginx.
首先为站点建立相应的目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
1
| -R nginx:adm / |
1
| -R nginx:adm / |
1
| -R nginx:adm / |
1
| -R nginx:adm / |
|
为了保证整个配置更加清晰,我们尽量不修改主配置文件/etc/nginx/nginx.conf , 而是在在/etc/nginx/conf.d目录下建立两个文件,一个是aaa.conf, 一个是bbb.conf
其中aaa.conf的内容如下 (aaa是一个普通的php网站): 参考于: http://wiki.nginx.org/PHPFcgiExample
查看源代码打印帮助
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
1
| listen 80 default_server; #当输入ip时,会访问aaa.com |
1
| server_name www.aaa.com aaa.com *.aaa.com; #这个应该是最好的写法了 |
1
| /log/nginx/aaa/access.log; #access_log属于ngx_http_log_module的设置, 缺省level为info |
1
| /log/nginx/aaa/error.log; # |
1
| 属于core module, 缺省的level是error |
1
| |
1
| |
1
| index index.php index.html index.htm; #由于是PHP类型的动态页面为主,所以把index.php放在前面效率会更高些 |
1
| ; #普通php网站因为没有rewrite的话,这个不需要 |
1
| error_page 404 /404.html; #error_page errcode uri (也就是说出现了404错误,会请求/404.html) |
1
| location = /404.html { #这是一个典型的location |
1
| # redirect server error pages to the |
1
| error_page 500 502 503 504 /50x.html; |
1
| location = /50x.html { |
1
| # proxy the PHP scripts to Apache listening on 127.0.0.1:80 |
1
| # 因为我们不用Nginx做Apache的反向代理,所以不需要这个 |
1
| #location ~ \.php$ { |
1
| # proxy_pass http: |
1
| # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 |
1
| # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 |
1
| # 这种写法可以防止把恶意程序伪装成.jpg之类的攻击,(其实有个更简单的方法,就是把php.ini中的cgi.fix_pathinfo=0,但有时候简单的修改cgi.fix_pathinfo会造成有的php脚本出错) |
1
| location ~ [^/]\.php(/|$) { |
1
| fastcgi_split_path_info ^(.+?\.php)(/.*)$; |
1
| |
1
| =404; #这个try_files说明:对于.php文件,直接执行 |
1
| ,直接给出404错误,(和 location / 定义不同!),主要是为了防止 伪装成图片的攻击 (目前看,最安全的方式,是用上面那一句话,官方推荐的) |
1
| fastcgi_pass 127.0.0.1:9000; |
1
| fastcgi_index index.php; |
1
| fastcgi_param SCRIPT_FILENAME |
1
| # deny access to .htaccess files, |
1
| Apache's document root |
1
| # concurs with nginx's one |
1
| location ~ /\.ht { |
|
设置完aaa.com的环境后,还需要设置bbb.com的 nginx的配置,因为bbb.com是wordpress的站点, 除了和aaa.com相同的设置外,还有些特殊的设置,具体设置请参考 http://codex.wordpress.org/Nginx
至此,从Apache向Nginx的移植基本完成。