How to Deploy MEAN (With MYSQL) Stack Application on AWS LightSail

Before we start deploying the application onto the server we need to make sure our application is working and all the components are installed in it for that you need to follow the following steps. 

Step 1:Run the Angular CLI project in IDE e.g. Webstorm

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

Step 2:Install the node modules using npm install

Now you need to run the following command to install the node module.

 npm install

Step 3: Run Ng Serve and access the application at http://localhost:4200/

Run the following command and then open http://localhost:4200/ to see the new app run in your browser. 

Note: When you use the ng serve command to build an app and serve it locally, the server automatically rebuilds the app and reloads the page when you change any of the source files.

 ng serve
ng serve command

Step 4: Run the Node JS project in IDE and Install the node modules 

Now you need to open your nodejs project or in the Integrated Development Environment(IDE).

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

 npm install    

Then Connect the node application with MYSQL.

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

 npm start

Once the application is working fine at http://localhost:4200/ – it’s time to start the deployment.

Go to the Angular project and update the rest API URL from http://localhost:3000/ to your domain name where you are deploying e.g. www.example.com or if it’s an IP address then change it to http://XX.XXX.XX.XXX/

Now we will run the ng build command in the terminal. ng build is the command you use when you’re ready to build your application and deploy it. The CLI will analyze the application and build the files, all while optimizing the application as best as it can

ng build
ng build command screenshot of IDE

Once the project is built. Go to the Angular directory and copy the files in the dist folder. Then navigate to the public folder of Node JS and paste the files there.

Step 1: Create the Database

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.

Step 2: Update the database connection settings 

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.

Step 3: Archive the MEAN Stack application Folder

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

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) 
  • 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 the 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

You can follow the steps given at the link here.

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

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 MEAN Stack application on (AWS) Lightsail. 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.

Leave a Comment

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