ヤマムギ

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

  関連記事

PHP7.3 Crayon Syntax Highlighterのエラー対応と削除したプラグイン

Amazon Linux2のPHPを7.2から7.3へアップデートしましたでアッ …

Amazon Linux2のCloud9でPython CDKのモジュールインストール

AMIがCloud9AmazonLinux2-2021-02-02T16-48の …

当ブログ(WordPress)のCloudFrontのキャッシュヒット率があがった

当ブログの構成です。 S3とALB+EC2 Auto Scalingで構成してい …

CloudWatch Internet Monitor(プレビュー)を試しました

Amazon CloudWatch Internet Monitor プレビュー …

AWS Backupで取得したAMIとスナップショットの削除

個人で使っているAWSリソースの断捨離をしてました。 Cloud9も複数アカウン …

特定AWSアカウント特定リージョンのSQSキューを削除するLambda(Python)

やりたいこと 特定アカウント内特定リージョン内のSQSキューを全部削除したいです …

AWS認定SAPの執筆開始にあたって環境を構築しました

AWS認定ソリューションアーキテクトプロフェッショナル対策本の執筆開始にあたりま …

EC2 Image BuilderでRocket.ChatのAMIを作って起動テンプレートを更新しました

EC2 Image Builderの練習をしようと思い、Rocket.Chatの …

「AWS認定資格試験テキスト AWS認定クラウドプラクティショナー」執筆裏話

今日2019/4/20発売となりました「AWS認定資格試験テキスト AWS認定ク …

Developers Summit 2018 「AWSのフルマネージドな環境でCI/CDをやってみよう!AWS Cloud9からAWS Fargateへの継続的デプロイをご紹介」を聞きました

※写真は展示のAmazon Echoです。 以下は、思ったことや気になったことを …