积累经验
2014年(55)
分类: 系统运维
2014-01-22 22:57:22
nginx
反向代理(Reverse Proxy)方式是指以代理服务器来接受 internet 上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给 internet 上请求连接的 客户端,此时代理服务器对外就表现为一个服务器。反向代理又称为 Web 服务器加速,是针对 Web 服务器提供加速功能的。它作为代理Cache,但并不针对浏览器用户,而针对一台或多台特定 Web 服务器(这也是反向代理名称的由来)。代理服务器可以缓存一些 web 的页面,降低了 web 服务器的访问量,所以可以降低 web 服务器的负载.
实验一 128搭建LEMP Discuz论坛
实验二 128 mysql 迁移到137, 128能继续访问论坛
实验三 128 cgi 迁移到138, 128能继续访问论坛
实验四 128 cgi 同时迁移到2台机器128作为前端反向代理,将请求以轮询方式转发138和139
实验五 添加一台NFS服务器140 (2台cgi 上传文件时分别上传到其中一台的aaa.com目录下,下载时/同时/刷新 1次成功1次会报错)
论坛用户名密码:admin uplooking
迁移时步骤:
1. nginx指向
2. 迁移服务
3. 迁移数据
4. mysql授权
实验一128搭建LEMP Discuz论坛
128monitor机器安装
LEMP装包:
先装LAMP
yum install mysql mysql-server php php-mysql –y
rpm -ivh spawn-fcgi-1.6.3-1.el6.x86_64.rpm
rpm –ivh GeoIP-1.4.8-1.el6.x86_64.rpm
rpm –ivh nginx-1.0.12-1.el6.x86_64.rpm
yum install -y gd
service nginx start
拷贝论坛并解压
mkdir /tmp/Discuz
cp Discuz_X2.5_SC_UTF8.zip /tmp/Discuz/
cp -r upload/* /usr/share/nginx/aaa.com/
chown –R nginx. /usr/share/nginx/aaa.com/*
vi /etc/sysconfig/spawn-fcgi
#OPTIONS="-u apache -g apache -s $SOCKET -S -M 0600 -C 32 -F 1 -P /var/run/spawn-fcgi.pid -- /usr/bin/php-cgi"
OPTIONS="-u nginx -g nginx -C 32 -F 1 -P /var/run/spawn-fcgi.pid -p 9000 -- /usr/bin/php-cgi"
-C 32//创建32子进程 –F 1//创建1个主进程 –p 9000端口
service spawn-fcgi start
ps -ef | grep php.cgi //进程是否开启
netstat –ntalp | grep 9000 //监听9000端口
yum install telnet -y
telnet 127.0.0.1 9000 //测试端口是否连接
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
vim /etc/nginx/conf.d/default.conf //复制以下行到virtual.conf
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
vim /etc/nginx/conf.d/virtual.conf
server {
listen 80;
server_name
# location / { //注释掉后全局生效
root /usr/share/nginx/aaa.com;
index index.php index.html index.htm;
# }
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/aaa.com$fastcgi_script_name;
include fastcgi_params;
}
service nginx restart
网页输入
建一个index.php
vi phpinfo.php
phpinfo();
?>;
实验二 128 mysql 迁移 到6ngnix137, 128能继续访问论坛
128 停mysql
此时访问(论坛)会报错
137 起mysql
137 /etc/hosts 与138保持一致 添加 192.168.11.128 desktop128.examle.com
128做以下修改
[root@desktop128 aaa.com]# for i in `find ./ -name "*.php"`;do grep uplooking $i &> /dev/null && echo $i ;done
或者[root@desktop128 aaa.com]# grep uplooking `find ./ -name "*.php" 2> /dev/nell`|awk -F ":" '{ print $1 }'
./config/config_global.php
./config/config_ucenter.php
./uc_server/data/config.inc.php
[root@desktop128 aaa.com]vim ./config/config_global.php
$_config['db']['1']['dbhost'] = 'localhost';
$_config['db']['1']['dbhost'] = '192.168.11.137';
[root@desktop128 aaa.com]vim ./config/config_ucenter.php
define('UC_DBHOST', 'localhost');
define('UC_DBHOST', '192.168.11.137');
[root@desktop128 aaa.com]vim ./uc_server/data/config.inc.php
define('UC_DBHOST', 'localhost');
define('UC_DBHOST', '192.168.11.137');
service spawn-fcgi restart
service nginx restart
在137mysql中授权给128,允许128登录137mysql
[root@desktop137 ~]# mysql –puplooking
mysql> grant all on *.* to root@'192.168.11.28' identified by 'uplooking';
//drop user ’ 删除授权 user表
养成好的习惯同时授权主机名
mysql> grant all on *.* to root@'desktop128.example.com' identified by 'uplooking';
128打开firefox,此时可以正常访问,说明mysql 128>137迁移成功。
实验三 128 spawn-fcgi 迁移到138, 128能继续访问论坛
128 spawn-fcgi 停掉
128机器登录论坛会报错
[root@desktop128 ~]# service spawn-fcgi stop
138装包spawn-fcgi php php-mysql,Mysql nginx不需要安装。
修改138 sapwn-fcgi配置文件
[root@desktop138 ~]# vim /etc/sysconfig/spawn-fcgi
OPTIONS="-u nginx -g nginx -C 32 -F 1 -P /var/run/spawn-fcgi.pid -p 9000 -- /usr/bin/php-cgi"
Service spawn-fcgi start //如果报错 yum install –y gd
修改128 nginx虚拟机配置文件
[root@desktop128 ~]# vim /etc/nginx/conf.d/virtual.conf
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass 192.168.11.138:9000;
[root@desktop128 ~]# service nginx restart
把128 /usr/share/nginx/aaa.com/* 论坛文件 拷贝到138 /usr/share/nginx/aaa.com/* 下
[root@desktop138~] mkdir –p /usr/share/nginx/aaa.com
[root@desktop128 aaa.com]# scp -r ./* 192.168.11.138:/usr/share/nginx/aaa.com
或者[root@desktop128 aaa.com]# tar -cf - . | ssh root@192.168.11.141 "tar -xf - -C /usr/share/nginx/aaa.com"
138上面没装Nginx,所以需创建nginx用户,参考128 uid gid
[root@desktop128 aaa.com]# id nginx
uid=493(nginx) gid=489(nginx) groups=489(nginx)
[root@desktop138 aaa.com]# groupadd -g 489 nginx
[root@desktop138 aaa.com]# useradd -u 493 -g nginx nginx
查看138nginx用户与128保持一致
[root@desktop138 aaa.com]# id nginx
uid=493(nginx) gid=489(nginx) groups=489(nginx)
修改aaa.com目录权限
[root@desktop138 aaa.com]# chown -R nginx. .
137 mysql给138授权
use mysql
select * from user \G; \\查看授权信息
128机器登录论坛成功
实验四 128 cgi 同时迁移2台机器138和139做轮询
139安装包
[root@desktop139 tmp]# rpm -ivh spawn-fcgi-1.6.3-1.el6.x86_64.rpm
[root@desktop139 tmp]# yum install php php-mysql –y
修改spawn-fcgi配置文件
[root@desktop139 tmp]# vim /etc/sysconfig/spawn-fcgi
OPTIONS="-u nginx -g nginx -C 32 -F 1 -P /var/run/spawn-fcgi.pid -p 9000 -- /usr/bin/php-cgi"
添加Nginx用户 //与128 nginx id保持一致
[root@desktop139 tmp]# groupadd -g 489 nginx
[root@desktop139 tmp]# useradd -u 493 -g nginx nginx
起服务
[root@desktop139 tmp]# service spawn-fcgi start
137mysql 授权给139允许访问
mysql> grant all on *.* to root@'192.168.11.139' identified by 'uplooking';
mysql> grant all on *.* to root@'desktop139.example.com' identified by 'uplooking';
mysql> flush privileges;
迁移论坛数据
[root@desktop128 aaa.com]# scp -r ./* 192.168.11.139:/usr/share/nginx/aaa.com
[root@desktop139 nginx]# cd /usr/share //139创建目录
[root@desktop139 share]# mkdir -p nginx/aaa.com
[root@desktop139 aaa.com]# chown -R nginx. . //修改权限
nginx指向
128机器修改 nginx主配置文件和虚拟机配置文件
[root@desktop128 250]# vim /etc/nginx/nginx.conf
//在http{}中添加:
upstream cgi_pool {
server 192.168.11.138:9000;
server 192.168.11.139:9000;
}
[root@desktop128 250]# vim /etc/nginx/conf.d/virtual.conf
//注释 # fastcgi_pass 192.168.11.138:9000;
//添加 fastcgi_pass cgi_pool;
128机器登录论坛 F5刷新,此时139、138轮询成功。
#proxy_pass
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404;
实验五 添加一台NFS服务器140 (2台cgi 上传文件时分别上传到其中一台的aaa.com目录下,下载时/同时/刷新 1次成功1次会失败)
上传mysql-mmm文件
修改nfs配置文件
[root@desktop140 ~]# vim /etc/exports
/var/aaa.com 192.168.11.0/24(rw,async)
[root@desktop140 ~]# service nfs restart
添加nginx用户 //与128 nginx id保持一致
[root@desktop140 tmp]# groupadd -g 489 nginx
[root@desktop140 tmp]# useradd -u 493 -g nginx nginx
数据迁移
[root@desktop139 nginx]#scp -r -p ./* 192.168.11.140:/var/aaa.com
138 139aaa.com 改名aaa.com.bak 并建一个空目录aaa.com,挂载nfs
[root@desktop138 nginx]# mv aaa.com aaa.com.bak
[root@desktop138 nginx]# mkidir aaa.com
[root@desktop138 nginx]# mount 192.168.11.140:/var/aaa.com /usr/share/nginx/aaa.com
[root@desktop1398nginx]#service spawn-fcgi restart
[root@desktop139 nginx]# mount 192.168.11.140:/var/aaa.com /usr/share/nginx/aaa.com
[root@desktop139 nginx]#service spawn-fcgi restart
此时登录128 下载时不会报错,实验成功。