Here are the steps I took to deploy a Grails app on AWS EC2 with Grails 2.2.3. This is a follow-up to my previous post but using EC2 instead of Elastic Beanstalk. I'm switching from Elastic Beanstalk to EC2 because EB doesn't allow you to stop an instance and as a result you also cannot change the instance type.
I'd like to be able to stop instances because sometimes I'm working on a test project and I don't want to pay for an instance to run all the time.
Create a Key Pair for SSH Access
Before launching into creating our EC2 instance, it will save several steps to create a Key Pair that we'll use for SSH access.
- Go to the EC2 Console
- Look in the upper right hand corner of the window and make sure you are in the desired region
- Click on Key Pair
- Click on the Create Key Pair button - Enter a Key Pair Name to be used for the SSH access We'll use the Key Pair Name in a few minutes when we create the new instance.
By default, your new instance will not be accessible. Let's change the default Security Group to make selected services available from the outside.
- From the EC2 Console select Security Groups
- Select the security group named default from the list of security groups
- In the bottom pane choose the Inbound tab
- For the Create a new rule choose the service you want to add and click the Add Rule button. For this post, enable: SSH, HTTP, MYSQL, and port 8080.
- Under the Outbound tab I also enable SMTP and SMTPS but it's not necessary for this post.
Let's create the AWS EC2 Instance to host our Grails application:
- Go to the EC2 console
- Look in the upper right hand corner of the window and make sure you are in the desired region
- Click the Launch Instance button
- On the next screen choose the Quick Launch Wizard
- Select the Key Pair you created in the previous step and the Amazon Linux AMI and then click Continue
- On the next screen click Edit Details so that we can change some of the default options.
- Click the Termination Projection checkbox to turn it on and enter an Instance Name
- Click the Security Settings section and choose default for the security group
- Click Save Details
- Click Launch to start the new instance
Before you can connect to your server with ssh, you'll also need to know the public DNS name for your EC2 instance.
- Go to the EC2 console
- Click on the name associated with the app you just created
- Scroll down to the bottom of the page and you'll find the Public DNS field. It will be something like ec2-54-212-82-123.us-west-2.compute.amazonaws.com.
Now that you know the name of your server, you can connect to it with ssh using that name and the key pair you downloaded earlier. I'm using the Linux ssh command but you could also use putty. Here's the Linux command to login:
ssh ec2-user@ec2-54-212-82-123.us-west-2.compute.amazonaws.com -i ~/myssh.pem
To install and start Tomcat, SSH into the server and enter the commands:
sudo yum install tomcat7 tomcat7-webapps mcat7-docs-webapp tomcat7-admin-webapps sudo service tomcat7 start
You should now be able to browse to your Tomcat server at http://<public DNS>: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"/> <user username="manager" password="secret" roles="manager,manager-gui,manager-status"/>
Restart Tomcat to enable the new user and setup Tomcat :
sudo service tomcat7 restart sudo chkconfig tomcat7 on
Now browse again to your Tomcat server at http://<public DNS>:8080. and select the Server Status button on the right.
MySQL Setup
To install and start MySQL, SSH into the server and enter the commands:
sudo yum install mysql-server sudo service mysqld start sudo chkconfig mysqld on mysqladmin -u root password 'yourpass'
You should now be able to browse to your Tomcat server at http://<public DNS>:8080. You're public DNS address can be found under the Instances tab.
I'm going to install phpMyAdmin to manage MySQL but you could also install the MySQL Workbench.
sudo yum install httpd php sudo service httpd start sudo wget http://sourceforge.net/projects/phpmyadmin/files/phpMyAdmin/4.0.4.2/phpMyAdmin-4.0.4.2-all-languages.tar.gz sudo tar xzf phpMyAdmin-4.0.4.2-all-languages.tar.gz mv phpMyAdmin-4.0.4.2-all-languages phpMyAdmin rm phpMyAdmin-4.0.4.2-all-languages.tar.gz
Edit the /etc/httpd/conf/httpd.conf file and add index.php to the line:
DirectoryIndex index.php index.html index.htm
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://ec2-54-213-105-149.us-west-2.compute.amazonaws.com: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"]
Bobby Warner made thre
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
SSH
Before you can connect to your server with ssh, you'll also need to know the public DNS name for your EC2 instance.
- Go to the EC2 console
- Click on the name associated with the app you just created
- Scroll down to the bottom of the page and you'll find the Public DNS field. It will be something like ec2-54-212-82-123.us-west-2.compute.amazonaws.com.
Now that you know the name of your server, you can connect to it with ssh using that name and the key pair you downloaded earlier. I'm using the Linux ssh command but you could also use putty. Here's the Linux command to login:
ssh ec2-user@ec2-54-212-82-123.us-west-2.compute.amazonaws.com -i ~/myssh.pem