How to Package PHP with Electron

To package a PHP application with Electron, you need to

bundle a local PHP runtime and server within your Electron app and manage it using Node.js scripts. The most common approach involves using community tools and libraries to handle the complexities of starting/stopping the PHP server and packaging the executables. 

For Laravel projects, the NativePHP framework provides a streamlined, official solution. For other PHP applications, you can use general-purpose libraries and tools. 
Option 1: Using NativePHP (For Laravel Applications) 
NativePHP is specifically designed to bridge Laravel and Electron (or Tauri), handling the complexities of embedding a PHP interpreter and running a local server automatically. 
  1. Install NativePHP via Composer in your existing Laravel project:
    bash
  • composer require nativephp/electron
    
  • Run development servers: During development, you’ll run both the PHP and UI development servers:
    bash
  • php artisan native:serve & npm run dev &
    
  • Configure and build for release: NativePHP uses configuration files (e.g., config/nativephp.php) to manage the build process. To create a distributable, you can use the built-in Artisan command, which leverages tools like Electron Forge or Builder under the hood. 
Option 2: Manual Setup with Libraries (For Other PHP Applications)
For a non-Laravel PHP project, you can integrate a local PHP server manager into your Electron setup. 
Step 1: Set up your Electron project 
  1. Initialize a Node.js project and install Electron:
    bash
  • npm init -y
    npm install electron --save-dev
    
  • Create a main.js file (the main process script) and a package.json file for your app’s main process and details. 
Step 2: Include a PHP Server Manager 
Use a library like php-server-manager to control a PHP development server from your Electron’s main process. 
  1. Install the library:
    bash
  • npm install php-server-manager --save-dev
    
  • In your main.js file, add code to start the PHP server before opening the Electron window, and close it when the app quits: 
javascript
const { app, BrowserWindow } = require('electron');
const PHPServer = require('php-server-manager');

const server = new PHPServer({
    port: 8000,
    directory: __dirname + '/public_html' // Directory where your PHP files are
});

function createWindow() {
    server.run(); // Start the PHP server
    const mainWindow = new BrowserWindow({
        width: 1024,
        height: 768,
        webPreferences: {
            nodeIntegration: false
        }
    });

    // Load the URL from the local PHP server
    mainWindow.loadURL(`http://${server.host}:${server.port}/index.php`);

    mainWindow.on('closed', function () {
        server.close(); // Stop the PHP server when the window is closed
        mainWindow = null;
    });
}

app.on('ready', createWindow);
// ... other app lifecycle events
Step 3: Package the Application for Distribution 
Once your app runs in development mode, use a packaging tool like Electron Forge or electron-builder to create distributable files (e.g., .exe, .dmg, .deb). 
  1. Install Electron Forge:
    bash
  • npm install --save-dev @electron-forge/cli
    npx electron-forge import
    
  • The import command adds necessary scripts to your package.json. You can then run the make script to generate a distributable:
    bash
npm run make

This command packages your code with the Electron binary and the embedded PHP runtime, creating a standalone application that can be distributed to users without requiring them to install PHP manually. 

Leave a Reply

Your email address will not be published. Required fields are marked *