Deploying Apache-Tomcat Postgresql Web Applications on Ubuntu 9.04
Hmm this seems like easy task to do right? but believe me sometimes this simple job gives you real pain in ass while deploying some web applications.
I decided to write this blog post as a reference material for newbies. Following steps are tested and works perfect on Ubuntu 9.04
This post includes HowTo:
- Install Apache-Tomcat 6.x.x
- Install Postgresql
- Make them communicate with each other.
Lets first get started with the Apache-Tomcat Installation. Before we proceed lets make sure if we have JDK installed or not.
$dpkg –-get-selections | grep sun-java
Above command should yield output similar to following
sun-java6-bin install sun-java6-jdk install sun-java6-jre install
If output yields no result, you’ll need to install sun-java using following command
$ sudo apt-get install sun-java6-jdk
I ran into problems while using Tomcat versions from repositories, so here we will download the Tomcat from official Apache site.
wget http://mirror.jimbojay.com/apache/tomcat/tomcat-6/v6.0.18/bin/apache-tomcat-6.0.18.tar.gz
Extract the tarball and move it to some permanent location like /usr/local/
mv apache-tomcat-6.0.18 /usr/local/apache-tomcat-6.0.18
JAVA_HOME Variable is needed for tomcat to work properly so we need to export it.
export JAVA_HOME=/usr/lib/jvm/java-6-sun
This variable will get unset once you logoff and to set this variable permanently, you will need to edit ~/.bashrc file.
Open it in your favorite editor and put following line at the end of file.
export JAVA_HOME=/usr/lib/jvm/java-6-sun
save the file and you are done.
Once variable is set, we are now ready to start the tomcat server by executing /usr/local/apache-tomcat-6.0.18/bin/startup.sh script.
root@bughira:~# /usr/local/apache-tomcat-6.0.18/bin/startup.sh Using CATALINA_BASE: /usr/local/apache-tomcat-6.0.18 Using CATALINA_HOME: /usr/local/apache-tomcat-6.0.18 Using CATALINA_TMPDIR: /usr/local/apache-tomcat-6.0.18/temp Using JRE_HOME: /usr/lib/jvm/java-6-sun
Lets quickly check if we can see the apache process in memory.
root@bughira:~# ps -ef | grep apache root 3089 1 45 23:56 pts/0 00:00:01 /usr/lib/jvm/java-6-sun/bin/java -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=/usr/local/apache-tomcat-6.0.18/conf/logging.properties -Djava.endorsed.dirs=/usr/local/apache-tomcat-6.0.18/endorsed -classpath :/usr/local/apache-tomcat-6.0.18/bin/bootstrap.jar -Dcatalina.base=/usr/local/apache-tomcat-6.0.18 -Dcatalina.home=/usr/local/apache-tomcat-6.0.18 -Djava.io.tmpdir=/usr/local/apache-tomcat-6.0.18/temp org.apache.catalina.startup.Bootstrap start root 3100 3068 0 23:56 pts/0 00:00:00 grep apache root@bughira:~#
yes indeed and we can confirm that apache is running on default TCP port 8080
We can also write init control script for starting/stopping or restarting tomcat. Just copy and paste following shell script and save it as tomcat-ctl.sh
#!/bin/bash # # Tomcat-ctl to start/stop/restart apache-tomcat server. # export JAVA_HOME=/usr/lib/jvm/java-6-sun case $1 in start) sh /usr/local/tomcat/bin/startup.sh ;; stop) sh /usr/local/tomcat/bin/shutdown.sh ;; restart) sh /usr/local/tomcat/bin/shutdown.sh sh /usr/local/tomcat/bin/startup.sh ;; esac exit 0
Now lets install and configure postgresql server. Again we will be using pure postgresql server from its official website.
The latest version available at the time of writing this post was 8.3.7 so lets Download the tarball and untar it.
$wget http://wwwmaster.postgresql.org/download/mirrors-ftp/source/v8.3.7/postgresql-8.3.7.tar.gz $tar zxvf postgresql-8.3.7.tar $cd postgresql-8.3.7
Decide the permanent location for the postgresql and use it in –prefix parameter of configuration script as follows.
$./configure --prefix=/usr/local/pgsql
Compile postgresql
$make
and now install it to put necessary libraries and binaries in place.
$sudo make install
It is recommended to create a separate user to own the PostgreSQL files and processes that will be installed. Typically user ‘postgres’ is created for this purpose.
By default, POSTGRESQL allows database access only to users logged into the computer running the database server.
$sudo useradd postgres $sudo mkdir /usr/local/pgsql/data $sudo chown postgres /usr/local/pgsql/data
Lets initialize the postgres sever
$su - postgres -c "/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data"
Once initialization if successful; we can start the server using following command.
$su - postgres -c "/usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &"
Lest test our server by creating a test database and connecting to it.
$su - postgres -c "/usr/local/pgsql/bin/createdb testdb" $su - postgres -c "/usr/local/pgsql/bin/psql testdb" $su - postgres -c "/usr/local/pgsql/bin/psql testdb"
Above command if successful should yield following output
Welcome to psql 8.3.7, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit testdb=#
If you are able to log in successfully and get psql common prompt, we have successfully installed postgresql server.
Now its time to configure server to accept remote connections – unless you only want to access the database on the local machine. To do this, first, we need to edit the postgresql.conf file:
$ sudo vi /etc/postgresql/8.3/main/postgresql.conf
Now, to edit a couple of lines in the ‘Connections and Authentication’ section…
Change the line:
#listen_addresses = 'localhost'
to
listen_addresses = '*'
and
#password_encryption = on
to
password_encryption = on
Save the file and you are done.
We also must define who can access the server. This is all done using the pg_hba.conf file.
$ sudo vi /etc/postgresql/8.3/main/pg_hba.conf
Comment out, or delete the current contents of the file, then add this text to the bottom of the file:
# DO NOT DISABLE! # If you change this first entry you will need to make sure that the # database # super user can access the database using some other method. # Noninteractive # access to all databases is required during automatic maintenance # (autovacuum, daily cronjob, replication, and similar tasks). # # Database administrative login by UNIX sockets local all postgres ident sameuser # TYPE DATABASE USER CIDR-ADDRESS METHOD # "local" is for Unix domain socket connections only local all all md5 # IPv4 local connections: host all all 127.0.0.1/32 md5 # IPv6 local connections: host all all ::1/128 md5 # For remote PC connections host all all 0.0.0.0/0 md5
The last line says allow all computers to connect to all database with any username.
Above procedure and configuration will help normal users to connect and operate on our installed Postgresql server but how will tomcat connect it?
JDBC driver is needed for apache-tomcat to make connections with Postgresql server. Download the valid JDBC driver(jar file) from postgreql site and copy it under lib folder of tomcat.
In our case download and copy postgresql-8.3-604.jdbc4.jar under /usr/local/apache-tomcat-6.0.18/lib/ directory and restart the tomcat server using our script.
$ sudo cp postgresql-8.3-604.jdbc4.jar /usr/local/apache-tomcat-6.0.18/lib/ $ sudo tomcat-ctl restart
Now your tomcat-postgresql connectivity should be working. I hope you find this post useful.
About this entry
You’re currently reading “Deploying Apache-Tomcat Postgresql Web Applications on Ubuntu 9.04,” an entry on Bughira's Weblog
- Published:
- May 27, 2009 / 10:38 PM
- Category:
- General Talks, HOWTO's
1 Comment
Jump to comment form | comment rss [?] | trackback uri [?]