Node Js server in EC2 instane
Deploying a sample node server in EC2 instances.
First, when i started exploring AWS, I thought it could be difficult to setup a server in AWS EC2. But once i started doing it, I felt it very easy.
Let’s assume EC2 machine is started and running. If not kindly follow this article for setting up EC2 instance.
Now let’s create a Git repository for the server code.
I have created a sample Node server with Express. You can find the repo here.
Now login to AWS console using ssh(Explained in this article ).
We need few applications to be installed. Let’s complete installation first.
- Node- to start and run the server.
- Git- clone the repo and deploy in aws
- pm2- Process manager to manage the running the servers
- Nginx- To act as a load balancer.
Install Node
First we need NVM(Node Virtual Machine) to install node.js as it provides a neat way to manage multiple node.js versions on the instance if required. Run the following code in the terminal to get NVM and install it.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc). Now, run nvm --version
. The terminal will show command not found as the code has been added to your profile but hasn’t run yet. It runs on every login, so login again by creating a new session. Run nvm --version
again and this time you should be able to see the installed version of NVM.
To install node, you can either use nvm install node
to install the latest version of node or use nvm install <node_version>
to install a specific version of node. Once node is installed, you can check the installed version with node --version
.
Install Git and clone repository
To install Git use the following command.
sudo yum install git
To clone the above mentioned server use the command
git clone https://github.com/suganthskce/nodeServer-Express.git
You can also clone your our server code. Once cloned, go to the repo and start server.
cd nodeServer-Express/
npm i
node server.js
Now the server is running in port 8080. But once you terminate the connection to AWS, the terminal in closed and the process is terminated.
To run the server even after we close connection we need pm2.
Install pm2
npm install pm2 -g
Once pm2 is installed run the following command to start pm2 whenever the server is started.
pm2 startup
To start the server using pm2 run
pm2 start server.js
To know more about pm2, check the following website.
Install Nginx
sudo yum install nginx
Nginx by default runs on port 80. Hence we need to open the port from Security Groups.
Check which Security Group is assigned for our machine. Edit that group for inbound ports. Add rule with HTTP as type and port to 80.
Start Nginx using
sudo service nginx start
Now open the Public IP of our EC2 machine in browser. You must see the landing page of Nginx.
Now redirect the request to port 8080(server running port). You must edit nginx.conf file.
sudo nano /etc/nginx/nginx.conf
You can find the following lines in the file
location /{}
Replace it with
location / {
proxy_pass http://127.0.0.1:8080;
}
Save and close the file. Now restart the nginx.
sudo service nginx restart
Finished.
Now go to your machine IP in browser.
Feel free to contact me for any queries.
Email: suganth2610@gamil.com