How to deploy a NodeJs server using Laravel Forge – Tutorial

Valerio Barbera
node server logo

Hi, I’m Valerio, a software engineer from Italy and CTO at Inspector.

We recently worked to replace the HTTP handler behind our ingestion endpoint (ingestion.inspector.dev) with a new implementation. We used 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.

By using node.js, we see a 5x performance improvement in managing this large traffic volume. This makes Inspector more reliable and fast.

This article will show you how to deploy a NodeJs server using Forge. Forge is the most popular server management tool in the Laravel community.

It is designed to deploy PHP apps. Still, it’s simple enough to get a node application running. It can because Forge includes a set of services like NGINX and SupervisorD. These can significantly simplify the server configuration.

Laravel Forge NGINX configuration

We need to take advantage of nginx’s excellent built-in 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 our application’s node process on port 3000.

At the bottom of the site details section in your Forge server, you’ll find the “Files” button. Use it to access the site’s NGINX configuration file. The area 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;
}

We’re running a Node.js process bound to a specific port on our VM. So, we need to inform NGINX that all HTTP requests should forward to our Node.js 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 the NodeJs application (e.g. ExpressJs, Fastify, etc.) and return the response it generates to the requesting client.

Supervisor configuration

It would be best to keep your node server working in production. This prevents the process from shutting down due to unexpected errors. For example, the Node.js server process may stop running for various reasons. They could include an exceeded timeout or a script exception.

For this reason, you need to configure a process monitor that can detect when your Node.js process exits and automatically restart it. Supervisor is a process monitor 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 below to start your node server:

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

Doing so will ensure your nodejs server keeps running and restarts in case of an 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 strategy is the easiest as you don’t know the 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 is killed, the supervisor automatically restarts it loading the last code changes.

Conclusion

Once you are familiar with the process, it will seem very quick and easy, and there are huge benefits. Forge has other features that work with non-Laravel apps.

They include the Schedule tab, which provides an easy way to manage cron jobs. Database helps you use MySQL (or a Postgres) database in your node script.

Autofix your Laravel application for free

Inspector is a Code Execution Monitoring tool specifically designed for software developers. You don’t need to install anything on the infrastructure, just install the Laravel package and you are ready to go.

If you are looking for effective automation, and the ability to automatically receive code change proposals to fix application errors try Inspector for free. Register your account.

Or learn more on the website: https://inspector.dev

Related Posts

Laravel Http Client Overview and Monitoring

Laravel HTTP client was introduced starting from version 10 of the framework, and then also made available in all previous versions. It stands out as a powerful tool for making HTTP requests and handling responses from external services. This article will delve into the technical foundations of the Laravel HTTP client, its motivations, and how

Laravel Form Request and Data Validation Tutorial

In this article I will talk about Laravel Form Request to send data from your application frontend to the backend. In web applications, data is usually sent via HTML forms: the data entered by the user into the browser is sent to the server and stored in the database eventually. Laravel makes it extremely simple

Upload File in Laravel

You can upload file in Laravel using its beautiful unified API to interact with many different types of storage systems, from local disk to remote object storage like S3. As many other Laravel components you can interact with the application filesystem through the Storage Facade: Illuminate/Support/Facades/Storage This class allows you to access storage drivers called