How to deploy a Node.js application on a server using PM2 and Nginx. All steps: install, configure, run.

Tr0jan_Horse

Moderator
Staff member
MODERATOR
ULTIMATE
PREMIUM
MEMBER
Joined
Oct 23, 2024
Messages
304
Reaction score
8,793
Deposit
0$
How to Deploy a Node.js Application on a Server Using PM2 and Nginx

Deploying a Node.js application can be a straightforward process if you follow the right steps. In this article, we will cover how to install, configure, and run your Node.js application using PM2 as a process manager and Nginx as a reverse proxy. Let’s dive in!

Prerequisites
Before we start, ensure you have the following:
- A server running Ubuntu (or any other Linux distribution).
- Node.js and npm installed.
- Access to the terminal with sudo privileges.

Step 1: Install Node.js and npm
If you haven't installed Node.js yet, you can do so by running the following commands:

```bash
sudo apt update
sudo apt install nodejs npm
```

To verify the installation, check the versions:

```bash
node -v
npm -v
```

Step 2: Install PM2
PM2 is a powerful process manager for Node.js applications. Install it globally using npm:

```bash
sudo npm install -g pm2
```

To verify the installation, run:

```bash
pm2 -v
```

Step 3: Create Your Node.js Application
For demonstration purposes, let’s create a simple Node.js application. Create a directory for your app:

```bash
mkdir myapp
cd myapp
```

Now, create a file named `app.js`:

```javascript
const http = require('http');

const hostname = '0.0.0.0';
const port = 3000;

const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});

server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
```

Step 4: Start Your Application with PM2
Now that we have our application, let’s start it using PM2:

```bash
pm2 start app.js --name myapp
```

To check the status of your application, run:

```bash
pm2 status
```

You can also set PM2 to restart your application on server reboots:

```bash
pm2 startup
pm2 save
```

Step 5: Install Nginx
Next, we need to install Nginx to serve as a reverse proxy. Install it using:

```bash
sudo apt install nginx
```

To verify that Nginx is running, you can check its status:

```bash
sudo systemctl status nginx
```

Step 6: Configure Nginx
Now, let’s configure Nginx to forward requests to our Node.js application. Create a new configuration file:

```bash
sudo nano /etc/nginx/sites-available/myapp
```

Add the following configuration:

```nginx
server {
listen 80;
server_name your_domain_or_IP;

location / {
proxy_pass http://localhost: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;
}
}
```

Make sure to replace `your_domain_or_IP` with your actual domain name or server IP address.

Now, enable the configuration by creating a symbolic link:

```bash
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
```

Finally, test the Nginx configuration for syntax errors:

```bash
sudo nginx -t
```

If everything is fine, restart Nginx:

```bash
sudo systemctl restart nginx
```

Step 7: Access Your Application
Now, you should be able to access your Node.js application by navigating to your server's domain or IP address in a web browser. You should see "Hello World" displayed.

Conclusion
You have successfully deployed a Node.js application using PM2 and Nginx! This setup ensures that your application runs smoothly and can handle multiple requests efficiently. For further enhancements, consider implementing SSL with Let's Encrypt for secure connections.

Happy coding! If you have any questions or run into issues
 
𝐇𝐌𝐔 𝐟𝐨𝐫 𝐘𝐨𝐮𝐫 𝐓𝐫𝐚𝐧𝐬𝐟𝐞𝐫𝐬

𝐂𝐚𝐬𝐡𝐀𝐩𝐩
𝐏𝐚𝐲𝐏𝐚𝐥
𝐀𝐩𝐩𝐥𝐞 𝐏𝐚𝐲
Crypto Transfer
𝐙𝐞𝐥𝐥𝐞
𝐒𝐤𝐫𝐢𝐥𝐥
𝐕𝐞𝐧𝐦𝐨
𝐆𝐏𝐚𝐲 𝐚𝐧𝐝 𝐦𝐨𝐫𝐞


𝐏𝐑𝐈𝐂𝐄𝐒


𝐓𝐄𝐒𝐓 𝐑𝐮𝐧


$15 𝐟𝐨𝐫 $450

$20 𝐟𝐨𝐫 $6𝟎𝟎

$25 𝐟𝐨𝐫 $850

$3𝟎 𝐟𝐨𝐫 $10𝟎𝟎

$35 𝐟𝐨𝐫 $1150

$40 for $1300

$50 for $1500

𝐈𝐟 𝐚𝐧𝐲𝐭𝐡𝐢𝐧𝐠 𝐡𝐚𝐩𝐩𝐞𝐧𝐬,,𝐖𝐞 𝐡𝐚𝐯𝐞 𝐫𝐞𝐟𝐮𝐧𝐝 𝐚𝐧𝐝 𝐫𝐞𝐩𝐥𝐚𝐜𝐞𝐦𝐞𝐧𝐭 𝐩𝐨𝐥𝐢𝐜𝐲 𝐚𝐧𝐝 𝐰𝐞 𝐨𝐟𝐟𝐞𝐫 𝐜𝐚𝐬𝐡𝐨𝐮𝐭 𝐠𝐮𝐢𝐝𝐚𝐧𝐜𝐞


TELEGRAM : @buzzinfr
 
Top Bottom