如何在Heroku多构建包PHP+Node.js环境中启动Express服务器
Got it, let's walk through how to get your Express/Socket.IO server up and running alongside your PHP app on Heroku—here's what you need to do:
1. Make Sure Heroku Installs Node Dependencies
Heroku's PHP buildpack automatically detects a package.json in your root folder and runs npm install during deployment, so you don't need to switch buildpacks. Just double-check that express and socket.io are listed in your package.json dependencies.
2. Fix the Startup Process (The Big Challenge)
The core issue is running both the PHP web server and your Express/Socket.IO server on Heroku. Here are two reliable ways to pull this off:
Option 1: Use a Process Manager to Run Both Servers
We'll use concurrently (a tool that runs multiple commands at once) to launch both servers in a single dyno.
First, install
concurrentlyas a dev dependency:npm install --save-dev concurrentlyUpdate your
package.jsonscripts to add separate commands for each server, plus a combined start command. Assuming your Express entry file isserve.jsand you're using Heroku's default PHP server command (heroku-php-apache2):"scripts": { "dev": "cross-env NODE_ENV=development webpack --config config/config.dev.js", "build": "cross-env NODE_ENV=production webpack --config config/config.prod.js", "start-node": "node serve.js", "start-php": "heroku-php-apache2", "start": "concurrently \"npm run start-php\" \"npm run start-node\"" }Create a
Procfilein your root directory to tell Heroku to use this combined start command:web: npm start
Option 2: Reverse Proxy Socket.IO Requests via Apache
If you want to keep Apache as your main web server, you can configure it to forward all /socket.io traffic to your Express server.
First, set your Express server to listen on an internal port (like 3001—since Heroku only exposes one public port per dyno). Update
serve.js: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: "https://your-app-name.herokuapp.com", // Replace with your actual Heroku app URL methods: ["GET", "POST"] } }); // Add your Socket.IO logic here... server.listen(3001, () => { console.log('Socket.IO server running on internal port 3001'); });Create an
.htaccessfile in your PHP app's public directory (where Apache serves files) with reverse proxy rules:RewriteEngine On # Forward socket.io requests to the Express server RewriteRule ^socket.io/(.*)$ http://localhost:3001/socket.io/$1 [P,L]Then use the same
concurrentlysetup from Option 1 to launch both servers via yourProcfile.
3. Configure CORS for Socket.IO
Your PHP app will connect to the Express server, so make sure Socket.IO allows cross-origin requests. As shown above, set the origin in your Socket.IO config to your Heroku app's URL (use * for local testing, but always use your actual production URL).
4. Test Locally First
Before deploying, test everything on your machine:
- Run
npm startto launch both servers - Load your PHP app and verify it works
- Test your real-time feature to ensure Socket.IO connects properly
5. Deploy to Heroku
Commit all your changes (including package.json, Procfile, and .htaccess if you used Option 2), then push to Heroku:
git add . git commit -m "Add Socket.IO server setup for real-time updates" git push heroku main
Check the logs to confirm both servers are running:
heroku logs --tail
That's it! Your PHP app will handle regular HTTP requests, and the Express/Socket.IO server will take care of real-time data updates.
内容的提问来源于stack exchange,提问作者vikmalhotra

