Centos7 安装 Tomcat8 伪集群 的正确姿势 并设置开机自启 实践笔记

我使用centos7X64最小化安装
CentOS-7-x86_64-Minimal-1708

挂在github上的个人博客:由hexo强力驱动 个人博客

没装jdk的同学,请移步【centos7 安装 jdk8 的正确姿势 并设置开机自启】的文章

1.配置防火墙:

关闭防火墙和加入放行端口二选一

1.1 直接关闭防火墙

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
[root@localhost ~]# firewall-cmd --state
not running
[root@localhost ~]#

1.2 加入放行端口

firewall-cmd --zone=public --add-port=18080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=28080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=38080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
firewall-cmd --reload #刷新防火墙 使其生效
firewall-cmd --zone=public --list-ports #查看防火墙放行端口列表
[root@localhost ~]# firewall-cmd --zone=public --add-port=18080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=28080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
success
[root@localhost ~]# firewall-cmd --zone=public --add-port=38080/tcp --permanent #添加放行端口(--permanent永久生效,没有此参数重启后失效)
success
[root@localhost ~]# firewall-cmd --reload #刷新防火墙 使其生效
success
[root@localhost ~]# firewall-cmd --zone=public --list-ports #查看防火墙放行端口列表
80/tcp 8080/tcp 18080/tcp 28080/tcp 38080/tcp
[root@localhost ~]#

2.安装wget

yum -y install wget

3.下载tomcat8

wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.5.24/bin/apache-tomcat-8.5.24.tar.gz

4.解压

tar -zxvf apache-tomcat-8.5.24.tar.gz

5.移动到/usr/local

mv apache-tomcat-8.5.24 /usr/local/tomcat8-node1

6.配置tomcat的jdk环境变量

vi /usr/local/tomcat8-node1/bin/catalina.sh

在 OS specific support. 前面加入如下代码:

JAVA_HOME=/usr/local/java/jdk1.8.0_151
JRE_HOME=$JAVA_HOME/jre

7.复制两份tomcat8-node1并重命名为tomcat8-node2,tomcat8-node3

cp /usr/local/tomcat8-node1 /usr/local/tomcat8-node2
cp /usr/local/tomcat8-node1 /usr/local/tomcat8-node3

8.修改tomcat8-node1,,tomcat8-node2和tomcat8-node3端口,共三处

vi /usr/local/tomcat8-node1/conf/server.xml
vi /usr/local/tomcat8-node2/conf/server.xml
vi /usr/local/tomcat8-node3/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

习惯上对应改为:

tomcat8-node1: 18005 18080 18009
tomcat8-node2: 28005 28080 28009
tomcat8-node3: 38005 38080 38009

7.配置tomcat8-node1,tomcat8-node2,tomcat8-node3开机启动

在/usr/lib/systemd/system目录下增加tomcat8-node1.service,tomcat8-node2.service,tomcat8-node3.service文件,具体内容如下:
vi /usr/lib/systemd/system/tomcat8-node1.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=oneshot
ExecStart=/usr/local/tomcat8-node1/bin/startup.sh
ExecStop=/usr/local/tomcat8-node1/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

vi /usr/lib/systemd/system/tomcat8-node2.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=oneshot
ExecStart=/usr/local/tomcat8-node2/bin/startup.sh
ExecStop=/usr/local/tomcat8-node2/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

vi /usr/lib/systemd/system/tomcat8-node3.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=oneshot
ExecStart=/usr/local/tomcat8-node3/bin/startup.sh
ExecStop=/usr/local/tomcat8-node3/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

8.允许tomcat8-node1,tomcat8-node2,tomcat8-node3开机启动

systemctl enable tomcat8-node1
systemctl enable tomcat8-node2
systemctl enable tomcat8-node3

9.tomcat8常见命令

systemctl start tomcat8-node1.service    #启动tomcat

systemctl stop tomcat8-node1.service    #关闭tomcat

systemctl restart tomcat8-node1.service   #重启tomcat

systemctl status tomcat8-node1.service   #查看状态tomcat

systemctl enable tomcat8-node1.service   #允许开机自启

你可能感兴趣的:(centos7,java,tomcat8)