Liferay daemon script

5 décembre 2011 2 commentaires

I’ve googled around looking for a good daemon script for Liferay,… and I finally wrote mine.

Here is the script I use for running a Liferay 6.0.6 instance as a sysv daemon under linux. The script has been tested on an Ubuntu 10.04 LTS server.
It just the runs a normal liferay-tomcat bundle, unzipped in the /var/liferay6 directory.

# Provides:          liferay
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Liferay portal daemon.
# Description:       Starts the Liferay portal.
# Author:            Julien Rialland <>

#Display name of the application
APP_NAME="Liferay 6.0.6"

#Location of Liferay installation
export LIFERAY_HOME=/var/liferay6

#unprivileged user that runs the daemon. The group/user should have been created separately,
#using groupadd/useradd

###This is end of the configurable section for most cases, other variable definitions follow :

#Only root user may run this script
if [ `id -u` -ne 0 ]; then
	echo "You need root privileges to run this script"
	exit 1

#tomcat directory
#detection of the tomcat directory within liferay
TOMCAT_DIR=`ls "$LIFERAY_HOME" | grep tomcat | head -1`

#location of pid file
export CATALINA_PID=/var/run/

# guess where is JAVA_HOME if needed (when then environment variable is not defined)
JVM_DIRS="/usr/lib/jvm/java-6-openjdk /usr/lib/jvm/java-6-sun /usr/lib/jvm/default-java /usr/lib/jvm/java-1.5.0-sun /usr/usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.5-ibm"
if [ -z "$JAVA_HOME" ]; then
        for jdir in $JVM_DIRS; do
                if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
                        export JAVA_HOME="$jdir"

#if JAVA_HOME is still undefined, try to get it by resolving the path to the java program
if [ -z "$JAVA_HOME" ]; then
        javaexe=`which java`
        if [ ! -z "$javaexe" ]; then
                javaexe=`readlink -m "$javaexe"`
                export JAVA_HOME=`readlink -m "$jdir"`

#if JAVA_HOME is still undefined, crash the script
if [ -z "$JAVA_HOME" ]; then
	echo 'The JAVA_HOME environment variable could not be determined !'
	exit 1

#extra jvm configuration : enable jmx

#extra jvm configuration : enable remote debugging
#export JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=9998" 


#verify that the user that will run the daemon exists
id "$USER" > /dev/null 2>&1
if [ "$?" -ne "0" ]; then
	echo "User $user does not exist !"
	exit 1

#load utility functions from Linux Standard Base
. /lib/lsb/init-functions

#starts the daemon service
function start {
        log_daemon_msg "Starting $APP_NAME"

        #create work directory if non-existent
        mkdir $CATALINA_HOME/work 2>/dev/null

        #clear temp directory
        rm -rf "$CATALINA_HOME/temp/*" 2>/dev/null
        mkdir $CATALINA_HOME/temp 2>/dev/null

        #fix user rights on liferay home dir
        chown -R "$GROUP":"$USER" "$LIFERAY_HOME"
        chmod -R ug=rwx "$LIFERAY_HOME"

        #ensure that pid file is writeable
        mkdir `dirname "$CATALINA_PID"` 2>/dev/null
        chmod ugo=rw `dirname "$CATALINA_PID"`

        su "$USER" -c "$CATALINA_HOME/bin/ start"

        log_end_msg $status
        exit $status

#stops the daemon service
function stop {
        log_daemon_msg "Stopping $APP_NAME"
        if [ ! -f "$CATALINA_PID" ];then
            echo "file $CATALINA_PID is missing !"
            unset CATALINA_PID
        su "$USER" -c "$CATALINA_HOME/bin/ stop 10 -force"
        log_end_msg $status
        if [ "$status" = "0" ];then
            rm -f "$CATALINA_PID"
        exit $status

#restarts the daemon service
function restart {
        sleep 3s

#prints service status
function status {
  if [ -f "$CATALINA_PID" ]; then
    pid=`cat "$CATALINA_PID"`
    echo "$APP_NAME is running with pid $pid"
    exit 0
    echo "$APP_NAME is not running (or $CATALINA_PID is missing)"
    exit 1

case "$1" in
		echo $"Usage: $0 {start|stop|restart|status}"
		exit 1

Just name the script ‘liferay’ and put in in /etc/init.d, If you want it to run automatically when the server starts up, you just have to run the following commands :

sudo chmod u+x /etc/init.d/liferay
sudo update-rc.d liferay defaults

Catégories :Uncategorized Étiquettes : , ,

WTP Eclipse project generation from Maven configuration

The maven ‘eclipse’ plugin is a bit outdated, but very useful. I had some issues when generating eclipse configuration specifically for web projects.
Here is a description on how I managed to configure the plugin finally !

This configuration generates an Eclipse project configuration when you run mvn eclipse:eclipse. All you have to do then is to import the project into Eclipse by running the File>Import menu entry in Eclipse

This configuration fixes some issues I used to have with mvn-generated Eclipse projects :

  • utf-8 encoding for all text files
  • good versions in project’s facets (servlet 3.0, java 1.6, javascript 1.0)
  • Correct web-specific settings (web root directory location, and use of the jar dependencies in the webapp)
  • Spring-enabled project nature

The plugins section of the pom.xml looks like that :





		<!-- Settings for generating eclipse project -->
							  <fixed facet=""/>
							  <fixed facet="jst.web"/>
							  <installed facet="" version="${java.version}"/>
							  <installed facet="jst.web" version="${servlet-api.version}"/>
							  <installed facet="wst.jsdt.web" version="1.0"/>


You may also have to ensure that you gave a value to the M2_REPO variable in the Eclipse settings points to your local .m2/repository.

Catégories :Uncategorized

From zero to Liferay portlet in less that 5 minutes (depending on your network connection)

Following this recipe, i can write a new portlets (for demoing purpose) in a very short time !

1) Create a the repertory structure and download a fresh bundle distribution:

mkdir $HOME/liferay
mkdir $HOME/liferay/portlets
mkdir $HOME/liferay/bundles
cd $HOME/liferay/bundles

(you may know want to run liferay : just run the script ‘$HOME/liferay/liferay-portal-6.0/jetty-6.1.24/bin/

3) Create a new portlet project using the liferay archetype

cd $HOME/liferay/portlets
mvn archetype:generate \
-DarchetypeGroupId=com.liferay.maven.archetypes \
-DarchetypeArtifactId=liferay-portlet-archetype \
-DarchetypeVersion=6.0.6 \
-DgroupId=net.jr.testapp \

You just have to modify the property at the end of the generated pom.xml :


The portlet can be recompiled/deployed easily by running the following command :

mvn clean package liferay:deploy

4) The only things that are needed to customize the portlet is to modify the main.js and view.jsp files…

5) More fancy things may be done by turning the project into an Eclipe project, and then import it using Eclipe :

mvn eclipse:eclipse

It doesn’t take more than 5 minutes, counting the time it needs to download the Liferay bundle !

Catégories :Uncategorized

OVH minicloud

11 septembre 2011 2 commentaires

J’ai testé il y a quelques temps l’offre minicloud d’OVH. J’ai trouvé la possibilité d’avoir une machine sur le net pour moins de 8 euros/mois vraiment géniale, mais un peu plus de stockage n’aurait pas été du luxe.

Création de l’image, première connexion :

Il faut d’abord créer un compte et se connecter au ManagerV5 d’ovh.

Créer une paire de clés ssh grace à l’interface web, puis télécharger la clé privée (j’utilise openssh, je choisit donc le format pem), ensuite il faut donner les bons droits à la clé : chmod go-rx sshkey.pem

Ensuite on peut créer son « cloud » via l’interface web, j’ai choisi Ubuntu (32bits et 64bits sont proposés, j’ai choisi 64bits).

Ensuite on peut se connecter à la machine une fois démarrée, La somme d’argent mise sur le « compte » est débité à raison d’un centime d’euro par heure, jusqu’a ce que le compte soit épuisé ou que l’on éteigne la machine depuis l’interface web.

NOTE : L’Ip / nom de domaine n’est valide qu’au cours d’une seule ‘session’, une nouvelle ip est attribuée a chaque démarrage de l’image !

On se connecte en ssh : ssh -i sshkey.pem root

Installation de paquets

Pour mon confort:


Pour Apache et Php :


Installation de Tomcat7

J’ai décidé d’installer Tomcat7, il n’y a pas (encore) de paquet apt correspondant. J’installe donc à la main :

mkdir /usr/local/tomcat7
cd /usr/local
tar zxvf apache-tomcat-7.0.16.tar.gz
mv apache-tomcat-7.0.16 tomcat7
rm apache-tomcat-7.0.16.tar.gz

Tomcat ne sera jamais accédé directement, mais toujours via le serveur Apache grâce à mod_proxy_ajp. Donc je commente le Connector http dans le fichier conf/server.xml.

Démarrage de Tomcat en tant que service

Je dispose d’un script de service ‘maison’ nommé tomcat, je le place dans /etc/init.d

Ensuite : update-rc.d tomcat defaults

Configuration d’Apache

Activation des modules proxy et proxy_ajp

a2enmod proxy
a2enmod proxy_ajp

Ensuite on vient modifier le ficher /etc/apache2/mods_available/proxy.conf :

ProxyRequests Off

AddDefaultCharset off
Order deny,allow
Deny from all
#Allow from

# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block

ProxyVia Off

#redirect to tomcat
ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

Mise en place d’un VirtualHost

1) Achetez votre nom de domaine !
2) Créer le dossier dans /var/www :

mkdir /var/www/

3) Ajouter la conf dans /etc/apache2/sites_available :


DocumentRoot /var/www/

Puis activer la conf a2ensite

Sécurisation du serveur : Script de Firewall

Un script de firewall est a mon sens une bonne mesure de sécurité :
J’utilise un script tout simple que je range dans /etc/init.d/firewall (que j’active ensuite avec updaterc.d firewall defaults)

# Provides: firewall
# Required-Start: $remote fs $syslog $network
# Required-Stop: $remote fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: firewall
# Description : iptables-based firewall script
# Author : Julien Rialland

IPT=`which iptables`

#firewall rules installation
#note that the order in which rules are appended is very important. For example, if your first rule is
#to deny everything... then no matter what you specifically allow, it will be denied.
function start {
echo 'Installing new firewall rules...'

#default policy for the predefined chains

#accept ssh connections
$IPT -A INPUT -p tcp --dport 22 -j ACCEPT

#accept http and https connections (80,443)
$IPT -A INPUT -p tcp --dport 80 -j ACCEPT
$IPT -A INPUT -p tcp --dport 443 -j ACCEPT

#accept anything from localhost

#Accept related or established connections so ftp can work, for example

#everything else is dropped

echo '...done.'

function stop {
echo 'Flushing all firewall rules...'
echo '...done.'

function restart {

function status {
$IPT -L -n -v

case "$1" in
echo "Usage : $0 (start|stop|restart|status)"

Mise en place de fail2ban

ce script surveille les logs de ssh, et bannit les ip qui tentent des connexions repétées sans réussir, ce qui empêche les attaques par dictionnaire par exemple : apt-get install fail2ban

DNS dynamique

Les machines minicloud n’ont pas d’ip fixe, Il faut donc configurer un dns dynamique. Même si on dispose d’un nom de domaine (en .fr par exemple) il faut pouvoir toujours rediriger vers la même machine, même aprés un redemarrage, sans avoir a reconfigurer les dns. Je me sers dont d’une adresse en (CNAME)=> => ip flottante minicloud

apt-get install libio-socket-ssl-perl ddclient

Voici ma configuration (/etc/ddclient.conf)

use=if, if=eth0

Configuration Email

Ovh empeche l’utilisation des miniclouds comme serveurs email, in vont même jusqu’a empécher le traffic vers les ports 25(smtp) d’autres machines.
Pour pouvoir envoyer des notifications par email depuis mes programmes, je contourne la limitation en configurant postfix pour utiliser mon compte gmail :

apt-get install postfix libsasl2-2 ca-certificates libsasl2-modules heirloom-mailx

vi /etc/postfix/

relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes
vi /etc/postfix/sasl_passwd:

chmod 400 /etc/postfix/sasl_passwd
postmap /etc/postfix/sasl_passwd

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

ln -s /etc/postfix/sasl_passwd.db /etc/postfix/sasl/sasl_passwd2.db

service postfix restart

La procédure relève un peu du parcours du combattant, mais en tout cas ça me permet de valider le fait qu’on peut envoyer des emails depuis un minicloud.

L’autre solution (meilleure a mon avis) serait d’utiliser le relais smtp d’ovh, qui permet d’envoyer 100 mails/heure.

Installation de subversion

J’ai décidé d’installer un serveur subversion sur cette machine, là encore Je me suis un peu battu, mais j’ai finalement une configuration qui me convient :

Il faut d’abord installer/configurer svn :

apt-get install subversion libapache2-svn
mkdir /var/svn
mkdir /var/svn/repo
svnadmin create /var/svn/repo

Modifier le fichier /var/svn/repo/conf/svnserve.conf

anon-access = none
auth-access = write
password-db = passwd
realm = SVN Repository

Modifier le fichier passwd

sudo addgroup svn --system
sudo adduser svn --system --home /var/svn --no-create-home --ingroup svn
sudo chown -R svn: /var/svn

Créer un script de démarrage dans /etc/init.d/svnserve :


set -e
if [ -x /usr/bin/svnserve ] ; then
echo "Svnserve not installed."
exit 0

. /lib/lsb/init-functions

case "$1" in
log_action_begin_msg "Starting SVN server"
/sbin/start-stop-daemon --start --chuid svn:svn --exec /usr/bin/svnserve -- -d -r /var/svn
log_action_end_msg $?
log_action_begin_msg "Stopping SVN server"
/sbin/start-stop-daemon --stop --exec /usr/bin/svnserve
log_action_end_msg $?
$0 stop
$0 start
echo "Usage: /etc/init.d/svnserve {start|stop|restart|force-reload}"
exit 1

exit 0

Configurer le démarrage

sudo update-rc.d svnserve defaults

Pour Apache : Ne pas oublier de changer les droits du dossier des dépots :

update-rc.d svnserve defaults
chown -R www-data /var/svn/

J’ai ajouté une configuration de site dans /etc/apache2/sites_available/svn :

<IfModule dav_svn_module>
SSLEngine On
SSLOptions +FakeBasicAuth +ExportCertData +StrictRequire
SSLCertificateFile /etc/pki_custom/certs/
SSLCertificateKeyFile /etc/pki_custom/private/

<Location /svn>
DAV svn
SVNParentPath /var/svn
SVNListParentPath On
AuthType Basic
AuthName "SVN Repository"
AuthUserFile /etc/apache2/dav_svn.passwd
Require valid-user

Puis on active la conf avec : a2ensite svn

On crée un certificat SSL pour Apache :

mkdir /etc/pki_custom
mkdir /etc/pki_custom/private
mkdir /etc/pki_custom/certs
mkdir /etc/pki_custom/csrs

openssl genrsa -out /etc/pki_custom/private/$domain.key 1024
openssl req -new -key /etc/pki_custom/private/$domain.key -out /etc/pki_custom/csrs/$domain.csr
openssl x509 -req -days 1825 -in /etc/pki_custom/csrs/$domain.csr -signkey /etc/pki_custom/private/$domain.key -out /etc/pki_custom/certs/$domain.crt

Pour créer un utilisateur subversion, il faut juste créer un utilisateur pour l’authentification Apache :

sudo htpasswd -s /etc/apache2/dav_svn.passwd utilisateur
sudo chown www-data:www-data /etc/apache2/dav_svn.passwd

Une fois ces étapes passées, le dépot est accessible via

Catégories :Uncategorized

jfastcgi 2.1 is available

6 septembre 2011 1 commentaire

jFastCGI 2.1 is available, it features some minor bug corrections. The library is now available through the plublic maven repositories.

Catégories :Uncategorized Étiquettes : ,

Forwarding X11 through ssh

sshThis is an information that you might easily find everywhere, this article is mainly a reminder for myself (and eventually for you 🙂 ).

No need for fancy protocols, in order to run graphical programs on a remote machine we might use ssh, and X11. This has prooved to work well since the eighties 🙂

NOTE : In my exemple, the client is my Ubuntu 11.04 laptop, and the server runs Debian Squeeze, so my explanation covers the ‘debian way’ of doing things.

On the client side :

X11 shall be accessible though tcp.

If you run X directly, which is not my case, Edit the /etc/X11/xinit/xserverrc file, and remove the « -nolisten tcp » option. This modification is optional if you, like me, use gdm.

gdm starts X sessions, so you you have to modify its configuration in /etc/gdm/custom.conf :





restarting gdm :
close you’re gnome session, and then crtl+alt+f1 opens a terminal in console mode.
type the following :

service gdm restart

and then ctrl+alt+f7 brings you back to the gdm prompt.

SSH config :
Then edit the /etc/ssh/ssh_conf :

X11Forwarding yes
X11DisplayOffset 10

On the server side,

edit /etc/ssh/sshd_conf :

Host *
ForwardX11 yes
ForwardX11Trusted yes

and restart the server (fortunately you won’t be kicked out if you do that through ssh), by doing :

$> /etc/init.d/ssh restart

Connections can now handle X11 forwarding

$> ssh -X user@server
$> echo $DISPLAY
$>xeyes &

Catégories :Uncategorized

Showing kernel modules dependencies graphically using graphviz

A few time ago, I had problems with my sound card module on my new linux laptop, I did not understand well how modules were loaded.

I finally solved my problem, but I was curious about the modules depency structure, and I started playing with graphviz to have a graphical view of that.

I finally came with this small & dirty python script :

# -*-coding:utf-8-*-
import os, re
#usual usage is : ./ | dot -Tpng > modules.png

print 'digraph linux_modules {'
lines = [line for line in os.popen('lsmod')]
for line in lines[1:] :
    line = [x for x in re.split('\ |,', line.replace('\n','')) if not len(x)==0]
    for dep in line[3:]:
        print '\t' + line[0] + ' -> ' + dep + ';'
print '}'

It reads the dependencies between the linux modules by calling lsmod(8), and then formats the output into a file acceptable by the dot utility, from the graphviz package.

The png output is quite cool, … And you can see how complex the sound handling is under linux compared to video !!

The other conclusion is to see how fast and easy it is to generate graphs using graphviz !

Catégories :Uncategorized