You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IONOS VPS部署Express.js网站无法访问,PORT环境变量未定义求助

Fixing Express.js Site Access Issues on IONOS VPS

Hey there! Let's break down how to get your Express site accessible on your new IONOS VPS—since you're new to VPS, we'll take it step by step to avoid missing anything.

1. Fix the process.env.PORT Undefined Issue

The root cause here is likely that IONOS doesn't automatically set a PORT environment variable for your VPS, unlike some PaaS platforms (like Heroku). Your local environment might have had this set, or you were relying on Express's default behavior, but we need to explicitly define a fallback.

Update your Express code to use:

const port = process.env.PORT || 3000; // Use env PORT if available, else 3000
app.listen(port, '0.0.0.0', () => {
  console.log(`Server running on port ${port}`);
});

Notice the '0.0.0.0'—this tells Express to listen on all network interfaces (not just localhost, which only allows server-internal access). This is critical for external visitors to reach your app.

2. Verify Your App is Running & Listening

Log into your VPS via SSH and run these commands to check:

  • Check if your Node.js process is active:
    ps aux | grep node
    
    You should see your Express app listed here. If not, run node your-app.js manually to see if there are any error messages (like missing dependencies).
  • Check which port your app is listening on:
    netstat -tulpn | grep node
    # Or use ss if netstat isn't installed:
    ss -tulpn | grep node
    
    Look for a line showing 0.0.0.0:3000 (or whatever port you set) to confirm it's listening publicly.

3. Ensure Firewall Rules Allow Your Port

You mentioned opening 80, 22, 443—but if your Express app is using port 3000 (or another non-standard port), you need to allow that port too. IONOS VPS has two layers of firewalls to check:

Server-Level Firewall (e.g., UFW)

Most IONOS VPS use UFW by default. Check current rules:

ufw status

If you don't see a rule for your port (e.g., 3000), add it:

ufw allow 3000/tcp
ufw reload

IONOS Cloud Firewall (Control Panel)

Log into your IONOS account, navigate to your VPS, and find the Firewall section. Add an inbound rule allowing TCP traffic on your app's port (3000) from any source (0.0.0.0/0). Save the changes—these can take a minute to apply.

4. Test Access From the Server First

Before testing externally, verify your app works locally on the VPS:

curl http://localhost:3000
# Or test using your server's public IP:
curl http://[YOUR_SERVER_IP]:3000

If this returns your app's content, your app is working correctly, and the issue is with external network access (double-check firewall rules). If it fails, debug your Express app for errors.

5. (Optional) Use Port 80/443 for Standard HTTP/HTTPS

If you want visitors to access your site without typing a port (e.g., http://your-ip instead of http://your-ip:3000), you have two options:

  • Run Express as root to listen on port 80: Not recommended, since running Node as root poses security risks.
  • Set up Nginx as a reverse proxy (better practice):
    1. Install Nginx: sudo apt install nginx
    2. Create a config file in /etc/nginx/sites-available/express-app with:
      server {
          listen 80;
          server_name [YOUR_SERVER_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;
          }
      }
      
    3. Enable the config: sudo ln -s /etc/nginx/sites-available/express-app /etc/nginx/sites-enabled/
    4. Test Nginx config: sudo nginx -t
    5. Restart Nginx: sudo systemctl restart nginx
      Now visitors can access your site via http://your-ip without a port.

6. Keep Your App Running After SSH Disconnects

Right now, if you close your SSH session, your Node process will stop. Use a process manager like PM2 to keep it running:

  1. Install PM2: npm install -g pm2
  2. Start your app: pm2 start your-app.js
  3. Save PM2 process list so it starts on boot: pm2 save && pm2 startup

内容的提问来源于stack exchange,提问作者Joaquim Antonio Kapel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:14:48