Configure Apache to run Tomcat and PHP

Posted by Rob James | Posted in Innovation, Software | Posted on 27-01-2010-05-2008

13

I have decided to blog about this, as I have had never ending problems in the past to try and get this configured. Here is the scenario; you have a project that requires hosting a single website in a single web domain, but you have a mix of PHP applications and Java (or in my instance, GRAILS) applications that you need to run. The way you do this, is configure both PHP and Java applications to run through an Apache front end. The problem is, that there is no good documentation to help you do this, so here I go!

In this example, I am going to show you how to run a static html site, plus a PHP application (we’ll use WordPress for this example) and a java .WAR applications all off the same instance of apache on the same server. Our site will look like this;

  • http://www.mydomain.com/ – will have a static html site and will have links to other pages and directories under this domain, with the exception of;
  • http://www.mydomain.com/blog – will be running WordPress, a PHP blogging application, &
  • http://www.mydomain.com/quotation – will run a java application packaged as a .WAR

I am assuming that you have access to your own server, or virtual private server (I recommend NeoSurge hosting if you need one), and that you are running Ubuntu Server (Although my instructions are for Ubuntu, they are easily transferable to other falvours of Linux). Here is the process form start to end to get it setup;

Server Setup

I am using Ubuntu Server 9.04, and I have set up a ‘vanilla’ instance of the server running with none of the options configured (DNS etc). Now start the server & login with the userename and password elected on the install

Firstly we will install some tools that will come in handy later such as ssh (for terminal access), I use nano for text editing (which may not installed), but you may prefer to install vi or vim, and an ftp server

NB: I have included SSH installation, but with most VPS and hosted providers, this will be done for you (Otherwise, you wouldn’t be able to get into your server :-) You will only need to do this if you want to run a virtual machine in your own environment).

Install OpenSSH

To install openSSH, type the following in the command line (NB: you will be prompted to enter your password again to gain SuperUser access – which is what the sudo command is)

sudo apt-get install openssh-server

After the installation process, you will have openSSH on the server. Next we install FTP

Install FTP Server – vsftpd

sudo apt-get install vsftpd

To allow log on access for authenticated users, you need to edit /etc/vsftpd.conf

sudo nano /etc/vsftpd.conf

find the following two lines and uncomment them (they are near each other)

local_enable=YES
write_enable=YES

Now restart the ftp server so your new settings can be picked up

sudo /etc/init.d/vsftpd restart

Now you should be able to ftp, and ssh to the server so that you can make changes and upload files – which you will need to do when you start configuring mod-proxy

Install required Software

Next you will need to install all the software such as mysql, apache, PHP, tomcat etc. I will be just installing all the basic software, so I won’t go into the details of installing PHPMyAdmin if you require it. As there are lots of tutorials out there on how to do that.

Install mysql

sudo apt-get install mysql-server

This will take a while, but after its done, you will have MySql installed. Again, you will need to configure MySql to be able to create databases etc remotely, but as is, you can ssh into your machine and do what you need to do in the MySql command line.

Install apache, java and tomcat

Now you will install Apache, Java, Tomcat and Tomcat Admin application so that your web server and Java setup is complete, this can be done in a single command;

sudo apt-get install apache2 sun-java6-jdk tomcat6 tomcat6-admin

You will need to go through the license acceptance process, and depending on your internet link speed, the above may take a while.

Install php

Now that Apache is installed, you can go ahead and install the PHP extensions so that php code can be parsed and rendered.

sudo apt-get install php5
sudo /etc/init.d/apache2 restart

That’s it!!! All the software is now installed and its time to do some configuration.

Configuration

First we are going to enable mod-proxy. Mod-proxy is the extension to apache that allows you to use apache as the proxy for web requests but then passes on the processing to tomcat (in our example). We are going to be using the AJP connector, but its pretty much the same process if you wanted to use the http connector or one of the other various adapters that apache provides.

Enable mod-proxy

sudo a2enmod proxy_ajp

After you enable the proxy, you now need to allow the proxying to occur. You do this by modifying your proxy.conf file.

sudo nano /etc/apache2/mods-enabled/proxy.conf

Find where it says “Order deny,allow”, and change the text underneath so it looks like the following

Order deny,allow
Allow from all
#Deny from all

Quite simply, you are now allowing all rules to be proxied, and turning off the Deny rules (you can obviously get more fine tuned here, and you will see lots of security warnings, which I recommend you listen to, but for the moment this will get you by.).

Allow AJP port by uncommenting the connector in Tomcat

sudo nano /etc/tomcat6/server.xml

and uncomment

<Connector port=”8009″ protocol=”AJP/1.3″ redirectPort=”8443″ />

Add an admin and manager user for tomcat

sudo nano /etc/tomcat6/tomcat-users.xml

<role rolename=”admin”/>
<role rolename=”manager”/>
<user username=”yourUser” password=”aPassword” roles=”admin,manager”/>

Give tomcat some more memory (default is never enough) and disable tomcat’s security

sudo nano /etc/default/tomcat6

and uncomment and change the following

JAVA_OPTS=”-Djava.awt.headless=true -Xms128M -Xmx1280M -XX:MaxPermSize=256m”
TOMCAT6_SECURITY=no

Setup tomcat

So now you are going to setup tomcat so that you can run WARs in the tomcat Servlet Container.

sudo nano /etc/tomcat6/server.xml

First thing we are going to do is change the host setting. I do this by leaving the default host and creating a new one for our purposes. Add an another <Host/> node below the existing one in server.xml

<Host name=”ServletName” appBase=”webapps/ServletName”
unpackWARs=”true” autoDeploy=”true” xmlValidation=”false” xmlNamespaceAware=”false”>
</Host>

The important thing in the above node is the appBase. This should point to the webapps directory (unless you have created a new one), and then the directory that will contain the servlet container. Even though unpackWARs is set to true, I find this a bit strange and like to unpack my own WARs, so I would also create the directory and in the location that is going to hold the exploded war.

sudo mkdir /var/lib/tomcat6/webapps/ServletName
sudo chown tomcat6:tomcat6  /var/lib/tomcat6/webapps/ServletName

Uplaod the War to /var/lib/tomcat6/webapps

And unpack it (unzip, and make sure it has the same name as the directory under webapps)

OK, now configure the proxy settings for apache, back in the proxy.conf

sudo nano /etc/apache2/mods-enabled/proxy.conf

Turn On Proxy requests on and configure the proxying (you can put this anywhere in the file, but I like to put the following at the top of the file)

ProxyRequest On
ProxyPreserveHost On
ProxyPass /ServletName ajp://localhost:8009/ServletName
ProxyPassReverse /ServletName ajp://localhost:8009/ServletName

and at the end of the file

ProxyVia On

The important part of the above change is that you are configuring what requests to apache are going to be passed to tomcat for processing. Above I am telling any request to /ServletName will be passed through. you can also tell it to ignore certain directories as well. For example, one common use would be to route all traffic to tomcat, but ignore static files that you want to apache to process for performance reasons. To do this, you would do something like;

ProxyPass /css !
ProxyPass /images !
ProxyPass / ajp://localhost:8009/

Pretty self explanatory, the only thing to keep in mind is any directories you want to ignore need to come first.

Finally, restart apache and then tomcat

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

That’s it – you are done!!!!

So now what?

At the start of this post, I talked about configuring the following sites;

  • http://www.mydomain.com/ – your static html;
  • http://www.mydomain.com/blog – WordPress PHP blog
  • http://www.mydomain.com/quotation – Java App

Well now that the configuration has been done – its pretty easy. To host your static site, just upload all your html, css, js & images etc into your root directory. You should find this in /var/www. To host your WordPress blog, its as easy as creating the directory /var/www/blog and uploading wordpress here.

PHP is all configured and you should be able to go through the standard installation process for WordPress by going to your url http://someurl.com./blog.

And finally the /quotation directory in my example is the directory that you need to use in the tutorial above where I have used ServletName.

Once you have done all this, and restarted tomcat and apache, all three sites; static, php and java should be working seamlessly….!!

Have Fun….

I’ve got the next Killer Idea!!!…………….Bullshit

Posted by Rob James | Posted in General News, Innovation, Software | Posted on 27-01-2010-05-2008

0

So you wake up in a cold sweat one night as you are in a ‘half sleep”. While you were in this delirious moment, you conjure up the most amazing idea that is going to make you a bazillion dollars and no one has done it before!!!!!

ho hum…………………..BORING!!!!

This is one of those urban myths that seems to never go away. People are always trying to stumble on that next killer idea. But the thing is, you and I probably have these great “wouldn’t it be awesome if…..” ideas almost every second day. And so does everyone. Since I am in the business of making ideas come to reality, I get my share of friends (also known as nut cases that think I am waiting for them to bombard me with their next great idea) calling me with their idea and are surprised by the fact that I don’t drop everything to work with them !?!?

So if this is not important, what is?

Your Customer!!! Its that simple…

Its much easier to fill an existing void for your customer than it is to convince your customer that they are in need of this fabulous invention. And timing is key, if you get it out too early, there will be no demand (you are trying to convince them they need it); too late and you have missed the market (someone else has beat you to it and your customers already have the product).

Its also important to extract the idea into reality. How much will it cost to build and distribute. “I can get it into every household in the country over the next 5 years!!!”, but when you do the numbers, the revenue you make never catches up to the cost of marketing and distribution (or maybe in 20 years it does) #FAIL.

You need to get ‘real’ customers in front of your product; get feedback, evolve, refine, more experimentation and testing, and work on filling that need.

The 24 Hour Startup

Posted by Rob James | Posted in General News, Innovation | Posted on 19-01-2010-05-2008

0

This morning I came across the most inspiring web site – http://www.24hour-startup.com/. (watch the 3 minute video presentation here)

Basically the idea was a bunch of developers, business guys and designers got together and delivered a startup, from start to finish in 24 hours. And I mean START to FINISH! They get together and don’t even have an idea, take it through to the point that on the 24th hour they launch the site live. The launch included a blog, twitter account and facebook page. “I love it when a plan comes together”

Then if that was not enough, they then list the startup on eBay and within 7 days get $5,100 for it. Not bad for a day’s work (sure, a long day and they have to split amongst a dozen of them)!

It reminds me of all the developer camps that go on, but I love the energy of this and how they deliver it.

Its a real inspirational story, and makes me want to lock myself in a room for a weekend to see what I could achieve!!!

Check ou their final product http://www.drhue.com – a shop-by-colour website