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 ( 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/

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/
git pull origin main 

# install or update npm modules
cd /home/forge/
npm install 

# restart node daemon  
pkill -f 'node /home/forge/'

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


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

How to prevent users from registering into your app with insecure passwords

Hi, I’m Valerio, software engineer and CTO at Inspector. About one year ago one of our accounts on an external platform has been hacked. Our credit card was attached to this account so we had to warn the bank to block it. Fortunately, there were no consequences, neither for our bank account, nor for our

Is it better to BUILD an internal monitoring environment, or BUY a prepackaged solution?

“Build vs Buy” Bake-off: Which should you choose? Every case is different, and all of these factors should be considered carefully before making a decision. There are situations where building a solution makes sense, either because you have the time, your requirements aren’t very complex, or if your applications simply don’t have enough load to

How to extend Laravel with driver-based services

Hi, I’m Valerio, software engineer and CTO at Inspector. In this article I talk about a Laravel internal feature not mentioned in the official documentation called “Driver Manager”. It can completely change the way you design and develop your application solving critical architectural bottlenecks, allowing you to build large systems built around decoupled, independent and