千家信息网

基于LNMP+HAproxy+Keepalived搭建高可用小型站点

发表于:2024-11-29 作者:千家信息网编辑
千家信息网最后更新 2024年11月29日,```* 基于LNMP+HAproxy+Keepalived搭建高可用小型站点前端服务器 192.168.55.7VIP 192.168.55.100 haproxy keepalived19
千家信息网最后更新 2024年11月29日基于LNMP+HAproxy+Keepalived搭建高可用小型站点
```* 基于LNMP+HAproxy+Keepalived搭建高可用小型站点前端服务器 192.168.55.7VIP  192.168.55.100 haproxy    keepalived192.168.55.10VIP  192.168.55.100 haproxy    keepalived后端服务器 192.168.55.5 php-7.1.30.tar.gznginx-1.14.2.tar.gz192.168.55.6 php-7.1.30.tar.gznginx-1.14.2.tar.gzmysql 192.168.55.8 mysql    nfs前端服务器192.168.55.7    和  192.168.55.10   1、yum 安装keepalived (可编译安装)yum install keepalived -y2、编辑配置文件vim /etc/keepalived/keepalived.conf--------------------------------------------------------------------------------! Configuration File for keepalivedglobal_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/bashif ! killall -0 haproxy &>/dev/null;then    systemctl restart haproxy     sleep 1    if ! killall -0 haproxy &>/dev/null;then        systemctl stop  keepalived     fifi--------------------------------------------------------------------------------cat /etc/keepalived/ping.sh #!/bin/bashping -c 2 172.22.0.1 &> /dev/nullif [ $? -eq 0 ];then  exit 0else  exit 2fi--------------------------------------------------------------------------------注:这俩个脚本起检测作用编译安装haproxyyum 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 BalancerAfter=syslog.target network.target[Service]ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -qExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pidExecReload=/bin/kill -USR2 $MAINPID[Install]WantedBy=multi-user.target--------------------------------------------------------------------------------创建目录和用户:mkdir /etc/haproxymkdir /var/lib/haproxyuseradd haproxy -s /sbin/nologinchown haproxy.haproxy /var/lib/haproxy/ -R--------------------------------------------------------------------------------vim /etc/haproxy/haproxy.cfg--------------------------------------------------------------------------------globalmaxconn 65536chroot /usr/local/haproxystats socket /var/lib/haproxy/haproxy.sock mode 600 level adminuid 1001gid 1001daemonnbthread 2nbproc 2cpu-map 1 0cpu-map 2 1#cpu-map 3 2#cpu-map 4 3spread-checks 5pidfile /run/haproxy.pidlog 127.0.0.1 local3 infodefaultsoption http-keep-aliveoption  forwardforoption redispatchoption abortonclosemaxconn 100000mode httptimeout connect 300000mstimeout client  300000mstimeout server  300000mslisten 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 5listen 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 5listen 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 -ymkdir /nfsdata/wordpress -pvi /etc/exports/nfsdata/wordpress *(rw,no_root_squash) systemctl start nfs systemctl enable  nfs 二进制安装mariadb 1 准备用户和组groupadd -r -g 336 mysqluseradd -r -g mysql -u 336 -s /sbin/nologin -d /data/mysql mysql2 准备二进制程序文件和相关文件属性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/ mysqlchown -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 -pvchown mysql.mysql /data/mysql/cd /usr/local/mysql./scripts/mysql_install_db --datadir=/data/mysql --user=mysql5 准备Mysql的服务器端的配置文件mkdir /etc/mysqlcp /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 mysqldservice mysqld start  或   service myseqld rstart7 安全加固mysql_secure_installation8 授权用户,用来博客连接数据库mysql  -uroot  -p 123456CREATE DATABASE wordpress;GRANT ALL PRIVILEGES ON  wordpress.* TO "wordpress"@"192.168.55.%" IDENTIFIED BY "123456"; flush privileges;192.168.55.5     192.168.55.6php-7.1.18.tar.ba2nginx-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 2make install修改配置文件cp /app/php/etc/php-fpm.conf.default /app/php/etc/www.conf 创建用户wwwuseradd www -u 2019进入编译安装的目录备份并改名配置文件cd  /app/php/etc/php-fpm.d  cp www.conf.default  www.confgrep -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-fpmss -ntl 创建php测试页面mkdir /data/nginx/wordpress -pvim /data/nginx/wordpress/index.php编译nginx-1.14.2tar 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_modulemake make  install ln -s /apps/nginx/sbin/nginx  /sbin/ 修改配置文件vim /apps/nginx/conf/nginx.confuser  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.6wordpress-5.0.3-zh_CN.tar.gzcd /data/nginx/woedpress/ tar xvf wordpress-5.0.3-zh_CN.tar.gz mv index.php wordpress-5.0.3-zh_CN.tar.gz /optmv 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',         '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]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
0