apache+nginx 实现动静分离
这两天做了一下apache+nginx实现动静分离的实验,实验大概是这样的,搭建LAMP之后,再装上Nginx。用户访问页面的请求到达服务器之后,静态页面又nginx出来,动态页面则交给apache处理。这是因为apache处理静态页面的效率不高,远不及nginx。通过nginx的反向代理加速,直接将请求丢给apache去处理,达到动静分离的效果。下面是实验的过程:实验平台:RHEL6.3_x64 最小化安装IP:192.168.30.114# yum install cmake gcc gcc-c++ make ncurses-devel bison wget
# wget http://dev.mysql.com/get/Downloa ... tp://cdn.mysql.com/
一、安装mysql
# useradd -s /sbin/nologin -M mysql
# cd mysql-5.5.29
# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DINSTALL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=complex -DMYSQL_USER=mysql
# make && make install
# cd /usr/local/mysql/
# chown -R mysql:mysql.
# mkdir /data
# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql
# chown -R root .
# rm -rf /etc/my.cnf
# cp support-files/my-medium.cnf /etc/my.cnf
bind-address = 127.0.0.1
port = 3306
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql
user = mysql
character_set_server = utf8
......
# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
二、安装apache
# wget http://mirror.bjtu.edu.cn/apache//apr/apr-1.4.6.tar.bz2
# wget http://mirror.bjtu.edu.cn/apache//apr/apr-util-1.5.2.tar.bz2
# wget http://mirror.bjtu.edu.cn/apache//apr/apr-iconv-1.2.1.tar.bz2
# tar xf apr-1.4.6.tar.gz
# cd apr-1.4.6
# ./configure
这里有一个报错:
config.status: executing libtool commands
rm: cannot remove `libtoolT': No such file or directory
config.status: executing default commands
解决办法:
# vim configure
# $RM "$cfgfile"
找到上面那句命令,并注释
# make && make install
# vim /etc/ld.so.conf.d/apr.conf
/usr/local/apr/lib
# ldconfig
# cd ..
# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --with-apr=/usr/local/apr
# make && make install
# cd ..
# tar xf apr-iconv-1.2.1.tar.bz2
# cd apr-iconv-1.2.1
# ./configure --with-apr=/usr/local/apr
# make && make install
# tar xf httpd-2.2.22.tar.gz
# cd httpd-2.2.22
# ./configure --prefix=/usr/local/apache2 --enable-ssl=share --enable-so
# make && make install
三、安装php
# yum install libxml2-devel bzip2-devel libcurl-devel libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel
readline-devel net-snmp-devel freetype-devel zlib-devel gd libjpeg-devel
# tar xf php-5.4.11.tar.bz2
# cd php-5.4.11
# ./configure --prefix=/usr/local/php--with-apxs2=/usr/local/apache2/bin/apxs --disable-ipv6 --with-pcre-regex --with-zlib --with-bz2 --with-curl --enable-dba=shared --with-pcre-dir --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype --enable-mbstring --enable-gd-native-ttf --with-mhash --with-mysql --with-mysqli --with-pdo-mysql --with-readline --with-snmp --enable-sockets --enable-zip
# make && make install
# cp php.ini-production /usr/local/php/lib/php.ini
为apache添加支持php
# vim /usr/local/apache2/conf/httpd.conf
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
Include conf/extra/php.conf
# vim /usr/local/apache2/conf/extra/php.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php // 引用模板使apache支持php
</FilesMatch>
创建动态页面
# mkdir /data/www
# vim /data/www/index.php
<?php
phpinfo();
?>
启动apache
# bin/apachectl start
测试访问是否正常支持php
四、编译安装nginx
#yum install pcre-devel openssl-devel perl-ExtUtils-Embed
# tar xf nginx-1.2.6.tar.gz
# cd nginx-1.2.6
# useradd -s /sbin/nologin -M www
#./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module--without-http_uwsgi_module --without-http_scgi_module --without-http_upstream_ip_hash_module --with-http_perl_module --with-pcre
# make && make install
五、配置nginx
# vim conf/nginx.conf
userwww www;
worker_processes8;
worker_rlimit_nofile 65535;
pid logs/nginx.pid;
events {
worker_connections65535;
use epoll;
}
error_log /usr/local/nginx/logs/error.log info;
http {
include mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
charset gb2312;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 256;
client_header_buffer_size 256k;
client_max_body_size 100m;
large_client_header_buffers 4 256k;
sendfile on;
tcp_nopush on;
server_tokens off;
tcp_nodelay on;
proxy_send_timeout300;
proxy_read_timeout300;
proxy_buffer_size4k;
proxy_buffers 16 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 30s;
keepalive_timeout10;
# 开启压缩功能
gzipon;
gzip_http_version 1.0;
gzip_min_length1100;
gzip_comp_level3;
gzip_buffers4 32k;
gzip_types text/plain text/xml text/css application/x-javascript application/xml application/xml+rss text/javascript application/atom+xml;
ignore_invalid_headers on;
client_header_timeout3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size256;
request_pool_size32k;
output_buffers 4 64k;
postpone_output1460;
open_file_cache max=1000 inactive=300s;
open_file_cache_valid 600s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
include "/usr/local/nginx/conf/vhosts/*.conf";
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_loglogs/access.logmain;
}
# vim conf/vhosts/vhost1.conf
server{
listen 80;
server_name 192.168.30.114;
root /data/www;
index index.html index.htm index.php;
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
#所有php的页面均交由apache处理
location ~ \.(php)?$ {
proxy_set_headerHost $host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.30.114:81;
}
}
创建静态页面
# echo "hello,this is the html test." > /data/www/index.html
测试81端口能不能成功访问到页面
测试访问静态页面
测试访问动态页面
通过curl -I 可以看到访问静态页面的时候是通过Nginx处理的
# curl -I http://192.168.30.114
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 06 Jun 2013 09:26:10 GMT
Content-Type: text/html; charset=gb2312
Content-Length: 29
Last-Modified: Thu, 06 Jun 2013 09:25:48 GMT
Connection: keep-alive
Accept-Ranges: bytes
由于动态页面是通过nginx进行反向代理交给apache处理,所以返回显示的也是nginx
# curl -I http://192.168.30.114/index.php
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 06 Jun 2013 09:26:58 GMT
Content-Type: text/html; charset=gb2312
Connection: keep-alive
X-Powered-By: PHP/5.4.11
验证php是通过apache 处理的:
关闭apache 再测试访问php页面,看到访问不到php,但是能访问到静态页面
为apache安装rpaf模块,该模块用于apache做后端时获取访客真实的IP
# wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
# tar xf mod_rpaf-0.6.tar.gz
# cd mod_rpaf-0.6
# /usr/local/apache2/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
编辑/usr/local/apache2/conf/httpd.conf添加模块参数查找LoadModule php5_module modules/libphp5.so,在下方添加:# vim /usr/local/apache2/conf/httpd.conf
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 192.168.30.114
RPAFsethostname On
RPAFheader X-Forwarded-For
过来看看的 恋爱就是无数个饭局,结婚就是一个饭局。 昨天,系花对我笑了一下,乐得我晚上直数羊,一只羊,两只羊,三只羊…… 此地禁止大小便,违者没收工具。 锻炼肌肉,防止挨揍! 勃起不是万能的,但不能勃起却是万万都不能的!
页:
[1]