phpnode.jslaravelubuntusupervisord

How to run Node.js index file using Supervisor in a Laravel project on a Ubuntu server?


How can I use Supervisor to automatically run the main Node.js file (index file) in my Laravel project on a server running Ubuntu?

I want to ensure that the Node.js process is continuously monitored and restarted if it crashes.


Solution

  • You have to follow the steps given below:

    1. Install Supervisor, Node.js, and npm:

    sudo apt-get install supervisor
    
    sudo apt install nodejs
    
    sudo apt install npm
    

    2. Check Node.js and npm versions:

    node -v
    
    npm -v
    

    3. Create a Supervisor config file:

    sudo nano /etc/supervisor/conf.d/laravel-npm.conf 
    

    Inside the file, add the following code:

    [program:laravel-npm]
    directory=/var/www/html   # Change to your Laravel app path
    command=node index.js    # Change to your Node.js command
    autostart=true
    autorestart=true
    stderr_logfile=/var/www/html/storage/logs/laravel-npm.err.log
    stdout_logfile=/var/www/html/storage/logs/laravel-npm.out.log
    

    Save and close the file.

    4. Run Supervisor commands:

    sudo supervisorctl reread
    sudo supervisorctl update
    sudo supervisorctl start laravel-npm:*
    

    This setup helps Supervisor monitor and restart your Node.js process in your Laravel project on an Ubuntu server. Make sure to customize the file paths and commands according to your project.