How to Deploy a Node.Js Application on AWS Lightsail

 

In this tutorial, we will teach you how you can deploy a nodejs application on Amazon Web Services (AWS) Lightsail. We will first explain these terminologies like  node.js, and AWS Lightsail so let’s get started

 

Node.js is a server-side platform built on Google Chrome’s JavaScript Engine (V8 Engine). It’s is an open-source, cross-platform runtime environment for developing server-side and networking applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime on Mac OS X, Microsoft Windows, and Linux.

Lightsail is an amazon web service for developers who need to build websites and web applications. You can choose an image for your Lightsail instance that jumpstarts your dev project so you don’t have to spend as much time installing software or frameworks. 

Instances in AWS are virtual environments. It’s an on-demand service, which means a user can rent the virtual server(instances) that has all the operating systems and software installed and deploy their applications on it.

Lightsail has images with base operating systems, development stacks like LAMP, LEMP (Nginx), and SQL Server Express, and applications like WordPress, Drupal, and Magento.

It includes everything you need to launch your project quickly – instances (virtual private servers), container services, managed databases, content delivery network (CDN) distributions, load balancers, SSD-based block storage, static IP addresses, DNS management of registered domains, and resource snapshots (backups) – for a low, predictable monthly price.

The first thing you need to do is open your project or application in your preferred Integrated Development Environment(IDE). We are using WebStorm for this particular application.

node js project opened in webstorm

Lightsail makes database administration more efficient by managing your common maintenance and security tasks. You can create your Mysql database by following the steps given here.


Once the database is created you need to connect and configure it. you can use any standard MySQL client application or utility to connect to it.You can connect your Mysql database by following the steps given here.

To set up your project you first need to login into the AWS account and go to the Lightsail dashboard and then perform the following steps.

Login and Create Instance

 

  • Click on create an instance.
  • Select a platform Linux/Unix.
  • Select a blueprint Apps + OS (checked by default) Select: Node.js (14.16.1).
  • Click Change SSH key pair Select existing key pair or create a new one.
  • Choose your instance plan for example USD 4.5.
  • Identify your instance (Your Lightsail resources must have unique names.) eg: firstnode_app.
  • Click on Create instance button.

Connect and Configure the Instance 

Once the instance is created you have to click on the terminal icon to connect instance and write the following command in the terminal.

 cd stack 
cd apps
Sudo mkdir myapp
sudo mkdir /opt/bitnami/apps/myapp/conf
sudo mkdir /opt/bitnami/apps/myapp/htdocs
sudo mkdir /opt/bitnami/apps/myapp/htdocs
sudo nano /opt/bitnami/apps/myapp/conf/httpd-prefix.conf
  •  Add Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
sudo nano /opt/bitnami/apps/myapp/conf/httpd-prefix.conf Add Include "/opt/bitnami/apps/myapp/conf/httpd-app.conf"
sudo nano /opt/bitnami/apps/myapp/conf/httpd-app.conf
Add ProxyPass / http://127.0.0.1:3000/

ProxyPassReverse / http://127.0.0.1:3000/
sudo nano /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf
  • Include "/opt/bitnami/apps/myapp/conf/httpd-prefix.conf

FileZilla is a powerful and free software for transferring files over the Internet. It is a very popular FTP client and is used by webmasters from all over the world. Here you can download FileZilla software.

We need this to transfer our files between our local computer and Linux instance on Amazon Lightsail. For this, we will connect our instance using SFTP (SSH File Transfer Protocol).To do this, we need to get the private key for our instance, and then use it to configure the FTP client.

Download, Install and Get the Public IP Address :

Download and Install the Filezilla from the link given above. Now get the public IP address of your instance. Login into the Lightsail console, and then copy the public IP address that is displayed next to your instance, as shown in the image.

 

Get the SSH key for your instance

Follow these steps to get the default private key for the AWS Region of your instance, which is required to connect to your instance using FileZilla.

Note: If you’re using your key pair, or you created a key pair using the Lightsail console, locate your private key and use it to connect to your instance. Lightsail does not store your private key when you upload your key or create a key pair using the Lightsail console. You cannot connect to your instance using SFTP without your private key.

  • Sign in to the Lightsail console.
  • Choose Account on the top navigation bar, and then choose Account from the drop-down.
  • Choose the SSH Keys tab.
  • Choose Download next to the default private key for the region where your instance is located.
  • Save your private key in a secured location on your local drive.

Configure FileZilla and connect to your instance

Complete the following steps to configure FileZilla to connect to your instance.

  1. Open FileZilla.
  2. Choose File, Site Manager.
  3. Choose New site, then give your site a name.
image of sftp configuration

4. In the Protocol dropdown, choose SFTP – SSH File Transfer Protocol.

5. In the Host text box, enter or paste your instance’s public IP address.

6. In the Logon Type dropdown, choose Key File.

7. In the User text box, enter one of the following default user names depending on your instance operating system:

  • Amazon Linux, Amazon Linux 2, FreeBSD, and OpenSUSE instances: ec2-user
  • CentOS instances: centos
  • Debian instances: admin
  • Ubuntu instances: ubuntu
  • “Certified by Bitnami” instances: bitnami
  • Plesk instances: ubuntu
  • cPanel & WHM instances: centos

Note: If you are using a different user name than the default user names listed here, then you might need to give the user write permissions to your instance.

8. Next to the Key File text box, choose Browse.

9. Locate the private key file that you downloaded from the Lightsail console earlier in this procedure, and then choose Open.

image of sftp configuration

10. Choose Connect.

You may see a prompt similar to the following example, indicating that the host key is unknown. Choose OK to acknowledge the prompt and connect to your instance.

Now You are successfully connected if you see status messages similar to the following example:

connected successfully

Locate the folder of the node.js application you create on your local machine. Select the folder and compress it to create a zip executable file.

Now in step 3 when we were configuring the instance we had created a folder named “myapp”. To upload your project you need to log into the Lightsail console. Once logged in you need to select the terminal of the instance you created and go to the folder you created or in our case “myapp” and upload the zip folder there.

Once the folder is uploaded you needed to unzip the folder containing your project.

Now you need to run the following commands in the terminal.

This command will restart the scripts on your server.

 

 sudo /opt/bitnami/ctlscript.sh restart apache  

After restarting the scripts you need to run the following command to install the node module.

 npm install    

Once installed we will start our application on the server by running this command.

 npm start

Now that everything is set up we will check if our application is successfully deployed or not. 

For that, you need to copy the IP address of the instance you created shown on your Lightsail dashboard. Copy the IP address and paste it into the URL bar of the browser.

If it’s successful this will appear upon loading

Now that our application is deployed we need to make sure that the process keeps running by itself even when we have closed our local machine. To do that you need to run this command in the terminal.

Forever start  bin/www

We have successfully deployed a Nodejs server on AWS using Lightsail service.  We hope you find this article helpful. Feel free to contact us if you need any help regarding this topic. Do let us know the topics you want us to cover in the comment section or reach out to us with your queries by clicking the contact form below.

60 thoughts on “How to Deploy a Node.Js Application on AWS Lightsail”

  1. When I initially commented I clicked the “Notify me when new comments are added”checkbox and now each time a comment is added I get four e-mails with the samecomment. Is there any way you can remove me from that service?Many thanks!

  2. Aw, this was an incredibly nice post. Taking a few minutes and actual effort to produce a superb articleÖ but what can I sayÖ I put things off a whole lot and don’t manage to get anything done.

  3. Thanks for your personal marvelous posting! I genuinely enjoyedreading it, you will be a great author. I will always bookmark yourblog and will often come back later in life. I wantto encourage one to continue your great job, have a nice weekend!

  4. whoah this blog is fantastic i love reading your articles. Keep up the great work! You know, many people are searching around for this information, you can aid them greatly. Lyndy Ingrim Henden

  5. I just like the valuable info you provide on your articles.I will bookmark your blog and check once more here frequently.I’m somewhat certain I will be informed a lot of new stuff right here!Good luck for the following!

  6. Thank you for some other informative blog. Where else could I get that kind of info written in such an ideal method?I have a undertaking that I’m just now operating on, and I have been at the look out for such information.

  7. I’ll right away grab your rss as I can’t to find your email subscription hyperlink or newsletter service. Do you’ve any? Kindly allow me understand so that I may subscribe. Thanks.

  8. I do trust all the ideas you’ve introduced on your post.They are very convincing and can certainly work. Nonetheless,the posts are too brief for novices. May you please extend them a bit from next time?Thank you for the post.

  9. Thank you very much for sharing. Your article was very helpful for me to build a paper on gate.io. After reading your article, I think the idea is very good and the creative techniques are also very innovative. However, I have some different opinions, and I will continue to follow your reply.

  10. Hi there! I’m at work browsing your blog frommy new apple iphone! Just wanted to say I love reading your blog and look forward to all yourposts! Carry on the excellent work!Take a look at my blog :: Bye Bye Barks

  11. It’s actually a nice and useful piece of info. I’m glad that you simply shared this helpful information with us. Please keep us up to date like this. Thanks for sharing.

  12. I wanted to thank you for this great read!! I definitely loved every little bit of it. I’ve got you bookmarked to check out new things you postÖ

  13. 1id2GV Si vous etes interesse, faites le pas et contactez un des mediums qui fait partie de notre centre d aastrologie et laissez-vous predire votre futur.

  14. It is more of the mind reading as well as a decision-making strategy and just a little percentage of fortune.
    The failure of equally standard method and card counting to identify
    these minuscule sections is the reason for their disappointment.

  15. I may need your help. I’ve been doing research on gate io recently, and I’ve tried a lot of different things. Later, I read your article, and I think your way of writing has given me some innovative ideas, thank you very much.

  16. An interesting discussion is definitely worth comment. I think that you need to publish more on this subject matter, it may not be a taboo subject but typically people don’t talk about such issues. To the next! Kind regards!!

  17. Thank you for some other great article. Where else could anyone get that type of info in such a perfect way of writing? I’ve a presentation next week, and I am on the look for such info.

  18. Aw, this was an exceptionally nice post. Spending some time and actual effort to produce a great article… but what can I say… I procrastinate a whole lot and never manage to get nearly anything done.

  19. I’ll right away take hold of your rss feed as Ican not find your e-mail subscription hyperlink or e-newsletter service.Do you have any? Please let me understand in order that I may subscribe.Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *