sails lift MyAwesomeWebApplication
Also, it just need me to press CTL+C keys in the terminal, to stop the running web application. This is a similar feature that exists in Play.
In the meantime, I was thinking about what if I deploy & run a sails web application on a cloud environment, let’s say, Amazon Web Service. So, i made the first attempt by deploying a simple sails web app on AWS through Elastic Beanstalk (EB). The result is my web app’s homepage did not show in my browser. Instead, it displays EB App’s default homepage.
Then, I took the other route. I create an EC2 Instance (It’s a virtual private server) using AMI (Amazon Machine Image) with pre-installed Ubuntu 14.04 OS. I installed required softwares in the created EC2 instance then lift my sails web app on it. Then, check it using my browser & it confirmed me that my sails web app was up & running.
In this article, I would like to share you the steps that I did to get my sails app running in a Ubuntu AWS EC2 instance. And, here they are:
Create a new AWS Instance using Ubuntu 64 AMI
- Browse to AWS console at https://console.aws.amazon.com & click EC2 link.

- On the EC2 Dashboard, click IMAGES->AMIs menu link.
- On the Filter menu bar, modify the options to Public images | 64-bit images | Ubuntu.
- On the returned filtered result, tick a desired AMI (e.g. ubuntu/images/ebs/ubuntu-trusty-14.04-amd64-server).

- Click [Launch] button.
- On the “Step 2: Choose an Instance Type” page, tick a desired EC2 Instance Type (e.g. Micro Instances) then click [Next: Configure Instance Details] button.

- On the “Step 3: Configure Instance Details” page, leave the default settings and click [Next: Add Storage] button.
- On the “Step 4: Add Storage” page, adjust the Root’s storage size or just leave the default settings then click [Next: Tag Instance] button.

- On the “Step 5: Tag Instance” page, leave the default settings and click [Next: Configure Security Group] button.
- On the “Step 6: Configure Security Group” page:
- Select “Create a new security group” option.
- Enter name & description for the new security group.
- Click [Add Rule] button & on the new entry, enter: Type = Custom TCP Rule, Protocol = TCP, Port Range = 1337, Source = Anywhere.
- Click [Review and Launch] button.

- On the “Step 7: Review Instance Launch”, click [Launch] button.
- On the displayed “Select an existing key pair of create a new key pair” dialog, select “Create a new key pair” on the combo field, enter Key pair name then click [Download Key Pair] & [Launch Instances] buttons.
- Save the downloaded .pem file into somewhere within your home directory & restrict its access by running this command in terminal:
chmod 400 yourdownloadedkeypair.pem
- Go back to browser, click [View Instance] button. Notice that the browser redirects to Instances dashboard page and the new AMI Instance is shown in the Instances list. Give the new Instance a name if you like ( by clicking the new instance’s empty Name cell and type the name on it). Make notes on the new Instance’s Public IP or Public DNS fields.

Connecting to the created AMI Instance using SSH
- On the Instances Dashboard page, click [Connect] button. A dialog would appear, showing 2 options for connecting to the Instance. Select “A standalone SSH client”, block and copy the command written under ‘Example’ section.

- Open the command line Terminal box, move to the directory that has the downloaded .pem file and run the command written in the earlier instructions dialog.
ssh -i downloaded_keypair.pem ubuntu@new_instance_public_ip_or_dns
- Confirm that you have logged in successfully.

- Set the root’s password by running these commands:
sudo su passwd
Setup required softwares on the created AMI Instance (as root)
- In the SSH terminal connected to the new Instance, run these following commands to update the new Instance’s software repositories:
apt-get upgrade && apt-get dist-upgrade && apt-get update && apt-get autoclean
- Run these commands to set up the git client:
apt-get install build-essential git
- Create a new directory & pull the latest Node.js source code in this directory by running this command ( using git client ):
git clone https://github.com/joyent/node.git
- Change directory to the cloned Node.js source directory, then run these commands to compile & install the Node.js:
sudo ./configure && make && make install
- Confirm that the compilation process is finished successfully.

- Install sail.js web MVC framework by running this command:
npm -g install sails
- Confirm that the installation is finished successfully

Deploy a sails.js app into the created Instance
- Ensure that you have put the sails.js app source code in your git account (e.g. github, bitbucket, etc ).
- On the created Instance, create a new directory and do git clone the sails.js app source code into this directory.

- Change directory to the cloned source code’s directory and run this command to install node module dependencies referenced by your sails.js app
npm install
- Run this command to lift the sails.js app online on the created instance:
sails lift
- Confirm that the sails.js app is lifted successfully

- Go back to your internet browser and browse to your instance’s public IP address , port 1337.

- Confirm that the lifted sails.js app’s home page is displayed

It’s alive now ! But, wait..
When I close my current SSH session that was connected to the EC2 instance & refreshed the sails app’s page in my browser, I noticed that it returns 404 error. My sails web app was offline. Apparently, each processes that have been started during SSH session within the EC2 instance, would be shutdown when the SSH connection is closed. Somehow, I need to prevent the running sails app from being closed even when the SSH session is ended.
Fortunately, the solution for this is already suggested in the documentation of sails.js. The document suggests us to install & start a sails.js app by using forever. Forever prevents any running scripts from being closed during SSH session by running them as a Daemon (*nix service). Then, I tried the solution and it worked well. I would explain the steps of how to forever my sails app in EC2, in the next section,
Run the deployed app as a running daemon in the EC 2 Instance
- Install forever globally:
npm -g install forever
- In the terminal connected to the EC2 Instance, change directory to the sails.js app’s root folder then run this command:
forever start -ae errors.log app.js --dev --port 1337
OR, run this command if you wish to run the production version:
forever start -ae errors.log app.js --prod --port 80
- If you write your controllers as coffee script files, open the errors.log file. Notice that there is error message written in it (http://tinyurl.com/p8y4ovl). This means the sails.js app is failing to be lifted by running prior command. This is a known issue in Sails.js version 0.9.16. This issue has been raised to balderdashy and it can be seen in this link, along with the temporary workaround as well : http://tinyurl.com/m2jyx24).
- Logout or disconnect from the EC2 instance’s SSH session and then browse to your lifted sails.js app’s url. Confirm that your lifted app is still up & running now.






