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
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.
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.
Sudo mkdir myapp
sudo mkdir /opt/bitnami/apps/myapp/conf
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-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
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.
- Open FileZilla.
- Choose File, Site Manager.
- Choose New site, then give your site a name.
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.
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:
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.
Once installed we will start our application on the server by running this command.
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.