How to deploy a NodeJs server using Laravel Forge

Valerio Barbera

Hi, I’m Valerio, software engineer from Italy, and C.T.O. at Inspector.

We recently worked to replace the http handler behind our ingestion endpoint (ingestion.inspector.dev) with a new implementation in pure NodeJs. This endpoint receives monitoring data sent from all applications connected to our Code Execution Monitoring engine, and it treats more than 5 million http requests per day.

Using Nodejs we are seeing a 5x performance improvement in managing this large volume of traffic making Inspector more reliable and fast.

In this article I’ll show you how to deploy a NodeJs server using Forge, the most popular server management tool in the Laravel community.

Forge was primarily designed to deploy PHP apps, but it’s simple enough to get a node application running because it includes a set of services like NGINX and SupervisorD that which can greatly simplify the server configuration.

NGINX configuration

We just need to take advantage of nginx’s excellent built-in proxy capabilities.

Suppose to have a NodeJs server running on port 3000:

// Start web server
http.createServer((req, res) => {

    // Managing the incoming http request

}).listen(3000, () => {
    console.log("Server is listening on port: 3000")
});

We need to tell nginx that inbound requests should be proxied to the node process running our application on port 3000.

At the bottom of the site details section in your Forge server you’ll find the “Files” button to access the site’s NGINX configuration file. The section that we’re interested in is the “location” block that begins with “/”. For a standard PHP or Laravel site, the default setting of try_files works well.

Here is the default configuration provided by Forge:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

In our case, we’re running a Nodejs process that’s bound to a specific port on our VM, so we need to inform NGINX that all http requests should be forwarded to our Nodejs process in the appropriate port.

Replace this section with the script below:

location / {
    proxy_pass http://localhost:3000; # Port number of node http server
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection 'upgrade';
   proxy_set_header Host $host;
   proxy_cache_bypass $http_upgrade;
}

Nginx will now forward incoming requests to our NodeJs (e.g. ExpressJs, Fastify, etc.) application and return the response it generates to the requesting client.

Supervisor configuration

In production you need to keep your node server working, preventing the process from shutting down due to unexpected errors. The nodejs server process may stop running for a variety of reasons, such as an exceeded timeout or a script execption.

For this reason, you need to configure a process monitor that can detect when your nodejs process exit and automatically restart it. Supervisor is a process monitor commonly used in Linux environments and it’s available by default in the servers provisioned by Forge.

In the Forge panel you can add a nodejs command as a daemon in the “Server > Daemons” menu.

Add the command to start your node server:

node /home/forge/mysite.com/public/server.js

This will make sure your server keeps running and is automatically restarted in case of error.

Deploy script

Additionally we can use quick deploy in Forge, below is a valid deployment script. It uses “pkill -f” to kill the supervisor process by full command name. This was the easiest I came up with as you don’t know PID number here:

# pull latest code version
cd /home/forge/mydomain.com
git pull origin main 

# install or update npm modules
cd /home/forge/mydomain.com
npm install 

# restart node daemon  
pkill -f 'node /home/forge/mydomain.com/public/server.js'

When the process will be killed the supervisor automatically restart it loading the last code changes.

Conclusion

Once you are familiar with the process it will seem very quick and easy, and there are some huge benefits. Forge also has other features that can be used for non-Laravel apps, such as the Schedule tab, which provides an easy way to manage cron jobs, and Databases if you want to use a MySQL (or Postgres) database in your node script.

I hope this article “How to deploy a NodeJs server using Laravel Forge” is a good starting point for your next Nodejs project. If you are interested in Monitoring your Code Execution in real-time, register your Inspector account for free.

Related Posts

In-Depth Scalable Applications monitoring

If you are responsible for the development of a software product you probably know that in one way or another if you can’t scale your application, if the software is slow or crashes, you can’t move your business forward. It’s basically locked. On the other hand, adding additional app servers isn’t just a click on