请求协助:已有Amazon VPS账号,部署Node.js REST API遇问题
Hey there! Let’s walk through the most common pitfalls when deploying a Node.js REST API on an Amazon VPS (I’m assuming you’re using EC2 here) and how to fix them—since your API works locally, the issue is almost always related to server configuration or network setup.
First, make sure your Amazon VPS has Node.js and npm/yarn installed, and that the versions match what you’re using locally. Version mismatches can break dependencies or cause unexpected behavior.
- Run these commands on your server to check:
node -v npm -v - If versions don’t align, use
nvm(Node Version Manager) to install the exact version you need:
Restart your terminal, then install your target version:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashnvm install <your-local-node-version>
This is the #1 reason APIs fail to connect after deployment. Amazon EC2’s default security groups block most incoming traffic, and your server’s local firewall might do the same.
- EC2 Security Group: Head to the EC2 console, find your instance’s security group, and add an inbound rule allowing TCP traffic on your API’s port (e.g., 3000, 8080). For testing, set the source to
0.0.0.0/0(you can restrict it to your IP later for security). - Server Firewall: If you’re using
ufw(common on Ubuntu), open the port with:
Verify the rule is active withsudo ufw allow <your-api-port> sudo ufw enablesudo ufw status.
A lot of local code listens on localhost or 127.0.0.1, which only allows connections from the server itself. Update your code to listen on 0.0.0.0 to accept external requests:
app.listen(3000, '0.0.0.0', () => { console.log('API running on http://0.0.0.0:3000'); });
Your local .env file won’t magically transfer to the server. Make sure all required environment variables (database URLs, API keys, etc.) are set:
- Create a
.envfile on the server and paste your local variables, or set them temporarily with:export DB_CONNECTION_STRING="your-db-url" export PORT=3000 - To make variables persist after server restarts, add them to
~/.bashrcor~/.profile, then runsource ~/.bashrcto apply changes.
If you run node server.js directly in the terminal, closing the terminal will kill the process. Use a process manager like PM2 to keep it running:
- Install PM2 globally:
npm install pm2 -g - Start your API with a name for easy management:
pm2 start server.js --name "my-rest-api" - Check status and logs anytime:
pm2 status pm2 logs my-rest-api - Set PM2 to start on server boot:
Follow the generated command to enable auto-start.pm2 startup
If you’re still stuck, logs are your best friend. PM2 logs will show you exactly what’s failing—whether it’s a missing dependency, database connection error, or port conflict.
- If a port is already in use, find and kill the process:
lsof -i :<your-api-port> kill <pid-from-output>
Never upload your local node_modules folder to the server—native dependencies (like database drivers) are compiled for your local OS, not the server’s Linux environment. Instead:
- Upload your code (via Git, SCP, or FTP)
- Run
npm install(oryarn install) on the server to install OS-compatible dependencies.
Try these steps one by one—start with the security group and listening IP, since those are the most frequent culprits. If you hit a specific error message, share it here and I can help dig deeper!
内容的提问来源于stack exchange,提问作者Nguyen Duc Quy

