LoginSignup
7
13

More than 5 years have passed since last update.

Wordpress をレンタルサーバーから GCP へ引っ越したまとめ

Last updated at Posted at 2019-03-17

さくらサーバーのレンタルサーバーで動かしていたワードプレスの個人ブログ swiswiswift.com を GCP に移行しました。
詰まった箇所が多かっため、忘備録としてまとめました。

VM インスタンスのセットアップその1

ssh-keygen で SSH 用の秘密鍵と公開鍵を作成します。

$ ssh-keygen -t rsa -b 4096 -C "google.account@gmail.com" -f id_rsa_kabigon

GCP でVM インスタンスを作成します。
ファイヤーウォールの設定で http と https を許可し、作成した公開鍵を登録します。

vm.png

秘密鍵を使って VM インスタンスに入ります。

$ ssh -i ./id_rsa_kabigon google.account@34.85.124.218

nginx のインストールと起動を行います。

$ sudo yum update
$ sudo yum install nginx
$ sudo systemctl enable nginx
$ sudo systemctl start nginx

ブラウザから VM インスタンスの外部IPを指定してアクセスすると nginx の初期画面が表示されます。

nginx.png

DNSの設定

お名前.com でドメインを取得します。今回はこの記事のために kabigon.xyz というドメインを取得しました。
Cloud DNS から取得したドメインと VM インスタンスのIPを紐付けます。

dns.png

kabigon.xyzwww.kabigon.xyz をVM インスタンスのIPを紐付けました。
お名前.comでドメインのネームサーバーに Cloud DNS を設定します。

スクリーンショット 2019-03-16 23.31.48.png

30分後にhttp://kabigon.xyz/http://www.kabigon.xyz/に アクセスして nginx の画面が出ていることを確認します。

Cloud SQLのセットアップ

WordPress の DB に Cloud SQL の MySQL を使います。
MySQL 5.7 のインスタンスを作成、初期パスワードの設定を行い、VMインスタンスのIP からのアクセスを許可します。
WordPress 用のデータベースも作成します。

create database wordpress;

VM インスタンスのセットアップその2

certbot で Let's Encrypt のSSL証明書を作成します。
今回はstandalone モードで作成するので、nginx を一度停止します。

$ sudo systemctl stop nginx
$ sudo yum -y install certbot
$ sudo certbot certonly --standalone -d kabigon.xyz --email google.account@gmail.com --agree-tos
$ sudo certbot certonly --standalone -d www.kabigon.xyz --email google.account@gmail.com --agree-tos
$ sudo systemctl start nginx

作成したSSL証明書は以下のコマンドで確認することができます。

$ sudo certbot certificates

Wordpress の Dockerイメージを使うため、 Docker をインストールします。

$ sudo yum install yum-utils
$ sudo yum install device-mapper-persistent-data
$ sudo yum install lvm2
$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
$ sudo yum install docker-ce
$ sudo yum install docker-ce-cli
$ sudo yum install containerd.io
$ sudo systemctl enable docker
$ sudo systemctl start docker

Wordpress を起動します。/html ディレクトリを永続化しました。

$ cd
$ mkdir wordpress
$ cd wordpress
$ mkdir html
$ sudo docker run -d -p 8080:80 -v $PWD/html:/var/www/html --restart=always wordpress:php7.3-apache
$ sudo docker ps

nginx のセットアップ

nginx.conf ファイルを修正します。

$ cd /etc/nginx
$ sudo vi nginx.conf
nginx.conf
user root;
worker_processes  4;

events {
    worker_connections 1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

kabigon.conf ファイルを作成します。
http://kabigon.xyz/http://www.kabigon.xyz/https://www.kabigon.xyz/ へのアクセスをhttps://kabigon.xyz/ へリダイレクトさせています。

$ cd conf.d
$ sudo vi kabigon.conf
kabigon.conf
server {
    listen 80;
    listen [::]:80;
    server_name kabigon.xyz;
    return 301 https://kabigon.xyz$request_uri;
}

server {
    listen 80;
    listen [::]:80;
    server_name www.kabigon.xyz;
    return 301 https://kabigon.xyz$request_uri;
}

server {
    listen 443;
    server_name www.kabigon.xyz;
    ssl on;
    ssl_certificate      /etc/letsencrypt/live/www.kabigon.xyz/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.kabigon.xyz/privkey.pem;
    return 301 https://kabigon.xyz$request_uri;
}

server {
    listen 443;
    server_name kabigon.xyz;
    ssl on;
    ssl_certificate      /etc/letsencrypt/live/kabigon.xyz/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/kabigon.xyz/privkey.pem;
    location / {
            proxy_set_header Host $http_host;
            proxy_pass http://localhost:8080;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Forwarded-Proto https;
    }
}

SE Linux の設定を変更します。

$ sudo setsebool -P httpd_can_network_connect 1

nginx を再起動します。
conf ファイルに問題がなければ再起動します。

$ sudo systemctl restart nginx

最後に wp-config.php ファイルを作成します。 DBの設定、https の設定などを行っています。
https にすること、 <?php の前に空行が入らないことに注意してください。

$ cd
$ /wordpress/html
$ sudo vi wp-config.php
wp-config.php
<?php

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
    && $_SERVER['HTTP_X_FORWARDED_PROTO'] === "https") {
  $_SERVER['HTTPS'] = 'on';
}

define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

/** DBの設定 */
define('DB_NAME', 'wordpress');
define('DB_USER', 'root');
define('DB_PASSWORD', 'ROOT_PASSWORD');
define('DB_HOST', 'DB_HOST');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');


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');

$table_prefix  = 'wp_';
define('WPLANG', 'ja');
define('WP_DEBUG', false);

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');

/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

define( 'WP_HOME', 'https://kabigon.xyz' );
define( 'WP_SITEURL', 'https://kabigon.xyz' );

これで ブラウザから kabigon.xyz へアクセスすると Wordpress が表示されます。

レンタルサーバーからデータを移す

All-in-One WP Migration がおすすめです。簡単に移行することができました。
ただ、バックアップファイルアップロード時に apache と nginx のアップロード制限に引っかかるので、一時的にアップロードサイズ上限を上げる必要があります。

.htaccess
php_value upload_max_filesize 128M
php_value post_max_size 128M
php_value memory_limit 256M
php_value max_execution_time 300
php_value max_input_time 300
.conf
client_max_body_size 128M;

最後に移行漏れが無いか確認

移行時にテーマを変えたせいで以下の設定が漏れていました。
自分用のチェックリストとして残します。

  • GoogleAnalytics
  • OGP
  • favicon
  • 外形監視
  • 固定ページのリンク切れ

追記

ipv4 ポートフォワードの永続化

$ sudo vi /etc/sysctl.conf 

net.ipv4.ip_forward = 1

$ cat /proc/sys/net/ipv4/ip_forward
7
13
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
7
13