Page 1 of 1

HOWTO Configure Tomcat+Vesta to serve Java Apps

Posted: Sun Mar 03, 2019 6:59 pm
by rasputero
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:

Code: Select all

 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
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:

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*;
}
With this config Nginx force https on your server:

Code: Select all

return 301 https://$server_name$request_uri;
- Nginx SSL config file:

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*;
}
The first location block acts as a proxy to Tomcat:

Code: Select all

location / {
        proxy_pass      http://YOUR_IP:TOMCAT_PORT;
        
    }
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:

Code: Select all

<Context path="" docBase="YourApp">
	     <!-- Default set of monitored resources -->
	     <WatchedResource>WEB-INF/web.xml</WatchedResource>
	 </Context>
And here is where you add the context path block:

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 &quot;%r&quot; %s %b" />
	<Context path="" docBase="YourApp">
	     <!-- Default set of monitored resources -->
	     <WatchedResource>WEB-INF/web.xml</WatchedResource>
	 </Context>

      </Host>
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.

Re: HOWTO Configure Tomcat+Vesta to serve Java Apps

Posted: Sun May 19, 2019 9:55 am
by azerphoenix
Hello!
Thank you for the information. I just want to complement your answer with my solution.
1) install tomcat (I used tomcat 9.x) and change the port to a custom one. (I used 7777)
To change port follow this:
- locate to tomcat installation folder (for me it was /opt/tomcat-9.x/conf )
- edit this file: server.xml

Code: Select all

<Connector port="8080" maxHttpHeaderSize="8192"
               maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
               enableLookups="false" redirectPort="8443" acceptCount="100"
               connectionTimeout="20000" disableUploadTimeout="true" />
- change port to another one.
- save & restart tomcat.

2) create domain / subdomain in Vesta CP & configure nginx.
Follow this steps:
- locate to /home/admin/conf/web or /home/YOUR_USERNAME/conf/web
- find your domain names nginx config file and edit this sections:

Code: Select all

location / {
        proxy_pass      http://YOUR_IP:TOMCAT_PORT;
        
}
location @fallback {
        proxy_pass      http://YOUR_IP:TOMCAT_PORT;
}
Add yout tomcat port...
Then restart nginx & apache

3) The last step is different from the previous answer.
- Locate to tomcat installation folder (for example - /opt/tomcat-9.x/conf )
- Edit server.xml
- Find the tag

Code: Select all

</Engine>
and before add this code

Code: Select all

<Host name="example.com"  appBase="webapps/exampleApp" unpackWARs="false" autoDeploy="false">
  <Alias>www.example.com</Alias>
  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
         prefix="localhost_access_log." suffix=".txt"
         pattern="%h %l %u %t &quot;%r&quot; %s %b" />
  <Context displayName="exampleApp" 
           docBase="/home/admin/web/example.com/public_html/exampleApp"
           path=""
           debug="0"
           reloadable="true" />
</Host>
Update name attribute and alias tag to your domain. Update docbase attribute to the directory where your application located (In my case I put my app directory to public_html into domain's folder instead webapps)