Version 8, last updated by dcbriccetti at May 13, 2011 13:51 UTC
Set Up Jetty and PostgreSQL
This is a step by step guide which shows you how I got my installation up and running. I used the following software/environments together with the Lift Framework Release 2.1.
Development Environment:
Kubuntu 9.10, Jetty 6.1 and PostgreSQL 8.3
Production Environment:
Ubuntu Server 8.04, Jetty 5.1 and PostgreSQL 8.3
The following configuration will be done on your production system (server).
1. Installing Jetty and PostgreSQL
First, you’ve to add the Multiverse Repositories to /etc/apt/sources.list, to get Jetty.
# apt-get update
# apt-get install jetty
# apt-get install postgresql-8.3
2. Firewall Configuration
By default, PostgreSQL 8.3 listens on port 5432 for incoming client connections. Jetty 5.1 listens by default on port 8280. So, you’ve to add two rules to your IPTables config which allow TCP connections to/from those ports. For PostgreSQL, a local rule is enought, because its not necessary to connect from the outside.
# iptables -A INPUT -s 127.0.0.1/32 -p tcp -m tcp --dport 5432 -j ACCEPT
# iptables -A INPUT -p tcp -m tcp --dport 8280 -j ACCEPT
3. Create PostgreSQL Database and User
Make sure that PostgreSQL is running. By default, a UNIX user ‘postgres’ is created during the installation. You can use this user to create/drop databases and/or create local database users. Before we start, make sure that PostgreSQL is running:
# /etc/init.d/postgresql-8.3 startBecome user postgres:
# su postgresCreate a new database:
# createdb liftdatabaseStart the PostgreSQL command prompt and create a new user:
# psql
postgres=# create user george password 'topsecret';
postgres=# \q
PostgreSQL will answer with “CREATE ROLE”
The next step needs to done on your development machine:
4. Configure Lift to use PostgreSQL
There’s another article about PostgreSQL here. In your Lift project you’ve to edit the file src/main/resources/props/default.props. Add the following entries:
db.driver=org.postgresql.Driver
db.url=jdbc:postgresql:liftdatabase
db.user=george
db.password=topsecret
In your Boot.scala, look for something like
val vendor = new StandardDBVendor(Props.get("db.driver") openOr "org.postgresql.Driver",
Props.get("db.url") openOr "jdbc:postgresql:mydatabase",
Props.get("db.user"), Props.get("db.password"))
When you start your application, Lift will try to read the values from the default.props like db.driver, db.url and so on. If the default.props file does not exist, Lift will use the default values provided by openOr.
Now, add the dependencies for PostgreSQL 8.3 to you pom.xml file
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>8.3-606.jdbc4</version>
</dependency>
5. Create WAR file and put it on the server
Go to the directory which contains your Lift project and do
# mvn clean install
# mvn package
This will compile a new and clean version of your application and create a .war file (with all dependencies) in the target sub-directory. Copy the .war file to your remote server e.g. you can do that with sftp alias SSH2.
Again, change to your production system.
6. WAR file and Jetty
Jetty has a directory where its looking for .war files on startup. On Ubuntu Server 8.04, this directory is /usr/share/jetty/webapps. Copy your .war there. Next do
# /etc/init.d/jetty stop
# /etc/init.d/jetty start
If you make any changes to this directory, Jetty will not notice it until you restart it!
Jetty will take some time to start, in my configuration 30 seconds or more were normal, so don’t get nervous to soon…
After it has started successfully you can visit your app at http://yourdomain:8280/your-app, where your-app is the name of the .war file (with .war being omitted).
In case it doesn’t start, you’ve to check the Jetty log files in /var/log/jetty/.
7. Custom CSS
In case you use your own CSS file with your custom design, you might wonder why its not loading when you use Jetty. I had this kind of problem, because the root of the application changed which lead to a wrong path to the CSS file. In my src/main/webapp/templates-hidden/default.html file I referenced the CSS file like that:
<head>
...
<link href="/static/design.css" rel="stylesheet" type="text/css" />
...
</head>
This means, that the design.css file was located in the static folder of the application. Now, to solve the problem, you’ve to add a LiftRule inside the boot() method of your Boot class. This might look like this:
LiftRules.fixCSS("static" :: "design" :: Nil, Empty)
Explanation: The List parameter for the method fixCSS defines the path to your CSS file, where “static” is a sub-folder under the root folder and “design” is a file with the extension .css inside that folder. This path has to match the path you defined in your default.html file! For more information have a look at the Ligt Book, page 229 ff.
done.