Deploy meaning on Amazon EC2

meaning is a mini blogging platform inspired by MEAN.JS(MongoDB, Express, AngularJS, and Node.js), and I will take notes here about how I deploy it on Amazon EC2.

Amazon EC2

First of all, you should pay for an Amazon Machine Image to launch an instance.
I choose Ubuntu and what is more, I buy an extra Reserved Instance to receive a discount on my instance usage compared to running On-Demand instances.
You can know more about Reserved Instance here.

Release

Now, we should release project to get the released code which will be deployed to production.

1
$ grunt build -release

And the dist folder contains all the released files.

FTP

For Windows, FileZilla is a nice choice for FTP solution.
And for Mac, I like Transmit.

First, copy your Amazon EC2 .pem file to your local ssh directory ~/.ssh.
Next, open ~/.ssh/config and add extra line to let your app know the PEM, for example:

1
IdentityFile "~/.ssh/just4fun.pem"

At last, save the config changes and alter the permissions of the PEM file to 700:

1
$ chmod 700 ~/.ssh/just4fun.pem

Try command below to test whether the PEM file works:

1
$ ssh -i ~/.ssh/just4fun.pem root@your_amazon_server

When we connect our server successfully, we should create a virtual host which will contains the released files:

1
$ mkdir /var/www/just4fun

Then copy directories and files to the virtual host which we created just now:

1
2
3
/dist/client
/dist/server
/package.json

Tap commnd below to install the node dependencies:

1
npm install -production

At last we get directory structure like below:

1
2
3
4
5
/var/www/just4fun
--client
--server
--package.json
--node_modules

DB

We use MongoDB in meaning and there is a post about how to install MongoDB on Ubuntu(14.04) for reference. Or you can visit official site to find the installation.

Web Server

I previously use Apache, and I changed it to nginx now.
In order to install Nginx we execute:

1
$ apt-get install nginx

Then create a virtual host configuration file in /etc/nginx/sites-available, such as /etc/nginx/sites-available/just4fun.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/just4fun/client;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name talent-is.me;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
rewrite ^/admin/?$ /admin/admin-index.html break;
rewrite ^/login/?$ /admin/admin-login.html break;
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
}

Next, we should disable the default vhost:

1
rm /etc/nginx/sites-enabled/default

And enable our virtual host:

1
ln -s /etc/nginx/sites-available/just4fun /etc/nginx/sites-enabled/just4fun

At last, restart nginx with new configuration:

1
service nginx restart

That’s all. :)