A Frontend Developer’s Journey into DevOps: Deploying a Backend with Nginx and PM2
As a frontend developer, I’ve spent most of my time focusing on crafting user interfaces, making sure things look great, and handling client-side logic. But as I’ve started venturing into the backend side of things, I’ve realized that the DevOps world — though daunting at first — holds some exciting opportunities. In particular, deploying and managing backend applications using tools like Nginx and PM2 has become a crucial skill that I’m eager to learn and share with you.
In this post, I’ll walk you through the basics of how to deploy a backend service using Nginx as a reverse proxy and PM2 to manage Node.js applications. This will help you understand how these tools fit together in the DevOps workflow, especially if you’re coming from a frontend background.
1. Understanding the Basics
Before we dive into the setup, let’s break down the two main components we’re using here:
What is PM2?
PM2 is a process manager for Node.js applications. It’s an incredibly useful tool when you want to manage the lifecycle of your backend apps. Think of it like a “task manager” for your Node.js services. It allows you to:
Keep your applications running continuously.
Automatically restart the app if it crashes.
Monitor and log application output.
Run multiple instances of your app for load balancing.
For a frontend developer used to working with React or Next.js, you might be familiar with running the app locally with npm start
during development. But in production, it’s essential to keep your app running reliably, even after crashes or restarts. PM2 makes that easy.
What is Nginx?
Nginx is a high-performance web server that is commonly used as a reverse proxy for backend services. A reverse proxy means that Nginx will handle incoming requests from users, and route those requests to your backend application. It can also serve static files, handle SSL encryption, and improve the performance of your site.
As a frontend developer, you’re used to handling things on the client-side, but once you dive into backend deployment, understanding Nginx is essential. It’s a powerful tool that acts as a traffic manager for your app.
2. The Flow: How Nginx and PM2 Work Together
To give you an idea of how Nginx and PM2 work in harmony, let’s look at the flow of a typical request:
Client Request: The user opens your website by navigating to
http://yourdomain.com
.Nginx Handles the Request: Nginx listens on port 80 (the default HTTP port). It receives the request and determines where to forward it based on your configuration.
Nginx Routes to the Backend: If Nginx is set up as a reverse proxy for your backend service, it forwards the request to the backend (e.g., a Node.js app running on port 3000).
PM2 Keeps the Backend Running: PM2 is responsible for keeping your backend application alive. If the backend app crashes, PM2 restarts it automatically.
Response: After the backend processes the request (e.g., fetching data from a database), it sends the response back to Nginx, which then delivers it to the client.
This ensures that your app is scalable, reliable, and can handle high traffic loads.
3. Setting Up PM2 to Manage Your Node.js Application
Now, let’s get our hands dirty with some commands to start using PM2.
Install PM2:
First, install PM2 globally on your server. This can be done easily with npm:
npm install -g pm2
Start Your Node.js Application with PM2:
To start your app with PM2, use the following command:
pm2 start npm --name "auth-service" -- run start
This command tells PM2 to run the start
script from your package.json
under the name auth-service
. PM2 will keep the app running even if it crashes.
Monitor Logs and Processes:
You can monitor the logs of your app with:
pm2 logs
And if you want to see the list of all PM2-managed processes:
pm2 list
4. Configuring Nginx to Serve as a Reverse Proxy
Next, let’s configure Nginx to forward requests to your backend app.
Install Nginx:
If you don’t already have Nginx installed, you can install it on your server with:
sudo apt update
sudo apt install nginx
Configure Nginx to Proxy Requests:
Create or edit an Nginx configuration file (usually located in /etc/nginx/sites-available/
).
sudo nano /etc/nginx/sites-available/yourdomain.com
Here’s a simple configuration example to route requests to your backend:
nginx
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000; # PM2 app running on port 3000
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;
}
}
This configuration tells Nginx to listen for requests on port 80 and forward them to the Node.js app running on localhost:3000
.
Enable the Site and Restart Nginx:
Enable the site by creating a symlink and restart Nginx:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
sudo systemctl restart nginx
5. Benefits of Using Nginx and PM2 for Your Node.js Backend
Scalability:
You can easily scale your backend app by running multiple instances with PM2 and using Nginx to load balance between them. Nginx can distribute incoming traffic to different instances of your app, ensuring that no single instance is overwhelmed.
Reliability:
PM2 ensures that your backend app is always running. If it crashes, PM2 will automatically restart it. This is crucial for production environments where uptime is critical.
Performance:
Nginx serves static files (like images, CSS, JS), reducing the load on your backend. It can also handle caching, improving your site’s performance and reducing the strain on the backend.
Security:
Nginx can be configured to handle SSL encryption (HTTPS), ensuring that your users’ data is secure. It also provides an extra layer of security by acting as a barrier between the outside world and your backend services.
6. Conclusion
As a frontend developer transitioning into the DevOps world, understanding how to deploy and manage backend applications is a valuable skill. Using tools like PM2 and Nginx, you can ensure that your Node.js applications are running smoothly, reliably, and securely. By combining the power of Nginx’s reverse proxying with PM2’s process management, you’ll be able to scale, monitor, and manage your backend with ease.
This is just the beginning of your DevOps journey. The more you practice and learn, the more powerful your deployments will become. As you get comfortable with these tools, you’ll be ready to tackle even more complex challenges and make your applications ready for production!
#devops #backend #pm2 #AWS