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

如何在Centos系统Bluehost VPS上同时运行Apache与Node.js服务

Got it, let's get your Apache + Node.js (Socket.io) setup running smoothly on your CentOS Bluehost VPS. I’ve tackled this exact scenario a handful of times, so here’s a step-by-step breakdown that should get you sorted:

1. First, Confirm & Install Required Software

First things first—make sure your VPS has Apache and Node.js up and running:

  • Apache: If it’s not already installed, run:
    sudo yum install httpd -y
    sudo systemctl start httpd
    sudo systemctl enable httpd
    
  • Node.js: I recommend using NVM (Node Version Manager) for easy version control. Install it with the standard curl command from the official NVM setup:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
    
    Then reload your shell, install a stable Node version, and set it as default:
    nvm install --lts
    nvm alias default lts/*
    
2. Configure Apache to Proxy Socket.io Traffic

Since Apache listens on ports 80/443 (standard web ports) and your Node.js app will use a different port (like 3000), you’ll need a reverse proxy to route Socket.io requests to Node.js. This keeps your PHP app running on the same domain without conflicts.

Step 2.1: Enable Required Apache Modules

Socket.io uses WebSockets, so you’ll need mod_proxy, mod_proxy_http, and mod_proxy_wstunnel. Enable them by editing your Apache config (usually /etc/httpd/conf/httpd.conf) and adding these lines if they’re not already present:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

Step 2.2: Create a Virtual Host Config

Create a new config file for your domain (e.g., /etc/httpd/conf.d/yourdomain.conf) with this setup:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    # Point to your PHP app's root directory
    DocumentRoot /var/www/html/your-php-project

    # Proxy Socket.io HTTP requests to Node.js port 3000
    ProxyPass /socket.io http://localhost:3000/socket.io
    ProxyPassReverse /socket.io http://localhost:3000/socket.io

    # Proxy WebSocket traffic for Socket.io
    ProxyPass /socket.io/ws ws://localhost:3000/socket.io/ws
    ProxyPassReverse /socket.io/ws ws://localhost:3000/socket.io/ws

    # Allow .htaccess overrides for your PHP app (if needed)
    <Directory /var/www/html/your-php-project>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Replace yourdomain.com and /var/www/html/your-php-project with your actual domain and PHP app path.

Step 2.3: Restart Apache

Apply the changes with:

sudo systemctl restart httpd
3. Adjust Your Node.js Socket.io App

Make sure your app is configured to work with the Apache proxy:

  • Listen on localhost (not 0.0.0.0—this keeps it secure by only allowing local connections via Apache)
  • Set the correct Socket.io path and CORS settings to match your domain

Here’s a minimal example of your Node.js app code:

const express = require('express');
const http = require('http');
const { Server } = require('socket.io');

const app = express();
const server = http.createServer(app);

const io = new Server(server, {
  cors: {
    origin: "http://yourdomain.com", // Use your actual domain here
    methods: ["GET", "POST"]
  },
  path: "/socket.io" // Must match the proxy path in Apache
});

// Your Socket.io logic goes here
io.on('connection', (socket) => {
  console.log('A user connected via Socket.io!');
  // Add chat events, message handling, etc.
});

// Listen on localhost:3000
server.listen(3000, 'localhost', () => {
  console.log('Node.js chat app running on localhost:3000');
});
4. Keep Your Node.js App Running Permanently

If you close your SSH session, your Node.js app will stop. Use PM2 (a process manager) to keep it running in the background:

  • Install PM2:
    npm install pm2 -g
    
  • Start your app with PM2:
    pm2 start your-app-file.js --name "chat-app"
    
  • Set PM2 to start on boot:
    pm2 startup centos
    
    Follow the on-screen command to save the startup script, then run:
    pm2 save
    
5. Firewall Configuration

CentOS uses firewalld by default—make sure it allows traffic to Apache’s ports (80/443):

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload

You don’t need to open port 3000 to the public since Apache is proxying traffic to it locally.

6. Test Everything
  • Visit your domain in a browser—your PHP app should load normally.
  • Open your chat app and check the browser console for Socket.io connection errors.
  • Check PM2 logs to confirm connections:
    pm2 logs chat-app
    
Troubleshooting Quick Wins
  • If Socket.io fails to connect, check Apache’s error logs at /var/log/httpd/error_log.
  • Ensure mod_proxy_wstunnel is properly enabled—this is often the culprit for WebSocket issues.
  • Double-check that your Socket.io client is connecting to /socket.io on your domain, not directly to port 3000. Example client code:
    const socket = io('http://yourdomain.com', { path: '/socket.io' });
    

内容的提问来源于stack exchange,提问作者Ashish Rawat

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:43:04