ヤマムギ

growing hard days.

*

EC2 Amazon LinuxのNginx+RDS MySQLにレンタルWebサーバーからWordPressを移設する(手順整理版)

      2015/05/16

nginx_logo copy

ブログサイト(WordPress)をレンタルWebサーバーからAWSに移設する事にしました。
手順を整理して記載します。

検証、確認や途中のエラーなどを見たい方は、そのまま記載しているこちら「Amazon LinuxのNginx+RDS MySQLにレンタルWebサーバーからWordPressを移設する(失敗、手戻りそのまま記載版)」をご覧ください。

環境

ドメインは仮に mydomain.comとしています

移設前

  • ロリポップ
  • ロリポプラン
  • MySQL
  • 多分Apache
  • Word Press
  • Word Press to Dropboxで日次バックアップ

移設後

  • AWS EC2 Amazon Linux(t2.small)
  • AWS RDS MySQL
  • Nginx
  • WordPress

移設先AWS環境の構築

EC2インスタンスの構築

こちら「試したい事があるのでAWS でとりあえずAmazon Linuxのサーバを作る」を参照してください。

作成したEC2インスタンスにグローバルIPアドレスを設定

こちら「AWS EC2 でインスタンスにIPアドレスを紐付ける」を参照してください。

Amazon LinuxにNginx他をインストールする

MySQLはRDSを使用するのでここではクライアントのみをインストールする

[bash]
$ sudo yum install nginx
$ sudo yum install mysql
$ sudo yum install php php-fpm php-mbstring php-mysql php-gd
[/bash]

Nginxの設定をする

default.confを設定する

[bash]
$ sudo vim /etc/nginx/conf.d/mydomain.conf
[/bash]

  • mydomain.conf

[vim]
server {
listen 80;
server_name mydomain.com www.mydomain.com;
root /usr/share/nginx/mydomain;

     location / {
index index.php index.html;
try_files $uri $uri/ /index.php;
if (!-e $request_filename) {
rewrite ^.+?(/wp-.) $1 last;
rewrite ^.+?(/.
.php)$ $1 last;
rewrite ^ /index.php last;
}
}

     location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[/vim]

Nginxとphp-fpmを自動起動してサービススタートする

[bash]
$ sudo chkconfig nginx on
$ sudo chkconfig php-fpm on
$ sudo service nginx start
$ sudo service php-fpm start
[/bash]

RDS MySQLの構築

こちら「AWS RDS でMySQLインスタンスを構築する」を参照してください。

MySQL 設定

移設前のバックアップからデータを移行しますので、Databaseとユーザーだけ作成します。

仮にそれぞれ下記の設定としています。

  • RDSエンドポイント : rdsendpoint
  • RDSユーザー : rdsuser
  • RDSパスワード : rdspassword
  • データベース名 : wpdatabase
  • WordPressユーザー : wpuser
  • WordPressパスワード : wppassword
  • EC2のプライベートIP : 172.1.1.1

[bash]
$ mysql -h rdsendpoint -u rdsuser -p
Enter password:rdspassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 290
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> CREATE DATABASE wpdatabase CHARACTER SET utf8;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT ALL PRIVILEGES ON wpdatabase.* TO wpuser@172.1.1.1 IDENTIFIED BY ‘wppassword’ WITH GRANT OPTION;
Query OK, 0 rows affected (0.10 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.02 sec)

mysql> exit
Bye

$ mysql -h rdsendpoint -u wpuser -p
Enter password:wppassword
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 290
Server version: 5.6.22-log MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql> exit
Bye
[/bash]

移設前サーバーのBackWPUpプラグインで手動バックアップを取得

  • 新規ジョブを作成します。
  • 対象は、データベース、ファイル、インストール済プラグインリストです。
  • 保存方法はDropboxにします。
  • スケジュールは手動です。
  • ジョブ一覧から「すぐに実行」で実行します。

AWSでDropboxからバックアップファイルをダウンロードする

展開用ディレクトリを作成して、そこにダウンロードして解凍します。
※ダウンロードしたファイルにはURLコマンドが付いた状態になっているのでダウンロード後、リネームします。

[bash]
$ mkdir mydomain
$ cd mydomain
$ wget <DropboxのバックアップファイルURL>
$ mv <Dropboxのバックアップファイル> mydomain.tar.gz
$ tar vxfz mydomain.tar.gz
[/bash]

データベースバックアップのSQLを置換する

[bash]
$ vim バックアップSQLファイル
[/bash]

[vim]
:%s;置換前のテキスト;置換後のテキスト;g
[/vim]

  • ABSPATHの置換
  • 他にデータベース名など変更している場合はそれも置換

データベースのリストア

[bash]
$ mysql -h rdsendpoint -u wpuser -pwppassword wpdatabase < バックアップSQLファイル
[/bash]

※ -pの後はスペースなしでパスワードを続けます
※ sqlファイル名の前は <

ファイルの移動

Nginxのデフォルトユーザーはapacheなのでapacheユーザーに権限付与します。

[bash]
$ cd ~
$ sudo mv mydomain /usr/share/nginx/mydomain
$ sudo chown -R apache:apache /usr/share/nginx/mydomain
[/bash]

wp_config.phpの編集

[bash]
$ sudo vim wp_config.php
[/bash]

[vim]
define(‘DB_NAME’, ‘wpdatabase’);

/** MySQL のユーザー名 */
define(‘DB_USER’, ‘wpuser’);

/** MySQL のパスワード */
define(‘DB_PASSWORD’, ‘wppassword’);

/** MySQL のホスト名*/
define(‘DB_HOST’, ‘rdsendpoint’);
[/vim]

nginx再起動

[bash]
$ sudo service nginx restart
[/bash]

DNSのAレコードをEC2のグローバルIPアドレスに変更する


最後までお読みいただきましてありがとうございました!

「AWS認定資格試験テキスト&問題集 AWS認定ソリューションアーキテクト - プロフェッショナル 改訂第2版」という本を書きました。

「AWS認定資格試験テキスト AWS認定クラウドプラクティショナー 改訂第3版」という本を書きました。

「ポケットスタディ AWS認定 デベロッパーアソシエイト [DVA-C02対応] 」という本を書きました。

「要点整理から攻略するAWS認定ソリューションアーキテクト-アソシエイト」という本を書きました。

「AWSではじめるLinux入門ガイド」という本を書きました。

 - Amazon Linux, AWS, WordPress , , , ,

ad

ad

  関連記事

Amazon Cognito User Poolsのデモをしてみました

AWS Summit 2016 Tokyoのアップデートおっかけ会をJAWS-U …

AWSアカウント ルートユーザーのMFAが使えなくなったので復旧

AWSのルートユーザーどころか、MFAが使えるログインすべてが使えなくなって焦り …

API Gatewayから直接 DynamoDBに書き込む

やりたいこと WebページでOやXを押したときに、どっちを押したかをDynamo …

PyCharmにAWS Tool kitをインストールしてサンプルのLambda関数をデプロイして実行しました

この記事はJetBrainsIDE Advent Calendar 2018に参 …

JAWS-UG関西「AI で人を笑わせてみよう!ハンズオン」に参加しました

AI で人を笑わせてみよう!ハンズオン 灼熱の7月最終日にJAWS-UG関西のオ …

[JapanTaxi] Athena 指向アナリティクス 〜真面目に手を抜き価値を得よ〜(AWS Summit Tokyo 2017)を聞いてきました

Athenaのユースケースとして聞きにいきましたが、最近触ってるRe:dashも …

WordPressの記事を公開日時に関係なくソート順を変更する

PostMash Customを使う WordPressのプラグインでPostM …

WordPressのPHPを7から8にしたらプラグインのエラー “Unparenthesized `a ? b : c ? d : e` is not supported”

WordPressのPHPを7から8にしたら次のエラーが発生しました。 PHP …

AWS Lambda(Python)からAmazon Connectで電話を発信する

自動で電話を発信する必要がありまして、電話発信APIを開発することになりましたの …

ヤマムギvol.27 Amazon Route 53プライベートホストゾーンとリゾルバーのデモをしました

今日は『AWS認定資格試験テキスト&問題集AWS認定ソリューションアーキ …