Tuesday, May 15, 2012

Installing Tomcat 7 and Apache2 with mod_jk on Ubuntu 12.04

This is a compact note on how to install Tomcat and Apache2, and then configure to connect the Apache Web Server to Tomcat using mod_jk on Ubuntu 12.04 (the basics are the same, so should work on all OS with minor modifications).

1. Installing Apache, Tomcat

sudo apt-get install apache2

Test the installation from the browser using http://localhost/

sudo apt-get install tomcat7
sudo apt-get install tomcat7-admin

Create a test application for tomcat.
cd /var/lib/tomcat7/webapps
sudo mkdir tomcat-demo
sudo mkdir tomcat-demo/helloworld
sudo vim tomcat-demo/helloworld/index.jsp

and paste the code
<HTML>
 <HEAD>
  <TITLE>Hello World</TITLE>
 </HEAD>
 <BODY>
  <H1>Hello World</H1>
  Today is: <%= new java.util.Date().toString() %>
 </BODY>
</HTML>
Test the installation from the browser using

http://localhost:8080/tomcat-demo/helloworld/

2. Installing and configuring mod_jk

sudo apt-get install libapache2-mod-jk

First lets enable the redirect port 8443 on Tomcat

sudo vim /etc/tomcat7/server.xml

and uncomment the following line
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
Now we will create our workers.properties file for Apache.

sudo vim /etc/apache2/workers.properties

paste the text
# Define 1 real worker using ajp13 
worker.list=worker1 
# Set properties for worker (ajp13) 
worker.worker1.type=ajp13 
worker.worker1.host=localhost
worker.worker1.port=8009
Now to ask Apache to use this worker

sudo vim /etc/apache2/mods-available/jk.conf

change the JkWorkersFile property to
/etc/apache2/workers.properties


Finally to configure the URL Apache should pass through the Tomcat

sudo vim /etc/apache2/sites-enabled/000-default

and add the following line in your configurtation
<VirtualHost *:80>
.......................................
.......................................
JkMount /tomcat-demo* worker1
</VirtualHost *:80>
Now, restart the servers

sudo /etc/init.d/tomcat7 restart
sudo /etc/init.d/apache2 restart

and test with
http://localhost/tomcat-demo/helloworld/
it should work similarly to
http://localhost:8080/tomcat-demo/helloworld/

43 comments:

Marcos Nunes said...

Hi,
tks for the tutorial. Has only one error.
In the workers.properties you define: worker.list=worker and in the VirtualHost you use worker1. So you should change from worker.list=worker to worker.list=worker1

Nandini said...

Thanks Marcos..Corrected...

Vivek Nair said...

Works like a charm except one correction, you seem to be using the restart script for tomcat6 whereas the installation details are for tomcat7. Replacing the 7 for 6 is working good.

Thanks again.

Nandini said...

Thanks a lot Vivek. Corrected....

Vlad Zee said...

On my server with default Apache2 and Tomcat6, directories and file content are somewhat different:
* /etc/libapache2-mod-jk/workers.properties
worker.list=ajp13_worker
worker.ajp13_worker.port=8009
worker.ajp13_worker.host=localhost
worker.ajp13_worker.type=ajp13

Takunda Shabi said...

I'M GETTING THIS ERROR when trying to restart apache.
Syntax error on line 23 of /etc/apache2/mods-enabled/jk.conf:
Invalid command '/etc/apache2/workers.properties', perhaps misspelled or defined by a module not included in the server configuration
Action 'start' failed.
The Apache error log may have more information....finally apache fails to start after this...it the work wen a changed /etc/apache2/workers.properties to its initial JkWorkersFile in /etc/apache2/mods-enabled/jk.conf......can anyone help pliz....

graywh said...

Excellent info. Thanks.

I used "ajp13_worker" as it was already configured in "/etc/libapache2-mod-jk/workers.properties".

Thushara Sampath said...

Thanx very much..

konze said...

It doesn't work. I can access http://192.168.1.17:8080/tomcat-demo/helloworld without any problems.

But I can't access without a port (http://192.168.1.17/tomcat-demo/helloworld) I get an HTTP 503 Error: Service Temporarily Unavailable

whats wrong?

ramesh jangala said...

Nice tutorial, But when i am accessing the website without port number sompages showing the code. can i know the reason?

abhijit said...

Thanks, it worked perfectly for me.

Nandini Bhaduri said...

May i know why my restart will fail? When i choose stop, it does not stop the server either.
ubuntu@xxx:~$ sudo /etc/init.d/tomcat7 restart
* Starting Tomcat servlet engine tomcat7 [fail]

Nandini Bhaduri said...

Thanks so much for this tutorial and for correcting issues found by the other commenters. This has been a huge help to me.

Nandini Bhaduri said...

Is there a way to make apache redirect me from
http://localhost ( no /tomcat-demo)
to
http://localhost:8080/tomcat-demo
?

Nandini Bhaduri said...

Check if tomcat is running in your server. use sudo netstat -lnp and look for a program with name as java and listening on 8080

Nandini Bhaduri said...

man you are the best

i have been searching for four hours and reading a lot from apache website but in vain!!, all of them are confusing except for you...well done man

Nandini Bhaduri said...

Thanks a lot! I've appreciate it!

Nandini Bhaduri said...

Thanks. Took me a while to find a post that wasn't overly complicated. Oh how I hate official documentation!

Nandini Bhaduri said...

Thanks a bunch! I could not find a more straight-forward tutorial for a newb like me!

Nandini Bhaduri said...

thanks you very much, very much

Nandini Bhaduri said...

thank bro

Nandini Bhaduri said...

superb it working fine for me but need explaination at server.xml connect port was port 8009 for connecting tomcat and apache. why we redirect port 8443 but

apache is 80

Nandini Bhaduri said...

I had same error ...
in my case problem was that I had not enabled module

To enable module you should create link in /etc/apache2/mods-enebled/
which will point to /etc/apache2/mods-available/jk.load

I used command : ln -s /etc/apache2/mods-available/jk.load jk.load

Nandini Bhaduri said...

THANK YOU VERY MUCHHHH

Nandini Bhaduri said...

tks worked fine on Debian with external Tomcat

Nandini Bhaduri said...

Thanks dude..!

Nandini Bhaduri said...

Thanks a lot!..........

Nandini Bhaduri said...

http://localhost:8080/tomcat-demo/helloworld/ works well

but http://localhost:80/tomcat-demo/helloworld/ says that service temporarily unavailable.



any ideas of this ?

Nandini Bhaduri said...

Yeah, this shit is most helpful. Thank you.

Nandini Bhaduri said...

Hi,
When i try to run http://localhost/tomcat-demo/helloworld/
i get 503: Service Temporally Unavailable

and similarly with "http://localhost:8080/tomcat-demo/helloworld/"

i get "Unable To Connect"

and Wen i try to Restart Apache2 i get an msg saying that

"*
Restarting web server apache2

apache2: Could not reliably determine the server's fully qualified
domain name, using 127.0.0.1 for ServerName
... waiting ..apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName"

please Help

Nandini Bhaduri said...

Thanks for the great tutorial! I only found this after lots of frustration. One question, when I tested this, only a directory named "tomcat-demo" works w/o specifying the port, and no other directories work. Why? (I am not using 'localhost', rather I'm using a vm as a server).

Nandini Bhaduri said...

Thank you, this tutorial very usefel. Thanks again . . .

Nandini Bhaduri said...

Estas instrucciones solo tiene una salvedad para que queden perfectas. Donde dice que se cree la carpeta: .../tomcat-demo/ parece que es dentro del servidor de aplicaciones de Tomcat (para la versión 8.0.15 /var/local/apache-tomcat-8.0.15/webapps/) Pero esa no es la ruta donde van a colgar la aplicaciones de Tomcat que va a correr Apache. Sino que la carpeta /tomcat-demo debe colgar de la carpeta donde tengamos nuestro servidor Apache; que por defecto es en /var/www/html/ o donde tengas configurada tu carpeta raiz de apache2.
Y siguiendo con el ejemplo del autor, poniendo la carpeta con los JSP dentro de /var/www/tomcat-demo/helloworld.jsp funciona todo correctamente.
La explicación es que Tomcat está funcionando por su cuenta y Apache coge su servicio (puesto que lo tiene como módulo) cuando necesita interpretar documentos JSP, y en este caso Apache se lava las manos.

Nandini Bhaduri said...

many thanks , it worked perfectly !

Nandini Bhaduri said...

i've this error when i try to access :

http://localhost/app02/index.xhtml

Not Found

The requested URL /app02/index.xhtml was not found on this server.
Apache/2.4.7 (Ubuntu) Server at localhost Port 80

but with port 8080 work fine

Nandini Bhaduri said...

Hi, What i need to write down in the virtual host ?? why i need to place ...................... in the vhosts file. Can you please describe this ?

Nandini Bhaduri said...

I installed jk module, but i dont found the file jk.conf in apache, why ?

Nandini Bhaduri said...

excellent. Thx.

Nandini Bhaduri said...

Hi,
Its work for me in Ubuntu 12.04 but i did some other configuration like ...
Open - sudo gedit /etc/apache2/httpd.conf
By default, it would be blank, simply add the following line
ServerName Localhost and save it then restart your apache2 server then run your project in your browsers, its work fine, but in Ubuntu 14.04 its not working even after I add the localhost in httpd.confi file "ServerName localhsot " like .....

Nandini Bhaduri said...

Please let me know anyone had install mod_jk in Ubuntu 14.04

Nandini Bhaduri said...

If directory that you visited doesn't work, try to add the following line into virtual host directive:


For instance, if you have /jira or /confluence directories, you should add :


JkMount /jira worker1
JkMount /confluence worker1

clickorbit said...

This post helps me to Install Tomcat 7 with mod_jk on Ubuntu Thank you.
Regards,
Tomcat training.

Luiz Barros said...

Hello

I am Need you help

My port tomcat is 8181
http://appsmobile.sailorweb.com.br:8181/tomcat-demo/
Work

http://appsmobile.sailorweb.com.br/tomcat-demo/
Error

Not Found
The requested URL /tomcat-demo/ was not found on this server.

I am Use Vesta Control Panel