使用NFS作为web服务共享存储
192.168.244.129
安装NFS软件包
[root@nfs ~]# yum install nfs-utils
创建共享存储挂载点并输出共享
[root@nfs ~]# mkdir /shared [root@nfs ~]# chmod 777 /shared/ [root@nfs ~]# vi /etc/exports /shared 192.168.244.0/24(rw,sync)
启动NFS服务
[root@nfs ~]# service nfs start Starting NFS services: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] [root@nfs ~]#
查看当前输出共享信息
[root@nfs ~]# showmount -e Export list for nfs: /shared 192.168.244.0/24 [root@nfs ~]#
配置第一台apache主机
192.168.244.130
安装PHP与HTTPD软件包
[root@ap1 ~]# yum install httpd php
编辑apache配置文件
[root@ap1 ~]# vi /etc/httpd/conf/httpd.conf Listen 8001 ServerName 192.168.244.130:8001
查看NFS主机共享存储状态
[root@ap1 ~]# showmount -e 192.168.244.129 Export list for 192.168.244.129: /shared 192.168.244.0/24 [root@ap1 ~]#
挂载NFS共享存储并查看挂载状态
[root@ap1 ~]# mount -t nfs 192.168.244.129:/shared /var/www/html/ [root@ap1 ~]# mount /dev/mapper/VolGroup-lv_root on / type ext4 (rw) proc on /proc type proc (rw) sysfs on /sys type sysfs (rw) devpts on /dev/pts type devpts (rw,gid=5,mode=620) tmpfs on /dev/shm type tmpfs (rw) /dev/sda1 on /boot type ext4 (rw) none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw) 192.168.244.129:/shared on /var/www/html type nfs (rw,vers=4,addr=192.168.244.129,clientaddr=192.168.244.130) [root@ap1 ~]#
修改分区挂载文件保证重启后自动挂载
[root@ap1 ~]# vi /etc/fstab 192.168.244.129 /var/www/html nfs defaults 0 0
创建第一个PHP测试文件
[root@ap1 ~]# touch /var/www/html/ap1.php [root@ap1 ~]# echo "<?php phpinfo(); ?>" > /var/www/html/ap1.php [root@ap1 ~]# service httpd start Starting httpd: [ OK ] [root@ap1 ~]#
配置第二台apache主机
192.168.244.131
[root@ap2 ~]# yum install httpd php [root@ap2 ~]# vi /etc/httpd/conf/httpd.conf Listen 8002 ServerName 192.168.244.131:8002 [root@ap2 ~]# mount -t nfs 192.168.244.129:/shared /var/www/html/ [root@ap2 ~]# vi /etc/fstab 192.168.244.129 /var/www/html nfs defaults 0 0 [root@ap2 ~]# touch /var/www/html/ap2.php [root@ap2 ~]# echo "<?php phpinfo(); ?>" > /var/www/html/ap2.php [root@ap2 ~]# service httpd start Starting httpd: [ OK ] [root@ap2 ~]#
配置第一台nginx主机
192.168.244.132
[root@ng1 ~]# yum install gcc make
[root@ng1 ~]# groupadd nginx [root@ng1 ~]# useradd -g nginx nginx -s /sbin/nologin
[root@ng1 ~]# yum install gcc-c++ perl
[root@ng1 ~]# cd nginx-1.4.0 [root@ng1 nginx-1.4.0]# ./configure --prefix=/usr/local/nginx \ > --with-http_ssl_module \ > --with-pcre=../pcre-8.32/ --with-zlib=../zlib-1.2.8 \ > --with-openssl=../openssl-1.0.1e [root@ng1 nginx-1.4.0]# make [root@ng1 nginx-1.4.0]# make install
[root@ng1 nginx-1.4.0]# export PATH=/usr/local/nginx/sbin:$PATH
修改nginx配置文件
[root@ng1 nginx-1.4.0]# cd /usr/local/nginx/conf/
[root@ng1 conf]# vi nginx.conf
user nginx;
server {
listen 8003;
server_name 192.168.244.132;
access_log logs/ng1.access.log;
location / {
root html;
index index.html index.htm;
}
}
检测nginx配置文件语法正确性
[root@ng1 conf]# nginx -t -c /usr/local/nginx/conf/nginx.conf nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@ng1 conf]#
挂载NFS共享存储
[root@ng1 conf]# mount -t nfs 192.168.244.129:/shared /usr/local/nginx/html/ [root@ng1 conf]# vi /etc/fstab 192.168.244.129 /usr/local/nginx/html nfs defaults 0 0 [root@ng1 conf]#
创建nginx静态测试文件
[root@ng1 conf]# touch ../html/ng1.html
[root@ng1 conf]# echo “hello nginx” > ../html/ng1.html
查看已上传测试文件及图片
[root@ng1 ~]# ls /usr/local/nginx/html/
ap1.php ap2.php ng1.html nginx.gif nginx.html
[root@ng1 ~]#
配置nginx反向代理服务器
192.168.244.140
使用两台后端apache主机的单一负载均衡配置
[root@rproxy ~]# cd /usr/local/nginx/conf/ upstream apache { server 192.168.244.130:8001; server 192.168.244.131:8002; } server { listen 80; server_name 192.168.244.140; location / { proxy_pass http://apache; } }
[root@ap1 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:12:32 +0800] “GET /icons/apache_pb.gif HTTP/1.0” 200 2326 “http://192.168.244.140/” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ap2 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:12:32 +0800] “GET / HTTP/1.0” 403 5039 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
192.168.244.140 – – [09/May/2013:14:12:32 +0800] “GET /icons/poweredby.png HTTP/1.0” 200 3956 “http://192.168.244.140/” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ap1 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:17:37 +0800] “GET /nginx.html HTTP/1.0” 200 333 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
192.168.244.140 – – [09/May/2013:14:17:37 +0800] “GET /favicon.ico HTTP/1.0” 404 283 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ap2 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:17:37 +0800] “GET /nginx.gif HTTP/1.0” 200 524 “http://192.168.244.140/nginx.html” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
使用两台apache主机与一台nginx主机分别处理动态和静态文件,同时负载均衡apache主机
动态文件需要服务器额外处理后通过浏览器将结果返回给客户
静态文件不需要额外服务器处理可以直接将结果给客户
upstream apache {
server 192.168.244.130:8001;
server 192.168.244.131:8002;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://192.168.244.132:8003;
expires 7d;
}
location ~ \.php$ {
proxy_pass http://apache;
}
}
[root@ng1 ~]# cat /usr/local/nginx/logs/error.log
2013/05/09 14:41:46 [error] 19504#0: *1 open() “/usr/local/nginx/html/ningix.html” failed (2: No such file or directory), client: 192.168.244.140, server: 192.168.244.132, request: “GET /ningix.html HTTP/1.0”, host: “192.168.244.132:8003”
[root@ng1 ~]# cat /usr/local/nginx/logs/ng1.access.log
192.168.244.140 – – [09/May/2013:14:43:23 +0800] “GET /nginx.html HTTP/1.0” 200 333 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
192.168.244.140 – – [09/May/2013:14:43:23 +0800] “GET /nginx.gif HTTP/1.0” 200 524 “http://192.168.244.140/nginx.html” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
192.168.244.140 – – [09/May/2013:14:43:24 +0800] “GET /favicon.ico HTTP/1.0” 404 570 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ap1 ~]# cat /var/log/httpd/access_log |grep 14:43:
[root@ap1 ~]#
[root@ap2 ~]# cat /var/log/httpd/access_log |grep 14:43:
[root@ap2 ~]#
[root@ap1 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:46:45 +0800] “GET /ap2.php HTTP/1.0” 200 45086 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
192.168.244.140 – – [09/May/2013:14:46:45 +0800] “GET /ap2.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.0” 200 2146 “http://192.168.244.140/ap2.php” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ap2 ~]# cat /var/log/httpd/access_log
192.168.244.140 – – [09/May/2013:14:46:45 +0800] “GET /ap2.php?=PHPE9568F34-D428-11d2-A769-00AA001ACF42 HTTP/1.0” 200 2524 “http://192.168.244.140/ap2.php” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”
[root@ng1 ~]# cat /usr/local/nginx/logs/ng1.access.log
192.168.244.140 – – [09/May/2013:15:56:03 +0800] “GET /ng1.html HTTP/1.0” 200 12 “-” “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)”