So, I'm aware that this might have been asked a lot already, but I really tried what I could and I couldn't fix this. I've been trying this for over two weeks and followed every single tutorial on the Apache Tomcat website, to no avail.
So, what do I want? I want to deploy Java webservices to my server. I've got Apache httpd and tomcat running. So when I've got my webservice, I build and clean it and I copy the .war
file to my tomcat tomcat_home\webapps
folder.
I've got the mod_jk
module installed and loaded into Apache httpd (actually apache2), and then I've got these settings:
Apache config (/etc/apache2/apache2.conf
):
# Tomcat
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
# jk_mod properties
JkWorkersFile /var/lib/tomcat7/conf/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# map requests to these folders to the tomcat worker
JkMount /webservices/* apacheworker
JkUnMount /*.html apacheworker
JkUnMount /*.jpg apacheworker
JkUnMount /*.gif apacheworker
This probably works, as the apache service restarts without a problem (when I hadn't created the log file yet, apache didn't start anymore).
This is my workers.properties
file (at /var/lib/tomcat7/conf/workers.properties
):
# the list of workers
worker.list=apacheworker
# define the workers properties
worker.apacheworker.type=ajp13
worker.apacheworker.host=MYDOMAIN.COM
worker.apacheworker.port=8009
worker.apacheworker.lbfactor=1
worker.apacheworker.connection_pool_timeout=600
worker.apacheworker.socket_keepalive=1
If I'm right, the JkMount
directive is a relative path in my apache folder. I tried it as an absolute path as well but that doesn't work either.
So I don't have an actual directory at my /var/www/webservices
, but I do have a symbolic link (with that exact path) pointing to my /var/lib/tomcat7/webapps
directory. This way, when I copy a .war
-file into my webapps
-directory, it gets deflated automatically by tomcat (that works) and it should be accessible from my address "MYDOMAIN.COM/webservices/MYNEWAPP".
However, it doesn't work. Is there anyone that can enlighten me in what I'm exactly doing wrong? Right now, when trying to open the page, I get the code just showing up - so it's not being processed at all.
Thanks in advance!
EDIT So I tried the following:
localhost
instead of my own domain in the workers.properties
lbfactor
This is how my workers.properties
file looks like now:
# the list of workers
worker.list=apacheworker
# define the workers properties
worker.apacheworker.type=ajp13
worker.apacheworker.host=localhost
worker.apacheworker.port=8009
worker.apacheworker.connection_pool_timeout=600
worker.apacheworker.socket_keepalive=1
Further more, I changed my /etc/apache2/apache2.conf
as well (I don't use a separate mod_jk.conf
), to block access to my WEB-INF directory and remove the unneccessary mappings:
# Tomcat
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
# jk_mod properties
JkWorkersFile /var/lib/tomcat7/conf/workers.properties
JkLogFile /var/log/apache2/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# map requests to these folders to the tomcat worker
JkMount /webservices/* apacheworker
<Location "/Hello_World/WEB-INF">
AllowOverride None
deny from all
</Location>
All this didn't help (restarted both apache and tomcat servers as well). I still see the source from my .jsp file:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Where I think I lost you:
"Notice that by default the URI for your war is the name of the war file, so that
myapp.war will be accessed by calling http://mydomain.com/myapp, not
http://mydomain.com/webservices/myapp. The /webservices/ mapping is relevant only if
you have a webservices.war file in the tomcat's webapps directory. "
So, my webserver (apache httpd
) serves everythings that is in my /var/www/
directory. In that directory, I have a symlink called webservices
that points directly to my tomcat_home\webapps
folder. So, calling ls -la
gives me this:
lrwxrwxrwx 1 root root 24 Nov 16 16:48 webservices -> /var/lib/tomcat7/webapps
In that webapps
folder I have my actual .war files, including Hello_World.
So I don't know how I'd need to reach those files by typing in "http://mydomain.com/Hello_World" - that simply gives me a 404...
Thanks for helping me out!
Ok, couple of issues with your configuration:
worker.apacheworker.host=localhost
. I think it does not reach the tomcat because of that/webservices/
mapping is relevant only if you have a webservices.war file in the tomcat's webapps directory. The mapping should also be /webservices/*
for security reasons you should block access to the WEB-INF directory. Add the following to the mod_jk.conf:
<Location "/myapp/WEB-INF/">
AllowOverride None
deny from all
</Location>
There is no real need to make this mapping, you can let the tomcat to serve the static files as well. In case you want to save the load, it is better to use a content delivery network (CDN) anyway. Amazon CloudFront is a cheap alternative for that.
I would also consider replacing the mod_jk with either mod_proxy or mod_proxy_ajp. I have used the first one for several applications with success. The configuration is much simpler then:
ProxyPass /myapp http://localhost:8080/myapp
ProxyPassReverse /myapp http://localhost:8080/myapp