Node.js is a popular and powerful JavaScript runtime widely used for building server-side applications. If you’re running a Ubuntu server, you may want to deploy a new Node.js service using a process manager like pm2. Pm2 is a process manager for Node.js applications that provides features such as automatic restarts, monitoring, and logging.
In this article, we’ll walk through the process of adding a new Node.js service to your Ubuntu server using pm2. We’ll cover the installation and setup of pm2, creating a new Node.js application, configuring pm2 to manage the application, and finally testing and monitoring the service. By the end of this article, you’ll have a better understanding of how to use pm2 to manage Node.js services on your Ubuntu server.
Before we start adding a new Node.js service on Ubuntu using pm2, let’s take a moment to discuss why you might want to use pm2 in the first place.
One of the main benefits of using pm2 is that it provides automatic restarts for your Node.js applications in the event of a crash. This means that you don’t need to manually restart your application every time there’s an error or your server goes down. Pm2 also allows you to monitor your application’s performance and view logs, making it easier to debug issues and optimize your service.
Another advantage of pm2 is that it allows you to manage multiple Node.js services on a single server. This can be particularly useful for larger projects with multiple components or microservices.
Pm2 also has a number of other valuable features, such as clustering, load balancing, and zero-downtime deployments. However, for the purposes of this article, we’ll focus on the basic steps required to add a new Node.js service using pm2.
Before we begin, it’s assumed that you have a basic understanding of Node.js and Ubuntu and that you have already installed Node.js and npm on your server. If you haven’t done so already, you can follow our guide on installing Node.js and npm on Ubuntu to get started.
With that in mind, let’s start adding a new Node.js service on Ubuntu using pm2!
1. Upload the file on your Ubuntu server in your selected directory. Basically, I usually love to upload on the “/var/www/” directory.
2. Make an essential change if needed. There basically needs to change…
3. Connect with the server by terminal with this command “ssh user@121.121.00.121“. Then enter your password to connect.
4. Create the server configuration service.conf file and put it inside “/etc/nginx/sites-available“. Here’s a sample file…
server {
listen 80;
root /var/www/service_name;
index index.html index.htm;
server_name youdomain.com;
location / {
proxy_pass http://localhost:8095;
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;
}
}
5. Add symlink/shortcut for this “service.conf” file inside “/etc/nginx/sites-available” in this directory. You may use the below command to make this symlink.
ln -s /etc/nginx/sites-available/service.conf /etc/nginx/sites-enabled/
Make sure the added service.conf is error free by running the below terminal command.
nginx -t
If everything is ok, then restart your nginx by running the below command.
sudo service nginx restart
6. Now, it’s time to run the service by PM2. make sure you already have installed the PM2 service. Run your service on PM2 by the below command.
# Install PM2 if not installed
sudo npm install pm2 -g
# Add new service on PM2
pm2 start --name "service_name" FULL_DIRECTORY/OF/app.js
#
Add/save this process to list (need to setup again if not saved when restart the system).
pm2 save
# Other important PM2 commands
# Get PM2 status
pm2 status
# Stop PM2
pm2 stop <process ID / name>
# Restart PM2
pm2 restart <process ID / name>
# Delete PM2
pm2 delete <process ID / name>
That’s it. Now your service should run with PM2.
In conclusion, pm2 is a powerful and easy-to-use process manager for Node.js applications that can help you manage your services effectively. By following the steps outlined in this article, you should be able to add a new Node.js service to your Ubuntu server using pm2.
Once your service is up and running, you can use pm2 to monitor its performance, view logs, and make any necessary configuration changes. You can also use pm2 to manage multiple Node.js services on the same server, which can be particularly useful for larger projects.
Overall, pm2 is a valuable tool for anyone working with Node.js on Ubuntu, and we hope that this article has helped you get started with using it. If you have any questions or comments, feel free to leave them below. Thanks for reading!