Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Running a Standalone PHP App in Windows

5.00/5 (3 votes)
19 Mar 2023CPOL2 min read 17.5K  
Portable nginx+php+mysql inside Windows
Run a portable PHP app in Windows

Introduction

Various tools for "portable" installations of a PHP stack in Windows exist. All of them require some manual download, interaction, start/stop, etc. Here's a static library you can link to your application to run PHP code in Windows.

The library uses ZipUtils to manipulate the ZIP archives.

The Stack

The library will configure:

  • Nginx, the fine web server. The library includes version 1.23.3. I got it from https://nginx.org/en/download.html.
  • PHP, currently 8.2.2 I got the "Thread Safe" zip from here.
  • MariaDB MySQL server, currently 10.10.2. I got it from here.

By removing the definitions for NEED_PHP and NEED_MDB, you may remove PHP and MySQL if you don't need it.

Using the Code

C++
//
    RUNWW w;
    w.hIcon = LoadIcon(h, L"ICON_1");
    w.nginx = { nginx.data(),nginx.size() };
    w.root = { f.data(),f.size() };
    w.php = { php.data(),php.size() };
    w.mdb = { mdb.data(),mdb.size() };
    w.PHPPort = 0;
    w.NginxPort = 0;
    w.MDBPort = 51000; 
    
    w.DataFolder = L"c:\\ww_data";
    w.WhereAt = L"c:\\ww_apps";

    RunWW(w);
//

You need to pass the zip memory for nginx, php, mariaDB and the root folder of your files. Also, you can pass zero for PHP and Nginx port. For MySQL, you can also pass 0, but then you would have to rewrite your PHP script from within the executable to connect to the server.

The WhereAt folder is where to put the binaries for the applications. In the above example, you have installations for:

  • nginx, in c:\ww_apps\nginx
  • PHP, in c:\ww_apps\php
  • MariaDB, in c:\ww_apps\mdb

nginx.conf is recreated by the library inside the nginx/conf folder to match the server name, port and PHP parameters.

php.ini is recreated by the library to load most common PHP extensions.

The DataFolder folder is where to put:

  • The root folder of your application PPH files (in this example, in c:\ww_data\html).
  • The MySQL data folder (c:\ww_data\mdb)

You can pass an existing folder as DataFolder and no zip to unpack. In that case, the library uses the existing installation folder (which contains folders "html" for the HTML nginx PHP code and "mdb" for the MySQL).

In addition, the library automatically creates a database "db1" in the MySQL data folder, you may change that to whatever you want. You can also create a new user (instead of the default "root").

Finally, RunWW() runs the server and creates an icon in the taskbar while also launching the browser with the root URL of your project. When the user right clicks to the tray icon and selects exit, this function returns.

When this function returns, your application should preserve the data folder (i.e., by zipping it to an archive, or otherwise keeping it) until next usage. The WhereAt folder can be recreated at any time.

The Test Code

The test solution utilizes this library to run a single index.php, which creates a SQLite3 database and also connects to MySQL and creates a table.

The library includes xdebug. This is installed as a PHP extension and configured to connect as callback when xdebug_break() is called to localhost:9003 (check launch.json and php.cpp to modify that). If you start VSCode with the Test's data folder and launch xdebug, the default index.php will break.

History

  • 6th February, 2023 - First release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)