Monday, August 2, 2010

Running PHP (eg. Drupal) under Tomcat - with Caucho's Quercus PHP engine

Hello,

As the title is self descriptive, I'm trying to guide you how to run eg. drupal or wordpress on a java servlet container, for now, on Apache Tomcat 6; with help the Caucho's Quercus Engine. The wordpress is pretty much the same - but no urlrewrite needed at all.

As you may know, quercus is a reimplementation of php5 and its core libraries in java natively, which means, that no CGI trick has been adapted.

I did it on ubuntu, with the easiest way to have the necessary tools:

sudo apt-get install tomcat6 mysql

then

mysql -u root -p
create database drupal default character set utf8;
grant all privileges on drupal.* to 'drupal'@'localhost' identified by 'drupal';

which creates the database for drupal, then creates the drupal user with drupal password and grant all privileges.

now, it's time to configure tomcat, /etc/tomcat6/server.xml:

<!-- MySQL database resource -->
<Resource name="jdbc/drupal"
type="javax.sql.DataSource"
auth="container"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/drupal"
username="drupal"
password="drupal"
maxActive="10"
maxIdle="0"
maxWait="-1"
description="Drupal MySQL Data Source"/>

because quercus when deployed as a web app, it needs a JNDI lookup name for database connection, defined here.

created a pretty simple /var/lib/tomcat6/webapps/drupal
which is exploded from http://quercus.caucho.com/download/quercus-3.1.2.war:

META-INF/
META-INF/MANIFEST.MF
WEB-INF/WEB-INF/lib/
WEB-INF/lib/quercus.jar
WEB-INF/lib/resin-util.jar
WEB-INF/lib/script-10.jar
WEB-INF/web.xml

and modifying the web.xml to let quercus php engine to use our prepared JNDI database:

<!--
Tells Quercus to use the following JDBC database and to ignore the
arguments of mysql_connect().
-->
<!--
<init-param>
<param-name>database</param-name>
<param-value>jdbc/drupal</param-value>
</init-param>
-->

Now let's download the mysql's jdbc connector to /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/ or, as I did,

sudo apt-get install libmysql-java
ln -s /usr/share/java/mysql.jar /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/mysql.jar

only symbolic linking mysql connector to the webapp. I never put anything into tomcat's lib dir (/var/lib/tomcat6/lib/) which is not organic part of the tomcat, mysql, as in this example, part of the webapp, not the container!

the others pretty much the same as in the original quercus webapp .war file.

now, the an important, to download the urlrewrite module for servet container, to let drupal run properly, because installed drupal has this .htaccess file (used by Apache HTTPD), the essentials:

<ifmodule mod_rewrite.c="">
RewriteEngine on
[..]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</ifmodule>

download it and put it to WEB-INF/lib directory, ie. to /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/urlrewrite-3.2.0.jar.

So we need to implement this under Tomcat. Since urlrewrite (ATM version 3.2) does not implements the REQUEST_FILENAME condition, we have two choices, first one to implement a custom class, as can be seen on http://www.brianshowalter.com/blog/running_drupal_on_quercus page, it's not so dynamic, since the install URI should be changed, so I decided to workaround, so I've put

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confReloadCheckInterval</param-name> <param-value>0</param-value> <param-name>confPath</param-name> <param-value>/WEB-INF/urlrewrite.xml</param-value>
<param-name>logLevel</param-name> <param-value>TRACE</param-value> </init-param></filter>

into the WEB-INF/web.xml to allow urlrewrite run and check it at every single hit, later the values recommended to change.

I placed the following config in WEB-INF/urlrewrite.xml (the whole file):

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN"
"http://tuckey.org/res/dtds/urlrewrite3.2.dtd">

<urlrewrite>
<!--
<class-rule class="com.brianshowalter.drupalrewrite.DrupalRule" />
-->
<rule>
<from>\.(php|gif|css|jpg|png|ico|js|html|htm|txt)</from>
<to last="true">.$1</to>
</rule>

<rule>
<from>^/(.*)</from>
<to>/index.php?q=$1</to>
</rule>
</urlrewrite>

which have been taken from http://wiki.caucho.com/Quercus:_Drupal page, and does pretty much the same, not on Caucho's Resin, but on Apache's Tomcat with urlrewrite, the same thing is the Quercus PHP engine.

If the the URL ended with .php or .gif etc., no action is taken, that's why it is the "last", on other cases, it is rewritten to forward the request to index.php.

and for the sake of understanding, the whole WEB-INF/web.xml file:

PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"&gt;

<web-app>
<description>Caucho Technology's PHP Implementation</description>

<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>confReloadCheckInterval</param-name> <param-value>0</param-value> <param-name>confPath</param-name> <param-value>/WEB-INF/urlrewrite.xml</param-value>
<param-name>logLevel</param-name> <param-value>TRACE</param-value> </init-param></filter>

<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern></filter-mapping>

<servlet>
<servlet-name>Quercus Servlet</servlet-name>
<servlet-class>com.caucho.quercus.servlet.QuercusServlet</servlet-class>
<init-param>
<param-name>license-directory</param-name> <param-value>WEB-INF/licenses</param-value> </init-param>
</servlet>

<servlet-mapping>
<servlet-name>Quercus Servlet</servlet-name>
<url-pattern>*.php</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.php</welcome-file>
</welcome-file-list></web-app>

let's see what we got here:

find /var/lib/tomcat6/webapps/drupal/WEB-INF/ -ls
6826814 4 drwxr-xr-x 4 tomcat6 tomcat6 4096 Jul 30 17:40 /var/lib/tomcat6/webapps/drupal/WEB-INF/
6826870 4 drwxr-xr-x 2 tomcat6 tomcat6 4096 Jul 30 17:04 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib
3064049 0 lrwxrwxrwx 1 tomcat6 tomcat6 25 Jul 21 11:38 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/mysql.jar -> /usr/share/java/mysql.jar
3065516 10296 -rw-r--r-- 1 tomcat6 tomcat6 10524501 Jul 1 16:47 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/resin.jar
6733981 368 -rw-r--r-- 1 tomcat6 tomcat6 371264 Jul 1 16:47 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/javamail-141.jar
6733982 96 -rw-r--r-- 1 tomcat6 tomcat6 91794 Jul 1 16:47 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/inject-16.jar
6733983 200 -rw-r--r-- 1 root root 198436 Dec 4 2008 /var/lib/tomcat6/webapps/drupal/WEB-INF/lib/urlrewrite-3.2.0.jar
6827312 4 -rw-r--r-- 1 tomcat6 tomcat6 2029 Jul 30 17:24 /var/lib/tomcat6/webapps/drupal/WEB-INF/web.xml
6826871 4 drwxr-xr-x 2 tomcat6 tomcat6 4096 Jul 1 16:47 /var/lib/tomcat6/webapps/drupal/WEB-INF/licenses
6827313 4 -rw-r--r-- 1 root root 454 Jul 30 17:40 /var/lib/tomcat6/webapps/drupal/WEB-INF/urlrewrite.xml

Make sure, that every file is under the permission of tomcat6:

sudo chown tomcat6:tomcat6 -R /var/lib/tomcat6/webapps/drupal/

and I'd recommend for now, NOT IN PRODUCTION to turn off the tomcat's java security in /etc/default/tomcat6 by:

# Use the Java security manager? (yes/no, default: yes)
# WARNING: Do not disable the security manager unless you understand
# the consequences!
TOMCAT6_SECURITY=no

and later to set up more granular settings in /etc/tomcat6/policy.d/*.policy

And at last but not least, the best news, that if we move /var/lib/tomcat6/webapps/drupal to another (eg. /var/lib/tomcat6/webapps/site or /var/lib/tomcat6/webapps/ROOT - which is the / on the site), it will work as well, and if you upgrade, you just need to update the urlrewrite configuration - no need to recompile your java classes!

Best luck for it!

Thursday, March 18, 2010

1848

I believe this is the one of the worst marketing actions ever: Putting a cockade, the symbol of Hungarian Revolution of 1848 to the Kaiser salami. Imagine a Julius Jacob von Haynau salami, it would be even worse! Not mentioning that the Hungarian cockade is different, it has the green outside, and the red inside, but I have never seen a correct one at any Hungarian sho. :) Nice one, but next time, think twice! Anyway please read more about probably the biggest Hungarian Celebration's background at: http://en.wikipedia.org/wiki/Hungarian_Revolution_of_1848

Saturday, December 26, 2009

Disney On Ice - The Big Red Button what shouldn't be pushed

Today with our family we were at Laszlo Papp Sport Arena in Budapest to watch Disney On Ice - Journey of Mickey & Minnie which was quite good (however it has a very long part about the tale of Peter Pan what we don't know too much), the kinds were amused and enjoyed the skating Disney characters. It was full of colours, the performers were talking and singing and the choreography was nice.

We had a cheap ticket from where we could see everything suprisingly. I'd recommend almost any place especially from the upstairs.

One thing what I did not understand: what was that Big Red Flashing Button when we were about to leave the building. Any idea about its purpose and this alarming state?

Thursday, December 24, 2009

What's in the Picasa for Linux?

I just wanted to put my new pictures somewhere so I was looking a good site to put - and here we go, I gave a shot to Google Picasa since it has a native linux client. I though.

At the first glance it's slow. And not handy since it import all my folders without any question. I checked, and what I found?

~/.google/picasa/3.0$ ls -la
total 648
drwxr-xr-x 4 toma toma   4096 2009-12-24 22:57 .
drwxr-xr-x 3 toma toma   4096 2009-12-24 22:54 ..
-rw-r--r-- 1 toma toma      0 2009-12-24 22:54 .firstrun
-rw-r--r-- 1 toma toma     16 2009-12-24 22:54 .gnomehal
-rw-r--r-- 1 toma toma     11 2009-12-24 22:54 .update-timestamp
drwxr-xr-x 2 toma toma   4096 2009-12-24 22:54 dosdevices
drwxr-xr-x 5 toma toma   4096 2009-12-24 22:54 drive_c
lrwxrwxrwx 1 toma toma     39 2009-12-24 22:54 generic.ppd -> /opt/google/picasa/3.0/wine/generic.ppd
-rw-r--r-- 1 toma toma   2867 2009-12-24 22:59 picasa.log
-rw-r--r-- 1 toma toma 549622 2009-12-24 22:55 system.reg
-rw-r--r-- 1 toma toma  68072 2009-12-24 22:57 user.reg
-rw-r--r-- 1 toma toma   2275 2009-12-24 22:54 userdef.reg
Wow, what? drive_c ? Doesn't ring the bell? It might be related to wine..
And unfortunately it is. So google is using wine for porting picasa for linux.
I have no comment ATM.

Wednesday, December 9, 2009

Installing packages on external storage device on OpenWRT

I've just put a flash memory stick into the OpenWRT device (Asus WL-500g Deluxe) to be able to run bigger application on it. So I formatted to ext3 and mounted. But for some reason however the opkg (the package manager) has been instructed to use this new destination
toma@OpenWrt:~# cat /etc/opkg.conf
src/gz snapshots http://downloads.openwrt.org/kamikaze/8.09.1/brcm-2.4/packages
dest root /
dest ram /tmp
dest opt /opt
lists_dir ext /var/opkg-lists
option overlay_root /jffs
This way:
opkg -d opt install tcpdump
Then I got this nasty message which refers to the root fs instead of the external storage however the -d used to put the package there:
# opkg -d opt install tcpdump
Installing tcpdump (3.9.8-1.1) to opt...
Collected errors:
 * Only have 352 available blocks on filesystem /opt/, pkg tcpdump needs 652
To resolve the issue, I cheated:

option overlay_root /opt
to the /etc/opkg.conf mentioned above already.

Everything went fine with:
# opkg -d opt install tcpdump
Installing tcpdump (3.9.8-1.1) to opt...
Downloading http://downloads.openwrt.org/kamikaze/8.09.1/brcm-2.4/packages/tcpdump_3.9.8-1.1_mipsel.ipk
Connecting to downloads.openwrt.org (78.24.191.177:80)
tcpdump_3.9.8-1.1_mi 100% |**************************************************************************************************************************************************************|   251k 00:00:00 ETA
Configuring tcpdump
Just a foot note, this is pretty handy tool anyway...

Tuesday, December 8, 2009

The Irony of Flash Blocker Firefox plugin


I just wanted to check the http://storyofstuff.com/ site, right after I just installed the great Flash Blocker plugin for Firefox (read: Iceweasel) when I got this:

Well, I can understand now how can we live without Flash. :)

Tuesday, December 1, 2009

Year of Linux Desktop?

I just wanted something not too special - listen On-Line radio stations with my favorite media player jukbox application called Rhythmbox, to watch (read: see) and listen (read: hear) flash videos in Icewasel browser on my Debian desktop, record and play back audio files and getting a working GTalk client which is the recent Pidgin IM software.

So I found myself in trouble - the chaos of ALSA, OSS, PulseAudio and many other (ESD, Jack, aRTs, PortAudio, etc.) sounds systems. However I was quite sure that since Ubuntu as the leader of `Linux Desktop' adopted the PulseAudio I have no choice, unless I'd like to hack all days.

Playing with them and figuring out that I completely don't understand the intentions of the different designs, I found the best documentation at http://www.pulseaudio.org/wiki/PerfectSetup where I could find all I needed. Just a note: check the page and decide - Is this the Year of Linux Desktop?

Cheers