Deploying a Node application on an EC2 instance may seem like a daunting task but if you know how to configure your instance it is not that hard.
We will be deploying a chat application written in Nodejs in the following steps
So let’s get started.
You can refer to my article Launching an Amazon EC2 instance to launch an EC2 instance and SSH into it.
When configuring the security group make sure you open the port on which your Node application will run. In my case, the port is 5000.
After doing SSH into your instance we will install Node on the instance.
To install node run the following commands in your terminal or git bash
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 8.10
The first command installs node version manager (nvm) that we are using to install node on Amazon Linux AMI.
The second command activates nvm.
The third command specifies which version of the node we would like to install. Here, I am installing the node version 8.10.
You can check your node version by typing the following command in your terminal.
node -v
installing node 8.10
Most commonly there are two ways to do that
This is one of the most simple ways to get your code on your EC2 instance. But first, you have install git on your instance. Type the following command to install git on Amazon Linux AMI.
sudo yum install -y git
Now, you can run the git clone
command to clone the project.
Here, I am cloning a node chat application.
git clone https://github.com/abkunal/Chat-App-using-Socket.io.git
We can make a zip file of our node project (without including node_modules folder) and upload that zip file to our S3 bucket. We can then make our zip file publicly accessible, download it on our instance and unzip it.
**For MacOS and Linux users**Right click on your node folder and select “compress” option. It will create a zip file
compress a folder in MacOS
**For Windows users**Right click on your node folder and select “Compressed (zipped) folder” option in “Send to”.
Now let’s upload our zip file to an S3 bucket.
Create a s3 bucket
1. Login to your AWS console and select S3. Click on the Create bucket button, enter a unique name for your bucket and click on the Next button.
2. Click Next button again and uncheck all the checkboxes in Set Permissions tab.
modify s3 permissions settings
3. Click Next button and then click Create bucket button.
upload zip file to s3 bucket
4. Select your newly created bucket and upload the project’s zip file to the bucket.
make the zip file publicly accessible
5. Click on the zip file and then click on the Make Public button.
6. Copy the link given at the bottom and paste the following command in your terminal or git bash where you have ssh access to your instance.
wget LINK
`
Example -wget [https://s3.ap-south-1.amazonaws.com/abkun-chat-bucket/Chat-App-using-Socket.io.zip](https://s3.ap-south-1.amazonaws.com/abkun-chat-bucket/Chat-App-using-Socket.io.zip)
7. Run the following command to decompress the zip file.
unzip filename.zip
Example -unzip Chat-App-using-Socket.io.zip
You can now install your node dependencies by navigating to your folder and running the following command
cd Chat-App-using-Socket.io npm install
install node dependencies
Now, to keep our server running forever (even when we are not SSH logged in to the instance) we will use an npm package called pm2.
1. Install pm2 by running the following command
npm install -g pm2
2. Set up pm2 to start the server automatically on server restart.
pm2 start app.js pm2 save pm2 startup
configure pm2
Note that after running the pm2 startup
command we get a command starting with “sudo”.
Copy that command from sudo
till the end of the next line and paste it in the terminal and press enter.
Now your node server is running and is set to start automatically whenever you restart the EC2 instance.
chat application running on EC2 instance
Now, you can use your instance’s IP address and the port on which your server is running to see your web application in action.
If you have any questions, please comment below.
Thanks for reading this article. If you liked it, please give a few claps so it reaches more people who would love it!