Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bainternet/bc5f9077af0f544f86e5604143d0e9fd to your computer and use it in GitHub Desktop.
Save bainternet/bc5f9077af0f544f86e5604143d0e9fd to your computer and use it in GitHub Desktop.
Using PHP's built in server for WordPress development.

Preface

So in the past, I've used MAMP/MAMP Pro apps and others alike. I've also, setup my own local MAMP stack with homebrew, that used dnsmasq to dynamically add vhosts anytime I added a new folder to the Sites folder which made it very convenient. But, then I started running into other environment issues with PHP versions on remote machines/servers not being updated or some other crazy thingamabob breaking. I researched and invested time in Vagrant, but that seem to break more often than my homebrew setup. So I researched again investing time into Docker via Docker for Mac (which is BAMF), which I'm sold on and use daily, but it still seems a little bleeding edge and not so simple to wrap your head around the concept. Not to mention I don't have a real use case to play with and take advantage of all the features that come packed with Docker.

This is the beginning of trying to find something more simple, and slightly quicker to setup.

===

This site is the first attempt at using PHP's built in server + mysql with WordPress

Setup

  1. Setup the wp-config.php like usaual -- should be straight forward, even if you use a local-wp-config.php file
  2. cd YOUR_WORDPRESS_PROJECT
  3. Start mysql - mysql.server start
  4. Then run php -S http://localhost:4000 (note: you can change the port to whatever as long as it doesn't conflict with mysql or anything else you might have running on localhost)

Extra Tidbits

Add Colors to your terminal logs from the PHP server output just copy and paste the command below:

  • mkdir ~/.php && echo 'cli_server.color = on' >> ~/.php/php-cli-server.ini if you want to know more about this file --> read the php docs

It might be wise to add a router.php to the root of your projet directory.

<?php

$root = $_SERVER['DOCUMENT_ROOT'];
chdir($root);
$path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
set_include_path(get_include_path().':'.__DIR__);
if(file_exists($root.$path))
{
  if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/')
    $path = rtrim($path,'/').'/index.php';
  if(strpos($path,'.php') === false) return false;
  else {
    chdir(dirname($root.$path));
    require_once $root.$path;
  }
}else include_once 'index.php';

// The just run --> php -S localhost:4000 router.php
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
chdir($root);
$path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
set_include_path(get_include_path().':'.__DIR__);
if(file_exists($root.$path))
{
if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/')
$path = rtrim($path,'/').'/index.php';
if(strpos($path,'.php') === false) return false;
else {
chdir(dirname($root.$path));
require_once $root.$path;
}
}else include_once 'index.php';
// php -S localhost:4000 router.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment