STAGE 9 · Lv.49
마이그레이션과 스테이징 환경
WP Migrate DB, All-in-One Migration을 이용한 사이트 이전, search-replace의 원리, 스테이징 환경과 로컬 개발 환경(LocalWP, DDEV) 설정을 배웁니다.
마이그레이션이란?
WordPress 사이트 마이그레이션의 개념
마이그레이션(Migration)은 WordPress 사이트를 하나의 환경에서 다른 환경으로 이전하는 작업입니다. 단순한 파일 복사가 아니라, 데이터베이스의 URL 참조까지 모두 변환해야 하는 복잡한 과정입니다.
마이그레이션이 필요한 상황:
- 호스팅 업체 변경 (예: A호스팅 → B호스팅)
- 도메인 변경 (예: old-site.com → new-site.com)
- HTTP → HTTPS 전환
- 서브디렉토리 → 루트 이전 (예: /blog/ → /)
- 로컬 개발 → 라이브 서버 배포
- 라이브 → 스테이징 환경 복제
- 서버 업그레이드 (PHP, MySQL 버전 변경)
마이그레이션의 핵심 과제: WordPress 데이터베이스에는 사이트 URL이 수백 곳에 저장되어 있습니다. wp_options의 siteurl과 home뿐만 아니라, 모든 이미지 경로, 내부 링크, 플러그인 설정 등에 URL이 포함됩니다. 특히 직렬화(serialized)된 데이터 안의 URL은 단순한 문자열 치환으로 변경하면 데이터가 손상됩니다.
직렬화 데이터와 search-replace
PHP 직렬화 데이터는 문자열 길이를 포함합니다. 예:
s:24:"https://old-domain.com/a"에서 24는 문자열 길이입니다. 도메인이 바뀌면 길이도 달라지므로, 단순 SQL의 REPLACE()를 사용하면 직렬화 데이터가 깨집니다. 반드시 WP-CLI의 search-replace나 전용 도구를 사용해야 합니다.파일 복사
(FTP/SCP)
(FTP/SCP)
→
DB 이전
(Export/Import)
(Export/Import)
→
URL 치환
(Search-Replace)
(Search-Replace)
→
설정 수정
(wp-config)
(wp-config)
→
테스트
및 확인
및 확인
WP-CLI를 이용한 수동 마이그레이션
가장 정확하고 유연한 방법
WP-CLI의 search-replace 명령은 직렬화된 데이터를 안전하게 처리하면서 URL을 치환합니다. 서버에 SSH 접근이 가능하다면 이 방법이 가장 권장됩니다.
# ===== 소스 서버에서 내보내기 =====
# 1. 데이터베이스 내보내기
wp db export /tmp/site_backup.sql --path=/var/www/html
# 2. 파일 압축
tar czf /tmp/site_files.tar.gz -C /var/www/html \
wp-content/uploads \
wp-content/themes \
wp-content/plugins \
wp-config.php
# 3. 대상 서버로 전송
scp /tmp/site_backup.sql user@new-server:/tmp/
scp /tmp/site_files.tar.gz user@new-server:/tmp/
# ===== 대상 서버에서 가져오기 =====
# 4. WordPress 코어 설치 (신규 서버인 경우)
wp core download --path=/var/www/html --locale=ko_KR
# 5. wp-config.php 설정 (새 DB 정보)
wp config create \
--dbname=new_database \
--dbuser=new_user \
--dbpass=new_password \
--dbhost=localhost \
--path=/var/www/html
# 6. 데이터베이스 생성 및 가져오기
wp db create --path=/var/www/html
wp db import /tmp/site_backup.sql --path=/var/www/html
# 7. 파일 복원
cd /var/www/html
tar xzf /tmp/site_files.tar.gz
# 8. URL 치환 (핵심!)
wp search-replace 'https://old-domain.com' 'https://new-domain.com' \
--all-tables \
--path=/var/www/html
# 경로도 치환 (서버 경로가 다른 경우)
wp search-replace '/home/olduser/public_html' '/var/www/html' \
--all-tables \
--path=/var/www/html
# 9. 퍼머링크 갱신
wp rewrite flush --path=/var/www/html
# 10. 캐시 초기화
wp cache flush --path=/var/www/html
# 11. 파일 권한 설정
chown -R www-data:www-data /var/www/html/wp-content/
find /var/www/html/ -type d -exec chmod 755 {} \;
find /var/www/html/ -type f -exec chmod 644 {} \;
search-replace 고급 옵션
WP-CLI search-replace 명령의 다양한 옵션을 활용하면 더 정밀한 치환이 가능합니다.
# 드라이런 - 실제 변경 없이 미리 확인
wp search-replace 'old-domain.com' 'new-domain.com' \
--all-tables --dry-run --path=/var/www/html
# 출력 예시:
# Table Column Replacements Type
# wp_options option_value 15 PHP
# wp_posts post_content 234 SQL
# wp_posts guid 150 SQL
# wp_postmeta meta_value 89 PHP
# ...
# Total replacements: 488
# 특정 테이블만 치환
wp search-replace 'old-domain.com' 'new-domain.com' \
wp_posts wp_postmeta wp_options --path=/var/www/html
# GUID 제외 (RSS 피드 호환성 유지)
wp search-replace 'old-domain.com' 'new-domain.com' \
--all-tables --skip-columns=guid --path=/var/www/html
# 네트워크 전체 (멀티사이트)
wp search-replace 'old-domain.com' 'new-domain.com' \
--all-tables --network --path=/var/www/html
# 대소문자 구분 없이 치환
wp search-replace 'Old-Domain.com' 'new-domain.com' \
--all-tables --regex --regex-flags=i --path=/var/www/html
# HTTP → HTTPS 전환
wp search-replace 'http://example.com' 'https://example.com' \
--all-tables --path=/var/www/html
# www 제거
wp search-replace 'https://www.example.com' 'https://example.com' \
--all-tables --path=/var/www/html
🔒
여기까지는 미리보기입니다
마이그레이션과 스테이징 환경
무료 가입하면 이어서 볼 수 있고, 강의를 완료할 때마다 XP와 레벨이 쌓입니다.
Google로 3초 만에 시작 →🧵 Threads로 시작무료 공개 강의 둘러보기 (Lv.1~3)무료 가입하면 이어서 볼 수 있고, 강의를 완료할 때마다 XP와 레벨이 쌓입니다.