User Subscription Input Users can input subscription details: name, price, frequency, renewal date, and category.
Payment Reminders Schedule reminders for upcoming renewals using Upstash workflows.
Upcoming Renewals Fetch subscriptions sorted by the next renewal date.
Total Monthly Expenses Calculate the total monthly/annual costs for all subscriptions.
Subscription Analytics Provide insights into subscription usage.
Secure User Authentication Implement user registration, login, and JWT-based authentication.
MONGO_URI=mongodb://localhost:27017/subscriptionDB
JWT_SECRET=your_jwt_secret ARCJET_KEY=your_arcjet_key
QSTASH_TOKEN=your_qstash_token QSTASH_CURRENT_SIGNING_KEY=your_current_signing_key QSTASH_NEXT_SIGNING_KEY=your_next_signing_key VPS_PUBLIC_IP=https://your-public-domain.com
EMAILJS_SERVICE_ID=your_emailjs_service_id EMAILJS_TEMPLATE_ID=your_emailjs_template_id EMAILJS_USER_ID=your_emailjs_user_id
Here's a comprehensive and step-by-step markdown guide to deploy your Node.js app to a VPS server, based on our troubleshooting journey:
This guide explains how to deploy your Node.js app to a VPS (Hostinger in this case) and ensure it is accessible via your server's public IP. It also includes the troubleshooting steps we resolved together to make the deployment work.
-
Host a Node.js App:
- Make sure your Node.js app (e.g.,
subscription-tracker-api
) is ready withapp.js
orserver.js
as the entry point. - Ensure it has the required dependencies listed in
package.json
.
- Make sure your Node.js app (e.g.,
-
VPS Access:
- Access to a VPS server (Hostinger in this case).
- SSH credentials for your VPS.
-
Tools Installed Locally:
ssh
to connect to the VPS.git
to clone the repository or transfer files.
-
Log into your VPS via SSH:
ssh root@<your-vps-ip>
Replace
<your-vps-ip>
with the IP address of your VPS (e.g.,147.93.94.120
). -
Once logged in, update your VPS packages:
apt update && apt upgrade -y
-
Install Node.js (LTS version):
curl -fsSL https://deb.nodesource.com/setup_18.x | bash - apt install -y nodejs
-
Verify Node.js and npm installation:
node -v npm -v
-
Install PM2 to manage your Node.js app:
npm install -g pm2
-
Install Nginx to act as a reverse proxy:
apt install nginx -y
-
Install
git
on your VPS:apt install git -y
-
Clone your repository:
git clone https://github.com/<your-username>/<your-repo-name>.git
-
Navigate to the project folder:
cd <your-repo-name>
If you’re not using Git:
-
Use
scp
to transfer files from your local machine to the VPS:scp -r /path/to/your/project root@<your-vps-ip>:/var/www/<your-project>
-
SSH into your VPS and navigate to the project directory:
cd /var/www/<your-project>
-
Install dependencies:
npm install
-
Create a
.env
file with your environment variables:nano .env
Example
.env
:PORT=5500 MONGO_URI=your_mongo_connection_string EMAILJS_SERVICE_ID=your_service_id EMAILJS_TEMPLATE_ID=your_template_id EMAILJS_USER_ID=your_user_id QSTASH_TOKEN=your_qstash_token
-
Start the app using PM2:
pm2 start app.js --name subscription-tracker
-
Save the PM2 process list to restart on reboot:
pm2 save pm2 startup
-
Create a new Nginx configuration file:
nano /etc/nginx/sites-available/subscription-tracker
-
Add the following content:
server { listen 80; server_name <your-ipv4-address>; # Replace with your VPS IPv4 address location / { proxy_pass http://localhost:5500; # Replace with your app's port 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; } }
-
Save and exit (
Ctrl + O
, thenEnter
, thenCtrl + X
).
-
Create a symlink to enable the configuration:
ln -s /etc/nginx/sites-available/subscription-tracker /etc/nginx/sites-enabled/
-
Test the Nginx configuration:
nginx -t
-
Restart Nginx:
systemctl restart nginx
Ensure that traffic on port 80
(HTTP) is allowed:
ufw allow 80
ufw enable
-
Visit your app in the browser using the public IPv4 address:
http://<your-ipv4-address>
-
If you see:
{"error":{"message":"Not Found","status":404}}
This means your app is running but doesn’t serve anything at the root (
/
). You can add routes to your app as needed.
- This happens when the default Nginx configuration is still active. Disable it:
sudo rm /etc/nginx/sites-enabled/default sudo systemctl restart nginx
- Ensure the app is running and accessible locally:
curl http://localhost:5500
- Use
sudo
to execute commands requiring elevated privileges.
- Point your domain’s DNS to your VPS IP (add an A record).
- Update
server_name
in your Nginx config with your domain name:server_name yourdomain.com;
- Restart Nginx:
systemctl restart nginx
With these steps, you should have your Node.js app successfully deployed and accessible on your VPS. Let me know if you need further clarification!
To update the code on your VPS, you need to pull the latest changes from your repository (if you're using Git) or re-upload the updated files. Here’s a step-by-step guide:
-
SSH into your VPS:
ssh root@<your-vps-ip>
-
Navigate to your project directory:
cd /path/to/your/project
-
Ensure the correct branch is checked out:
git branch
If not, switch to the appropriate branch:
git checkout <branch-name>
-
Pull the latest changes:
git pull origin <branch-name>
- From your local machine, upload the updated files to the VPS:
Replace
scp -r /local/path/to/project root@<your-vps-ip>:/path/to/project
/local/path/to/project
with your local project path and/path/to/project
with the directory on your VPS.
- You can use tools like FileZilla or WinSCP to upload updated files to the VPS.
If you’ve added or updated any dependencies in package.json
, run the following commands in your project directory:
-
Install dependencies:
npm install
-
If you’ve removed unused dependencies:
npm prune
After updating the code, you need to restart your app to apply the changes. Use PM2 (or any other process manager you’re using):
-
Restart the app:
pm2 restart subscription-tracker
-
If PM2 isn’t running the app, start it:
pm2 start app.js --name subscription-tracker
-
Save the process to restart automatically on reboot:
pm2 save
- Test if the app is running locally:
curl http://localhost:5500
- Replace
5500
with the port your app is running on.
- Replace
- Open your app’s public URL in the browser:
http://<your-vps-ip>
-
Check PM2 logs for errors:
pm2 logs subscription-tracker
-
Check Nginx logs:
tail -f /var/log/nginx/error.log
-
Test API endpoints using
curl
:curl -X POST http://<your-vps-ip>/api/workflow \ -H "Content-Type: application/json" \ -d '{"subscriptionId": "your-subscription-id"}'
- Use
git pull
or re-upload files to update your code. - Install dependencies with
npm install
if necessary. - Restart your app with PM2:
pm2 restart subscription-tracker
. - Test locally and publicly to ensure the update is applied.
Let me know if you need help with any specific step!