Setup Tomcat for multiple websites in one server (linux)

Viewed 977

I know this question might be similar to others, however, I haven't been able to solve this.

I have a server with 25 websites, all of them uses Tomcat. I'm migrating to a new server which has Tomcat 8 (the regular version), whereas the old server uses "CPanel's easy tomcat".

I started migrating one website, which is now running on the new server, however, when a JSP is called from the browser, the browser shows the JSP code instead of executing it.

In my old server, I had to execute a feature from CPanel's easy-tomcat called "install servlets", which I really don't know what it does, however, after executing that, Tomcat would execute JSP's.

Now, in my new server, accordgin to what I've read, I've added this to the %CATALINA_HOME%/conf/server.xml file, inside the <Engine></Engine> tags (which I also had to include in my old server):

<Host name="mydomain.com" appBase="/home/myAccName/public_html/">
<Context path="" reloadable="false" docBase="/home/myAccName/public_html" />
</Host>

As you can see, the application is not located under %CATALINA_HOME%/webapps/ directory, and that's the way I need it to be.

What am I missing?

Any help will be really appreciated

I'm using Tomcat 8, EasyApache 4 and CentOS 7.6

3 Answers

check that the following in in your tomcat/conf.web.xml file

  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />

You can create VirtualHosts to setup multiple websites with multiple domain names in one server. You can try out same in tomcat 7, 8 and in 9 as well. 1.Edit your relevant server.xml file and include Virtual hosts as below.

Make sure to restart your tomcat server for the applied changes to take effect.

<Host name="example.com"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Alias>www.example.com</Alias>

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="example_access_log" suffix=".txt"
           pattern="%h %l %u %t %r %s %b" />

    <Context path="" docBase="/opt/tomcat/webapps/myapp1"
           debug="0" reloadable="true"/>
</Host>


<Host name="mydomain.org"  appBase="webapps" unpackWARs="true" autoDeploy="true">
    <Alias>www.mydomain.org</Alias>

    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="mydomain_access_log" suffix=".txt"
           pattern="%h %l %u %t %r %s %b" />

    <Context path="" docBase="/opt/tomcat/webapps/myapp2"
           debug="0" reloadable="true"/>
</Host>

Explanation

For example.com domain, /opt/tomcat/webapps/myapp1 is the document root (for your web 1). For mydomain.org domain, /opt/tomcat/webapps/myapp2 is the document root(for your web 1).

This is the way I managed to solve this. I don't know if it's the best way, but it works. Just follow the next 3 steps:

1) In %CATALINA_HOME%/conf/server.xml:

<Host name="mydomain.com" appBase="webapps" autoDeploy="false" unpackWARs="false"></Host>

2) Then I had to add a file: %CATALINA_HOME%/conf/mydomain.com/ROOT.xml

<Context displayName="My Website 1" docBase="/home/accountfolder/public_html" reloadable="true">
        <Resource
                name="jdbc/rhwebDB"
                .
                .
                .
        (database connection info, optional)
    />
</Context>

Then on the Apache side, I had to configure the mod_proxy_ajp connector

I've edited the file:

3) /etc/apache2/conf.d/userdata/std/2/accountfolder/mydomain.com/cp_jkmount.conf

<IfModule mod_jk.c>
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13
  JkMount /servlets/* ajp13
</IfModule>

<IfModule mod_proxy_ajp.c>
  ProxyRequests Off
  <Proxy *>
    AddDefaultCharset Off
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass / ajp://mydomain.com:8009/
  ProxyPassReverse / ajp://mydomain.com:8009/

My application/website is located on /home/accountfolder/public_html/ and there's nothing on the %CATALINA_HOME%/webapps/ directory. For me this is better since I can upload a jsp or whatever directly where the app is located using a FTP user.

If you have any trouble, check the folder permissions and owners in your /home/accountfolder/public_html/ directory, Tomcat needs permissions for reading/executing etc. A Tomcat's 404 error will be shown if Tomcat can't access those files & folders.

As I mentioned in this post, my app is "exploded" (if that's the correct term), I mean, it's NOT packed in a WAR file.

Related