Select Page

Exchange Application Deployment on AWS Ubuntu 16.04

Introduction:

Node.js is a JavaScript runtime environment that allows one to run JS on the server. It is built on the open-source V8 JavaScript engine used in Chrome and written in C++ which executes JS in a standalone environment.

AWS (Amazon Web Services), a subsidiary of Amazon, is a cloud computing platform which provides resources for computation such as EC2 instances, various database and storage facilities as well as networking and content delivery options.

Apache is a free and open-source HTTP server which doubles up as a reverse proxy server or as a mail proxy server. It is a high performance server with simple configuration.

EC2 (Elastic Compute Cloud) is a web service which provides computing power in the form of scalable server instances. Multiple EC2 instances are run on the same machine inside Amazon’s computing infrastructure. When an EC2 instance is initiated, a set amount of resources (OS, RAM, Disk Space etc., chosen at the time of initiation) are allotted to the user from the machine it runs on. This allows sharing of resources between multiple users.

Ubuntu AMI Lean, fast and powerful, Ubuntu Server delivers services reliably, predictably and economically. It is the perfect base on which to build your instances. Ubuntu is free and will always be, and you have the option to get support and Landscape from Canonical.

RDS Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups.

Prerequisites

1. Create an AWS account
2. Buy and register a domain name
3. Obtain an SSL certificate for the registered domain
4. Buy SMTP to send notification/transactional email from application.

Things to do

1.Launch an EC2 instance
2.Launch RDS instance
3.SSH into your instance
4.Install npm and node.js
5.Install Git and clone repository from gitlab
6.Start the node.js app
7.Configure Apache Vhost to serve application on port 80/443

Launch an EC2 instance

●Now Login on AWS Console.
●In the top-right corner of the AWS Management Console, choose the AWS Region in which you want to create the DB instance. This example uses the US East (N. Virginia) region.
●Select EC2 from the Services dropdown (top-left), click on Launch Instance.
●Search for Ubuntu 16.04 AMI, & Select Ubuntu 16.04 AMI.
●On the next screen, select instance type c4.xlarge, select public subnet.
●Add Storage 50 GiB, Select Security Group, Create and download key pair.
●Allow port 22 for your public IP address and 80/443 for public in your security group.
●More instruction to launch an EC2 here.

Launch RDS instance
●Now Login on AWS Console.
●In the top-right corner of the AWS Management Console, choose the AWS Region in which you want to create the DB instance. This example uses the US East (N. Virginia) region.
●Select RDS from the Services dropdown (top-left), click on Create Database.
●You now have options to select your engine. For this project, click the MySQL icon and then click Select.
●The next screen gives you options to choose your environment. Select the MySQL option under Production – MySQL and click Next Step.
●You will now configure your DB instance. Select appropriate options that suits your infrastructure. Eg. DB Engine Version, Multi-AZ Deployment, Storage Type, Username, Password, Networking, Security group.
●Click Launch DB Instance, View the instance and access it using endpoint from your ec2 instance.
●More instruction to launch RDS here

SSH into your Instance and required softwares

●Connect to the EC2 instance using public IP Address and pem key which you launched earlier.
●Update/Upgrade OS using sudo apt-get update && sudo apt-get upgrade -y
●Install Development Tools using sudo apt-get install htop git curl wget zip build-essential unzip -y
●Install npm and some of it’s modules, as follows-
sudo apt-get install npm -y
sudo npm install -g n
sudo n 8.11.0 (you can chose other version if your application need)
sudo npm install -g pm2
●Install apache2 and run it on startup using following command.
sudo apt-get update
sudo apt-get install apache2
systemctl start apache2
systemctl enable apache2

Clone source code from Git Repository
Run the following command to clone your repository. Replace with your Gitlab username, with the name of your node.js app repository and with the name of the folder you want to clone the repository into. This folder will be created under /home/ubuntu/.

git clone https://gitlab.com//.git

Start the node.js app

●Run cd to move inside this folder and ls to see its contents,
●create/modify config/config.js and config/config.json file, and add require credentials, Eg. server port, globalDomain, DB Credentials, Currencies keys, SMTP Credentials.
Execute following commands to Install dependent node modules from package.json

npm install

Now, Start node app using pm2, Use below command.

pm2 start server.js –name $(basename $PWD)

Now even if you close the terminal and check the url in the browser, the app will be running. Sweet! For automatically running PM2 when the server restarts, issue the following command:

pm2 startup

This will output a script in the terminal which you need to run, copy the script that it outputs and run it in the terminal window. Now, save all the currently running processes so that they can be run again whenever PM2 restarts either manually or by a script with the following command:

pm2 save

Now you can try rebooting the instance and visit the url and see if the app works or not. To see a list of the currently active node processes with their process names, run pm2 list and to monitor any one of the processes run pm2 monit.

Configure Apache Vhost to serve application on port 80/443

●Enable dependent module for reverse proxy on apache2 using below following command

sudo a2enmod proxy proxy_http proxy_ajp rewrite macro deflate headers expires proxy_balancer proxy_connect proxy_html proxy_wstunnel ssl

sudo systemctl restart apache2

●Make sure you have allowed 80/443 port from EC2 security group for 0.0.0.0/0
●Change Directory inside /etc/apache2/sites-enabled/ and create a vhost.conf file with following content(in next page), Please modify ServerName, ServerAlias, Redirect, Logs, App Port, SSL Certificate path.  

# cat /etc/apache2/sites-enable/vhost.conf

TraceEnable Off
ServerTokens Prod
ServerSignature Off
HostnameLookups Off

ServerName example.com
ServerAlias example.com www.example.com
DocumentRoot /home/ubuntu/example.com
ServerAdmin root@localhost
ErrorLog ${APACHE_LOG_DIR}/example.error.log
CustomLog ${APACHE_LOG_DIR}/example.access.log combined

Allow from all

Redirect 301 / https://www.example.com/


ServerName example.com
ServerAlias example.com www.example.com
DocumentRoot /home/ubuntu/example.com
ErrorLog ${APACHE_LOG_DIR}/example.ssl_error_log
TransferLog ${APACHE_LOG_DIR}/example.ssl_access_log
LogLevel crit
SSLEngine on
SSLProtocol -all +TLSv1.2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /etc/ssl/example.com/certficate_file
SSLCertificateKeyFile /etc/ssl/example.com/private_key
SSLCertificateChainFile /etc/ssl/example.com/ca_bundle
SetEnvIf User-Agent “.*MSIE.*” nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
CustomLog ${APACHE_LOG_DIR}/example.ssl_request_log “%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \”%r\” %b”
KeepAlive On

Allow from all

RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://127.0.0.1:3210/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://127.0.0.1:3210/$1 [P,L]
ProxyPassReverse / http://127.0.0.1:3210/
Timeout 600
ProxyTimeout 600

●Restart apache2 to take this configuration in effects using below command.

sudo systemctl restart apache2 

Testing

Don’t forget to create “A” record for the domain pointing to server IP.

Now, Time to test whole things.

Go to your web browser and hit the domain which you have configured in virtual host, example.con, it will redirect to https://www.example.com
Now your application must be accessible, Try to login into and check the functionality of application.