如何在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:
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:
Then reload your shell, install a stable Node version, and set it as default:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashnvm install --lts nvm alias default lts/*
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
Make sure your app is configured to work with the Apache proxy:
- Listen on
localhost(not0.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'); });
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:
Follow the on-screen command to save the startup script, then run:pm2 startup centospm2 save
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.
- 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
- If Socket.io fails to connect, check Apache’s error logs at
/var/log/httpd/error_log. - Ensure
mod_proxy_wstunnelis properly enabled—this is often the culprit for WebSocket issues. - Double-check that your Socket.io client is connecting to
/socket.ioon your domain, not directly to port 3000. Example client code:const socket = io('http://yourdomain.com', { path: '/socket.io' });
内容的提问来源于stack exchange,提问作者Ashish Rawat

