How to add a context menu shortcut to .php files, so that a PHP server is run there. Tested on Windows 10.
Versions 5.4+ have a built-in web server.
Download PHP from windows.php.net.
Create a file run_php_server.bat:
cd "%1"
CALL "C:\Program Files\PHP\php-7.2.34-Win32-VC15-x64\php.exe" -S 127.0.0.1:8000
pauseModify the location if it doesn't match yours.
Save the above script in some good location – in my case that would be C:\Program Files\PHP.
- Open Registry Editor (Start ->
regedit-> Enter). - Add a new key (will be shown as a folder) at
HKEY_CLASSES_ROOT\SystemFileAssociations\.php\shell\PHP server here\command. If something doesn't exist along the path, create it. - In
command, double-click the default entry and set its value to"C:\Program Files\PHP\run_php_server.bat" "%1". - You can now close Registry Editor, or wait until you test it in case something went wrong.
Right-click an index.php file. A context menu entry "PHP server here" should appear, and run a command line window when clicked.
Navigate to 127.0.0.1:8000 in your browser. It should work with your index file.
To run the PHP server via AHK, the working directory has to be changed using pushd instead of cd.
Create a file run_php_server_at_given_directory.bat:
pushd "%1"
CALL "C:\Program Files\PHP\php-7.2.34-Win32-VC15-x64\php.exe" -S 127.0.0.1:8000
pauseThen, in your AHK script, add a Run call with path to the above batch file and path to the desired working directory (use a directory, don't point directly to the PHP file!):
^+p::Run "C:\Program Files\PHP\run_php_server_at_given_directory.bat" D:/path/to/working/directory