Java App not loading on tomcat 9 centos 7 - java

I'm pretty new to linux and I've been thrown into it. So far I made it work a few times, but this time it got me bad. The aim is to cluster tomcat and load balance with nginx but the app error is taking too much time from me. The app is correctly deployed by tomcat but is not accesible through browser. I've checked permissions, dns resolution, ports. The app resides out of webapps folder.
Java 8 installed from yum
Tomcat 9 taken with wget
Configured tomcat service to autostart
Added execution permits to .sh's
I honestly dont know what I'm missing. The same app currently runs under centos 6 on production server.
Thanks in advance

Finally I got it working. I was missing the <Context.../> just beneath my <Host.../> declaration. Production server doesn't have that line, so I supposed there was no need for it. Nevertheless the first time I got it working, I don't remeber having that set up.
Like this:
</Realm>
<Host name="hostname" appBase="/app/base/" unpackWARs="true" autoDeploy="false">
<Context path="" docBase="/app/base/" reloadable="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>
</Engine>
</Service>
</Server>

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.

SEVERE: Begin event threw error java.lang.ExceptionInInitializerError Intellij/Tomcat

I got a new project which I wanted to work with in Intellij. As local server we use Tomcat 7.0.68 and JDK 1.8.
This is my configuration in tomcat.
lib/catalina/org/apache/catalina/startup/Authenticators.properties:
NIGHTSHIFT=com.glit.swidA9O.v1.authenticator.NightShiftAuthenticator
conf/catalina.properties:
common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,/Users/Administrator/dev/server/apache-tomcat-7.0.68/shared/nightShift/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
conf/context:
<Valve className="com.glit.swidA9O.v1.authenticator.NightShiftAuthenticator" changeSessionIdOnAuthentication="true"/>
<Loader className="org.apache.catalina.loader.VirtualWebappLoader"
virtualClasspath="/Users/Administrator/dev/PROJECT/config;/Users/Administrator/dev/PROJECT/data"/>
conf/server.xml
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<Realm className="com.glit.swidA7U.v1.realm.NightShiftRealm" roleClassNames="com.glit.swidBS0.v1.shared.principals.NightShiftGroup" userClassNames="com.glit.swidBS0.v1.shared.principals.NightShiftUser"/>
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<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>
</Engine>
Our problem
Inside our local tomcat server we want to use a specific library called "NightShift". Unfortunately every time we try to build our application with intellij/tomcat we get this error somehow:
Complete stacktrace
Which is funny because if we try to build the application with eclipse tomcat runs smoothly and can actually build the project properly.
Those are the settings for Intellij/Tomcat we made:
These are my vm-options (actually I don't even know if you need this but just in case):
-DLOG4J2-ROOT=/Users/Administrator/dev/PROJECT/data/logs
-Djava.security.auth.login.config=/Users/Administrator/dev/PROJECT/config/NightShift-config/common/NightShiftJAAS.conf
-Dpu8.config.path=/Users/Administrator/dev/PROJECT/config/NightShift-config
-Dpu8.configuration.id=A7U
-Dpu8.environment=ide
-Djava.util.logging.config.file=/Users/Administrator/dev/PROJECT/config/NightShift-config/log-config.properties
-Djava.security.manager
-Djava.security.policy="/Users/Administrator/dev/server/apache-tomcat-7.0.68/conf/catalina.policy"
This is what our tomcat looks like:
Inside "shared" is a folder called "nightshift" and there are all the neccessary .jar files which we mentioned in the catalina.properties file.
Inside our artifacts we didn't include the "nightshift" jars, cause we want it installed in tomcat and not in our application.
There appears to be a problem with your application's logging initialization:
Caused by: java.util.NoSuchElementException
at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:365)
at java.util.ServiceLoader$LazyIterator.access$700(ServiceLoader.java:323)
at java.util.ServiceLoader$LazyIterator$2.run(ServiceLoader.java:407)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:409)
at java.util.ServiceLoader$1.next(ServiceLoader.java:480)
at com.glit.swidPV4.v1.common.log.LogFactory.getInstance(LogFactory.java:32)
at com.glit.swidPV4.v1.common.log.LogFactory.getLog(LogFactory.java:42)
at com.glit.swidA7R.v1.businessdelegate.VerifierBDFactory.<clinit>(VerifierBDFactory.java:29)
... 75 more
It looks like it is looking for a service provider that doesn't exist, and then failing to deal with that properly. That leads to a runtime exception which cascades into other things.
Look at the source code for com.glit.swidPV4.v1.common.log.LogFactory to figure out what what it is looking for.
Figure out why it is missing. My guess would be a missing / misplaced JAR file or a missing / misplaced config file.
Fix it.
(Maybe) fix the LogFactory code to be more resilient ... or to throw a custom exception rather than just bombing out with an obscure NoSuchElementException.
Figuring out remotely where JAR and config files should be for your particular case is too difficult. But this is just standard Java / Tomcat troubleshooting ... once you have figured out what is causing the problem.
Unfortunately every time we try to build our application with intellij/tomcat we get this error:
One "solution" would be to switch to using Maven or something to build WAR files, and test / deploy them by hand rather than relying of the "shiny" Intellij integration stuff.
(You shouldn't be hot deploying into a production server anyway. That's poor practice. You are one mistake away from trashing your production env. And if this is just a dev / test environment, it is a good idea to get into the habit of doings things the right way in dev.)

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 sub-domains in Apache Tomcat 8

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.

Categories

Resources