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
- Launch an EC2 instance and SSH into it.
- Install Node on EC2 instance.
- Copy code on your EC2 instance and install dependencies.
- Start server to run forever.
So let’s get started.
1. Launch an EC2 instance and SSH into it
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.
2. Install Node on EC2 Instance
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 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.
3. Copy code on your EC2 instance and install dependencies
Most commonly there are two ways to do that
1. Using Github, BitBucket or Gitlab
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
2. Using Amazon S3
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
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.
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.
3. Click Next button and then click Create bucket button.
4. Select your newly created bucket and upload the project’s zip file to the bucket.
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.
7. Run the following command to decompress the zip file.
You can now install your node dependencies by navigating to your folder and running the following command
4. Start server to run forever
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
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.
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.