nginx+apache動靜分離/負載均衡

【主從】

【Mysql-Master】

log-bin=mysql-bin
server-id = 1

MariaDB [(none)]> grant replication slave on *.* to 'slave'@'192.168.2.158' identified by '123.com';

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 551 | | |
+------------------+----------+--------------+------------------+

【Mysql-slave】

server-id = 2

MariaDB [(none)]> slave stop;
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [(none)]> change master to
-> master_host='192.168.2.157',
-> master_user='slave',
-> master_password='123.com',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=551;
Query OK, 0 rows affected (0.09 sec)

MariaDB [(none)]> slave start;
Query OK, 0 rows affected (0.00 sec)

 

主從成功~

【兩臺服務器站點虛擬主機配置】

# vim /etc/httpd/conf/httpd.conf

ServerRoot "/etc/httpd"
Listen 80
AddType     application/x-httpd-php  .php
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />
    AllowOverride none
    Require all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.php index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
Include  conf/vhosts.conf

 

mkdir -p  /var/www/html/dz.bxy.net/

mkdir -p  /var/www/html/wps.bxy.net/

#vim  /etc/httpd/conf/vhosts.conf 

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/dz.bxy.net/"
    ServerName dz.bxy.net
    ErrorLog "/etc/httpd/logs/www-error_log"
    CustomLog "/etc/httpd/logs/www-access_log" common
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/var/www/html/wps.bxy.net"
    ServerName wps.bxy.net
    ErrorLog "/etc/httpd/logs/www-error_log"
    CustomLog "/etc/httpd/logs/www-access_log" common
</VirtualHost>

 

192.168.2.157站點

【Discuz和workpress部署】

# cd /var/www/html/

#wget http://download.comsenz.com/DiscuzX/3.1/Discuz_X3.1_SC_UTF8.zip

#wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz

#unzip Discuz_X3.1_SC_UTF8.zip -d Discuz/

[root@localhost Discuz]# mv upload/* .
[root@localhost Discuz]# chmod o+x uc_server/ config/ uc_client/

MariaDB [(none)]> create database DiscuzDB charset=utf8;

MariaDB [(none)]> grant all on DiscuzDB.* to 'discuz'@'%' identified by '123.com';

MariaDB [(none)]> flush privileges

hosts解析:因爲要通過域名連接數據庫地址,那麼我們需要將數據庫的IP地址和自定義域名解析

 

 

【wordpress網站部署】

#tar zxvf wordpress-4.9.4-zh_CN.tar.gz -C wordpress/

【數據庫授權】
MariaDB [(none)]> create database wpsDB charset=utf8;

MariaDB [(none)]> grant all on *.* to 'wpsuser'@'%' identified by '123.com';

MariaDB [(none)]> flush privileges;

host解析:我們將wordpress域名解析到2.157主庫ip地址跟後面即可

 

[root@lamp-master wps.bxy.net]# vim wp-config.php

<?php
/**
 * WordPress基礎配置文件。
 *
 * 這個文件被安裝程序用於自動生成wp-config.php配置文件,
 * 您可以不使用網站,您需要手動複製這個文件,
 * 並重命名爲“wp-config.php”,然後填入相關信息。
 *
 * 本文件包含以下配置選項:
 *
 * * MySQL設置
 * * 密鑰
 * * 數據庫表名前綴
 * * ABSPATH
 *
 * @link https://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php
 *
 * @package WordPress
 */

// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wpsDB');

/** MySQL數據庫用戶名 */
define('DB_USER', 'wpsuser');

/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123.com');

/** MySQL主機 */
define('DB_HOST', 'wps.mysql.net');

/** 創建數據表時默認的文字編碼 */
define('DB_CHARSET', 'utf8mb4');

/** 數據庫整理類型。如不確定請勿更改 */
define('DB_COLLATE', '');

/**#@+
 * 身份認證密鑰與鹽。
 *
 * 修改爲任意獨一無二的字串!
 * 或者直接訪問{@link https://api.wordpress.org/secret-key/1.1/salt/
 * WordPress.org密鑰生成服務}
 * 任何修改都會導致所有cookies失效,所有用戶將必須重新登錄。
 *
 * @since 2.6.0
 */
define('AUTH_KEY',         'kXeW~n6gq[_u;k=49+z=Am9a15F@D)|?8~SKt(A0K,^!U/u4yOvb ]6J._LJ!KPg');
define('SECURE_AUTH_KEY',  'ui|@1SGGC!Wpi)lMdBuh^~.ueevR$ar]B_X8#|&&U.z4YGH02RVSK(`8X2hho)Xm');
define('LOGGED_IN_KEY',    'cd)V9bEPLk4|Q=Z<Ri_Sfp*nvg/Ui+BgHXGL./o@AUP!dMRE=PYV7nJ_&ijK*#y~');
define('NONCE_KEY',        ':-sd O<^pJvN. ;:SuxaSx5kd%W`.Y.z_xN6Nc@zhz&d_^SM*X4(K@&;aQo2wAko');
define('AUTH_SALT',        'O[@Bx|zqp@7{^NF2Q,N6y4d]9&|+Qy$qB6zknzm4tkiOqa4Si5;)/lst@&;!r8%;');
define('SECURE_AUTH_SALT', '~ r8gh?MF$I-)I~9kCC0>p(|nx7vF34<^ZN~AS~lb1Tzx-0SEvf3V1vi0+kQS#>_');
define('LOGGED_IN_SALT',   '-CgDBdxb$N9Kw:MI+>c5WUxW,W141KP-6/tDV!I5s]m/H:#ltw(dP?0Ar]0cz]Yw');
define('NONCE_SALT',       'ze9RdLoKcC-Bsl%<n6VjzDVz#Gu(h&t ^UkuqIpAz<$kN.7Cm|4p|{!Cyg[lOia;');

/**#@-*/

/**
 * WordPress數據表前綴。
 *
 * 如果您有在同一數據庫內安裝多個WordPress的需求,請爲每個WordPress設置
 * 不同的數據表前綴。前綴名只能爲數字、字母加下劃線。
 */
$table_prefix  = 'wp_';

/**
 * 開發者專用:WordPress調試模式。
 *
 * 將這個值改爲true,WordPress將顯示所有用於開發的提示。
 * 強烈建議插件開發者在開發環境中啓用WP_DEBUG。
 *
 * 要獲取其他能用於調試的信息,請訪問Codex。
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define('WP_DEBUG', false);

/**
 * zh_CN本地化設置:啓用ICP備案號顯示
 *
 * 可在設置→常規中修改。
 * 如需禁用,請移除或註釋掉本行。
 */
define('WP_ZH_CN_ICP_NUM', true);

/* 好了!請不要再繼續編輯。請保存本文件。使用愉快! */

/** WordPress目錄的絕對路徑。 */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');

/** 設置WordPress變量和包含文件。 */
require_once(ABSPATH . 'wp-settings.php');
View Code

 

到此爲止,2.157服務器上的兩個網站已經全部發布完成,接下來,將該站點上的內容全部同步到2.158上,爲後續的負載均衡做準備

 rsync -aP --delete /var/www/html/dz.bxy.net/ [email protected]:/var/www/html/dz.bxy.net/
rsync -aP --delete /var/www/html/wps.bxy.net/ [email protected]:/var/www/html/wps.bxy.net/

同理2.158也需要做本地hosts域名解析

兩條命令搞定,2.158只需要將httpp虛機主機搭建起來,配置好與之相同的虛擬主機即可,然後將2.157部署的好網站源碼同步過去即可,不需要重新解壓~

【Nginx前端負載均衡+動靜分離】

 

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
include vhosts/*.conf;
}

[root@dz nginx]# mkdir -p /etc/nginx/vhosts/

[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/upstream.conf 
upstream httpd_webapp{
    server 192.168.2.157;
    server 192.168.2.158;
}
[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/wps.conf
server {
listen 80;
server_name wps.bxy.net;
location /  {
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://httpd_webapp;
              }
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
       root /var/www/html/wps.bxy.net;
      expires 3d;
}
}

 

[root@dz nginx]# egrep -v "#|^$" /etc/nginx/vhosts/dz.conf 
server {
listen 80;
server_name dz.bxy.net;
location /  {
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://httpd_webapp;
              }
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
       root /var/www/html/dz.bxy.net;
      expires 3d;
}
}

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章