LoginSignup
3
2

More than 5 years have passed since last update.

WordPressのローカル開発をPHPのビルトインサーバーでやる場合

Posted at

みんなローカル環境でWordPressの開発をするときどうやってるんですかね。
個人的にPHPのビルトインサーバーをよく使うので、WordPressもやってみたいなと思ってメモ。
(わざわざ Vagrant とか使わず手軽にやりたかっただけ)

ちなみに、当然ローカルにPHPとMySQLはインストールされている必要あり。

まずサイトのURLを強制

環境ごとにサイトURLを設定しなおすのが面倒なので wp-config.php で強制してしまう。
書く場所は require_once(ABSPATH . 'wp-settings.php'); より前ならどこでも良いと思う
詳しくは wp-config.php の編集 などを。

wp-config.php
// localhostならサイトURLを強制
if (0 === strpos($_SERVER['HTTP_HOST'], 'localhost')) {
    define('WP_SITEURL', 'http://'.$_SERVER['HTTP_HOST']);
    define('WP_HOME', 'http://'.$_SERVER['HTTP_HOST']);
}

簡易ルーターを作成

Phalcon の .htrouter.php のような、自前のURLリライト処理を用意。

.htrouter.php
<?php
$root = $_SERVER['DOCUMENT_ROOT'];

$path = parse_url($_SERVER['REQUEST_URI']);
$path = '/'.ltrim($path['path'], '/');

if(is_file($root.$path)) {
    if(is_dir($root.$path) && '/' !== substr($path, strlen($path) - 1, 1)) {
        $path = rtrim($path,'/').'/index.php';
    }

    if(false !== strpos($path, '.php')) {
        require_once($root.$path);
    } else {
        return false;
    }
}else {
    include_once('index.php');
}

ビルトインサーバー起動

起動時に簡易ルーターを通すように指定する。

$ php -S localhost:8080 -t /path/to/wordpress .htrouter.php

これで一応は動くけど、.htaccessを書き換えるようなプラグインとかは無理。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2