首页 > 代码库 > Nginx+NFS+LAMP主从架构
Nginx+NFS+LAMP主从架构
业务需求
1、nginx服务器负责调度2台(2台服务器均布署discuz和wordpress服务)web服务器
2、NFS服务器存放2台web服务器的网页文件
3、mysql服务器存放2台web服务器网站数据文件
4、它们的域名分别是:d.zjzd.cn(discuz)和w.zjzd.cn(wordpress)
1、IP地址规划
172.16.30.1 Nginx
172.16.30.2 NFS
172.16.30.3 LAMP(Mysql Master)
172.16.30.4 LAMP(Mysql Slave)
172.16.30.5 Windows
2、mysql主从配置
172.16.30.3 LAMP(Mysql Master)
#/etc/init.d/iptables stop #setenforce 0 #vim /etc/my.cnf [mysqld] server-id = 1 log-bin=mysql-bin #/etc/init.d/mysqld restart #mysql mysql> grant replication slave on *.* to ‘tongbu‘@‘%‘ identified by ‘sa123456‘; Query OK, 0 rows affected (0.02 sec) mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 252 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
172.16.30.4 LAMP(Mysql Slave)
#/etc/init.d/iptables stop #setenforce 0 #vim /etc/my.cnf [mysqld] server-id = 2 #/etc/init.d/mysqld restart #mysql mysql> change master to master_host=‘172.16.30.3‘,master_user=‘tongbu‘,master_password=‘sa123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=252; Query OK, 0 rows affected (0.07 sec) mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.30.3 Master_User: tongbu Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 252 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 252 Relay_Log_Space: 407 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)
注:IO和SQL进程同为YES则代表主从配置成功
3、搭建NFS服务
172.16.30.2 NFS
(1)创建挂载和共享目录
#/etc/init.d/iptables stop #setenforce 0 #fdisk /dev/sda n p 回车 +200G p w #因为我的主分区已经使用了三个,所以这里默认新建的主分区是第四个分区(建议将第四个分区分为扩展分区,而不是主分区,输入e即可) #mkfs.ext4 /dev/sda4 #mkdir /data #blkid /dev/sda4 /dev/sda4: UUID="520d6185-1545-4ee2-87c4-84c535a4a534" TYPE="ext4" #vim /etc/fstab UUID=520d6185-1545-4ee2-87c4-84c535a4a534 /data ext4 defaults 0 0 #mount -a #df -Th /dev/sda4 ext4 197G 188M 187G 1% /data #mkdir /data/www #mkdir /data/www/{discuz,wordpress}
(2)下载并解压discuz
#wget http://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip #cd /data/www #unzip /root/Discuz_X3.2_SC_UTF8.zip -d discuz/ #cd discuz #mv upload/* . #ll
(3)下载并解压wordpress
#wget https://cn.wordpress.org/wordpress-4.5.3-zh_CN.tar.gz #tar xf wordpress-4.5.3-zh_CN.tar.gz -C /data/www/wordpress/ #cd /data/www/wordpress/wordpress #mv * ../ #cd .. #rm -fr wordpress
(4)配置nfs服务
#vim /etc/exports /data/www/discuz *(rw,sync) /data/www/wordpress *(rw,sync) #/etc/init.d/nfs restart
4、在两台LAMP服务器上分别挂载Discuz和Wordpress目录
(1)172.16.30.3 Mysql Master #yum install nfs-utils #cd /var/www/html/ #mount -t nfs 172.16.30.2:/data/www/ . 或者 #mount -t nfs 172.16.30.2:/data/www/discuz . #mount -t nfs 172.16.30.2:/data/www/wordpress .
注:挂载过后需要退出当前目录,然后再次进入挂载目录才会看到挂载后的内容
(2)172.16.30.4 Mysql Slave
#yum install nfs-utils #cd /var/www/html/ #mount -t nfs 172.16.30.2:/data/www/ . 或者 #mount -t nfs 172.16.30.2:/data/www/discuz . #mount -t nfs 172.16.30.2:/data/www/wordpress .
5、在两台LAMP服务器上配置相同虚拟主机并进行向导安装
(1)、配置虚拟主机
#cd /etc/httpd/conf #vim vhosts.conf NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin fengzq@gnnt.com.cn DocumentRoot "/var/www/html/discuz" ServerName d.zjzd.cn <Directory "/var/www/html/discuz"> AllowOverride All Options -Indexes FollowSymLinks Order allow,deny Allow from all </Directory> ErrorLog logs/error_log CustomLog logs/access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin fengzq@gnnt.com.cn DocumentRoot "/var/www/html/wordpress" ServerName w.zjzd.cn <Directory "/var/www/html/wordpress"> AllowOverride All Options -Indexes FollowSymLinks Order allow,deny Allow from all </Directory> ErrorLog logs/error_log CustomLog logs/access_log common </VirtualHost> #vim httpd.conf Include conf/vhosts.conf #/etc/init.d/httpd restart
(2)、配置主机解析
首先找到C:\Windows\System32\drivers\etc\hosts文件,然后右击选择"Edit with notepad++"添加以下内容,最后通过浏览器访问进行测试
172.16.30.3 d.zjzd.cn
172.16.30.3 w.zjzd.cn
172.16.30.4 d.zjzd.cn
172.16.30.4 w.zjzd.cn
(3)、创建数据库及授权用户
172.16.30.3 Mysql Master
#mysql mysql> create database discuz; Query OK, 1 row affected (0.01 sec) mysql> create database wordpress; Query OK, 1 row affected (0.00 sec) mysql> grant all on discuz.* to discuz@‘172.16.30.%‘ identified by ‘discuz‘; Query OK, 0 rows affected (0.00 sec) mysql> grant all on wordpress.* to wps@‘172.16.30.%‘ identified by ‘wordpress‘; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye
(4)、配置数据库服务器为域名地址
172.16.30.3 Mysql Master
#vim /etc/hosts 172.16.30.3 mysql.zjzd.cn #注意:在LAP环境下,解析是配置在web服务器上的,并非是数据库服务器,这里LAP+Mysql是同一台服务器,所以才这样配置
172.16.30.4 Mysql Slave
#vim /etc/hosts 172.16.30.3 mysql.zjzd.cn
(5)网页向导安装Discuz服务(略)
步骤:检查安装环境-->设置运行环境-->创建数据库-->安装
172.16.30.5 Windows
#http://d.zjzd.cn //当检查安装环境的时候,个别目录会提示不存在或不可写的状态,只需要执行以下操作即可
172.16.30.2 NFS
#cd /data/www/discuz #chmod -R 777 data/ uc_* config/
(6)网页向导安装Wordpress服务(略)
安装进行时若有警告提示,按以下操作即可
172.16.30.2 NFS
#vim /data/www/wordpress/wp-config.php #根据提示复制框中的内容
6、配置nginx负载均衡
(1)nginx的安装
#/etc/init.d/iptables stop #setenforce 0 #useradd www #yum install pcre pcre-devel openssl openssl-devel #wget http://nginx.org/download/nginx-1.10.2.tar.gz #tar -xf nginx-1.10.2.tar.gz #cd nginx-1.10.2.tar.gz #./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-pcre #make && make install #/usr/local/nginx/sbin/nginx -t #/usr/local/nginx/sbin/nginx #netstat -tunlp | grep 80
(2)配置nginx负载均衡虚拟主机
#cd /usr/local/nginx/conf #mkdir domains #cd domains
#vim d.zjzd.cn upstream discuz_web { server 172.16.30.3:80 weight=1 max_fails=2 fail_timeout=30s; server 172.16.30.4:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name d.zjzd.cn; #注:这里的域名可以和apache中虚拟主机的域名不一致 location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://discuz_web; } }
#vim w.zjzd.cn upstream wordpress_web { server 172.16.30.3:80 weight=1 max_fails=2 fail_timeout=30s; server 172.16.30.4:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name w.zjzd.cn; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://wordpress_web; } }
#/usr/local/nginx/sbin/nginx -s reload
(3)、配置nginx.conf文件
#vim /usr/local/nginx/conf/nginx.conf worker_processes 1; error_log logs/error.log; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; gzip on; include domains/*; #主要是添加这一行内容,导入vhosts文件 }
(4)、配置主机解析
172.16.30.3 windows
首先找到C:\Windows\System32\drivers\etc\hosts文件,然后右击选择"Edit with notepad++"添加以下内容,最后通过浏览器访问进行测试
172.16.30.1 d.zjzd.cn w.zjzd.cn //这里请求的是nginx服务器的内容
(5)、测试
通过停止所有后端服务器,然后访问网站,最后查询nginx错误日志即可
172.16.30.3 LAMP(Mysql Master)
#/etc/init.d/httpd stop
172.16.30.4 LAMP(Mysql Slave)
#/etc/init.d/httpd stop
172.16.30.5 windows
http://d.zjzd.cn
http://w.zjzd.cn
172.16.30.1 nginx
#tail /usr/local/nginx/logs/error.log 2016/11/23 17:11:47 [error] 71293#0: *339 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /forum.php HTTP/1.1", upstream: "http://172.16.30.3:80/forum.php", host: "d.zjzd.cn" 2016/11/23 17:11:47 [error] 71293#0: *339 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /forum.php HTTP/1.1", upstream: "http://172.16.30.4:80/forum.php", host: "d.zjzd.cn" 2016/11/23 17:11:47 [error] 71293#0: *339 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.16.30.4:80/favicon.ico", host: "d.zjzd.cn", referrer: "http://d.zjzd.cn/forum.php" 2016/11/23 17:11:47 [error] 71293#0: *339 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.16.30.3:80/favicon.ico", host: "d.zjzd.cn", referrer: "http://d.zjzd.cn/forum.php" 2016/11/23 17:11:50 [error] 71293#0: *349 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: w.zjzd.cn, request: "GET / HTTP/1.1", upstream: "http://172.16.30.3:80/", host: "w.zjzd.cn" 2016/11/23 17:11:50 [error] 71293#0: *349 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: w.zjzd.cn, request: "GET / HTTP/1.1", upstream: "http://172.16.30.4:80/", host: "w.zjzd.cn" 2016/11/23 17:13:51 [error] 71293#0: *355 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /forum.php HTTP/1.1", upstream: "http://172.16.30.3:80/forum.php", host: "d.zjzd.cn" 2016/11/23 17:13:51 [error] 71293#0: *355 connect() failed (111: Connection refused) while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /forum.php HTTP/1.1", upstream: "http://172.16.30.4:80/forum.php", host: "d.zjzd.cn" 2016/11/23 17:13:51 [error] 71293#0: *355 no live upstreams while connecting to upstream, client: 172.16.0.25, server: d.zjzd.cn, request: "GET /favicon.ico HTTP/1.1", upstream: "http://discuz_web/favicon.ico", host: "d.zjzd.cn", referrer: "http://d.zjzd.cn/forum.php"
本文出自 “紫禁之巅” 博客,请务必保留此出处http://zjzd86.blog.51cto.com/6124664/1876045
Nginx+NFS+LAMP主从架构