Apache + Tomcat - java

i need this kind of configuration:
Apache will respond to my blog if it is called on www.mydomain.com and i want to tomcat to respond to thirddomain.mydomain.com
I've configured a worker to respond to thirddomain.mydomain.com in this way:
<VirtualHost thirddomain.mydomain.com:80>
JkMount /* myworker
ServerName thirddomain.mydomain.com
</VirtualHost>
my worker is configured in this way:
worker.list=myworker
worker.myworker.port=8009
worker.myworker.host=thirddomain.mydomain.com
worker.myworker.type=ajp13
i've also a standard virtualhost that point to www
<VirtualHost www.mydomain.com:80>
DocumentRoot /var/www/html/blog/
ServerName www.mydomain.com
</VirtualHost>
the server.xml on tomcat is this one:
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="thirddomain.mydomain.com">
<Host name="thirddomain.mydomain.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" resolveHost="false"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
The problem is that apache is responding on both www and mythirddomain. How i can configure it to respond on different third domains?
Thank you

First map "thirddomain.mydomain.com" (subdomain) to a valid directory, to check if that config works without ajp and Tomcat (comment out JKMount).
DocumentRoot /var/www/html/anotherDir/index.html
If that works uncomment JKMount and change this to use an ip:
worker.myworker.host=thirddomain.mydomain.com
should be:
worker.myworker.host=127.0.0.1
(I guess Tomcat is on the same server as your web server). Add the worker name in the Engine element as well:
<Engine jvmRoute="myworker" name="Catalina" defaultHost="thirddomain.mydomain.com">

I think you should use mod_proxy for this type of configuration. You can follow this link to learn more about mod_proxy module of apache. Apache Mod_Proxy.
Add the below two lines in your httpd.conf.
ProxyPass thirddomain.mydomain.com/ localhost:8080
ProxyPassReverse thirddomain.mydomain.com/ localhost:8080
Here your Apache HTTP Server will act as an reverse proxy.

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.

Duplication server name in URL

I have a trouble with configure of Apache and Tomcat. My purpose is to get ability to adress to tui.org/servlet, when tui.org is my virtual host configured on local machine and /servlet/ is url-pattern which is defined at web.xml. But instead the servelet I get 404 Not Found by Tomcat but I can to adress to my server by tui.org/tui.org/servlet - it works. Structure of files on my server is the following:
Root: /var/www/tui.org
-------->/WEB-INF/classes/a
-------->/WEB-INF/classes/a/MainServlet.class
-------->/WEB-INF/web.xml
-------->/index.html
How can I achieve this behaviour?
web.xml:
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>/servlet</url-pattern>
</servlet-mapping>
My server.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="/var/www/"
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>
</Engine>
</Service>
</Server>
Configuration of tui.org (tui.org.conf at /etc/apache2/sites-available:
<VirtualHost *:80>
ServerName tui.org
DocumentRoot /var/www/tui.org
<Directory /var/www/tui.org>
AllowOverride All
Require all granted
</Directory>
JkMount /servlet* ajp13_worker
</VirtualHost>
I use Apache2, tomcat9 and Ubuntu 18.04.
Thank you in advance!
P.S. Sorry for my english.
There are several issues here:
You must not serve the web application directory with Apache - e.g. you're declaring DocumentRoot /var/www/tui.org for Apache and <Host appBase="/var/www/"> for Tomcat.
e.g but not limited to: The servlet spec specifically disallows to serve the directory WEB-INF to ever be served to a client, and you're explicitly bypassing this limitation.
An application deployed in a Host's appBase is always deployed under that name (unless specified differently in a context.xml file). The only other exception to that rule is if a directory is called ROOT - in that case, it's deployed without a name, thus omitting the tui.org that follows your server name.
but this is secondary to the issue of serving the whole webapp through Apache httpd. Fix that one first.
One way to fix your biggest problem is to omit the DocumentRoot directive in Apache and rather serve everything from Tomcat: JkMount * ajp13_worker.
For solving problem with adressing by tui.org/servlet I had to add <Context> directive to server.xml:
<Context path="" docBase="/var/www/tui.org/"/>
and change appBase from /var/www/ to /var/www/tui.org/.
And for fix problem with access to WEB-INF (thanks to #OlafKock) I had to add next to tui.org.conf:
<Directory /var/www/tui.org>
AllowOverride All
Deny from all
</Directory>

Syslog support under Tomcat 7

Does Tomcat 7 java.util.logging support sending logs to a remote syslog server?
It looks like log4j supports this:
log4j.appender.SYSLOG = org.apache.log4j.net.SyslogAppender
However, we are currently using RemoteIpValve so I'm not sure that will work under log4j.
server.xml
<Engine name="Catalina" defaultHost="localhost">
<Valve className="org.apache.catalina.valves.RemoteIpValve"
internalProxies="x.x.x.x" />
<Valve className="org.apache.catalina.valves.AccessLogValve"
directory="${LOG_DIR}/tomcat" prefix="access" suffix=".log"
pattern="combined" renameOnRotate="true" requestAttributesEnabled="true" />

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>

Running Tomcat server on two different ports

I want to to deploy a tomcat server such that it listens on two ports simultaneously (both for http protocol).
Just to make sure that you understand this requirement correclty , We have only one server instance but want to listen on two ports for HTTP protocol. For example anybody can access applications deployed in my server using port numbers 7080 and 8080
Is it possible to do that? If possible how can we achive this?
It's very simple. You only need to take a look at the conf/server.xml configuration file to add a new connector for the port you want. For example, if you have a connector like this:
<Connector port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
URIEncoding="UTF-8" />
Just add a new connector same as above in the configuration file, but altering the port parameter. That's all. Restart and you're done.
Yes, it is possible. Just edit server.xml (located in the folder named conf) like this:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8444" />
This will setup Tomcat to listen to both ports 8080 and 8081.
The documenation states:
port: The TCP port number on which this Connector will create a server socket and await incoming connections. Your operating system will allow only one server application to listen to a particular port number on a particular IP address. If the special value of 0 (zero) is used, then Tomcat will select a free port at random to use for this connector. This is typically only useful in embedded and testing applications.
redirectPort: If this Connector is supporting non-SSL requests, and a request is received for which a matching <security-constraint> requires SSL transport, Catalina will automatically redirect the request to the port number specified here.
So, altering the redirectPort is optional, depending on how you want such a redirection to work.
You can define 2 different services in /conf/server.xml .
The example is as below,
<Service name="Catalina_2">
<Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8444" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />
<Engine name="Catalina_2" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps_2" 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>
</Engine>
</Service>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
</Realm>
<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>
</Engine>
</Service>
Note : You may have required to increase the tomcat heap size.
you can specify the following code in your server.xml
<Service name="sample">
<Connector port="81" protocol="HTTP/1.1" maxThreads="100" connectionTimeout="2000"/>
<Engine name="sample" defaultHost="sample">
<Host name="myhostname" appBase="webapp2">
<Context docBase="C:\websites\sample\" />
</Host>
</Engine>
</Service>
Please, be sure on which user you are running Tomcat, since if you want to use it on any privileged port, you must use it under the root user.
Another thing you can do is to redirect port 80 to 8080 with iptables.
Something like this:
iptables -t nat -A PREROUTING -d 192.168.10.16 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080
Hope it helps
running tomcat in different port. We have to change four things inside service tag of server.xml file
we have to change port no. like 8080 to 80
we have to change redirectPort no like 8443 to 8444
we have to change Engine name like Catalina to Catalina_2
we have to change appBase name like webapps to webapps_1

Categories

Resources