本文我们分享在Centos安装tomcat,然后用systemd实现tomcat多实例的方法,有需求的朋友可以认真参考一下。
系统环境:CentOS7x86_64
1、安装jdk
rpm-ivhjdk-8u60-linux-x64.rpm
2、创建普通用户
getentgrouptomcat||groupadd-rtomcat
getentpasswdtomcat||useradd-r-d/opt-s/bin/nologintomcat
3、下载tomcat7
cd/opt
wgethttp://mirrors.ustc.edu.cn/apache/tomcat/tomcat-7/v7.0.64/bin/apache-tomcat-7.0.64.tar.gz
tarzxvfapache-tomcat-7.0.64.tar.gz
mvapache-tomcat-7.0.64tomcat01
chown-Rtomcat:tomcattomcat01
tarzxvfapache-tomcat-7.0.64.tar.gz
mvapache-tomcat-7.0.64tomcat02
chown-Rtomcat:tomcattomcat02
两个实例同时监听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=ApacheTomcat7
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、启动服务
systemctlenabletomcat01
systemctlenabletomcat02
systemctlstarttomcat01
systemctlstarttomcat02
6、查看进程
psaux|greptomcat
附上一份nginx反向代理tomcat的配置文件
proxy_cache_path/var/cache/nginx/proxy_cachelevels=1:2keys_zone=static:10minactive=1hmax_size=1g;
upstreamtomcat{
ip_hash;
#hash$remote_addrconsistent;
server127.0.0.1:8081max_fails=1fail_timeout=2s;
server127.0.0.1:8082max_fails=1fail_timeout=2s;;
keepalive16;
}
server{
listen80;
server_nametomcat.example.com;
charsetutf-8;
access_log/var/log/nginx/tomcat.access.logmain;
root/usr/share/nginx/html;
indexindex.htmlindex.htmindex.jsp;
location/{
proxy_passhttp://tomcat;
proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_cache_use_staleerrortimeoutinvalid_headerupdatinghttp_500http_502http_503http_504;
proxy_connect_timeout300;
proxy_send_timeout300;
proxy_read_timeout300;
proxy_http_version1.1;
proxy_set_headerConnection"";
add_headerX-Backend"$upstream_addr";
}
location~*^.+.(js|css|ico|gif|jpg|jpeg|png)${
proxy_passhttp://tomcat;
proxy_redirectoff;
proxy_set_headerHost$host;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_cache_use_staleerrortimeoutinvalid_headerupdatinghttp_500http_502http_503http_504;
proxy_connect_timeout300;
proxy_send_timeout300;
proxy_read_timeout300;
proxy_http_version1.1;
proxy_set_headerConnection"";
proxy_cachestatic;
proxy_cache_key$host$uri$is_args$args;
proxy_cache_valid2003025m;
proxy_cache_valid4041m;
proxy_cache_validany1h;
add_headerX-Cache$upstream_cache_status;
#log_not_foundoff;
#access_logoff;
expiresmax;
}
location~/.ht{
denyall;
}
}