先上个规划图,自己随便画的,大家也随便看看

wKioL1RTUAeQsDmnAAFAnW_wHjY774.jpg


192.168.1.119为nginx做反向代理并处理静态页面,apache处理动态页面,nfs共享网站家目录,svn代码管理。

192.168.101为apache处理动态页面,挂载192.168.1.119的共享目录。

192.168.1.96为mysql数据库,允许两台web服务的连接权限。


系统使用centos6.5

一,配置192.168.1.119

1.首先在192.168.1.119上安装nginx+apache+php+nfs+svn

yum install -y nginx httpd php php-mysql nfs-utils portmap subversion

2.修改nginx配置文件

vim /etc/nginx/nginx.conf
user  www www;

worker_processes auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
     {
          use epoll;
          worker_connections 51200;
          multi_accept on;
     }

http
     {
          include       mime.types;
          default_type  application/octet-stream;

          server_names_hash_bucket_size 128;
          client_header_buffer_size 32k;
          large_client_header_buffers 4 32k;
          client_max_body_size 50m;

          sendfile on;
          tcp_nopush     on;

          keepalive_timeout 60;

          tcp_nodelay on;

          fastcgi_connect_timeout 300;
          fastcgi_send_timeout 300;
          fastcgi_read_timeout 300;
          fastcgi_buffer_size 64k;
          fastcgi_buffers 4 64k;
          fastcgi_busy_buffers_size 128k;
          fastcgi_temp_file_write_size 256k;

          gzip on;
          gzip_min_length  1k;
          gzip_buffers     4 16k;
          gzip_http_version 1.0;
          gzip_comp_level 2;
          gzip_types       text/plain application/x-javascript text/css application/xml;
          gzip_vary on;
          gzip_proxied        expired no-cache no-store private auth;
          gzip_disable        "MSIE [1-6]\.";

          #limit_conn_zone $binary_remote_addr zone=perip:10m;
          ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

          #log format
          log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

upstream backend_http {
     server 192.168.1.101:80;
     server 127.0.0.1:88;
}


server
     {
          listen 80 default;
          #listen [::]:80 default ipv6only=on;
          server_name 192.168.1.119;
          index index.html index.htm index.php;
          root  /home/wwwroot/repo;
          access_log logs/access.log combined;

          #location / {
          #     try_files $uri @apache;
          #     }

          #location @apache {
          #     internal;
          #     proxy_pass http://127.0.0.1:88;
          #     include proxy.conf;
          #     }
         
          location ~ [^/]\.php(/|$)
               {
                    proxy_pass http://backend_http;
                    include proxy.conf;
               }

          location /nginx_status {
               stub_status on;
               access_log   off;
          }

          location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
               {
                    expires      30d;
               }

          location ~ .*\.(js|css)?$
               {
                    expires      12h;
               }

          access_log  /home/wwwlogs/access.log  access;
     }
include vhost/*.conf;
}
检查配置文件
nginx -t
启动服务
nginx
重新加载配置
nginx -s reload


3.修改apache配置文件

vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ServerAdmin charlie.cen@tappal.com
    DocumentRoot /home/wwwroot/repo
    ServerName 192.168.1.119
</VirtualHost>

启动服务
service httpd start
chkconfig httpd on

4.创建svn仓库

mkdir /opt/svn && svnadmin create /opt/svn/repo
使用密码认证
vim /opt/svn/repo/conf/svnerve.conf
password = 取消这行的注释
添加用户和密码
echo "charlie = charlie" >> /opt/svn/repo/conf/passwd
启动服务
svnserve -d -r /opt/svn
取出svn仓库
cd /home/wwwroot
svn co svn://localhost/repo
修改svn更新后直接到网站家目录
cd /opt/svn/repo/hooks
cp post-commit.tmpl post-commit
vim post-commit
export LANG=en_CN.UTF-8
svn update /home/wwwroot/repo --username charlie --password charlie --no-auth-cache
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf (此行注释)
添加执行权限
chmod +x post-commit

5.配置共享目录

vim /etc/exports
/home/wwwroot/repo 192.168.1.0/24(sync,rw,no_root_squash)
启动服务
/etc/init.d/rpcbind start
/etc/init.d/nfs start
查看共享目录
exportfs -v
加载共享目录使生效
exportfs -r

二,配置192.168.1.101


6.安装httpd

yum install -y httpd php php-mysql

7.配置apache

vim /etc/httpd/conf/httpd.conf
<VirtualHost *:80>
    ServerAdmin charlie.cen@tappal.com
    DocumentRoot /home/wwwroot/repo
    ServerName 192.168.1.101
</VirtualHost>

8.挂载共享目录

mkdir /home/wwwroot/repo -pv
mount -t nfs 192.168.1.119:/home/wwwroot/repo /home/wwwroot/repo

9.启动服务

service httpd start
chkconfig httpd on

三,安装mysql

10.安装mysql

yum install -y mysql mysql-server

11.启动服务

service mysqld start
chkconfig mysqld on

12.mysql创建密码

mysqladmin -uroot -password charlie

13.授权用户

mysql -uroot -pcharlie
grant all on *.* to charlie@'192.168.1.%' identified by 'charlie';
flush privileges;

四,测试网站

14.svn导出到本地

wKiom1RTVuDjR0CAAAE9-vJnSv4985.jpg

新建个测试页面

wKioL1RTVzvwBt83AABZGGTMuX0158.jpg

然后访问测试

wKiom1RTVuHgwc26AACP9c9ac78718.jpg

刷新后版本不同,证明客户端请求php页面会轮询给后端的apache服务器。

wKioL1RTVzuDaaMyAACj7w5g-EY125.jpg

如果客户端请求的页面不能保持会话,会经常切换页面导致用户账户登录等问题,所以需要更改nginx配置

wKiom1RTVuHwnx-yAABgjlmiERo615.jpg

添加ip_hash保持会话连接,然后重启nginx -s reload

再次刷新页面不会切换



测试连接数据库,下面页面是测试页

wKiom1RTVuKQUQccAADIwANIdgM606.jpg


添加并确认

到两台网站服务器上查看是否存在该文件,并检查内容是否一致!

wKioL1RTVz3yWBDnAAEINc8d2c0435.jpg

然后访问,证明成功连接后端数据库

wKiom1RTVuLRT89eAABOsnOUSK8040.jpg