• Let’s talk about your project
  • contact@ohm.solutions

AWS Lightsail Node JS Express Server

October 31, 2020
October 31, 2020 Nico Lauria

AWS Lightsail Node JS instances come pre-configured with Node already installed on the machine. That means you can easily transfer over a Node application and run npm install to have your app up and running quickly. There are however some configuration steps required for getting the express server to run on Apache.

One option is to use the guide provided by Bitnami for setting up an Express server on a Node JS instance. You can find that guide here:

https://docs.bitnami.com/aws/infrastructure/nodejs/administration/create-custom-application-nodejs/

In this post we will discuss what is considered the old way of setting up an express server on a Bitnami Node JS instance. In certain situations I prefer this setup as it does not create extra folders and files at the root of your application. You are able to simply clone a repository that already has an express application built and then point Apache to that server.

The first step if you haven’t done so already is to create a repository and add the code for your express server. If you don’t have any code for your server yet:

Create a file named server.js at the root of your application and add this code:


const express = require('express');
const app = express();

const port = 3000;

app.listen(port, function() {
console.log('listening on port 3000');
})

app.get('/', function(req, res) {
res.send('hello world');
})

You will also need to npm install express or add it to your package.json dependencies. Once you have added the code and committed it head over to AWS.

Create a Lightsail Node JS instance and SSH into the machine. I find using the VS Code Remote SSH extension is incredibly helpful for development. I put together a short video on how to use it. You can find that video here: VS Code Remote SSH Extension Setup.

You will want to enter sudo su to switch to the root user. You can then run mkdir /home/bitnami/stack/apps. This will create the folder where we will place our application.

Next, cd /home/bitnami/stack/apps and git clone your repo. Run npm install afterwards.

After doing that we need to add some Apache configuration.

mkdir conf

cd conf

nano httpd-prefix.conf and add the following line (replacing appname with the name of your repo):

Include "/opt/bitnami/apps/appname/conf/httpd-app.conf"

Save the file then run, nano httpd-app.conf. Add the following lines to the file:


ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/

Our final step is to point Apache to the httpd-prefix.conf file that we just created:

nano /opt/bitnami/apache2/conf/bitnami/bitnami.conf and add the following line (again replacing appname with your repo name):

Include "/opt/bitnami/apps/appname/conf/httpd-prefix.conf"

Restart apache: sudo /opt/bitnami/ctlscript.sh restart apache

Navigate to the root of your app and run node server.js

If you open a browser tab with the ip address of the Lightsail instance you should see ‘hello world’.

You can keep your server running with forever which is bundled in the Bitnami installation:

Navigate to the root of your application and run forever start server.js

To stop the server: forever stop server.js

Contact

Get Connected.

We welcome you to contact us for more information
about any of our products or services.

Let’s Talk About Your Project

We’d love to hear about your project and your ideas. Give us a call or send us an email to set up a time to speak.

Connect With Us On Social Media

Find OHM on one of our social media accounts below and let us know what you think about our work.

Set Up A Free Consultation

Ready to get started? Tell us some details about your project using our questionnaire form.Consultation

CALL: 916-335-8159

1029 H Street Suite 306
Sacramento, CA 95814

Contact