实验目的:当客户访问nginx就可以提供论坛页面
实现流程:客户访问》》》nginx方向代理》》》apache+php+论坛》》》php连接数据库》》》MHA中的mysql数据库
MHA:是一套优秀的作为MySQL高可用性环境下故障切换和主从提升的高可用软件
LAMP是常见的Web服务器环境解决方案,用于创建和管理Web应用程序的开源开发平台。Linux用作后端操作系统,Apache是Web服务器,MySQL是数据库,PHP是脚本语言。
nginx反向代理(Reverse Proxy)方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器
Keepalived高可用:两台业务系统启动着相同的服务,如果有一台故障,另一台自动接管,我们将将这个称之为高可用;
一共是8台服务器
MHA需要4台服务器
基于mysql3.6版本
MHA | 服务地址 |
---|---|
MHA | 192.168.136.88 |
主 | 192.168.136.40 |
主从 | 192.168.136.10 |
从 | 192.168.136.20 |
安装依赖环境
yum -y install ncurses-devel gcc-c++ perl-Module-Install
导入安装包
安装gmake 编译软件
[root@mysql1 ~]# tar zxvf cmake-2.8.6.tar.gz
[root@mysql1 ~]# cd cmake-2.8.6/
[root@mysql2 cmake-2.8.6]# ./configure
[root@mysql1 cmake-2.8.6]# gmake && gmake install
安装mysql数据库
[root@mysql3 ~]# tar zxvf mysql-5.6.36.tar.gz
[root@mysql2 ~]# cd mysql-5.6.36/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DSYSCONFDIR=/etc
[root@mysql2 mysql-5.6.36]# make && make install
[root@mysql1 mysql-5.6.36]# cp support-files/my-default.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? yes
启动脚本
[root@mysql1 mysql-5.6.36]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@mysql3 mysql-5.6.36]# chmod +x /etc/rc.d/init.d/mysqld
[root@mysql3 mysql-5.6.36]# chkconfig --add mysqld
申明环境变量
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
[root@mysql3 mysql-5.6.36]# source /etc/profile 刷新生效
建立程序用户
[root@mysql3 sbin]# groupadd mysql
[root@mysql3 sbin]# useradd -M -s /sbin/nologin mysql -g mysql
给他权限
[root@mysql3 sbin]# chown -R mysql.mysql /usr/local/mysql
创建数据存放位置
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
修改master的主配置文件 三台服务器id不一样
主服务器:
vim /etc/my.cnf
server-id=1 ## 特别注意三台服务器id不能相同
log_bin=master-bin
log-slave-updates=true
主备从服务器:
server-id=2
log_bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
从服务器:
server-id=3
log_bin=master-bin
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
重启mysql服务
systemctl restart mysqld
mysql1 msyql2 mysql3 分别在3台主机上做软连接
[root@mysql1 ~]# ln -s /usr/local/mysql/bin/mysql /usr/sbin/
[root@mysql1 ~]# ln -s /usr/local/mysql/bin/mysqlbinlog /usr/sbin/
3台主机启动mysql(不要暂停重新开启)
[root@mysql1 ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql &
查看是否开启
[root@mysql1 ~]# netstat -ntap | grep 3306
systemctl stop firewalld
iptables -F
setenforce 0
配置MySQL、一主两从
grant replication slave on *.* to 'myslave'@'192.168.136.%' identified by '123';
grant all privileges on *.* to 'mha'@'192.168.136.%' identified by 'manager';
flush privileges;
下面三条授权按理论是不用添加的,但是做案例实验环境时候是通过MHA检查MySQL主从有报错,报两个从库通过主机名连接不上主库,所以所有数据库加上下面的授权
grant all privileges on *.* to 'mha'@'master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave-master' identified by 'manager';
grant all privileges on *.* to 'mha'@'slave' identified by 'manager';
flush privileges;
在master主机上查看二进制文件和同步点
show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 | 1294 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
-接下来在mysql2和mysql3分别执行同步
change master to master_host='192.168.136.20',master_user='myslave',master_password='123',master_log_file='master-bin.000001', master_log_pos=1294;
开启从服务
start slave;
mysql> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
必须设置两个从库为只读模式
mysql> set global read_only=1;
mysql> flush privileges;
所有服务器上都安装MHA依赖的环境,首先安装epel源
yum -y install epel-release --nogpgcheck
[root@localhost ~]# cd /opt/mysql-5.6.36/
yum -y install perl-DBD-MySQL \
perl-Config-Tiny \
perl-Log-Dispatch \
perl-Parallel-ForkManager \
perl-ExtUtils-CBuilder \
perl-ExtUtils-MakeMaker \
perl-CPAN
解释
MHA软件包对于每个操作系统版本不一样,这里CentOS 7 必须选择0.57版本,在<注意:所有服务器>上必须先安装node组件,最后在MHA-manager节点上安装manager组件,因为manager依赖node组件,下面都是在master上操作演示安装node组件
部署安装node组件
cd /opt
tar zxvf mha4mysql-node-0.57.tar.gz
perl语言进行配置
cd mha4mysql-node-0.57
perl Makefile.PL
make&&make install
在mha-manager上安装manager组件(!注意:一定要先安装node组件才能安装manager组件)
cd /opt
tar zxvf mha4mysql-manager-0.57.tar.gz
cd mha4mysql-manager-0.57
perl Makefile.PL
make&&make install
查看
[root@mha mha4mysql-manager-0.57]# ls /usr/local/bin/
//mha-manager安装后/usr/local/bin 下面会生成几个工具,主要包括以下几个//
masterha_check_ssh 检查MHA的SSH配置状况
masterha_check_repl 检查MySQL复制状况
masterha_manager 启动manager的脚本
masterha_check_status 检测当前MHA运行状态
masterha_master_monitor 检测master是否宕机
masterha_master_switch 控制故障转移 (自动或手动)
masterha_conf_host 添加或删除配置的server信息
masterha_stop 关闭manager
//node安装后也会在/usr/local/bin下面会生成几个脚本(这些工具通常由 MHA Manager的脚本除法,无需人为操作)主要如下//
save_binary_logs 保存和复制 master 的二进制日志
apply_diff_relay_logs 识别差异的中继日志事件并将其差异的事件应用于其他的slave
filter_mysqlbinlog 去除不必要的ROLLBACK事件(MHA已不再使用这个工具)
purge_relay_logs 清除中继日志 (不会紫塞SQL线程)
ssh-keygen -t rsa
推送密钥对给3台的服务器(密码123123)
ssh-copy-id 192.168.136.88
ssh-copy-id 192.168.136.88
ssh-copy-id 192.168.136.88
2:mysql1上配置数据库节点mysql2和mysql3的无密码登录
ssh-keygen -t rsa
推送密钥对给其他2台的服务器
ssh-copy-id 192.168.136.40
密钥123123
ssh-copy-id 192.168.136.10
3:mysql2上配置数据库节点mysql1和mysql3的无密码登录
ssh-keygen -t rsa
推送密钥对给其他2台的服务器
ssh-copy-id 192.168.136.88
密钥123123
ssh-copy-id 192.168.136.10
4:mysql3上配置数据库节点mysql1和mysql2的无密码登录
ssh-keygen -t rsa
推送密钥对给其他2台的服务器
ssh-copy-id 192.168.136.88
密钥123123
ssh-copy-id 192.168.136.40
在mha-manager节点上复制相关脚本到/usr/local/bin 目录.
cp -ra /root/mha4mysql-manager-0.57/samples/scripts /usr/local/bin
//拷贝后有四个执行文件
ls /usr/local/bin/scripts/
master_ip_failover master_ip_online_change power_manager send_report
复制上述的自动切换时VIP管理的脚本到/usr/local/bin 目录,这里使用脚本管理 VIP
cp /usr/local/bin/scripts/master_ip_failover /usr/local/bin
修改内容如下:(删除原有内容,直接复制)
vim /usr/local/bin/master_ip_failover
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
$command, $ssh_user, $orig_master_host, $orig_master_ip,
$orig_master_port, $new_master_host, $new_master_ip, $new_master_port
);
####################添加内容部分#########################################
my $vip = '192.168.136.200';
my $brdc = '192.168.136.255';
my $ifdev = 'ens33';
my $key = '1';
my $ssh_start_vip = "/sbin/ifconfig ens33:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig ens33:$key down";
my $exit_code = 0;
#my $ssh_start_vip = "/usr/sbin/ip addr add $vip/24 brd $brdc dev $ifdev label $ifdev:$key;/usr/sbin/arping -q -A -c 1 -I $ifdev $vip;iptables -F;";
#my $ssh_stop_vip = "/usr/sbin/ip addr del $vip/24 dev $ifdev label $ifdev:$key";
#######################################################################
GetOptions(
'command=s' => \$command,
'ssh_user=s' => \$ssh_user,
'orig_master_host=s' => \$orig_master_host,
'orig_master_ip=s' => \$orig_master_ip,
'orig_master_port=i' => \$orig_master_port,
'new_master_host=s' => \$new_master_host,
'new_master_ip=s' => \$new_master_ip,
'new_master_port=i' => \$new_master_port,
);
exit &main();
sub main {
print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
if ( $command eq "stop" || $command eq "stopssh" ) {
my $exit_code = 1;
eval {
print "Disabling the VIP on old master: $orig_master_host \n";
&stop_vip();
$exit_code = 0;
};
if ($@) {
warn "Got Error: $@\n";
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "start" ) {
my $exit_code = 10;
eval {
print "Enabling the VIP - $vip on the new master - $new_master_host \n";
&start_vip();
$exit_code = 0;
};
if ($@) {
warn $@;
exit $exit_code;
}
exit $exit_code;
}
elsif ( $command eq "status" ) {
print "Checking the Status of the script.. OK \n";
exit 0;
}
else {
&usage();
exit 1;
}
}
sub start_vip() {
`ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
`ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
print
"Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}
创建mha软件目录并拷贝配置文件
mkdir /etc/masterha
cp /root/mha4mysql-manager-0.57/samples/conf/app1.cnf /etc/masterha/
vim /etc/masterha/app1.cnf
[server default]
manager_log=/var/log/masterha/app1/manager.log
manager_workdir=/var/log/masterha/app1
master_binlog_dir=/usr/local/mysql/data
master_ip_failover_script=/usr/local/bin/master_ip_failover //查看路径
master_ip_online_change_script=/usr/local/bin/master_ip_online_change
password=manager
ping_interval=1
remote_workdir=/tmp
repl_password=123
repl_user=myslave
secondary_check_script=/usr/local/bin/masterha_secondary_check -s 192.168.136.100 -s 192.168.136.200
shutdown_script=""
ssh_user=root
user=mha
[server1]
hostname=192.168.136.88
port=3306
[server2]
candidate_master=1
check_repl_delay=0
hostname=192.168.136.40
port=3306
[server3]
hostname=192.168.136.10
port=3306
修改路径
[root@mha-manager scripts]# cp /usr/local/bin/scripts/master_ip_online_change /usr/local/bin/
[root@mha-manager scripts]# cp /usr/local/bin/scripts/send_report /usr/local/
测试无密码认证,如果正常最后会输出successfully接检查是OK即可
masterha_check_ssh --conf=/etc/masterha/app1.cnf
masterha_check_repl --conf=/etc/masterha/app1.cnf
在主服务器上
[root@master ~]# /sbin/ifconfig ens33:1 192.168.136.200/24
在mha服务上启动MHA
nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null >/var/log/masterha/app1/manager.log 2>&1 &
masterha_check_status --conf=/etc/masterha/app1.cnf
cat /var/log/masterha/app1/manager.log
[root@master ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.136.20 netmask 255.255.255.0 broadcast 192.168.136.255
inet6 fe80::f3d9:2d42:9596:bfb9 prefixlen 64 scopeid 0x20
ether 00:0c:29:81:28:de txqueuelen 1000 (Ethernet)
RX packets 610896 bytes 855719895 (816.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 205699 bytes 16844848 (16.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163 mtu 1500
inet 192.168.136.200 netmask 255.255.255.0 broadcast 192.168.136.255
ether 00:0c:29:81:28:de txqueuelen 1000 (Ethernet)
读取下载好的源码文件,并解压
yum -y install lrzsz 安装文件传输
[root@localhost opt]# mkdir apache mysql php
[root@localhost ~]# cd apache/
apr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2
2.txt apr-1.6.2.tar.gz
[root@localhost apache]# tar jxvf httpd-2.4.29.tar.bz2
[root@localhost apache]# tar zxvf apr-1.6.2.tar.gz
[root@localhost apache]# tar zxvf apr-util-1.6.0.tar.gz
[root@localhost apache]# mv apr-1.6.2 httpd-2.4.29/srclib/apr
[root@localhost apache]# mv apr-util-1.6.0 httpd-2.4.29/srclib/apr-util
yum -y install \
gcc \
gcc-c++ \
make \
pcre-devel \
expat-devel \
perl
[root@localhost apache]# yum -y install \
gcc \ 编译器'
gcc-c++ \ 编译器'
make \ make工具'
pcre-devel \ 支持正则表达式的工具'
expat-devel \ 使网站能解析标签语言的工具'
perl Perl语言工具'
cd httpd-2.4.29 (进入Apache目录进行配置)
./configure \
--prefix=/usr/local/httpd \
--enable-so \
--enable-rewrite \
--enable-charset-lite \
--enable-cgi
[root@hostname httpd-2.4.29]#./configure \
--prefix=/usr/local/httpd \ 指定路径'
--enable-so \ 开启核心功能模块'
--enable-rewrite \ 开启重写功能,如防盗链保护'
--enable-charset-lite \ 开启字符集'
--enable-cgi 开启通用网关接口'
[root@hostname httpd-2.4.29]# make && make install
把启动脚本放到init.d目录下让service服务进行管理
cp /usr/local/httpd/bin/apachectl /etc/init.d/httpd
下面进行服务优化
vim /etc/init.d/httpd
# chkconfig: 35 85 21 35级别自动运行 第85个启动 第21个关闭'
# description:Apache is a World Wide Web server
使用chkconfig命令将httpd服务添加进service便于系统进行管理
chkconfig --add httpd
将配置文件建立软连接便于管理,要不然源路径太长不方便
ln -s /usr/local/httpd/conf/httpd.conf /etc/https.conf
修改httpd服务配置文件
编辑:vim /etc/https.conf
修改或替换以下内容:
Listen 192.168.50.132:80 (将#去掉,将IP修改为本地服务器IP地址,端口号80)
#Listen 80 (将IPV6这一行注释掉)
ServerName www.test.com:80 (修改域名,将example修改为test)
usr/local/bin是用户放置自己的可执行程序的地方,将httpd服务命令便于系统管理
ln -s /usr/local/httpd/bin/* /usr/local/bin/
测试一下httpd的命令语法是否正确
httpd -t
PS:-t:测试配置文件的语法是否正确
关闭系统核心防护并清空防火墙规则
iptables -F
setenforce 0
开启httpd服务
service httpd start
查看服务端口是否开启
netstat -anpt | grep 80
PHP是一门后端动态解释型计算机高级语言,一般用来编写或者生成动态网页,主要负责数据的处理与渲染。(这里是指用PHP嵌入网页里面的形式,现在可以直接用一些JS的框架去渲染网页数据了,PHP主要用来做数据处理)
[root@localhost php]#cd php/
[root@localhost php]# yum -y install \
gd \
libpng \
libpng-devel \
pcre \
pcre-devel \
libxml2-devel \
libjpeg-devel
[root@192 opt]# cd php/
[root@localhost php]# tar jxvf php-5.6.11.tar.bz2
[root@localhost php]# cd php-5.6.11/
./configure \
--prefix=/usr/local/php5 \ 指定安装路径'
--with-gd \ 指定GD库'
--with-zlib \ 支持数据压缩函数库'
--with-apxs2=/usr/local/httpd/bin/apxs \ 设置Apache服务提供的apxs模块支持程序的文件位置'
--with-config-file-path=/usr/local/php5 \ 设置PHP配置文件存放的位置'
--enable-mbstring 启用多字节字符串功能,以便支持中文等代码'
make && make install
复制如下
[root@localhost php-5.6.11]# ./configure \
--prefix=/usr/local/php5 \
--with-gd \
--with-zlib \
--with-apxs2=/usr/local/httpd/bin/apxs
--with-config-file-path=/usr/local/php5 \
--enable-mbstring
进行编译及安装
make && make install
root@localhost php-5.6.11]# cp php.ini-development /usr/local/php5/php.ini
[root@localhost php-5.6.11]# ln -s /usr/local/php5/bin/* /usr/local/bin/ 创建命令软链接
[root@localhost php-5.6.11]# vim /etc/httpd.conf
382行下添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.php index.html
因为之前修改过Apache配置文件,所以需要重启服务
service httpd stop
service httpd start
编写一个PHP测试页面
编辑:vi /usr/local/httpd/htdocs/index.html
删除文件内原有内容并添加如下内容:
重命名后缀为PHP
mv /usr/local/httpd/htdocs/index.html /usr/local/httpd/htdocs/index.php
.4 安装BBS论坛
1、进入目录,解压缩包
unzip Discuz_X2.5_SC_UTF8.zip
2、将论坛源码放入htdocs站点中
[root@localhost dir_SC_UTF8]#
cp -r upload/ /usr/local/httpd/htdocs/bbs
PS:-r:递归复制目录下所有文件及目录
3、安装论坛
http://192.168.136.30/bbs/install/
进入站点目录:cd /usr/local/httpd/htdocs/bbs
执行以下四行命令进行赋权操作:
chown -R daemon ./config
chown -R daemon ./data
chown -R daemon ./uc_client
chown -R daemon ./uc_server/data
设置运行环境,点击第一项全新安装即可
因为还没有建议该论坛的数据,所以要先建立数据库再进行下一步安装操作
登陆数据库:mysql -u root -p
创建数据库(注意!分号不能少,下同):create database bbs;
查看数据库是否建立成功:show databases;
用密码为admin123:grant all on bbs. * to 'bbsuser'@'%' identified by 'admin123';
flush privileges;
退出数据库:quit
这里数据库服务器master地址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fodCUbmh-1602125049732)(…/AppData/Roaming/Typora/typora-user-images/image-20201008102933001.png)]
两台服务都要配置nginx
#安装环境
[root@localhost opt]# yum -y install gcc gcc-c++ pcre pcre-devel zlib-devel
#解压缩源码包
[root@localhost opt]# tar zxvf nginx-1.12.2.tar.gz
#创建nginx用户
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
#编译安装
[root@localhost opt]# cd nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
#路径优化
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin
#校验配置文件语法错误并开启服务查看
[root@localhost nginx-1.12.2]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost nginx-1.12.2]# nginx
[root@localhost nginx-1.12.2]# netstat -antp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 22287/nginx: master
#添加Nginx系统服务
为了使用Nginx服务启动,停止,重载方便,可以编写基于Nginx服务控制文件使用systemctl
[root@localhost nginx-1.12.2]# vim /lib/systemd/system/nginx.service
[Unit]
Description = nginx
After = network.target
[Service]
Type = forking
PIDFile = /usr/local/nginx/logs/nginx.pid
ExecStart = /usr/local/nginx/sbin/nginx
ExecReload = /usr/bin/kill -s HUP $MAINPID
ExecStop = /usr/bin/kill -s QUIT $MAINPID
PrivateTmp = true
[Install]
WantedBy = multi-user.target
[root@localhost nginx-1.12.2]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost nginx-1.12.2]# systemctl enable nginx.service
[root@localhost nginx-1.12.2]# systemctl start nginx.service
Nginx反向代理来实现负载均衡
1.源码编译安装nginx
2.修改主配置文件,添加反向代理参数(两台nginx相同配置)
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
\#gzip on;
upstream tomcat_server {
server 192.168.136.70:8080 weight=1;
server 192.168.136.80:8080 weight=1;
}
server {
listen 80;
server_name localhost;
\#access_log logs/host.access.log main;
location ~ .*.jsp$ { #动态请求转发给Tomcat处理,同时实现了负载均衡
proxy_pass http://tomcat_server;
proxy_set_header Host $host;
}
安装:
yum install keepalived -y
修改配置文件
MASTER 端:
[root@localhost ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
vrrp_script check_nginx {
script "/etc/keepalived/check_nginx.sh"
interval 2 #脚本执行间隔时间2秒(每2秒执行一次)
}
global_defs {
notification_email {
[email protected]
[email protected]
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL1
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
14.0.0.100/24
}
track_script {
check_nginx
}
}
BACKUP端:
修改三处:
router_id LVS_DEVEL2
state BACKUP
我们断掉哪一个服务都可以在查看到论坛服务