Nginx+NFS+LAMP主從架構

業務需求php

一、nginx服務器負責調度2臺(2臺服務器均佈署discuz和wordpress服務)web服務器html

二、NFS服務器存放2臺web服務器的網頁文件mysql

三、mysql服務器存放2臺web服務器網站數據文件nginx

四、它們的域名分別是:d.zjzd.cn(discuz)和w.zjzd.cn(wordpress)web


一、IP地址規劃sql

172.16.30.1  Nginx數據庫

172.16.30.2  NFSapache

172.16.30.3  LAMP(Mysql Master)vim

172.16.30.4  LAMP(Mysql Slave)windows

172.16.30.5  Windows

二、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則表明主從配置成功

三、搭建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

四、在兩臺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 .

五、在兩臺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/

wKioL1g2T1DQD5ZvAADGCx8Yb2E850.png

(6)網頁嚮導安裝Wordpress服務(略)

wKiom1g2T1Cg5vL1AAAmKTovsHc609.png

安裝進行時如有警告提示,按如下操做便可

172.16.30.2 NFS

#vim /data/www/wordpress/wp-config.php  #根據提示覆制框中的內容

wKiom1g2T1GQaMJFAADj07hjEns714.jpg

六、配置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"
相關文章
相關標籤/搜索