In this comprehensive tutorial, we're configuring Nginx for Node.js application and setting up a systemd service to enhance its security, performance, and scalability. Additionally, you'll discover how to create a systemd service file to manage your Node.js application efficiently.
This guide covers installing Node.js and Nginx, setting up a simple Node.js application, configuring Nginx to proxy requests, and creating a systemd service to ensure your application runs smoothly on startup and can be easily managed. Follow these steps to streamline your Node.js application deployment on a Linux server.
Prerequisites
- A Ubuntu 24.04 dedicated server or KVM VPS
- You need root or sudo privileges to install software and modify system configurations.
- Basic Linux Command Line Knowledge
- A Domain Name with added
A
record pointed to server.:
Configure Nginx for Node.js Application
Step 1: Install Node.js and Nginx
1.1. Install Node.js
You can install Node.js using the NodeSource binary distributions.
# Update the package index
sudo apt update
# Install Node.js from NodeSource
curl -sL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
1.2. Install Nginx
# Install Nginx
sudo apt install -y nginx
Step 2: Configure Firewall
We need to add ports in firewall. Execute following set of commands:
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 3000/tcp
ufw reload
Step3: Set Up a Node.js Application
Create a simple Node.js application to test the reverse proxy setup.
3.1. Create a new directory for your application
mkdir myapp
cd myapp
3.2. Initialize a new Node.js application
npm init -y
3.3. Install Express
npm install express
3.4. Create an app.js
file
nano app.js
Add following content.
// app.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, World!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
3.5. Start the Node.js application
node app.js
Your Node.js application should now be running on http://localhost:3000
.
Step 4: Configure Nginx as a Reverse Proxy
4.1. Create an Nginx configuration file for your application
Create a new configuration file in the /etc/nginx/sites-available
directory.
sudo nano /etc/nginx/sites-available/myapp
Add the following content to the file:
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;
}
}
Replace your_domain_or_IP
with your domain name or server IP address.
4.2. Enable the configuration by creating a symbolic link to the sites-enabled directory
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
4.3. Test the Nginx configuration for syntax errors
sudo nginx -t
4.4. Reload Nginx to apply the changes
sudo systemctl reload nginx
Step 5: Optional - Enable HTTPS with Let's Encrypt
To secure your application with HTTPS, you can use Certbot to obtain and install a Let's Encrypt SSL certificate.
5.1. Install Certbot
sudo apt install certbot python3-certbot-nginx -y
5.2. Obtain and install the SSL certificate
sudo certbot --nginx -d your_domain
Follow the on-screen instructions to complete the process.
5.3. Test the renewal process
sudo certbot renew --dry-run
Step 6: Create a systemd Service File
Create a new service file for your Node.js application.
sudo nano /etc/systemd/system/myapp.service
Add the following content to the file:
[Unit]
Description=My Node.js Application
After=network.target
[Service]
Environment=NODE_PORT=3000
WorkingDirectory=/root/myapp
ExecStart=/usr/bin/node /root/myapp/app.js
Restart=always
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
# Consider using 'StandardOutput' and 'StandardError' to redirect logs
StandardOutput=/var/log/nginx/syslog
StandardError=/var/log/nginx/syserror
SyslogIdentifier=myapp
[Install]
WantedBy=multi-user.target
Note: Replace /root/myapp
with the actual path to your Node.js application directory. Adjust the User and Group fields to an appropriate user and group that should run the application. Typically, you wouldn't run applications as root for security reasons.
Reload systemd to Apply the New Service File
sudo systemctl daemon-reload
Start your service:
sudo systemctl start myapp
Enable your service to start on boot:
sudo systemctl enable myapp
You can check the status of your service to ensure it's running correctly:
sudo systemctl status myapp
Step 7: Access Your Node.js Application via Nginx
Open a web browser and navigate to https://your_domain
. You should see the message "Hello, World
!" from your Node.js application.
Conclusion
You've successfully seen how we configured Nginx as a reverse proxy for your Node.js application. This setup enhances the security and scalability of your application by offloading some tasks to Nginx, such as handling SSL/TLS termination and serving static files.