Stivlo'st in Asia

Programming and Travel

Browsing Posts published in March, 2007

PHP / Java Bridge is an interesting technology to allow to connect PHP with a Java virtual machine. On the official site they claim “It is more than 50 times faster than local RPC via SOAP, requires less resources on the web-server side, and it is faster and more reliable than communication via the Java Native Interface.”

http://php-java-bridge.sourceforge.net/pjb/

STEP 1: Compiling and installing Apache

$ tar xfvz apache_1.3.37.tar.gz
$ tar xfvz mod_ssl-2.8.28-1.3.37.tar.gz
$ cd mod_ssl-2.8.28-1.3.37
$ ./configure –with-apache=../apache_1.3.37
$ cd ../apache_1.3.37
$ SSL_BASE=/usr ./configure –prefix=/usr/local –enable-module=so \
–enable-module=speling –enable-module=rewrite –enable-module=ssl
$ make
$ make certificate TYPE=dummy
# make install

STEP 2: Compiling and installing php module

$ cd ..
$ tar xfvj php-5.2.1.tar.bz2
$ cd php-5.2.1
$ ./configure –enable-memory-limit \
–disable-url-fopen-wrapper –with-gd –enable-dbase –enable-exif \
–enable-ftp –with-gettext –with-iconv –enable-mbstring \
–with-jpeg-dir=/usr/lib –with-zlib-dir=/usr/lib –with-kerberos \
–with-png-dir=/usr/lib –enable-gd-native-ttf –with-freetype-dir=/usr/lib \
–with-openssl –with-xml –with-dom –disable-cgi –with-mcrypt \
–with-xsl –enable-javascript –with-mcal \
–with-sablot-js –with-readline \
–with-mysql –with-mysqli \
–with-config-file-path=/usr/local/conf/php-module \
–disable-cli –with-apxs=/usr/local/bin/apxs \
–with-curl
$ make
$ make install
# mkdir /usr/local/conf/php-module
# cp php.ini-recommended /usr/local/conf/php-module/php.ini
$ cd ..

STEP 3: Compiling and installing php cgi

$ cd ../php-5.2.1
$ ./configure –enable-memory-limit \
–disable-url-fopen-wrapper –with-gd –enable-dbase –enable-exif \
–enable-ftp –with-gettext –with-iconv –enable-mbstring \
–with-jpeg-dir=/usr/lib –with-zlib-dir=/usr/lib –with-kerberos \
–with-png-dir=/usr/lib –enable-gd-native-ttf –with-freetype-dir=/usr/lib \
–with-openssl –with-xml –with-dom –disable-cgi –with-mcrypt \
–enable-xslt –with-xslt-sablot –enable-javascript –with-mcal \
–with-sablot-js –with-readline \
–with-mysql –with-mysql \
–with-config-file-path=/usr/local/conf/php-cli \
–disable-cgi \
–with-curl
$ make
# make install
# mkdir /usr/local/conf/php-cli
# cp php.ini-recommended /usr/local/conf/php-cli/php.ini
$ cd ..

STEP 4: Installing PHP-Java Bridge

$ tar xfvz php-java-bridge_4.0.1.tar.gz
$ cd php-java-bridge-4.0.1
$ phpize && ./configure \
–with-java=/usr/local/jdk1.6.0,/usr/local/jdk1.6.0/jre \
&& make
# sh install.sh

Edit /usr/local/conf/php-module/php.ini to add the following lines:

extension_dir = “/usr/local/lib/php/extensions/no-debug-non-zts-20060613/”
extension=java.so

[Java]
java.class.path =
java.servlet = On
java.hosts=”localhost:8080″
java.persistent_connections=Off
;java.home = c:\jdk
;java.library = c:\jdk\jre\bin\hotspot\jvm.dll
;java.library.path = .\

STEP 5: Deploy JavaBridge.war

In order to use the JavaBridge extension the JavaBridge.war have to be deployed in tomcat

$ cp JavaBridge.war /usr/local/tomcat/webapps/

If Tomcat is running it will automatically deploy the application under webapps.

To enable Tomcat Manager/Admin Login on Tomcat 5.5 edit conf/tomcat-users.xml which by default has the following content:

<?xml version=’1.0′ encoding=’utf-8′?>
<tomcat-users>
<role rolename=”tomcat”/>
<role rolename=”role1″/>
<user username=”tomcat” password=”tomcat” roles=”tomcat”/>
<user username=”both” password=”tomcat” roles=”tomcat,role1″/>
<user username=”role1″ password=”tomcat” roles=”role1″/>
</tomcat-users>

Change it into:

<?xml version=’1.0′ encoding=’utf-8′?>
<tomcat-users>
<role rolename=”manager”/>
<role rolename=”admin”/>
<user username=”admin” password=”your_password_here” roles=”manager,admin”/>
</tomcat-users>

To apply the changes restart Tomcat (as user tomcat)

$ cd bin
$ sh shutdown.sh
$ sh startup.sh

Tomcat Admin is not installed by default from the core package, but you can login with username admin and the password provided in the Tomcat Application Manager http://your_web_server_here:8080/manager/

You should see a web page similar to the following:

tomcat manager

Installing Tomcat 5.5 on Linux is rather simple. Tomcat 5.5 requires a JDK version 1.5 or above. I previously installed a JDK 1.6 SE downloaded jdk-6-linux-i586.bin from http://java.sun.com/ and I’ve installed it into /usr/local/jdk1.6.0

STEP 1: Adding user tomcat

# useradd -m -d /home/tomcat tomcat

Note: useradd and groupadd are the POSIX standardized tools to create users and groups. adduser and groupadd are distribution specific wrappers which make it simpler to create users which conform to some (distribution-specific or local) policy.

STEP 2: Setting JAVA_HOME environment variable

Add the following line at the end of /home/tomcat/.bash_profile pointing to your JDK

export JAVA_HOME=”/usr/local/jdk1.6.0″

STEP 3: Downloading tomcat

Tomcat can be downloaded from http://tomcat.apache.org/ – The current patchlevel for tomcat 5.5 at the time of writing (21st march 2007) is 5.5.23. Download the binary distribution Core with filename apache-tomcat-5.5.23.tar.gz and save it in /usr/local

STEP 4: Installing tomcat

# cd /usr/local
# tar xfvz apache-tomcat-5.5.23.tar.gz
# ln -s apache-tomcat-5.5.23 tomcat
# cd tomcat
# chown -R tomcat *
# chown -R root bin common

STEP 5: Testing tomcat

# su – tomcat
$ cd /usr/local/tomcat/bin
$ sh startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk1.6.0

Point your browser to the webserver, port 8080 you should see tomcat welcome screen.

tomcat home

To shut down tomcat

$ sh shutdown.sh

Give a look to catalina.out in the logs directory to look for any errors or warnings.

To add a JDBC resource manually in the server.xml file:

Be aware that you hand-edit the server.xml file at your own risk; the IDE cannot repair a damaged server.xml file. You are strongly encouraged to create a backup version of your working server.xml file before beginning to edit by hand.

  • In the Runtime window, expand the Servers node and the Tomcat 5 Servers node. Stop the server by right-clicking the Tomcat instance node and choosing Stop.
  • Right-click the Tomcat instance node and choose Edit server.xml from the contextual menu to open the server.xml file in the Source Editor.
  • Add the following in between the </Context> tag of the examples context and the </Host> tag closing the localhost definition.
<Context path=”/DBTest” docBase=”DBTest”
debug=”5″ reloadable=”true” crossContext=”true”>

<Logger className=”org.apache.catalina.logger.FileLogger”
prefix=”localhost_DBTest_log.” suffix=”.txt”
timestamp=”true”/>

<Resource name=”jdbc/TestDB”
auth=”Container”
type=”javax.sql.DataSource”/>

<ResourceParams name=”jdbc/TestDB”>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>

<!– Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
–>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>

<!– Maximum number of idle dB connections to retain in pool.
Set to -1 for no limit. See also the DBCP documentation on this
and the minEvictableIdleTimeMillis configuration parameter.
–>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>

<!– Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
–>
<parameter>
<name>maxWait</name>
<value>10000</value>
</parameter>

<!– MySQL dB username and password for dB connections –>
<parameter>
<name>username</name>
<value>javauser</value>
</parameter>
<parameter>
<name>password</name>
<value>javadude</value>
</parameter>

<!– Class name for the official MySQL Connector/J driver –>
<parameter>
<name>driverClassName</name>
<value>com.mysql.jdbc.Driver</value>
</parameter>

<!– The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
–>
<parameter>
<name>url</name>
<value>jdbc:mysql://localhost:3306/javatest?autoReconnect=true</value>
</parameter>
</ResourceParams>
</Context>

Finally, use the JDBC resource in your web application.

References:

http://dev.mysql.com/doc/refman/5.0/en/connector-j-usagenotes-j2ee.html
http://tomcat.apache.org/tomcat-5.0-doc/jndi-datasource-examples-howto.html

Apache Logrotate

No comments

Today I wrote a script to rotate apache logfiles using logrotate. The scripts to rotate logs have to be in /etc/logrotate.d directory and can have any name. I rotate logs whenever they grow bigger than 200Mb (size 200M) and keep 30 archives (rotate 30). Before rotating I execute awstats script to calculate statistics and after rotation i reload apache so it can create new logfiles in case some have been rotated.

It’s also possible to use wildcards in the filenames.

/etc/logrotate.d/apache

/path/to/logfile1/access.log
/path/to/logfile2/access.log
{
rotate 30
size 200M
compress
sharedscripts
prerotate
/usr/local/scripts/awstats.php
endscript
postrotate
/usr/local/bin/apachectl restart
endscript
}

Powered by WordPress Web Design by SRS Solutions © 2010 Stivlo'st in Asia Design by SRS Solutions