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/iptablesstop #setenforce0 #vim/etc/my.cnf [mysqld] server-id=1 log-bin=mysql-bin #/etc/init.d/mysqldrestart #mysql mysql>grantreplicationslaveon*.*to'tongbu'@'%'identifiedby'sa123456'; QueryOK,0rowsaffected(0.02sec) mysql>showmasterstatus; +------------------+----------+--------------+------------------+ |File|Position|Binlog_Do_DB|Binlog_Ignore_DB| +------------------+----------+--------------+------------------+ |mysql-bin.000001|252||| +------------------+----------+--------------+------------------+ 1rowinset(0.00sec) 172.16.30.4 LAMP(Mysql Slave) #/etc/init.d/iptablesstop #setenforce0 #vim/etc/my.cnf [mysqld] server-id=2 #/etc/init.d/mysqldrestart #mysql mysql>changemastertomaster_host='172.16.30.3',master_user='tongbu',master_password='sa123456',master_log_file='mysql-bin.000001',master_log_pos=252; QueryOK,0rowsaffected(0.07sec) mysql>stopslave; QueryOK,0rowsaffected,1warning(0.00sec) mysql>startslave; QueryOK,0rowsaffected(0.00sec) mysql>showslavestatus\G ***************************1.row*************************** Slave_IO_State:Waitingformastertosendevent 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: 1rowinset(0.00sec) 注:IO和SQL进程同为YES则代表主从配置成功 3、搭建NFS服务 172.16.30.2 NFS (1)创建挂载和共享目录 #/etc/init.d/iptablesstop #setenforce0 #fdisk/dev/sda np回车+200Gpw#因为我的主分区已经使用了三个,所以这里默认新建的主分区是第四个分区(建议将第四个分区分为扩展分区,而不是主分区,输入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/dataext4defaults00 #mount-a #df-Th /dev/sda4ext4197G188M187G1%/data #mkdir/data/www #mkdir/data/www/{discuz,wordpress} (2)下载并解压discuz #wgethttp://download.comsenz.com/DiscuzX/3.2/Discuz_X3.2_SC_UTF8.zip #cd/data/www #unzip/root/Discuz_X3.2_SC_UTF8.zip-ddiscuz/ #cddiscuz #mvupload/*. #ll (3)下载并解压wordpress #wgethttps://cn.wordpress.org/wordpress-4.5.3-zh_CN.tar.gz #tarxfwordpress-4.5.3-zh_CN.tar.gz-C/data/www/wordpress/ #cd/data/www/wordpress/wordpress #mv*../ #cd.. #rm-frwordpress (4)配置nfs服务 #vim/etc/exports /data/www/discuz*(rw,sync) /data/www/wordpress*(rw,sync) #/etc/init.d/nfsrestart 4、在两台LAMP服务器上分别挂载Discuz和Wordpress目录 (1)172.16.30.3MysqlMaster #yuminstallnfs-utils #cd/var/www/html/ #mount-tnfs172.16.30.2:/data/www/. 或者 #mount-tnfs172.16.30.2:/data/www/discuz. #mount-tnfs172.16.30.2:/data/www/wordpress. 注:挂载过后需要退出当前目录,然后再次进入挂载目录才会看到挂载后的内容 (2)172.16.30.4 Mysql Slave #yuminstallnfs-utils #cd/var/www/html/ #mount-tnfs172.16.30.2:/data/www/. 或者 #mount-tnfs172.16.30.2:/data/www/discuz. #mount-tnfs172.16.30.2:/data/www/wordpress. 5、在两台LAMP服务器上配置相同虚拟主机并进行向导安装 (1)、配置虚拟主机 #cd/etc/httpd/conf #vimvhosts.conf NameVirtualHost*:80 <VirtualHost*:80> ServerAdminfengzq@gnnt.com.cn DocumentRoot"/var/www/html/discuz" ServerNamed.zjzd.cn <Directory"/var/www/html/discuz"> AllowOverrideAll Options-IndexesFollowSymLinks Orderallow,deny Allowfromall </Directory> ErrorLoglogs/error_log CustomLoglogs/access_logcommon </VirtualHost> <VirtualHost*:80> ServerAdminfengzq@gnnt.com.cn DocumentRoot"/var/www/html/wordpress" ServerNamew.zjzd.cn <Directory"/var/www/html/wordpress"> AllowOverrideAll Options-IndexesFollowSymLinks Orderallow,deny Allowfromall </Directory> ErrorLoglogs/error_log CustomLoglogs/access_logcommon </VirtualHost> #vimhttpd.conf Includeconf/vhosts.conf #/etc/init.d/httpdrestart (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>createdatabasediscuz; QueryOK,1rowaffected(0.01sec) mysql>createdatabasewordpress; QueryOK,1rowaffected(0.00sec) mysql>grantallondiscuz.*todiscuz@'172.16.30.%'identifiedby'discuz'; QueryOK,0rowsaffected(0.00sec) mysql>grantallonwordpress.*towps@'172.16.30.%'identifiedby'wordpress'; QueryOK,0rowsaffected(0.00sec) mysql>flushprivileges; QueryOK,0rowsaffected(0.00sec) mysql>quit Bye (4)、配置数据库服务器为域名地址 172.16.30.3 Mysql Master #vim/etc/hosts 172.16.30.3mysql.zjzd.cn#注意:在LAP环境下,解析是配置在web服务器上的,并非是数据库服务器,这里LAP+Mysql是同一台服务器,所以才这样配置 172.16.30.4 Mysql Slave #vim/etc/hosts 172.16.30.3mysql.zjzd.cn (5)网页向导安装Discuz服务(略) 步骤:检查安装环境-->设置运行环境-->创建数据库-->安装 172.16.30.5 Windows #http://d.zjzd.cn //当检查安装环境的时候,个别目录会提示不存在或不可写的状态,只需要执行以下操作即可 172.16.30.2 NFS #cd/data/www/discuz #chmod-R777data/uc_*config/ (6)网页向导安装Wordpress服务(略) 安装进行时若有警告提示,按以下操作即可 172.16.30.2 NFS #vim/data/www/wordpress/wp-config.php#根据提示复制框中的内容 6、配置nginx负载均衡 (1)nginx的安装 #/etc/init.d/iptablesstop #setenforce0 #useraddwww #yuminstallpcrepcre-developensslopenssl-devel #wgethttp://nginx.org/download/nginx-1.10.2.tar.gz #tar-xfnginx-1.10.2.tar.gz #cdnginx-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&&makeinstall #/usr/local/nginx/sbin/nginx-t #/usr/local/nginx/sbin/nginx #netstat-tunlp|grep80 (2)配置nginx负载均衡虚拟主机 #cd/usr/local/nginx/conf #mkdirdomains #cddomains #vimd.zjzd.cn upstreamdiscuz_web{ server172.16.30.3:80weight=1max_fails=2fail_timeout=30s; server172.16.30.4:80weight=1max_fails=2fail_timeout=30s; } server { listen80; server_named.zjzd.cn;#注:这里的域名可以和apache中虚拟主机的域名不一致 location/ { proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://discuz_web; } } #vimw.zjzd.cn upstreamwordpress_web{ server172.16.30.3:80weight=1max_fails=2fail_timeout=30s; server172.16.30.4:80weight=1max_fails=2fail_timeout=30s; } server { listen80; server_namew.zjzd.cn; location/ { proxy_next_upstreamhttp_502http_504errortimeoutinvalid_header; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://wordpress_web; } } #/usr/local/nginx/sbin/nginx-sreload (3)、配置nginx.conf文件 #vim/usr/local/nginx/conf/nginx.conf worker_processes1; error_loglogs/error.log; pidlogs/nginx.pid; events{ worker_connections1024; } http{ includemime.types; default_typeapplication/octet-stream; sendfileon; keepalive_timeout65; gzipon; includedomains/*;#主要是添加这一行内容,导入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/httpdstop 172.16.30.4 LAMP(Mysql Slave) #/etc/init.d/httpdstop 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/2317:11:47[error]71293#0:*339connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/forum.phpHTTP/1.1",upstream:"http://172.16.30.3:80/forum.php",host:"d.zjzd.cn" 2016/11/2317:11:47[error]71293#0:*339connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/forum.phpHTTP/1.1",upstream:"http://172.16.30.4:80/forum.php",host:"d.zjzd.cn" 2016/11/2317:11:47[error]71293#0:*339connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/favicon.icoHTTP/1.1",upstream:"http://172.16.30.4:80/favicon.ico",host:"d.zjzd.cn",referrer:"http://d.zjzd.cn/forum.php" 2016/11/2317:11:47[error]71293#0:*339connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/favicon.icoHTTP/1.1",upstream:"http://172.16.30.3:80/favicon.ico",host:"d.zjzd.cn",referrer:"http://d.zjzd.cn/forum.php" 2016/11/2317:11:50[error]71293#0:*349connect()failed(111:Connectionrefused)whileconnectingtoupstream,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/2317:11:50[error]71293#0:*349connect()failed(111:Connectionrefused)whileconnectingtoupstream,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/2317:13:51[error]71293#0:*355connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/forum.phpHTTP/1.1",upstream:"http://172.16.30.3:80/forum.php",host:"d.zjzd.cn" 2016/11/2317:13:51[error]71293#0:*355connect()failed(111:Connectionrefused)whileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/forum.phpHTTP/1.1",upstream:"http://172.16.30.4:80/forum.php",host:"d.zjzd.cn" 2016/11/2317:13:51[error]71293#0:*355noliveupstreamswhileconnectingtoupstream,client:172.16.0.25,server:d.zjzd.cn,request:"GET/favicon.icoHTTP/1.1",upstream:"http://discuz_web/favicon.ico",host:"d.zjzd.cn",referrer:"http://d.zjzd.cn/forum.php"