Getting a Subdomain Pointing to a PM2 App on an Nginx Ubuntu Server
In this article, we will cover the steps required to point a subdomain to a PM2 application running on an Nginx server. We will be using an Ubuntu server for this tutorial. This process involves creating a new subdomain, configuring Nginx, and setting up PM2. By the end of this article, you will have a good understanding of how to set up a subdomain that points to a PM2 application on an Nginx Ubuntu server.
Prerequisites
To follow along with this tutorial, you will need the following:
- An Ubuntu server
- A domain name configured with your server
- A PM2 application that you want to point a subdomain to
Creating a Subdomain
To create a new subdomain, you will need to edit your domain's DNS records. This process will vary depending on your domain registrar, but the basic concept is to create a new DNS record that points to your server's IP address.
In this example, we will create a new subdomain called app.example.de. The A record will point to the server's IP address:
app IN A 12.34.56.78
Where 12.34.56.78 is the IP address of your server.
Configuring Nginx
Once you have created your new subdomain, you will need to configure your Nginx server to handle traffic for that subdomain. This involves creating a new Nginx config file that points to the PM2 application.
First, create a new Nginx config file:
sudo nano /etc/nginx/sites-available/app.example.de.conf
Next, add the following configuration:
server {
listen 80;
server_name app.example.de;
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;
}
}
Where http://localhost:3000 is the address of your PM2 application. If your application listens on a different port, change the port number accordingly.
Once you have added the configuration, test the Nginx config:
sudo nginx -t
If the test is successful, enable the new Nginx config:
sudo ln -s /etc/nginx/sites-available/app.example.de.conf /etc/nginx/sites-enabled/
Finally, restart the Nginx server for the changes to take effect:
sudo systemctl restart nginx
Setting up PM2
Assuming you have a PM2 application already, you can start it with:
pm2 start your-app.js
Where your-app.js is your PM2 application entry point.
Once the PM2 application is running, you can check the status with:
pm2 list
- Create a new subdomain that points to your server's IP address
- Create a new Nginx config file that proxies traffic to your PM2 application