Sometimes we need to run multiple web applications with the corresponding virtual hosts in one Tomcat server. Actually, this is not simple to config and there is more than one solution. For example, you can install each web application in separated Tomcat servers.
2. Requirement
Assumed that you have:
Tomcat: /opt/tomcat-dev/
Apache 2: /etc/httpd/
Registered two sub-domains:
api.mobabel.net
manager.mobabel.net
The domains DNS has pointed to your server
And the main domain mobabel.net is available.
Two web applications
nova-api.war
nova-manager.war
CentOS or other Linux
3. Use Case
3-1. 1. Config single web application with a virtual host ( with port + context path)
We use web application nova-api as an example.
1. Copy nova-api.war into /opt/tomcat-dev/webapps
Start Tomcat and this application nova-api can be found in Tomcat Web Application Manager (http://www.mobabel.net:8080/manager/html/), check whether http://www.mobabel.net:8080/nova-api/ is working or not.
Edit this virtual host declaration in /etc/httpd/conf/httpd.conf or other custom config file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<VirtualHost *:80>
ServerName api.mobabel.net
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost Off
<Proxy *>
Order Deny,Allow
Allow from all
</Proxy>
ErrorLog logs/api.mobabel.net-error_log
CustomLog logs/api.mobabel.net-access_log common
ProxyPass/http://localhost:8080/
ProxyPassReverse/http://localhost:8080/
</VirtualHost>
Restart Apache server.
4. Test http://api.mobabel.net/ The boring port and context path is gone!
Conclusion
This solution is fit for Continuous Integration CI, because Jenkins can deploy war into Tomcat server via Tomcat txt manager remotely.
But this solution will make your Tomcat only work for one web application with the domain.
3-3. 3. Config multiple web applications with the virtual hosts ( without port + context path)
1. Keep the previous settings for nova-api
2. create a new webapps container for nova-manager and grant permission. Assume that your tomcat-dev is running under group tomcat and with owner tomcat-dev.
Please use different port “8013” for connector AJP and “8444” for redirectPort. Otherwise, you could not start Tomcat server because of the port conflict.
Restart Tomcat server.
You can use netstat command to check the port usage.
1
netstat-plntu
5. Set the reverse proxy in Apache
Add this virtual host declaration in /etc/httpd/conf/httpd.conf or other custom config file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<VirtualHost *:80>
ServerName manager.mobabel.net
ProxyRequests Off
ProxyVia Off
ProxyPreserveHost Off
<Proxy *>
Order Deny,Allow
Allow from all
</Proxy>
ErrorLog logs/manager.mobabel.net-error_log
CustomLog logs/manager.mobabel.net-access_log common
ProxyPass/http://localhost:8083/
ProxyPassReverse/http://localhost:8083/
</VirtualHost>
Restart Apache server.
6. Test http://api.mobabel.net/ and http://manager.mobabel.net/
Conclusion
Jenkins can not deploy war into Tomcat server via Tomcat txt manager directly. But Jenkins can use FTP protocol to upload war remotely.
3-4. 4. Add another application in case 3
You can repeat step 1 to 5 to add one new web application, for example: nova-other
In step 2 a new webapps-other is created, and nova-other.war is copied into /opt/tomcat-dev/webapps-other/. It seems correct and everything works fine.
3-4-1. BUT
When nova-other.war is moved to /opt/tomcat-dev/webapps, everything still works fine!!!
That means multiple web applications with different port and virtual domain can be put into one webapps directory!
You can still use separate appBase in Host depends on your request. But Jenkins can deploy war into Tomcat server via Tomcat txt manager directly if all web applications are in appBase webapps.
Further Test
Copy Tomcat’s application manager from /opt/tomcat-dev/webapps/manager to /opt/tomcat-dev/webapps-manager/manager, and give a free port and virtual host(newmanager.mobabel.net) to it.
And do not forget put Realm inside Engine.
http://newmanager.mobabel.net/manager/html works!! Jenkins can deploy war to the custom webapps directory now!
3-5. 5. Config multiple web applications with the virtual hosts in multiple instances in Single Tomcat server
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.