15 Oct 2013

Installing Tomcat & MySQL on Ubuntu 13.04

This post walks through the steps for setting up an Ubuntu to host a Grails application on Tomcat 7 and MySQL.

Install Open Java

sudo apt-get install openjdk-6-jdk 
sudo update-java-alternatives -s java-6-openjdk
export JAVA_HOME=/usr/lib/jvm/java-6-openjdk-i386
echo "export JAVA_HOME=$JAVA_HOME" | sudo tee -a /etc/environment

Tomcat Setup

There's a good Ubuntu tutorial on how to install Tomcat. To install and start Tomcat, SSH into the server and enter the commands:

sudo apt-get install tomcat7 tomcat7-docs tomcat7-admin
sudo chgrp -R tomcat7 /etc/tomcat7
sudo chmod -R g+w /etc/tomcat7 
sudo service tomcat7 start

You should now be able to browse to your Tomcat server at http://<hostname>:8080. You're public DNS address can be found under the Instances tab.

To allow uploading of our Grails WAR file to the Tomcat server we'll enable access to the Tomcat Manager app. Open the tomcat-users.xml in vi.

sudo vi /etc/tomcat7/tomcat-users.xml

I'm going to use the Grails Tomcat Plugin so I'll create the following entries in the tomcat-users.xml file:

<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="manager-script"/>
<user username="manager" password="secret" roles="manager,manager-gui,manager-status,manager-script"/>

Set CATALINA_OPTS to increase the memory allocation by creating the file /usr/share/tomcat7/bin/setenv.sh:

#!/bin/bash
CATALINA_OPTS="-Xms512M -Xmx1024M"

Restart Tomcat to enable the new user and setup Tomcat :

sudo service tomcat7 restart

After installation, the Tomcat log files can be found in /var/log/tomcat7.

Now browse again to your Tomcat server at http://<hostname>:8080/manager. and select the Server Status button on the right. Test the script interface by broswing to http://<hostname>:8080/manager/text/serverinfo.

MySQL Setup

As with Tomcat, Ubuntu provides an excellent guide to installing MySQL. To install and start MySQL, SSH into the server and enter the commands:

sudo apt-get install mysql-server
sudo service mysql restart

phpMyAdmin

I'm going to install phpMyAdmin to manage MySQL but you could also install the MySQL Workbench.

sudo apt-get install phpmyadmin

In a browser, go to http://localhost/phpmyadmin

Install SSH

sudo apt-get install openssh-server

Grails Tomcat Plugin & Instance Java Memory Settings

I've installed the Grails Tomcat Plugin per the instructions and am using these settings in my Config.groovy:

tomcat.deploy.username="manager"
tomcat.deploy.password="password"
tomcat.deploy.url="http://hostname:8080/manager/text"
grails.tomcat.jvmArgs = ["-server", "-XX:MaxPermSize=512m", "-XX:MaxNewSize=256m", "-XX:NewSize=256m",
 "-Xms768m", "-Xmx1024m", "-XX:SurvivorRatio=128", "-XX:MaxTenuringThsreshold=0",
"-XX:+UseTLAB", "-XX:+UseConcMarkSweepGC", "-XX:+CMSClassUnloadingEnabled",
"-XX:+CMSIncrementalMode", "-XX:-UseGCOverheadLimit", "-XX:+ExplicitGCInvokesConcurrent"]

Grails DataSource.groovy

I have the following settings in the production section my DataSource.groovy:

username = "root"
password = "password"
driverClassName = "com.mysql.jdbc.Driver"
dbCreate = "create-drop"
url = "jdbc:mysql://localhost/MyDB"

Build & Upload Your Grails App

With the Tomcat plugin installed, the following command will build and deploy the war file to your Tomcat server:

grails prod tomcat deploy

Tags:
0 comments