LoginSignup
7
6

More than 5 years have passed since last update.

【JAWS-UG 初心者支部】ハンズオン補助資料

Last updated at Posted at 2016-04-18

コンソールがスクリーンショットと同じじゃなくても恐れない!AWSのコンソールは頻繁に変更されます

本資料は4月18日開催のJAWS-UG初心者支部#5のハンズオン補助資料です。


前提条件

  • WEBサーバ作成まで完了し、作成したインスタンスへSSH接続ができていること

yumアップデートを実行します

コマンド
sudo yum update -y

wordpressで使用するmysqlをインストールします

コマンド
sudo yum install -y mysql-server
確認
yum list installed | grep mysql-server

mysqlを起動します

コマンド
sudo service mysqld start

mysqlサービスが起動しているか確認します

コマンド
sudo service --status-all | grep mysqld

以下の様に結果が帰ってくればmysqlは起動しています

結果
mysqld (pid  xxxx) is running...

mysqlをインスタンス起動時に自動起動するよう設定します

コマンド
sudo chkconfig --level 345 mysqld on

設定が反映されているか確認します

コマンド
chkconfig --list | grep mysqld
結果
mysqld          0:off   1:off   2:off   3:on    4:on    5:on    6:off

mysqlの管理者パスワードを設定します

パスワードはサンプルで記載してます。
自身で 忘れないもの を設定して下さい。このパスワードはハンズオン中で再度使用します

変数
MYSQL_ADMIN_PASSWD='hands-on'
コマンド
sudo mysqladmin password ${MYSQL_ADMIN_PASSWD}

mysqlのDB接続用ユーザを作成します

コマンド
mysql -u root -p
結果
Enter password: hands-on

パスワード入力を求められるため先ほどのパスワードを入力します。

結果
mysql> 

ユーザー:admin を作成

コマンド
CREATE DATABASE wordpress;
GRANT ALL ON wordpress.* to admin@localhost;
FLUSH PRIVILEGES;
SET PASSWORD FOR admin@localhost=password('hands-on-pass-123');
結果
Query OK, 1 row affected (0.00 sec)

各コマンドに対し上記の表示が出力されれば成功です。

mysqlから出ます

コマンド
exit

コンソールが通常の状態に戻れば終了です


Nginx、PHPのインストール

コマンド
sudo yum install -y nginx
sudo yum install -y php php-fpm php-mbstring php-mysql
確認
yum list installed | grep nginx 
yum list installed | grep php 
yum list installed | grep php-fpm 
yum list installed | grep php-mbstring 
yum list installed | grep php-mysql

上記結果に対して、インストールパッケージが表示されれば問題ありません。

nginxとphp-fpmを起動します

コマンド
sudo service nginx start
sudo service php-fpm start
確認
sudo service --status-all | grep nginx
sudo service --status-all | grep php-fpm
結果
nginx (pid  xxxx) is running...
php-fpm (pid  xxxx) is running...

起動時に自動起動するよう設定します

コマンド
sudo chkconfig --level 345 nginx on
sudo chkconfig --level 345 php-fpm on
確認
chkconfig --list | grep nginx
chkconfig --list | grep php-fpm
結果
nginx           0:off   1:off   2:off   3:on    4:on    5:on    6:off

php-fpm         0:off   1:off   2:off   3:on    4:on    5:on    6:off
変数
MY_EIP='<自分のEIP>'
コマンド
curl -LI ${MY_EIP} -o /dev/null -w '%{http_code}\n' -s
結果
200

NginxでPHPを呼び出せるように設定するファイルをダウンロードします

コマンド
sudo wget http://bit.ly/WP-Config -O /etc/nginx/conf.d/wordpres.conf
確認
cat /etc/nginx/conf.d/wordpres.conf
結果
server {
  listen 80;
  location / {
    root /var/www/html;
    index index.php index.html;
  }
  location ~ \.php$ {
    root /var/www/html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

コンフィグファイルを置き換えます。

コマンド
sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.old
sudo wget https://s3-ap-northeast-1.amazonaws.com/20151028/nginx.conf -O /etc/nginx/nginx.conf
sudo wget http://bit.ly/WP-Config -O /etc/nginx/conf.d/wordpres.conf

nginxを再起動します

コマンド
sudo service nginx restart


Wordpressのダウンロードと配置

コマンド
wget http://wordpress.org/latest.tar.gz
tar xzvf latest.tar.gz
sudo mv wordpress/* /var/www/html/
sudo chown -R apache:apache /var/www/html/

html配下にファイルが作成されれば完了です


以上でWordPressの導入は完了です。
ブラウザからWordpressにアクセスし、初期設定を完了して下さい(ハンズオン資料p.77〜)


参照するDBをRDSに変更します

RDSの作成が終わった後の手順です
エンドポイントはポート番号を除いた.comまでです

変数
RDS_END_POINT='<RDSのエンドポイント>'
コマンド
mysql -u admin -p -h ${RDS_END_POINT}
Enter password: <RDS作成時に入力したパスワード>
show databases;
結果
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+
6 rows in set (0.00 sec)

上記結果が表示されれば成功です

コマンド
exit

EC2上のmysqlのデータを移行します

作業ディレクトリを確認します

コマンド
pwd
結果
/home/ec2-user
コマンド
mysqldump -u root -p wordpress > export.sql
Enter password: <mysqlをインストールした際に設定したパスワード(この例だと hands-on)>
確認
ls ~/ | grep export.sql
結果
export.sql

EC2上のMysqlは使用しないため停止します

コマンド
sudo service mysqld stop
sudo chkconfig --level 345 mysqld off

確認
sudo service --status-all | grep mysqld
chkconfig --list | grep mysqld
結果
mysqld is stopped
mysqld          0:off   1:off   2:off   3:off   4:off   5:off   6:off

mysqlからエクスポートしたデータをRDSへインポートする

確認
ls ~/ | grep export.sql
結果
export.sql
変数
EXPORT_SQL_PATH='~/export.sql'
コマンド
mysql -u admin -p -h ${RDS_END_POINT} wordpress < export.sql
Enter password: <RDS作成時に指定したパスワード>

インポートされたか確認する

確認
mysql -u admin -p -h ${RDS_END_POINT} wordpress
Enter password:<RDS作成時に指定したパスワード>
show tables;
結果
mysql> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)
コマンド
exit

Wordpress設定ファイルの退避

コマンド
sudo mv /var/www/html/wp-config.php ~/
確認
ls /var/www/html | grep wp-config.php

何も表示されないことを確認する


Wordpressの設定変更

作成した2台のwebサーバの任意の1台にSSH接続をして作業する

コマンド
mysql -u admin -p -h  ${RDS_END_POINT}  wordpress
Enter password: <指定したRDSの管理者パスワード>

update wp_options set option_value='<ELBのエンドポイント>' where option_name='siteurl' or option_name='home';
結果
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

想定通りに行かないときに

VPCの作成⇒スクリーンショットと同じようにできなかった!
⇒自力でVPCを作成すれば大丈夫
ただし、1から作るととサブネット等は作成がされない。
【publicサブネット】
10.0.0.0/24
AZ:1a

作成してもまだルートテーブルが作成されていないので編集する

【IGWの作成】
タグは適当につける:hands-on-igwとした
作成したIGWをVPCをにアタッチする

作成後、サブネットを確認して使用しているルートテーブルを確認する

【ルートテーブルの編集】
ルートテーブルに0.0.0.0/0で先ほど作成したIGWを追加する

[ルートテーブルの編集]
先ほどもし、igwの経路を追加していたら新しいルートテーブ(igwへの経路を持たないルートテーブル)を作成する


【インスタンスの作成】
デフォルトではサブネットがデフォルトのサブネット担っているため注意


【SSH】
インスタンス接続時に

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for '/Users/****/*****/******.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "/Users/*****/******/*****.pem": bad permissions
Permission denied (publickey).

みたいなのがでても焦らない。
確認:chmodはちゃんと設定できているか

【別のログインできない何か】
2-3分時間おく。(少し置かないと接続できません)
以前に同じIPで別インスタンスに接続している場合その履歴を消さないとダメだったりといろいろあります。


AmazonLinuxはデフォルトでUTCです。
今回は問題ないですがJSTじゃないとマズイって事は多いので覚えておきましょう。

【EC2_日本時間変換】
vim /etc/sysconfig/clock

下記の内容に変更
ZONE="Japan"
UTC=true

システムを再起動する
sudo reboot


【AMIの作成】
 AMI取得時はリブートチェックに気をつける!
 チェックを付けないとインスタンスは再起動します。

7
6
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
6