Running Liferay 6.2 on Raspberry Pi 2

Liferay Pi2After developing Social Apps Proxy for the Java based Liferay platform just over a year ago, I have been looking for an inexpensive way to host a demo environment. I found the Raspberry Pi2 at £25 was ideal for this, so I will share my experience in configuring this. The general opinion of the web seems to be that the Pi2 and its 1GB RAM in particular is simply too under resourced to run Liferay, but these posts were often made before the release of Java 8 for ARM processors which makes a huge difference, especially when combined with ZRAM which I will cover later. I have Apache2, Liferay Portal 6.2 and MySQL 5.6 all running smoothly on a single Pi2 with about 200-300MB RAM free!

You are probably wondering what kind of performance to expect…

Liferay starts up in just under 10 minutes, and after a page has been requested at least once, it is served in about 1 second. That’s without any Apache / CDN type optimisation. Not bad I think!

I will explain all necessary steps to get Liferay running, but I will cover the Apache2 installation & configuration in a future post focussed on preparing a Liferay Pi2 stack for Internet facing applications.

All Linux commands are explained so no need to have prior Linux knowledge.

Start by prepare your Pi2 SD Card with the latest Raspbian distribution. Raspbian Jesse at the time of writing. This tutorial will not cover how to get your Pi to a basic running state, this is already very well documented on the Raspberry Pi website .

All steps of this tutorial will be done over a SSH connection. So no need to have a keyboard/mouse/screen plugged into your Pi2, though a screen might be useful to ensure it has booted up OK if you are having trouble establishing a SSH connection.

Prepare/optimise your Raspberry Pi to run the Liferay stack

Go ahead and connect to your Pi2 across your home network using the command:

ssh pi@raspberrypi

By default the Raspbian GUI will have been loaded. This takes unnecessary resources since the Pi will be running headless, so enter the configuration wizard:

sudo raspi-config

In “Boot options” select one of the “console” options

In “Advanced” set memory split giving the GPU the minimum allocation possible (16MB).

Verify Java 8 is installed

It is worth verifying that Java 8 is installed. Since Feb 2015 it has come as standard with Raspbian Jesse. Just execute:

java -version

You should see something similar to the following:

java version "1.8.0"
Java(TM) SE Runtime Environment (build 1.8.0-b132)
Java HotSpot(TM) Client VM (build 25.0-b70, mixed mode)

If you have an different version, then you can install it by executing this command:

sudo apt-get install oracle-java8-jdk

Expand the Pi2’s memory using ZRAM for swap

As mentioned earlier, the main problem with running Liferay Portal 6.2 on Pi2 is the limited amount of RAM. Liferay alone will not run well without at least 1GB so we’re going to need to expand this by using the Linux ZRAM feature. This enables you to allocate a percentage of the 1GB RAM as swap memory. When this swap memory is used, ZRAM will read/write back to the RAM, but decompress/compress in the process. At the cost of some minor additional CPU load, you potentially double the available memory!

Before we do this, ensure that there is no SD card swap enabled, because the read/write performance of SD cards will bring your server to a grinding halt. Execute:

cat /proc/swaps

There should be no swaps listed. Some distributions of Raspbian adds a default 100MB swap file. If you find a swap, you can prevent this being configured upon the next reboot using this command:

sudo update-rc.d -f dphys-swapfile remove

Let’s configure ZRAM. There are many scripts available for this, but I found the following one by Antonio Galea worked best. Before installing it as a service, you can try it out by executing:

cd /etc/init.d
wget https://raw.githubusercontent.com/stian-sigvartsen/liferay-pi/master/etc/init.d/zram -O zram
sudo chmod +x zram
sudo ./zram start
cat /proc/swaps

You should ee 4 swap devices, one for each of the 4 CPU cores of the Pi2. The script has configured 80% of RAM to be used for ZRAM, I found this to be a sensible amount for running the Liferay stack. Go ahead and make the script run automatically at boot time will the following command:

sudo update-rc.d zram defaults

Configure Swappiness more appropriately

Before installing the Liferay stack you need to set the Swappiness of Raspbian to a more sensible value. Its value can range from 0 to 100 and by default with Raspbian it is set to 1.The higher the value the more aggressively Raspbian will favour swap memory over RAM memory. It makes sense that it is set very low given that the default swap device for Raspberry Pi is the SD card. However, with the new high performance ZRAM based swap, you want to encourage its use. In fact if you leave it set to 1, your Pi2 will become unresponsive from time to time whilst running Liferay and a process called kswapd will be eating a lot of CPU cycles.

I found a sensible swappiness value is 60, but you can experiment. Execute:

sudo nano /etc/sysctl.conf

This will launch the nano editor. Append the following to the end of the file on a new line.

vm.swappiness = 60

Save the changes by pressing ctrl+o and then <enter> . Finally press ctrl+x to exit the editor.

Verify readiness for Liferay stack

Let’s reboot and check that everything is as expected.

sudo reboot

When your Pi has finished rebooting, re-establish your SSH connection.

Check the general state of memory utilisation on the Pi2 by executing:

free -m

You should see something resembling the following, meaning there is now approximately 1.6GB available memory.

             total       used       free     shared    buffers     cached
Mem:           973        206        767          6         13         90
-/+ buffers/cache:        101        871
Swap:          778          0        778

If your stats are drastically different then either I have omitted something vital or later versions of software has affected this in some way. Either way I would appreciate if you leave a comment below!

Also check that Swappiness is set to 60:

cat /proc/sys/vm/swappiness

Install the Liferay stack:

Install MySQL 5.6

I will not go into great depth on how to install this because the topic is already well covered in other blogs. Simple executing the following command and entering a password for the MySQL root user when prompted will be sufficient.

sudo apt-get install mysql-server

I found that the default configuration of MySQL 5.6 works well with the Raspbian optimisation we made earlier.

Next we will create an empty database and an additional user for Liferay. For security reasons it is best not to use the MySQL root user to run Liferay (or any other platform). In order to do that you need to install the MySQL CLI (Command Line Interface).

sudo apt-get install mysql-client

Now connect to your MySQL server:

mysql -uroot -hlocalhost -p

You will be prompted for the MySQL root user’s password which you set earlier. When you get a command prompt, enter the following command to create a database called “lportal62”:

create database lportal62 character set utf8;

Next create a user called lportal62 which can only be accessed from the localhost. Just substitute “any_password” with a password of your choice:

CREATE USER 'lportal62'@'localhost' IDENTIFIED BY 'any_password';

Next, grant full privileges for the lportal62 user to the lportal62 database.

GRANT ALL PRIVILEGES ON lportal62.* TO 'lportal62'@'localhost';

In order for the granted privileges to take effect, execute the following command:

FLUSH PRIVILEGES;

Now disconnect from MySQL server with the “quit” command.

Install Liferay Portal 6.2

For security reasons you should run Liferay with a Raspbian system user with less permissions than the standard pi user. We’ll create a new system user called “liferay” which has a home directory (despite being a system user), and a corresponding “liferay” group:

sudo adduser --system --shell /bin/bash --gecos 'Liferay' --group --disabled-password --home /home/liferay liferay

Now switch to this system user:

sudo su - liferay

Whilst operating as the “liferay” user we will download the liferay binaries and create a symlink structure which will make it easier to upgrade liferay in the future.

mkdir liferay
cd liferay
wget http://downloads.sourceforge.net/project/lportal/Liferay%20Portal/6.2.5%20GA6/liferay-portal-tomcat-6.2-ce-ga6-20160112152609836.zip
unzip liferay-portal-tomcat-6.2-ce-ga6-20160112152609836.zip
ln -s liferay-portal-6.2-ce-ga6 liferay62
cd liferay62
ln -s tomcat-7.0.62 tomcat

Next we’ll configure Liferay’s Tomcat with a fixed size heap of 1024 MB which will prevent gradual heap expansion, because we know Liferay will need most of this allocation in order to start up:

nano ~/liferay/liferay62/tomcat/bin/setenv.sh

By default only -Xmx will have been set via CATALINA_OPTS, so we will add the missing -Xms switch. Using the nano editor, replace the following…

-Xmx1024m

with…

 -Xms1024m -Xmx1024m

You could remove the -XX:MaxPermSize switch because Java 8 no longer needs this, but leaving it there doesn’t cause any harm.

Save the changes by pressing ctrl+o and then <enter> . Finally press ctrl+x to exit the editor.

Next ensure that all the bin directory scripts are executable:

chmod 750 ~/liferay/liferay62/tomcat/bin/*.sh

Now it is time to start Liferay which will enable you to configure its database connection!

~/liferay/liferay62/tomcat/bin/startup.sh

It will take approximately 10 minutes to start Liferay. You can monitor its progress with the following command:

tail -f ~/liferay/liferay62/tomcat/logs/catalina.out

When the server is ready, use a browser to and request http://raspberrypi:8080 to access the setup wizard.

Go ahead and customise your portal as you want. You probably want to uncheck “Add sample data” if this is not your first time using Liferay.

Most importantly, make sure you change to a MySQL database setup as follows:

JDBC URL:
jdbc:mysql://localhost/lportal62?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false

Username:
lportal62

Password:
[your MySQL lportal62 user's password]

Run Liferay as a service

Now that your portal is running nicely you may want to have it start up automatically every time your Pi2 is rebooted. Back at the your SSH prompt type “exit” to resume control of the standard pi user, then execute the following:

cd /etc/init.d
wget https://raw.githubusercontent.com/stian-sigvartsen/liferay-pi/master/etc/init.d/liferay62 -O liferay62
sudo chmod +x liferay62
sudo update-rc.d liferay62 defaults

That’s it, you’re done! In my next post I will cover how to install & configure Apache 2 as a fronting proxy and obtain a DNS name for your Pi2 server.

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s