HOWTO Configure Tomcat+Vesta to serve Java Apps
Posted: Sun Mar 03, 2019 6:59 pm
Hello, finally I have found a way to make Tomcat and Vesta CP work together .
- First of all, you need to install Vesta Cp and Tomcat. There is enough info and tutorials out there of this part.
- The comes the tricky part, because Vesta CP comes with Nginx+Apache installed, and Apache is installed on port 8080, so the first thing you need is change this port. You can change it on Apache or on Tomcat. I choose changing the Tomcat one.
Search for the server.xml config file into the Tomcat installation directory and search for this conector:
Change the port 8080 for the new port.
- After that you have a problem because Nginx is working as a proxy to Apache, so you have two ways to do this:
1. Nginx ->Apache -> Tomcat
You need to enable the proxy mod to do this. I haven´t chosen this way, so I don´t have all the info about it.
2. Nginx -> Tomcat
This is the way I have used because I think it is the best way to do it, but any advice will be welcome.
To use this way you need to edit the Nginx config file located on /home/admin/conf/web.
If you turn SSL on, you will have two config files, one extra for SSL config. What I have done here is :
Nginx config file:
With this config Nginx force https on your server:
- Nginx SSL config file:
The first location block acts as a proxy to Tomcat:
The other location blocks acts as a proxy to Apache. I did this in order to keep working webmail,phpmyadmin,...These were managed by Apache and I don´t know if there is a way to do it in Tomcat.
After change these config files and restart Nginx and Tomcat, you can try if everything works. You can type "yourdomain.com" or "www.yourdomain.com" and you hopefully will see the Tomcat welcome page.
When you deploy your webapp on Tomcat, a new folder is created at /tomcat_path/webapps/ with the name of your Java App. If you want to run your app, you must type "yourdomain.com/YourApp".
If you want to access to your app only typing "yourdomain.com", you must add a new "context path" on the server.xml file:
And here is where you add the context path block:
After this you can type "yourdomain.com" and enjoy your Java webapp!
After that I hope it can help anybody because I have spent a lot of time researching for this info (I am a newbie, not an expert).
If anyone can help to improve anything, he will be welcome.
- First of all, you need to install Vesta Cp and Tomcat. There is enough info and tutorials out there of this part.
- The comes the tricky part, because Vesta CP comes with Nginx+Apache installed, and Apache is installed on port 8080, so the first thing you need is change this port. You can change it on Apache or on Tomcat. I choose changing the Tomcat one.
Search for the server.xml config file into the Tomcat installation directory and search for this conector:
Code: Select all
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
- After that you have a problem because Nginx is working as a proxy to Apache, so you have two ways to do this:
1. Nginx ->Apache -> Tomcat
You need to enable the proxy mod to do this. I haven´t chosen this way, so I don´t have all the info about it.
2. Nginx -> Tomcat
This is the way I have used because I think it is the best way to do it, but any advice will be welcome.
To use this way you need to edit the Nginx config file located on /home/admin/conf/web.
If you turn SSL on, you will have two config files, one extra for SSL config. What I have done here is :
Nginx config file:
Code: Select all
server {
listen YOUR_IP:80;
server_name yourdomain.com www.yourdomain.com;
error_log /var/log/apache2/domains/yourdomain.com.error.log error;
return 301 https://$server_name$request_uri;
location @fallback {
proxy_pass http://YOUR_IP:TOMCAT_PORT;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include /home/admin/conf/web/nginx.yourdomain.com.conf*;
}
Code: Select all
return 301 https://$server_name$request_uri;
Code: Select all
server {
listen YOUR_IP:443;
server_name your domain.com www.yourdomain.com;
ssl on;
ssl_certificate /home/admin/conf/web/ssl.yourdomain.com.pem;
ssl_certificate_key /home/admin/conf/web/ssl.yourdomain.com.key;
error_log /var/log/apache2/domains/yourdomain.com.error.log error;
location / {
proxy_pass http://YOUR_IP:TOMCAT_PORT;
}
location /webmail/ {
proxy_pass http://YOUR_IP:APACHE PORT;
}
location /phpmyadmin/ {
proxy_pass http://YOUR IP:APACHE_PORT;
}
location /vstats / {
proxy_pass http://YOUR_IP:APACHE_PORT;
}
location /error/ {
alias /home/admin/web/yourdomain.com/document_errors/;
}
location @fallback {
proxy_pass http://YOUR_IP:TOMCAT_PORT;
}
location ~ /\.ht {return 404;}
location ~ /\.svn/ {return 404;}
location ~ /\.git/ {return 404;}
location ~ /\.hg/ {return 404;}
location ~ /\.bzr/ {return 404;}
include /home/admin/conf/web/snginx.yourdomain.com.conf*;
}
Code: Select all
location / {
proxy_pass http://YOUR_IP:TOMCAT_PORT;
}
After change these config files and restart Nginx and Tomcat, you can try if everything works. You can type "yourdomain.com" or "www.yourdomain.com" and you hopefully will see the Tomcat welcome page.
When you deploy your webapp on Tomcat, a new folder is created at /tomcat_path/webapps/ with the name of your Java App. If you want to run your app, you must type "yourdomain.com/YourApp".
If you want to access to your app only typing "yourdomain.com", you must add a new "context path" on the server.xml file:
Code: Select all
<Context path="" docBase="YourApp">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
Code: Select all
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<Context path="" docBase="YourApp">
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
</Context>
</Host>
After that I hope it can help anybody because I have spent a lot of time researching for this info (I am a newbie, not an expert).
If anyone can help to improve anything, he will be welcome.