centos7下systemd实现tomcat多实例配置方法

发布时间:2022-05-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了centos7下systemd实现tomcat多实例配置方法脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

本文我们分享在CentOS安装tomcat,然后用systemd实现tomcat多实例的方法,有需求的朋友可以认真参考一下。
系统环境: CentOS 7 x86_64

1、安装jdk

rpm -ivh jdk-8u60-linux-x64.rpm

2、创建普通用户

getent group tomcat || groupadd -r tomcat
getent passwd tomcat || useradd -r -d /opt -s /bin/nologin tomcat

3、下载tomcat 7
cd /opt
wget http://mirrors.ustc.edu.cn/apache/tomcat/tomcat-7/v7.0.64/bin/apache-tomcat-7.0.64.tar.gz
tar zxvf apache-tomcat-7.0.64.tar.gz
mv apache-tomcat-7.0.64 tomcat01
chown -R tomcat:tomcat tomcat01
 
tar zxvf apache-tomcat-7.0.64.tar.gz
mv apache-tomcat-7.0.64 tomcat02
chown -R tomcat:tomcat tomcat02

两个实例同时监听8080端口必然会产生冲突,所以必须修改.

sed -i 's/8080/8081/g' /opt/tomcat01/conf/server.XMl
sed -i 's/8005/8001/g' /opt/tomcat01/conf/server.xML
sed -i 's/8080/8082/g' /opt/tomcat02/conf/server.xml
sed -i 's/8005/8002/g' /opt/tomcat02/conf/server.xml

AJP通常用不上,这里统一关闭

sed -i '/8009/d' /opt/tomcat01/conf/server.xml
sed -i '/8009/d' /opt/tomcat01/conf/server.xml

tomcat在Linux 下默认工作模式是bio,性能非常低,建议使用aPR或者nio,
这里改为nio,即非阻塞IO,性能比较好。

sed -i.bak 's#HTTP/1.1#org.apache.coyote.http11.Http11NioProtocol#' /opt/tomcat01/conf/server.xml
sed -i.bak 's#HTTP/1.1#org.apache.coyote.http11.Http11NioProtocol#' /opt/tomcat02/conf/server.xml

4、创建启动文件

cd /usr/lib/Systemd/system
cat >tomcat01.service <<EOF
[UnIT]
Description=Apache Tomcat 7
After=network.target
[Service]
TyPE=oneshot
ExecStart=/opt/tomcat01/bin/startup.sh
ExecStop=/opt/tomcat01/bin/shutdown.sh
RemainAfterExit=yes
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
EOF
tomcat02同理
sed 's/tomcat01/tomcat02/g' tomcat01.service > tomcat02.service

5、启动服务

systemctl enable tomcat01
systemctl enable tomcat02
systemctl start tomcat01
systemctl start tomcat02

6、查看进程

ps aux |grep tomcat
附上一份nginx反向代理tomcat的配置文件
Proxy_cache_path /VAR/cache/nginx/proxy_cache levels=1:2 keys_zone=static:10m inactive=1h max_size=1g;
 
upstream tomcat {
        ip_hash ;
        #hash           $remote_addr consistent;
        server          127.0.0.1:8081 max_fails=1 fail_timeout=2s ;
        server          127.0.0.1:8082 max_fails=1 fail_timeout=2s ; ;
        keepalive       16;
}
 
server {
        listen          80;
        server_name     tomcat.example.COM;
 
        charset         utf-8;
        access_LOG      /var/log/nginx/tomcat.access.log  main;
        root            /usr/share/nginx/html;
        index           index.html index.htm index.jsp;
 
        location / {
                proxy_pass              http://tomcat;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;
 
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";
 
                add_header              X-Backend "$upstream_addr";
        }
 
        location ~* ^.+.(js|css|ico|gif|jpg|jpeg|png)$ {
                proxy_pass              http://tomcat ;
                proxy_redirect          off;
                proxy_set_header        Host $host;
                proxy_set_header        X-Real-IP $remote_addr;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_cache_use_stale   error timeout invalid_header updating http_500 http_502 http_503 http_504;
 
                proxy_connect_timeout   300;
                proxy_send_timeout      300;
                proxy_read_timeout      300;
                proxy_http_version      1.1;
                proxy_set_header        Connection "";
 
                proxy_cache             static;
                proxy_cache_key         $host$uri$is_args$args;
                proxy_cache_valid       200 302 5m;
                proxy_cache_valid       404 1m;
                proxy_cache_valid       any 1h;
                add_header              X-Cache $upstream_cache_status;
 
                #log_not_found off;
                #access_log off;
                expires max;
        }
 
        location ~ /.ht {
                deny  all;
        } 
}

脚本宝典总结

以上是脚本宝典为你收集整理的centos7下systemd实现tomcat多实例配置方法全部内容,希望文章能够帮你解决centos7下systemd实现tomcat多实例配置方法所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。