Configuring Tomcat, PostgreSQL and Grails on Ubuntu
First install tomcat and tomcat on your ubuntu machine if you have not already done so.
sudo apt-get install openjdk-7-jdk tomcat7 postgresql |
Setup the required PostgreSQL database on ubuntu. We will connect to it as a JNDI DataSource later on.
Usually I create a new superuser to do maintenance tasks.
CREATE USER <role_name> superuser login encrypted password '<password>'; |
-- Connect to the database from command line psql -d postgres CREATE USER userName WITH PASSWORD 'yourPassword'; CREATE DATABASE databaseName; GRANT ALL PRIVILEGES ON DATABASE databaseName TO userName; |
Get the postgreSQL JDBC driver.
cd /usr/share/tomcat7/lib sudo wget http://jdbc.postgresql.org/download/postgresql-9.1-901.jdbc3.jar |
Create a Tomcat Context configuration for your grails webapp.
sudo vi /etc/tomcat7/Catalina/localhost/yourappname.xml |
Fill it with something like the following, (adapted to your webapps name / context root of course)
<Context path="/yourContextPath" reloadable="false"> <Resource name="jdbc/yourName" auth="Container" type="javax.sql.DataSource" driverClassName="org.postgresql.Driver" url="jdbc:postgresql://127.0.0.1:5432/mydb" username="myuser" password="mypasswd" maxActive="20" maxIdle="10" maxWait="-1"/> </Context> |
We’re almost there. Update DataSource.groovy
production { dataSource { dbCreate = "update" jndiName = "java:comp/env/jdbc/yourName" } } |
Awesome! Restart tomcat (sudo /etc/init.d/tomcat7 restart). Build (grails prod war) and deploy (copy file to /var/lib/tomcat7/webapps) your grails webapp and go bananas!
Update:
I got an “Unsupported major.minor version 51.0” error. This was because by default java 6 was used instead of java 7 on my machine.
Caused by: java.lang.UnsupportedClassVersionError: org/codehaus/groovy/grails/scaffolding/GrailsTemplateGenerator : Unsupported major.minor version 51.0 (unable to load class org.codehaus.groovy.grails.scaffolding.GrailsTemplateGenerator) |
My default-java was set wrong in the /usr/lib/jvm directory. (ls -l /usr/lib/jvm) default-java was pointing to the java 6 version.
cd /usr/lib/jvm sudo rm default-java sudo ln -s java-1.7.0-openjdk-amd64 default-java |
Great post! I had the same error with java 6 and solved it thanks to this post