第一台客户端
IPADDR=192.168.1.1
NETMASK=255.255.255.0
第二台keepalive+hap
IPADDR=192.168.1.3
NETMASK=255.255.255.0
第三台keepalive+hap
IPADDR=192.168.1.4
NETMASK=255.255.255.0
第四台nginx1
IPADDR=192.168.1.5
NETMASK=255.255.255.0
第五台nginx2
IPADDR=192.168.1.6
NETMASK=255.255.255.0
第六台lamp1
IPADDR=192.168.1.10
NETMASK=255.255.255.0
第七台lamp2
IPADDR=192.168.1.20
NETMASK=255.255.255.0
基础环境:关防火墙 沙盒 挂载 其中keepalive如果yum安装失败重新搭建yum库即可!!!
LAMP搭建
配置IP地址:192.168.1.7
安装服务
[root@localhost ~]# mount /dev/cdrom /media/
[root@localhost ~]# rm -rf /etc/yum.repos.d/*
[root@localhost ~]# vim /etc/yum.repos.d/yum.repo
[yum]
baseurl=file:///media
enabled=1
gpgcheck=0
[root@localhost ~]# yum -y install httpd* php* mariadb mariadb-server mariadb-devel
配置mariadb服务
[root@localhost ~]# systemctl restart mariadb
[root@localhost ~]# mysqladmin -uroot password 123.com
[root@localhost ~]# mysql -uroot -p123.com
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.1.%' identified by '123.com';
MariaDB [(none)]> exit
配置PHP服务
[root@localhost ~]# vim /etc/php.ini
修改:
default_charset = "UTF-8" 692
short_open_tag = On 211
配置httpd服务
vim /etc/httpd/conf/httpd.conf
修改:在以下容器中添加内容
<IfModule dir_module> 163
DirectoryIndex index.php index.html
</IfModule>
<IfModule mime_module> 285
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php
</IfModule>
[root@localhost ~]# systemctl restart httpd
LAMP-1的页面:(不动)
LAMP-2的页面:
[root@localhost ~]# vim /var/www/html/index.php
添加:
<?php
$link=mysqli_connect('192.168.1.7','root','123.com');
if($link) echo "恭喜我,数据库连接成功啦!!";
?>
[root@localhost ~]# systemctl restart mariadb
①LAMP
先做最后两个lamp 这些服务自己看文档(略)
做完后 firefox http://192.168.1.10 firefox http://192.168.1.20
本机ip看能不能显示这里给的内容
vim /usr/local/httpd/htdocs/index.php 这里是更改内容的地方
其中这两台是可以互相访问的 网页的
然后lamp搭建完成!!!
②nginx 先简单安装 两个都这么安装
安装nginx
yum -y install pcre-devel zlib-devel
useradd -M -s /sbin/nologin nginx
tar -zxvf nginx-1.12.0.tar.gz -C /usr/src/ (nginx工具)
cd /usr/src/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
tomcat
2、安装Tomcat
拖包tomcat和jdk
两台tomcat同样的操作来安装
[root@tomcat1 ~]# tar zxf jdk-8u201-linux-x64.tar.gz
[root@tomcat1 ~]# mv jdk1.8.0_201/ /usr/local/java
将java环境变量添加到全局变量中,方便tomcat运行
[root@tomcat1 ~]# echo 'export JAVA_HOME=/usr/local/java
> export JRE_HOME=/usr/local/java/jre/
> export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
> export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' >> /etc/profile
# 设置Java的安装目录变量
echo 'export JAVA_HOME=/usr/local/java
# 设置jre(Jave Runtime Environment:java运行时环境)的安装目录变量
> export JRE_HOME=/usr/local/java/jre/
# 设置类文件路径,冒号代表加的意思,引用以上设置的变量
> export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
# 最后一行,相当于windows系统的命令环境变量,就是新添了两个java以及jre的命令变量
# $PATH代表原有的环境变量,使用变量复用
> export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' >> /etc/profile
删除旧版本的java
[root@tomcat1 ~]# rm -rf /usr/bin/java
[root@tomcat1 ~]# source /etc/profile // 运行该脚本使变量生效
解压tomcat
[root@tomcat1 ~]# tar zxf apache-tomcat-8.5.35.tar.gz
[root@tomcat1 ~]# mv apache-tomcat-8.5.35 /usr/local/tomcat
做完一台记得做另一台tomcat同样的步骤
3、修改两台tomcat的首页内容,用来验证Nginx负载均衡轮询页面
192.168.1.1
echo "tomcat1" > /usr/local/tomcat/webapps/ROOT/index.jsp
192.168.1.3
echo "tomcat2" > /usr/local/tomcat/webapps/ROOT/index.jsp
然后启动两台tomcat
/usr/local/tomcat/bin/startup.sh
然后第一台nginx
vim /usr/local/nginx/conf/nginx.conf
#gzip on; 下面粘贴这条!!!!!!!!!!!!!
upstream apache-server {
server 192.168.1.10:80 weight=1;
server 192.168.1.20:80 weight=1;
}
upstream tomcat-server {
server 192.168.1.30:8080 weight=1;
}
然后往下修改!!!!!!!!!!!!!
location / {
root html;
index index.php index.html index.htm; 修改这里 加上了index.php
}
然后下面这些取消注释 改http://后的127.0.0.1为server!!!!!!!
location ~ \.php$ {
proxy_pass http://apache-server;
}
location ~ \.jsp$ {
proxy_pass http://tomcat-server;
}
!!!!!!!!!!
<h1>这是nginx构建的静态页面</h1>
<h1><a href="index.php">点击此处访问PHP页面</a></h1>
<h1><a href="index.jsp">点击此处访问JSP页面</a></h1> 这是加超链接
保存退出
killall -9 nginx
nginx即可
netstat -anpt | grep nginx 看看有没有nginx
然后
echo "nginx1-1.5" >/usr/local/nginx/html/index.html
firefox http://192.168.1.5 可以看到自己的 如果在ip后加上/index.php就是有了apache的
firefox http://192.168.1.5 /index.php
然后第二台nginx 同理这样搭建 改了这个就行
echo "nginx2-1.6" >/usr/local/nginx/html/index.html
访问是firefox http://192.168.1.6
③搭建keepalive1和2
安装keepalive
yum -y install kernel-devel openssl-devel popt-devel keepalived
systemctl enable keepalived
1、配置keepalived(主服务器)
[root@localhost src]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
修改:
router_id LVS_DEVEL-r1
}
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 {
192.168.1.254
}
}
到这个括号
删除后边的 100行
systemctl restart keepalived
ip add
可以看到虚拟ip
2、配置keepalived(从服务器)!!!!!!!!!!!!!!!!!!
安装keepalived(nginx1和nginx2上安装步骤相同)
yum -y install kernel-devel openssl-devel popt-devel keepalived
systemctl enable keepalived
配置keepalived(备份服务器)
[root@localhost src]# cd /etc/keepalived/
[root@localhost keepalived]# cp keepalived.conf keepalived.conf.bak
[root@localhost keepalived]# vim keepalived.conf
修改:
router_id LVS_DEVEL-r2
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.254
}
}
到那个括号
删除后边的 100行
systemctl restart keepalived
ip add
④在两台keepalive上搭建haphaproxy
1、安装
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# tar -zxvf haproxy-1.5.19.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/haproxy-1.5.19
[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy
2、配置haproxy
[root@localhost ~]# mkdir /etc/haproxy
[root@localhost ~]# cp /usr/src/haproxy-1.5.19/examples/haproxy.cfg /etc/haproxy/
[root@localhost ~]# vim /etc/haproxy/haproxy.cfg
修改:只改最下边的listen listen往下全删掉再改 还有个注释
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
chroot /usr/share/haproxy
uid 99
gid 99
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
# redispatch
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
listen webcluster 0.0.0.0:80
option httpchk GET /index.html
balance roundrobin
server inst1 192.168.1.5:80 check inter 2000 fall 3
server inst2 192.168.1.6:80 check inter 2000 fall 3
应该是1.3和1.4因为文档的nginx1和nginx2的ip是这个
保存退出
mkdir /usr/share/haproxy
ln -s /usr/local/haproxy/sbin/* /usr/sbin/
cp /usr/src/haproxy-1.5.19/examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
/etc/init.d/haproxy start
/etc/init.d/haproxy status
netstat -anp | grep haproxy
chkconfig --add haproxy
chkconfig haproxy on
注意:systemctl restart haproxy
第二台的hap安装同上
如果主服务器不显示虚拟ip 关闭防火墙和沙盒一直重启keepalived和haproxy
然后验证
nginx先firefox http://192.168.1.5 然后加上/index.php 出现apache的
然后第一台客户端firefox http://192.168.1.254 有nginx的两个
ip后边加上/index.php出现了apache的网页