Nginx+Apache配置
Nginx+Apache配置
http://www.litvip.com/525/
我们依然尽可能采用yum来安装我们需要的软件,由系统官方维护的软件,其安全性和稳定性都值得信赖,并且容易管理,升级方便,但是在CentOS和RHEL的官方yum源中暂时没有Nginx等软件包,所以我们需要使用EPEL的yum源。EPEL是什么,EPEL的yum源怎么添加,点击这里查看。
本文将要介绍的Nginx+Apache结构,其实就是Nginx做前端,Apache做后端,充分发挥他们各自的优势之处。Nginx对于高并发性能出众,Proxy功能强效率高,占用系统资源少,而Apache在高并发时对队列的处理比FastCGI(Nginx需要通过fastcgi等方式运行php)更好,并且在处理动态php页面时,mod_php也比php-cgi更稳定更高效。
也就是说,我们的目的是,由Nginx来接收客户端的请求,如果是动态页面请求,就交给Apache处理,然后经由Nginx再返回给客户端,其余的请求,则由Nginx自己处理,然后把结果返回给客户端,。当然了,你完全可以让Nginx只做Proxy功能,所有的请求都交给Apache,Tomcat等处理,本文使用前者。
但是,在本文中,我们实现的是在一台服务器里一个Nginx加一个Apache的简单结构,在实际应用中,可能前端是由一台或多台Nginx组成的代理服务器,后端则是多台Apache或其他Web服务器,再加上多种第三方软件而组成的集群。
前提约定:假设我们系统默认主站点名是www.litvip.com,网站根目录是/var/www/litvip
第一步,安装并配置Nginx,安装完之后,配置文件都在/etc/nginx目录下,主设置文件/etc/nginx/nginx.conf:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
[root@ test
~] # yum -y install nginx [root@ test
~] # vi /etc/nginx/nginx.conf user nginx; worker_processes 2; error_log
/var/log/nginx/error .log; pid
/var/run/nginx .pid; events { use epoll; worker_connections 2048; } http { include
/etc/nginx/mime .types; include
/etc/nginx/proxy .conf; include
/etc/nginx/gzip .conf; default_type application /octet-stream ; index index.html index.htm index.php; log_format main
'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' ; sendfile on; tcp_nopush on; server_tokens off; keepalive_timeout 60; server_names_hash_bucket_size 128; server { listen 80; server_name www.litvip.com; root
/var/www/litvip ; location ~* .*\.(gif|jpg|jpeg|png|bmp|ico|css|js|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { expires 2d; } # 如果你需要客户端缓存的内容以及媒体,图片等文件固定放置一些目录下的话,就把上面那个 # location注释掉,用下面这种方式 # location ~ ^/(images|javascript|js|css|flash|media|static)/ { # expires 2d; #} location ~ .*\.(php?|cgi|pl|py)$ { proxy_pass http: //127 .0.0.1:8888; } location ~ /\.ht { deny all; } location ~ /.+\.(inc|conf|cnf) { deny all; } access_log
/var/log/nginx/litvip-access .log main; #access_log off } # Load config files from the /etc/nginx/conf.d directory include
/etc/nginx/conf .d/*.conf; } [root@ test
~] # vi /etc/nginx/proxy.conf proxy_redirect off; proxy_hide_header Vary; proxy_set_header Accept-Encoding
'' ; 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; proxy_send_timeout 90; proxy_read_timeout 90; proxy_buffer_size 4k; proxy_buffers 32 4k; proxy_busy_buffers_size 64k; [root@ test
~] # vi /etc/nginx/gzip.conf gzip
on; gzip_http_version 1.0; gzip_disable
"MSIE [1-6]\." ; gzip_disable
"Mozilla/4" ; gzip_comp_level 3; gzip_proxied any; gzip_vary on; gzip_buffers 4 16k; gzip_min_length 1100; gzip_types text /plain
text /xml
text /css application /xml
application /xhtml +xml application /rss +xml application /atom_xml
application /javascript
application /x-javascript ; |
为了让nginx.conf简洁,我把一些相关的共通设定放到同一个专门的文件里,然后在主配置文件nginx.conf里加载。如果你使用了VirtualHost运营多个站点,你可以根据不同站点的需求而配置不同的设定文件,然后分别在各自的server域里加载。
第二步,安装并配置Apache,配置文件在/etc/httpd/下,我们修改主配置文件/etc/httpd/conf/httpd.conf,配置文件太长,我只写我需要改的地方:
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
|
[root@ test
~] # yum -y install httpd [root@ test
~] # vi /etc/httpd/conf/httpd.conf Listen 80 ↓改成 Listen 127.0.0.1:8888 DocumentRoot
"var/www/html" ↓改成 DocumentRoot
"/var/www/litvip" <Directory
"/var/www/html" > ↓改成 <Directory
"/var/www/litvip" > Options Indexes FollowSymLinks ↓改成如下,允许CGI,SSI Options Includes ExecCGI FollowSymLinks AllowOverride None ↓改成如下,支持.htaccess AllowOverride All Order allow,deny Allow from all < /Directory >
ServerSignature On ↓改成如下,在现实错误页面的时候不会显示服务器和apache的版本 ServerSignature Off #AddHandler cgi-script .cgi ↓改成如下,cgi脚本使用.cgi,.pl,.py AddHandler cgi-script .cgi .pl .py |
第三步,设置VirtualHost(如果你需要在本机上运营多个站点,否则略过此步):
1.修改/etc/httpd/conf/httpd.conf:
1
2
3
4
5
6
7
8
|
#NameVirtualHost *:80 ↓改成如下 NameVirtualHost 127.0.0.1:8888 #追加下面4行,www.litvip.com是我们的默认主站 <VirtualHost 127.0.0.1:8888> ServerName www.litvip.com < /VirtualHost >
Include virtual/*.conf |
2.修改/etc/nginx/nginx.conf,在server{}域的后面追加如下一句:
1
|
include /etc/nginx/virtual/ *.conf; |
3.假设我们还要在本机上运行一个站点叫www.cooliter.com:
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
|
[root@ test
~] # mkdir /etc/nginx/virtual/ [root@ test
~] # mkdir /etc/httpd/virtual/ [root@ test
~] # vi /etc/nginx/virtual/www.cooliter.com.conf server { listen 80; server_name www.cooliter.com; root
/var/www/cooliter ; location ~* .*\.(gif|jpg|jpeg|png|bmp|ico|css|js|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { expires 24h; } location ~ .*\.(php?|cgi|pl|py)$ { proxy_pass http: //127 .0.0.1:8888; } location ~ /\.ht { deny all; } location ~ /.+\.(inc|conf|cnf) { deny all; } access_log
/var/log/nginx/cooliter-access .log main; #access_log off } [root@ test
~] # vi /etc/httpd/virtual/www.cooliter.com.conf <VirtualHost 127.0.0.1:8888> ServerAdmin webmaster@cooliter.com DocumentRoot
/var/www/cooliter ServerName www.cooliter.com UseCanonicalName Off ErrorLog logs /www .cooliter.com-error_log CustomLog logs /www .cooliter.com-access_log common < /VirtualHost > |
第四步,安装MySql,参照这篇教程。
第五步,安装php及其他常用组件:
1
|
[root@ test
~] # yum -y install php php-mysql php-gd php-xml php-mbstring php-mcrypt
|
第六步,安装php加速器eaccelerator和php-pecl-memcached模块,提高性能:
1
|
[root@ test
~] # yum install php-eaccelerator php-pecl-memcached |
设定文件分别是/etc/php.d/eaccelerator.ini和/etc/php.d/memcached.ini,如果你采用默认设置,就不需要修改设定文件了。
第七步,安装并配置Zend提供的php加速、解密软件,yum源里没有,直接从官方下载即可,不需要编译,下载的时候注意根据你的系统选择32位或者64位的版本。
1:如果你用的php是php5.3.x版本,需要安装的是Zend Guard Loader:
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@ test
~] # tar -xzvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz [root@ test
~] # ls ZendGuardLoader-php-5.3-linux-glibc23-i386/ README.txt php-5.3.x #小提示,看解压目录下的README.txt里有使用说明 [root@ test
~] # mv -T ZendGuardLoader-php-5.3-linux-glibc23-i386 /usr/lib/php/ZendGuardLoader-php-5.3 [root@ test
~] # vi /etc/php.d/zend.ini #添加如下内容,保存退出 [Zend Guard Loader] zend_extension= "/usr/lib/php/ZendGuardLoader-php-5.3/php-5.3.x/ZendGuardLoader.so" zend_loader. enable =1 zend_loader.disable_licensing=0 zend_loader.obfuscation_level_support=3 |
2:如果你用的php是php5.2或更早的版本,需要安装的是Zend Optimizer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@ test
~] # tar -xzvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz [root@ test
~] # ls ZendOptimizer-3.3.9-linux-glibc23-i386 EULA-ZendOptimizer Inventory.xml LICENSE README-ZendOptimizer data md5 #小提示1,看解压目录下的README-ZendOptimizer里有使用说明 [root@ test
~] # ls ZendOptimizer-3.3.9-linux-glibc23-i386/data 4_2_0_comp 4_3_x_comp 5_0_x_comp 5_2_x_comp 4_2_x_comp 4_4_x_comp 5_1_x_comp poweredbyoptimizer.gif #小提示2,解压目录下的data目录下根据不同的php版本提供了不同的so文件,假设我们的版本是php5.1.x的 [root@ test
~] # mv -T ZendOptimizer-3.3.9-linux-glibc23-i386 /usr/lib/php/ZendOptimizer-3.3.9 [root@ test
~] # vi /etc/php.d/zend.ini #添加如下内容,保存退出 [Zend Optimizer] zend_optimizer.optimization_level=1 zend_extension =
"/usr/lib/php/ZendOptimizer-3.3.9/data/5_1_x_comp/ZendOptimizer.so" |
第八步,启动Apache和Nginx,并测试(MySql就不测试了):
1
2
3
4
5
6
7
|
[root@ test
~] # /etc/init.d/httpd start [root@ test
~] # /etc/init.d/nginx start [root@ test
~] # chkconfig httpd on [root@ test
~] # chkconfig nginx on [root@ test
~] # mkdir /var/www/litvip [root@ test
~] # echo "hello,litvip" > /var/www/litvip/index.html [root@ test
~] # echo -e "<?php\nphpinfo();" > /var/www/litvip/index.php |
打开浏览器分别访问一下index.html和index.php,然后关闭Apache,再分别访问一下(php页面应该就访问不了了)。
测试gzip是否有效,因为我们设置了gzip_min_length为1100,所以低于1100bytes的页面不会压缩,你测试的页面内容最好大于1100bytes。
1
2
3
4
5
6
7
8
9
10
11
|
[root@ test
~] # curl -I -H "Accept-Encoding:gzip,deflate" http://www.cooliter.com/test.txt HTTP /1 .1 200 OK Server: nginx Date: Fri, 22 Jul 2011 04:45:30 GMT Content-Type: text /plain Last-Modified: Fri, 22 Jul 2011 04:40:30 GMT Connection: keep-alive Vary: Accept-Encoding Expires: Sun, 24 Jul 2011 04:45:30 GMT Cache-Control: max-age=172800 Content-Encoding:
gzip <---证明服务器支持 gzip 压缩 |
(转发请注明转自:学PHP)
- Nginx、MySQL 和 PHP-FPM 优化 (2012-09-29 07:22:29)
- 新一篇Apache2与Tomcat7整合 (2012-09-29 21:21:52)
- Nginx 的多站点配置 (2012-10-05 23:47:36)
- Apache Lucene 4.0 正式版发布 (2012-10-13 10:54:16)
- apache两种工作模式详解 (2012-11-12 13:12:51)
- Nginx学习笔记3—FastCGI与CGI解释器的区别,及其工作原理 (2012-09-27 22:42:46)
- Nginx多域名多Server反向代理配置 (2012-09-27 22:42:35)
- php 5.3.3 中 php-fpm 的重启、终止操作命令 (2012-08-16 11:52:55)
- Apache多路处理模块(MPM)之prefork和worker的配置 (2012-08-16 11:52:51)
- (win7)IIS7下配置php5.3 (5.3以后的版本只可以使用FastCGI) (2012-08-13 11:55:27)
- 1楼 Ogun说: 2013-01-10 17:48:16
-
I thuohgt finding this would be so arduous but it's a breeze!
- 2楼 学phper说: 2014-08-16 07:54:00
-
-1'
- 3楼 学phper说: 2014-08-16 07:54:02
-
1
- 4楼 -1'说: 2014-08-16 07:54:04
-
1
- 5楼 -1'说: 2015-06-03 02:54:37
-
1
- 6楼 Bubba说: 2016-12-21 19:47:19
-
I have been so belrwdeied in the past but now it all makes sense!