
AWS Lightsail Node JS Express Server
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