How to create sub-domains in Apache Tomcat 8 - java

I have installed Apache tomcat 8 on my windows server 2012 where I need to store my Java applications. I have 3 apps: app1, app2, app3
Right now if I put into my - Tomcat 8.0\webapps folder war file called ROOT.war app starts on address mydomain.com and works fine - but I need to be able to run several apps at the same tomcat - Thats's why I want to create subdomains
app1 should launch on app1.mydomain.com
app2 should launch on app2.mydomain.com
app3 should launch on app3.mydomain.com
How can I fulfill this?
I search similar topics here on stack-overflow but nothing helped.
Please help.

Actually I solved my issue like this:
I have configured server.xml file located in my case in Program Files (x86)\Apache Software Foundation\Tomcat 8.0\conf folder
found there this code:
<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" />
</Host>
made a copy of it and replaced the values with my subdomains:
<Host name="app1.mydomain.com " appBase="app1.mydomain.com "
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" />
</Host>
and that's it actually.
For myself I also removed default localhost HOST and now I have only subdomains.

Related

Setup Tomcat for multiple websites in one server (linux)

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

Tomcat7 trying to run a war that doesn't exist and then failing

java.lang.IllegalArgumentException: Document base /var/lib/tomcat7/webapps/Monocle does not exist or is not a readable directory
at org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:137)
at org.apache.catalina.core.StandardContext.resourcesStart(StandardContext.java:5197)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5386)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1572)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1562)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Out tomcat7 is failing to start. to find if the issue is with tomcat itself or with my code, I deleted all the war files from the webapps folder except ROOT folder. When I started, it failed throwing the above error. Monocle is one war that I deleted. I don't understand why it is still trying to find Monocle. I already deleted Catalina folder from work folder. Can someone help me? do I need clear caches from any other location?
You are able to verify TOMCAT_HOME/conf/server.xml file
In the tag, appBase attribute has default value is wepapps, mabe you changed appBase="/var/lib/tomcat7/webapps/Monocle".
Hope this helps!!!
ps: if not, add more information such. e.g:TOMCAT_HOME/conf/server.xml, TOMCAT_HOME/conf/context.xml
<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" />
</Host>

How to remove jsessionid from URL in my local Tomcat7 in Eclipse

I have a java web app that I deploy in production with Tomcat7.
I use the Tomcat Web Application Manager page in production, where I deploy my WAR at the context path "/". In production I'm not seeing jsessionid in URL.
In my development environment though, the same application (hence the same web.xml), started with Tomcat7 inside eclipse is showing jsessionid in URL.
The only session configuration I have in my web.xml is:
<session-config>
<session-timeout>15</session-timeout>
</session-config>
The only difference I can see in both Tomcat7 is the server.xml:
Production:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t "%r" %s %b" />
</Host>
Local:
<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log." suffix=".txt"/>
<Context docBase="MyApp" path="/" reloadable="true" source="org.eclipse.jst.jee.server:MyApp" />
</Host>
Another difference is that I use NGinx in production to do a proxy pass from port 80 to 8080 from Tomcat.
What may I be missing?
Thanks
You may using a browser or other client that doesn't support (or disabled) cookies in your development environment.
Another Tip: you may use this code in tomcat 7 (in your web.xml file):
<session-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
The problem is that for cookies to work properly, the domain name must have at least two dots (https://curl.haxx.se/rfc/cookie_spec.html).
So to make it work locally, I had to do the following:
Change the /etc/hosts file to include any domain name with at least one period, pointing to 127.0.0.1:
127.0.0.1 localhost
127.0.0.1 localhost.test
255.255.255.255 broadcasthost
::1 localhost
Change the context.xml to include the new domain:
<Context sessionCookieDomain=".localhost.test" sessionCookiePath="/">
...
</Context>

how to create localhost_access_log file in tomcat 6

In my localhost I am using tomcat 8 which is having localhost_access_log file.In this file all the requests along with IPAddress,dateTime,Request type(get/post) along with full URL are captured.
But main server is not using tomcat 8 and they are using tomcat 6.So here there is no localhost_access_log file and hence I could not know what and when requests are made to server.
So is there any way to create localhost_access_log or any other way to know the requests made to server?
You have to change yout configuration to:
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<!-- 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 -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
</Host>
See here for more information.

Servlet context loading twice with tomcat

When I have specified multiple host tags in the conf/server.xml, the servlet context is loading twice. It is spring based web application. I have extended the spring ContextLoader and customize it.
My server.xml (tomcat 7.0.22)
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
<Host name="domain.com" appBase="webapps/Domain"
unpackWARs="true" autoDeploy="true"
>
<Context path="" docBase="."/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
<Context docBase="/mnt/databank/posters" path="/Domain/posters"/>
<Context docBase="/mnt/databank/advertisement" path="/Domain/advertisement"/>
<Context docBase="/mnt/databank/star" path="/Domain/star"/>
</Host>
<Host name="www.domain.com" appBase="webapps/Redirecter"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="."/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/mnt/databank/logs/tomcat7"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" resolveHosts="false"/>
</Host>
I have added a log in the ContextLoader it trigering twice as follows,
2011-10-21 12:11:22,933 ERROR [Thread-2] c.i.b.u.DomainUtilInitializer [DomainUtilInitializer.java:38] Init Method Triggered
2011-10-21 12:11:46,621 ERROR [Thread-15] c.i.b.u.DomainUtilInitializer [DomainUtilInitializer.java:38] Init Method Triggered
Any help would be appreciated.
This seems like it should be expected behavior - you have configured Tomcat to run the application twice, once for each host. What do you expect? If you want a single instance of the webapp running, then you shouldn't configure two <host>s like so - I believe you add a host name alias to the single/default <Host>.

Categories

Resources