1. 程式人生 > >nginx+apache動靜分離/負載均衡

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 [email protected] <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/

[[email protected] Discuz]# mv upload/* .
[[email protected] 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地址跟後面即可

 

[[email protected] 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;[email protected])|?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./[email protected]!dMRE=PYV7nJ_&ijK*#y~');
define('NONCE_KEY',        ':-sd O<^pJvN. ;:SuxaSx5kd%W`[email protected]&d_^SM*X4([email protected]&;aQo2wAko');
define('AUTH_SALT',        'O[@Bx|[email protected]{^NF2Q,N6y4d]9&|+Qy$qB6zknzm4tkiOqa4Si5;)/[email protected]&;!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;
}

[[email protected] nginx]# mkdir -p /etc/nginx/vhosts/

[[email protected] nginx]# egrep -v "#|^$" /etc/nginx/vhosts/upstream.conf 
upstream httpd_webapp{
    server 192.168.2.157;
    server 192.168.2.158;
}
[[email protected] 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;
}
}

 

[[email protected] 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;
}
}