WordPress Development Environment
Posted by Paul on 28th February 2017
Here’s a little detail about the development environment setup that I use whenever I need to develop a WordPress theme or plugin. My aim is to somewhat isolate each project (sadly, not quite as effectively as Python’s VirtualEnv) and keep it from impacting more general workstation setup such as configs under /etc/
.
Run PHP in Isolation
No need to set up a new virtual web server for each site. Use PHP’s CLI and a router…
Get and unzip the latest version of WordPress and rename as necessary for your new project:
$ wget https://wordpress.org/latest.tar.gz $ tar xzvf latest.tar.gz $ mv wordpress newproject $ cd newproject $ mv wp-config-sample.php wp-config.php
Ensure the PHP command-line is installed:
$ sudo apt-get install php7.0-cli
Create a router.php
file in the root directory (we named ours ‘newproject’, above) of the WordPress install:
<?php $root = $_SERVER['DOCUMENT_ROOT']; chdir($root); $path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'] )['path'],'/'); if (file_exists($root.$path)) { if (is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/') { header('Location: '.rtrim( $path,'/' ).'/'); exit; } if (strpos($path,'.php') === false) { return false; } else { chdir(dirname($root.$path)); require_once $root.$path; } } else { include_once 'index.php'; }
From within the root directory of the WordPress installation run PHP’s built-in server from the command-line, passing the name of our router.php
file as an argument:
$ php -S localhost:8080 router.php
PHP will then pass all HTTP requests through the router, which either, redirects, passes control to WordPress or serves up static assets as necessary.
From a browser access the newly created project (http://localhost:8080) and the familiar WordPress installation routine should be presented.
If you’d additionally like to avoid setting up a database instance for your new dev project, then read on…
Disposable Database Setup
Get latest version of WordPress SQLite plugin and extract inside the plugins directory:
$ cd wp-content/plugins $ wget https://downloads.wordpress.org/plugin/sqlite-integration.zip $ unzip sqlite-integration.zip
Copy db.php
from the sqlite-integration plugin directory into the wp-content
directory:
$ cp sqlite-integration/db.php ../.
Ensure php7.0-sqlite3
and sqlite3
are both installed:
$ sudo apt-get install sqlite3 php7.0-sqlite3
You should now be able to run the PHP built-in webserver, as described above, but now your WordPress data will be persisted to an SQLite file, wp-content/database/.ht.sqlite
.