Tomcat multi-instance implementation using systemd in centos7

Source: Internet
Author: User
Tags tomcat nginx reverse proxy apache tomcat

System environment: CentOS 7 x86_64

1. Install jdk
Rpm-ivh jdk-8u60-linux-x64.rpm

2. Create a common user
Getent group tomcat | groupadd-r tomcat
Getent passwd tomcat | useradd-r-d/opt-s/bin/nologin tomcat

3. Download 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 01
 
Tar zxvf apache-tomcat-7.0.64.tar.gz
Mv apache-tomcat-7.0.64 tomcat02
Chown-R tomcat: tomcat 02

When two instances listen to port 8080 at the same time, a conflict is inevitable. Therefore, you must modify the settings.
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

Generally, AJP is unavailable. It is disabled here.
Sed-I '/8009/D'/opt/tomcat01/conf/server. xml
Sed-I '/8009/D'/opt/tomcat01/conf/server. xml

In Linux, tomcat works in bio mode by default and has low performance. We recommend that you use apr or nio,

Here it is changed to nio, that is, non-blocking IO, with better performance.
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. Create a startup file
Cd/usr/lib/systemd/system
Cat> tomcat01.service <EOF
[Unit]
Description = Apache Tomcat 7
Afterdomainnetwork.tar get
[Service]
Type = oneshot
ExecStart =/opt/tomcat01/bin/startup. sh
ExecStop =/opt/tomcat01/bin/shutdown. sh
RemainAfterExit = yes
User = tomcat
Group = tomcat
[Install]
Wantedbypolicmulti-user.tar get
EOF

Likewise, tomcat02
Sed's/tomcat01/tomcat02/g' tomcat01.service> tomcat02.service

5. Start the service
Systemctl enable tomcat01
Systemctl enable tomcat02
Systemctl start tomcat01
Systemctl start tomcat02

6. View processes

Ps aux | grep tomcat

Appendix: nginx Reverse proxy tomcat configuration file

Proxy_cache_path/var/cache/nginx/proxy_cache levels = keys_zone = static: 10 m inactive = 1 h max_size = 1g;

 
Upstream tomcat {

Ip_hash;

# Hash $ remote_addr consistent;

Server 127.0.0.1: 8081 max_fails = 1 fail_timeout = 2 s;

Server 127.0.0.1: 8082 max_fails = 1 fail_timeout = 2 s ;;

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_timeouts 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_timeouts 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 5 m;

Proxy_cache_valid 404 1 MB;

Proxy_cache_valid any 1 h;

Add_header X-Cache $ upstream_cache_status;

 

# Log_not_found off;

# Access_log off;

Expires max;

        }

 

Location ~ /. Ht {

Deny all;

        }

 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.