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

Node.js网站搭建与公网发布咨询:已建站域名部署求助

How to Deploy Your Node.js App to the Public Web (With a Custom Domain)

Hey there! Since you’ve got PHP deployment under your belt, you’ll find Node.js deployment shares a lot of the same foundational steps—we just need to adjust for how Node apps run. Let’s walk through this from start to finish:

1. Get a Public Server

First, you’ll need a cloud server/VPS (like the ones you’ve probably used for PHP sites). Pick one with a Linux distro (Ubuntu is a solid, beginner-friendly choice) and note down its public IP address.

2. Upload Your Node.js Code to the Server

Use whatever method you prefer for PHP deployments:

  • Git: Initialize a repo for your project, push it to a private repo (or public if you don’t mind), then clone it onto your server with git clone your-repo-url.
  • SFTP: Use an app like FileZilla to drag-and-drop your project files to a directory on the server (e.g., /var/www/your-node-app).

3. Install Node.js & npm on the Server

Your server probably doesn’t have Node pre-installed. For Ubuntu/Debian:

# Add NodeSource repository for a recent Node version (e.g., v20)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
# Install Node.js and npm
sudo apt-get install -y nodejs

Verify it worked with node -v and npm -v.

4. Install Dependencies & Test the App

Navigate to your project directory:

cd /var/www/your-node-app
npm install

Test if the app runs locally on the server with node app.js (replace app.js with your entry file, like server.js). You should see a message saying it’s listening on a port (usually 3000). Hit Ctrl+C to stop it—we don’t want to run it this way long-term.

5. Use a Process Manager to Keep the App Running

Unlike PHP (which runs via Nginx/Apache), Node apps run as persistent processes. PM2 is the most popular tool for this:

# Install PM2 globally
npm install pm2 -g
# Start your app
pm2 start app.js --name "your-node-app"
# Set PM2 to start on server boot
pm2 startup
# Save your current PM2 process list
pm2 save

Check the status with pm2 status—your app should show as "online".

6. Configure Nginx as a Reverse Proxy

Since you know PHP, you’re already familiar with Nginx. We’ll use it to route traffic from your domain to your Node app’s port (3000):

  1. Create a new Nginx config file:
    sudo nano /etc/nginx/sites-available/your-domain.com
    
  2. Paste this config (replace your-domain.com with your actual domain and 3000 with your app’s port):
    server {
        listen 80;
        server_name your-domain.com www.your-domain.com;
    
        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_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_cache_bypass $http_upgrade;
        }
    }
    
  3. Enable the config and restart Nginx:
    sudo ln -s /etc/nginx/sites-available/your-domain.com /etc/nginx/sites-enabled/
    sudo nginx -t  # Check for config errors
    sudo systemctl restart nginx
    

7. Point Your Domain to the Server

Go to your domain registrar’s DNS settings and create an A record:

  • Host: @ (for your root domain) and www (for the www subdomain)
  • Value: Your server’s public IP address
  • TTL: Set to 5 minutes (so changes take effect quickly)

Wait a few minutes for DNS propagation, then try visiting your domain—you should see your Node app!

Use Let’s Encrypt to get a free SSL certificate. For Nginx:

# Install Certbot
sudo apt install certbot python3-certbot-nginx
# Auto-configure SSL for your domain
sudo certbot --nginx -d your-domain.com -d www.your-domain.com

Follow the prompts, and Certbot will automatically update your Nginx config to use HTTPS.

Critical Note for Local vs. Public Running

Make sure your Node app listens on 0.0.0.0 instead of localhost in your code. Otherwise, it’ll only be accessible from the server itself, not the public web. Your listen code should look like this:

app.listen(3000, '0.0.0.0', () => {
  console.log('App running on port 3000!');
});

That’s it! Most of this should feel familiar from your PHP deployments—just the Node-specific bits (PM2, listening on 0.0.0.0) are the new pieces. Let me know if you hit any snags!

内容的提问来源于stack exchange,提问作者Sukran Eken

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:27:09