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

如何在Heroku多构建包PHP+Node.js环境中启动Express服务器

Running Express/Socket.IO alongside a PHP App on Heroku

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 concurrently as a dev dependency:

    npm install --save-dev concurrently
    
  • Update your package.json scripts to add separate commands for each server, plus a combined start command. Assuming your Express entry file is serve.js and 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 Procfile in 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 .htaccess file 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 concurrently setup from Option 1 to launch both servers via your Procfile.

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 start to 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 10:17:40