wordpress 로 사이트를 만들 일이 있어서 개발 환경을 만들다 보니 다음과 같은 고민이 있었다.

 

  1. devel, test, staging, production 환경에 맞게 각각 별도의 환경 구축. 단 환경을 구성할 때 수정이나 설정은 최소화
  2. 워드프레스 설치본과 플러그인, 테마를 손쉽게 다른 환경으로 디플로이할 수 있는 방안
  3. 워드프레스 데이타를 다른 환경으로 migration 

위 문제를 해결하기 위해 여러 툴과 매뉴얼을 찾아봤는데 보통 1,2 는 해결해 주지만 3번까지 해결해 주는 것은 많지 않았다.

찾아 보고 실행해 본 방법은 다음과 같다.

PHP Composer 로 배포 관리

http://roots.io/using-composer-with-wordpress/

PHP 의 의존성 관리툴인 Composer 를 이용하여 워드프레스와 플러그인, 테마를 관리하는 방법이다.

단점은 플러그인 이나 테마 설치/관리가 워드프레스의 일반적인 관리 방법과 다르다보니 익숙해져야 하고

워드프레스의 폴더 구조가 변경되는 문제가 있다. 또 생각만큼 잘 동작하지는 않았다.

 

Capistrano로 배포 관리

http://roots.io/screencasts/deploying-wordpress-with-capistrano/

ruby 로 개발된 app 을 디플로이하는 툴인 capistrano 로 하는 방법이다. 문제는 capistrano 에 익숙하지 않으므로 사용법부터 배워야 하는데 

그렇게 시간을 들이기 위한 동기 부여가 안 되므로 제외했다.

 

git + 환경 파일로 배포 관리

이것 저것 찾다가 http://abandon.ie/notebook/wordpress-configuration-for-multiple-environments 를 참고하여 만들었다.

최초 설정

  1. 워드프레스를 다운로드 한 후에 압축을 해제한다.

    wget http://wordpress.org/latest.tar.gz
    tar zxvf tar zxvf latest.tar.gz
    cd wordpress
    CODE
  2. 기존 wp-config.php 를 다음 내용으로 대치한다. WP_ENV 아파치 환경 변수에 따라 각각의 설정 파일을 로딩하게 된다. 아차피 웹서버를 사용하지 않는다면 apache_getenv 대신 PHP의 getenv 함수를 사용해도 될 것이다.

    <?php
    /**
     * The base configurations of the WordPress.
     *
     * This file is a custom version of the wp-config file for multiple
     * environment.
     * Inspired by https://github.com/Abban/WordPress-Config-Bootstrap
     *
     * @package WordPress
     */
    // determine PHP is running as standalone or apache module
    // if not set just assume PHP run with build-int web server. 
    if (!function_exists(apache_getenv)){
        define('WP_ENV', 'local');
    }
    else {
        //  If no environment is set default to local
        if (apache_getenv('WP_ENV') == false) {
            define('WP_ENV', 'local');
        } else {
            define('WP_ENV', apache_getenv ('WP_ENV'));
        }
    }
    /**#@-*/
    /**
     * You can have multiple installations in one database if you give each a unique
     * prefix. Only numbers, letters, and underscores please!
     */
    $table_prefix  = 'wp_';
    /**
     * WordPress Localized Language, defaults to English.
     *
     * Change this to localize WordPress. A corresponding MO file for the chosen
     * language must be installed to wp-content/languages. For example, install
     * de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
     * language support.
     */
    define('WPLANG', '');
    /**
     * For developers: WordPress debugging mode.
     *
     * Change this to true to enable the display of notices during development.
     * It is strongly recommended that plugin and theme developers use WP_DEBUG
     * in their development environments.
     */
    define('WP_DEBUG', false);
    /* That's all, stop editing! Happy blogging. */
    /** Absolute path to the WordPress directory. */
    if ( !defined('ABSPATH') )
    	define('ABSPATH', dirname(__FILE__) . '/');
    // load config for multiple environment
    $config_file = ABSPATH . 'wp-config.' . WP_ENV . '.php';
    file_exists($config_file) || die ("'$config_file' Not Found!");
    require_once($config_file);
    /** Sets up WordPress vars and included files. */
    require_once(ABSPATH . 'wp-settings.php');
    PHP
  3. 아파치 웹서버에 가상 호스트마다 WP_ENV 환경 변수를 설정해 준다.

  4. WP_ENV 환경 변수 값에 맞게 wp-config.${WP_ENV}.php 를 생성한다. devel 환경이라면 wp-config.devel.php 파일이다.

    wp-config.devel.php

    <?php
    /**
     * The configurations of the WordPress on development environment.
     *
     * @package WordPress
     */
    // ** MySQL settings - You can get this info from your web host ** //
    /** The name of the database for WordPress */
    define('DB_NAME', 'database_name_here');
    /** MySQL database username */
    define('DB_USER', 'username_here');
    /** MySQL database password */
    define('DB_PASSWORD', 'password_here');
    /** MySQL hostname */
    define('DB_HOST', 'localhost');
    /** Database Charset to use in creating database tables. */
    define('DB_CHARSET', 'utf8');
    /** The Database Collate type. Don't change this if in doubt. */
    define('DB_COLLATE', '');
    #define('WP_SITEURL', 'http://dev-wordpress.example.com/');
    #define('WP_HOME', 'http://dev-wordpress.example.com/');
    /**#@+
     * Authentication Unique Keys and Salts.
     *
     * Change these to different unique phrases!
     * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
     * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
     *
     * @since 2.6.0
     */
    define('AUTH_KEY',         'put your unique phrase here');
    define('SECURE_AUTH_KEY',  'put your unique phrase here');
    define('LOGGED_IN_KEY',    'put your unique phrase here');
    define('NONCE_KEY',        'put your unique phrase here');
    define('AUTH_SALT',        'put your unique phrase here');
    define('SECURE_AUTH_SALT', 'put your unique phrase here');
    define('LOGGED_IN_SALT',   'put your unique phrase here');
    define('NONCE_SALT',       'put your unique phrase here');
    // Turn on WP_DEBUG
    define('WP_DEBUG', true);
    ?>
    PHP
  5. 다음 내용을 .gitignore 파일로 만들고 저장소에 추가한다.

    # Created by http://www.gitignore.io
    
    ### WordPress ###
    *.log
    .htaccess
    sitemap.xml
    sitemap.xml.gz
    ###wp-config.php
    wp-content/advanced-cache.php
    wp-content/backup-db/
    wp-content/backups/
    wp-content/blogs.dir/
    wp-content/cache/
    wp-content/upgrade/
    wp-content/uploads/
    wp-content/wp-cache-config.php
    
    
    ### vim ###
    [._]*.s[a-w][a-z]
    [._]s[a-w][a-z]
    *.un~
    Session.vim
    .netrwhist
    *~
    CODE
  6. wp-config.* 파일과 설치본 워드프레스와 플러그인과 테마는 git 저장소에 넣어서 푸시한다.
  7. 플러그인과 테마 설치시 wp-content/plugins/ 과 wp-content/themes/ 테마 폴더를 git 저장소에 푸시한다.

다른 환경에 배포

  1. git pull 로 저장소 내용을 체크아웃한다.
  2. 디플로이 하려는 아파치 웹서버의 가상 호스트 설정에 WP_ENV  환경 변수 값을 추가한다. 

    conf/http.conf

    <VirtualHost *:80>
        SetEnv WP_ENV devel
        ServerName dev-wp.example.com
    
        DocumentRoot /var/www/dev-wordpress/
        ErrorLog logs/dev-wp-error_log
        CustomLog logs/dev-wp-access_log common
    </VirtualHost>
     
    <VirtualHost *:80>
        SetEnv WP_ENV test
        ServerName test-wp.example.com
    
        DocumentRoot /var/www/test-wordpress/
        ErrorLog logs/test-wp-error_log
        CustomLog logs/test-wp-access_log common
    </VirtualHost>
    CODE
  3. service httpd restart 로 재구동한다.
  4. 브라우저로 연결하여 정상 동작 여부를 확인한다.

 

PHP stanalone 환경

PHP 의 내장 웹서버를 사용하는 경우 WP_ENV 가 local 로 설정되므로 wp-config.local.php  파일을 수정하면 된다.

PHP -S localhost:8080
CODE

 

TODO

1, 2