一、web环境架构
二、web服务器搭建
1、配置jdk环境
jdk获取
(1)配置jdk环境
tar xf jdk-8u212-linux-x64.tar.gz #解压jdk文件
ln -sv /usr/local/src/jdk1.8.0_212/ /usr/local/jdk #配置软链接,方便之后jdk版本升级
ln -sv /usr/local/src/jdk1.8.0_212/bin/java /usr/bin #之后jenkins需要在/usr/bin中使用java命令
(2)修改环境变量
vim /etc/profile #修改环境变量
export HISTTIMEFORMAT="%F %T `whoami` " #修改history命令的输出,添加时间和用户
export export LANG="en_US.utf-8"
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile #加载环境变量
2、搭建tomcat服务
(1)创建tomcat服务
mkdir /apps
ln -sv /apps/apache-tomcat-8.5.39 /apps/tomcat #添加软链接,便于更新tomcat版本
useradd www -u 2023 -m -s /bin/bash #添加账户,指定账户的uid便于统一管理,shell使用bash,默认是sh
passwd www #添加密码
mkdir /data/tomcat/tomcat_appdir -p #保存 web 压缩包
mkdir /data/tomcat/tomcat_webdir #保存解压后的 web 目录
mkdir /data/tomcat/tomcat_webdir/myapp #web页面存放目录
vim /apps/tomcat/conf/server.xml #修改tomcat配置文件
<Host name="localhost" appBase="/data/tomcat/tomcat_webdir"
echo 192.168.43.111 > /data/tomcat/tomcat_webdir/myapp/index.html #创建访问的页面
chown www.www /apps/apache-tomcat-8.5.39 /apps/tomcat /data/tomcat/ -R #修改权限
(2)启动tomcat的脚本
#!/bin/bash
PORT=8088
JDK_HOME=/usr/local/jdk
CATALINA_HOME=/apps/tomcat
export JDK_HOME CATALINA_HOME
source /etc/profile
start(){
echo "正在判断服务状态,请稍等!"
echo "请稍等3秒钟"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
echo "Tomcat已经正在运行了!"
else
echo "Tomcat没有运行,1秒后启动!"
echo 1;sleep 1
$CATALINA_HOME/bin/catalina.sh start
echo "Tomcat已经成功启动完成,5秒后判断是否启动成功"
echo "5";sleep 1;echo "4";sleep 1
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
PID=`ps -ef|grep tomcat |grep jdk |awk '{print $2}'`
NUM=`ps -ef|grep tomcat |grep jdk |awk '{print $2}'|wc -l`
echo "Tomcat已经成功启动${NUM}个Tomcat进程!,PID为${PID}"
else
echo "Tomcat启动失败,请重新启动!"
echo 1
fi
fi
}
stop(){
PID=`ps -ef|grep -v grep |grep java |awk '{print $2}'`
NUM=`ps -ef|grep -v "color"|grep tomcat |awk '{print $2}'|wc -l`
echo "正在判断服务状态,请稍等3秒钟!"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
if
ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
echo "Tomcat运行中,1秒后关闭!"
echo "1";sleep 1
echo "即将关闭Tomcat服务,请稍等!"
$CATALINA_HOME/bin/catalina.sh stop;
echo "已经执行关闭命令,正在检查关闭多少Tomcat进程,请稍等5秒钟"
sleep 2
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
pkill java && pkill tomcat
if
PID=`ps -ef|grep -v grep |grep java |awk '{print $2}'`
NUM=`ps -ef|grep -v "color"|grep tomcat |awk '{print $2}'|wc -l`
ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
kill -9 $PID ;echo "已经成功关闭${NUM}个tomcat"
else
echo "Tomcat已经关闭完成"
echo "3";sleep 1;echo "2";sleep 1;echo "1";sleep 1
fi
else
echo "Tomcat没有运行"
echo 1
fi
if
PID=`ps -ef|grep -v grep |grep java |awk '{print $2}'`
ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
echo "关闭失败,即将强制删除tomcat进程!"
sleep 2
pkill tomcat
sleep 2
if
ss -ntl |grep $PORT |grep LISTEN >/dev/null
then
echo "强制关闭失败,即将再次强制删除tomcat进程!"
pkill java
sleep 2
fi
fi
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
esac
(3)实现远程控制tomcat
mv tomcat /etc/init.d/
ssh www@192.168.43.112 "/etc/init.d/tomcat stop"
三、haproxy+keepalived配置
(1)keepalived配置
apt install -y keepalived
cp /usr/share/doc/keepalived/samples/keepalived.conf.sample /etc/keepalived/keepalived.conf #由于安装好之后/etc/keepalived目录下没有相关的配置文件,需要复制一个配置文件的模板
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
interface eth0
virtual_router_id 50
#nopreempt #非抢占模式
priority 100
advert_int 1
virtual_ipaddress {
192.168.43.248 dev eth0 label eth0:1
}
}
systemctl start keepalived.service
systemctl enable keepalived
启动服务,并验证vip是否可以正常飘走。
(2)haproxy配置
apt install haproxy -y
vim /etc/sysctl.conf #如果机器上没有监听vip,修改配置内核参数,保证haproxy可以正常运行
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
vim /etc/haproxy/haproxy.cfg
listen myapp
bind 192.168.43.248:80
mode http #模式选择http,轮循效果明显
server 192.168.43.111 192.168.43.111:8088 check
server 192.168.43.112 192.168.43.112:8088 check
systemctl start haproxy
systemctl enable haproxy