您好,登錄后才能下訂單哦!
```* 基于LNMP+HAproxy+Keepalived搭建高可用小型站點
前端服務器
192.168.55.7VIP 192.168.55.100 haproxy keepalived
192.168.55.10VIP 192.168.55.100 haproxy keepalived
后端服務器
192.168.55.5 php-7.1.30.tar.gznginx-1.14.2.tar.gz
192.168.55.6 php-7.1.30.tar.gznginx-1.14.2.tar.gz
mysql
192.168.55.8 mysql nfs
前端服務器
192.168.55.7 和 192.168.55.10
1、yum 安裝keepalived (可編譯安裝)
yum install keepalived -y
2、編輯配置文件
vim /etc/keepalived/keepalived.conf
--------------------------------------------------------------------------------
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from localhost.localdomain
smtp_server 172.22.0.1
smtp_connect_timeout 30
router_id localhost.localdomain1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_iptables
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_haproxy {
script /etc/keepalived/chk_haproxy.sh
interval 2
weight -50
fall 3
rise 5
timeout 2
}
vrrp_instance VIP1 {
state BACKUP #從配置換成這個MASTER
interface ens37
virtual_router_id 36
priority 100
advert_int 2
nopreempt
unicast_src_ip 192.168.55.7
unicast_peer {
192.168.55.10
}
authentication {
auth_type PASS
auth_pass linux36
}
virtual_ipaddress {
172.20.200.200 dev ens37 label ens37:1
172.20.200.201 dev ens37 label ens37:2
}
notify_master "/etc/keepalived/ping.sh"
}
--------------------------------------------------------------------------------
配置中的腳本
cat /etc/keepalived/chk_haproxy.sh
#!/bin/bash
if ! killall -0 haproxy &>/dev/null;then
systemctl restart haproxy
sleep 1
if ! killall -0 haproxy &>/dev/null;then
systemctl stop keepalived
fi
fi
--------------------------------------------------------------------------------
cat /etc/keepalived/ping.sh
#!/bin/bash
ping -c 2 172.22.0.1 &> /dev/null
if [ $? -eq 0 ];then
exit 0
else
exit 2
fi
--------------------------------------------------------------------------------
注:這倆個腳本起檢測作用
編譯安裝haproxy
yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y
解壓安裝包
cd /usr/local/src/
tar xvf haproxy-1.8.20.tar.gz
編譯安裝
進入目錄
cd haproxy-1.8.20
開始編譯
make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy
生成目錄
make install PREFIX=/usr/local/haproxy
查看版本
./haproxy -h
拷貝主文件
cp /usr/local/src/haproxy-1.8.20/haproxy /usr/sbin/
創建啟動腳本:
--------------------------------------------------------------------------------
vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
--------------------------------------------------------------------------------
創建目錄和用戶:
mkdir /etc/haproxy
mkdir /var/lib/haproxy
useradd haproxy -s /sbin/nologin
chown haproxy.haproxy /var/lib/haproxy/ -R
--------------------------------------------------------------------------------
vim /etc/haproxy/haproxy.cfg
--------------------------------------------------------------------------------
global
maxconn 65536
chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 1001
gid 1001
daemon
nbthread 2
nbproc 2
cpu-map 1 0
cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
spread-checks 5
pidfile /run/haproxy.pid
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
option redispatch
option abortonclose
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms
listen stats
bind :9999
stats enable
# stats hid-version
stats uri /haproxy-status
stats realm HAPorxy\ Stats\ Page
stats auth haadmin:123456
stats auth admin:123456
stats auth lvze:123456
stats refresh 30s
# stats admin if TRUE
listen web_port
bind 0.0.0.0:8080
mode http
log global
server web1 127.0.0.1:8080 check inter 3000 fall 2 rise 5
listen WEB_PORT_80
mode tcp
bind 192.168.55.7:80
server web1 192.168.55.6:80 cookie web-103 weight 2 check inter 3000 fall 3 rise 5
server web2 192.168.55.5:80 cookie web-104 weight 1 check inter 3000 fall 3 rise 5
listen MySQL_PORT
bind 192.168.55.8:3306
mode tcp
server web1 192.168.55.8:3306 cookie web-103 weight 1 check inter 3000 fall 3 rise 5
--------------------------------------------------------------------------------
啟動HAProxy:
systemctl start haproxy 啟動
systemctl restart haproxy 重啟
systemctl enable haproxy 開機自啟動
后端服務器
192.168.55.8
創建共享目錄
yum install nfs-utils -y
mkdir /nfsdata/wordpress -p
vi /etc/exports
/nfsdata/wordpress *(rw,no_root_squash)
systemctl start nfs
systemctl enable nfs
二進制安裝mariadb
1 準備用戶和組
groupadd -r -g 336 mysql
useradd -r -g mysql -u 336 -s /sbin/nologin -d /data/mysql mysql
2 準備二進制程序文件和相關文件屬性
tar xvf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
ln -s mariadb-10.2.23-linux-x86_64/ mysql
chown -R root.root /usr/local/mysql/
3 PATH變量
cat /etc/profile.d/mysql.sh
vim /etc/profile.d/mysql.sh 編輯文件
PATH=/usr/local/mysql/bin:$PATH 寫入變量
source /etc/profile.d/mysql.sh 激活變量
echo $PATH 查看變量
4 準備數據庫數據目錄和數據--改成邏輯卷
mkdir /data/mysql -pv
chown mysql.mysql /data/mysql/
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
5 準備Mysql的服務器端的配置文件
mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
vim /etc/mysql/my.cnf
[mysqld]
datadir=/data/mysql 加一行
6 準備服務啟動腳本
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
查看服務 chkconfig --list
添加服務 chkconfig --add mysqld
service mysqld start 或 service myseqld rstart
7 安全加固
mysql_secure_installation
8 授權用戶,用來博客連接數據庫
mysql -uroot -p 123456
CREATE DATABASE wordpress;
GRANT ALL PRIVILEGES ON wordpress.* TO "wordpress"@"192.168.55.%" IDENTIFIED BY "123456";
flush privileges;
192.168.55.5 192.168.55.6
php-7.1.18.tar.ba2
nginx-1.14.2.tar.gz
源碼編譯php
下載需要的軟件
yum -y install wget vim pcre pcre-devel openssl openssl-devel libicu devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2- devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap jemalloc-devel cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libmcrypt libmcrypt-devel mcrypt mhash libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg devel
進入目錄 解壓包
cd //usr/local/src/
tar xvf php-7.1.18.tar.ba2
開始編譯
cd php-7.1.18.tar.bz2
./configure --prefix=/app/php \--enable-mysqlnd \--with-mysqli=mysqlnd \--with-pdo-mysql=mysqlnd \--with-openssl \--with-freetype-dir \--with-jpeg-dir \--with-png-dir \--with-zlib \--with-libxml-dir=/usr \--with-config-file-path=/etc \--with-config-file-scan-dir=/etc/php.d \--enable-mbstring \--enable-xml \--enable-sockets \--enable-fpm \--enable-maintainer-zts \--disable-fileinfo
make -j 2
make install
修改配置文件
cp /app/php/etc/php-fpm.conf.default /app/php/etc/www.conf
創建用戶www
useradd www -u 2019
進入編譯安裝的目錄備份并改名配置文件
cd /app/php/etc/php-fpm.d
cp www.conf.default www.conf
grep -v ";" www.conf |grep -v "^$"
cd /app/php/etc
cp php-fpm.conf.default php-fpm.conf
啟動php
/app/php/sbin/php-fpm -v
/app/php/sbin/php-fpm -t
/app/php/sbin/php-fpm -c /app/php/etc/php.ini
ps -ef | grep php-fpm
ss -ntl
創建php測試頁面
mkdir /data/nginx/wordpress -p
vim /data/nginx/wordpress/index.php
<?php
phpinfo();
?>
編譯nginx-1.14.2
tar xvf nginx-1.14.2.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-1.14.2
./configure --prefix=/apps/nginx \--user=nginx \--group=nginx \--with-http_ssl_module \--with-http_v2_module \--with-http_realip_module \--with-http_stub_status_module \--with-http_gzip_static_module \--with-pcre \--with-stream \--with-stream_ssl_module \--with-stream_realip_module
make
make install
ln -s /apps/nginx/sbin/nginx /sbin/
修改配置文件
vim /apps/nginx/conf/nginx.conf
user www www;
include /apps/nginx/conf/server/*.conf;
--------------------------------------------------------------------------------
創建副配置目錄
mkdir /apps/nginx/conf/server -pv
--------------------------------------------------------------------------------
vim /apps/nginx/conf/server/www.magedu.net.conf
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/woedpress;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /data/nginx/woedpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
nginx -t
測試
后端服務器配置
192.168.55.5 192.168.55.6
wordpress-5.0.3-zh_CN.tar.gz
cd /data/nginx/woedpress/
tar xvf wordpress-5.0.3-zh_CN.tar.gz
mv index.php wordpress-5.0.3-zh_CN.tar.gz /opt
mv wordpress/* .
mv wordpress /opt/
cp wp-config-sample.php wp-config.php
vim /data/nginx/woedpress/wp-config.php
// ** MySQL 設置 - 具體信息來自您正在使用的主機 ** //
/** WordPress數據庫的名稱 */
define('DB_NAME', 'wordpress');
/** MySQL數據庫用戶名 */
define('DB_USER', 'wordpress');
/** MySQL數據庫密碼 */
define('DB_PASSWORD', '123456');
/** MySQL主機 */
define('DB_HOST', 'centos7.magedu.com‘);
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('DB_COLLATE', '');
* 或者直接訪問{@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org密鑰生成服務}
define('AUTHKEY', '<dWC,ujj+=eMzSkU~w` 5?Tt-NUnW|CTvRF`>S XT j5R<(4+.ku`1#CN:D1Xjrb');
define('SECUREAUTHKEY', 'v&:.i|%(6CsL[jU,5+TU-cAvm+A}2{V/(=(3Cks]L~|g+ljE5m=B/{}:oq.w<');
define('LOGGEDINKEY', 'd~a,Lt#-)ykANn;TW=!sXd#+o>a]+KTYWK un|=eLGHVyYYGD+aKkm};8|raW+@d');
define('NONCEKEY', 'zr$LcVo|!pPr@ 4Q1~i8>S]<QK2e;SBT g>VN{<@/Q;=eJ`Q|9N`kAHKy}e$Kxw#');
define('AUTHSALT', '{`+8c/igV^=SW#[QW+%Kf:0v^F=~##C70ao#J1yO[W&XWnsd.|6nxTGyD+hx>u8');
define('SECUREAUTHSALT', '|rp0)=Qs91]+^M/XFG{2q#K(&)c)z45P7-@@nyiU8.t}%kDGl8# u+uo?n-U');
define('LOGGEDINSALT', 'cP!+/s^urC-LQ3mw<A#ro6v$h^d+@k!WA66;9TU%=|#|MW1J^u4t0io<#M+7w');
define('NONCESALT', '0mPY(C:&c<Q&[$k[YOWt9;]U6Fo-4ZglmZoke`(&BrnPx|ExQ5Xyw!E5|#MXgG');*
注:mysql主機可以寫mysql的機器的主機名字,但一定要在本主機的hosts文件寫解析 ,也可以寫mysql主機的IP地址
在下面的紅色字體的網站是生成密鑰的意思,建議打開網站把人家生成的密鑰與本配置文件的密鑰更換掉,這樣做更安全**
--------------------------------------------------------------------------------
vim /etc/hosts
192.168.55.8 centos7.magedu.com
chown www.www /data/nginx/woedpress/ -R
192.168.55.5 192.168.55.7上操作
yum install nfs-utils -y
掛載共享目錄 數據實時同步
mount -t nfs 192.168.55.8:/nfsdata/wordpress /data/nginx/woedpress/wp-content/uploads/
www.magedu.net/wp-admin
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。