環境
今回構築する環境は下記のものになります。
・ Amazon Linux
インストールパッケージ | バージョン |
---|---|
httpd(Apach) | 2.4.46 |
PHP | 7.2.33 |
MySQL | 5.6.49 |
WordPress | 4.9.14 |
インストール
Apachのインストール
今回Apachはバージョン2.4を指定しているため、コマンドのhttpdの後ろの24にしていますが
後ろの値を変更すると違うバージョンを指定可能です。
sudo yum install -y httpd24
PHPのインストール
Apache同様にバージョンを指定し、インストールしていきます。
sudo yum install -y php72
インストールが終わったらモージュールの確認として下記コマンドを実行してください。
sudo yum list available | grep php72
下記WordPressの公式から必要なモジュールを確認し、インストールしていく。
今回は下記モジュールを追加しました。
sudo yum install -y php72-mbstring.x86_64
yum install -y php72-pecl-imagick
sudo yum install -y php72-mysqlnd php72-gd php72-pecl-mcrypt
yum install -y php72-pecl-ssh2
ちなみに私は最初モジュールを入れることに
気づかずに進めていきWordPressに接続しようとして
「お使いのサーバーの PHP では WordPress に必要な MySQL 拡張を利用できないようです。」と言われました。
mysqlのインストール
Apache同様にバージョンを指定し、インストールしていきます。
sudo yum install -y mysql56-server
WordPressのインストール
WordPressを行うのですが、今までのインストールとは少し異なり
WordPressを格納したいディレクトリに移動してからインストールを行います。
今回は/var/www
配下に格納しました。
cd /var/www
移動後、worrdpressのzipファイルをダウンロードし、解凍します。
リリースノートを確認の上、ダウンロードを行ってください。
sudo wget https://ja.wordpress.org/wordpress-4.9.14-ja.zip
sudo unzip wordpress-4.9.14-ja.zip
Apachの設定
/etc/httpd/conf/httpd.conf
の内容を書き換えていきます。
書き換える場所は2つありServerName
とDocumentRoot
になります。
まずは下記コマンドで編集していきます。
sudo vi /etc/httpd/conf/httpd.conf
編集内容は下記の通りです。
###ServerName
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName ドメインまたは、IPアドレス指定:80
DocumentRoot
ワードプレスインストール先をDocumentRootに記載する。
今回は/var/www
配下にWordPressをインストールしたので下記のように記載した。
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/wordpress"
httpd.conf
の編集が完了したら、httpdをリスタートする。
sudo service httpd restart
DBの作成
まず下記コマンドでMySQLをスタートさせます。
/etc/init.d/mysql start
次にMySQLの初期設定をします。mysql_secure_installationというコマンドを使用します。
sudo mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] n
... skipping.
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...
rootユーザーのパスワード設定が完了したら、MySQLにログインします。
sudo mysql -u root -p
DBを作成する。
create database DB名 CHARACTER SET utf8;
ユーザーを作成し、権限を付与。
GRANT ALL PRIVILEGES ON DB名.* TO DBのユーザー名@localhost IDENTIFIED BY 'DBのパスワード';
完了したらMySQLから抜けて問題ない。
WordPressの初期設定
ここまで設定が出来たらいよいよブラウザからWordPressを設定していきます。
まずはhttp://ドメインかIP/wp-admin/setup-config.php
を
ブラウザに書き込んでみましょう。
下記画面が出たらさあ、始めましょう!
を選択してください。。
DB作成時に決めたデータベース名、ユーザ名、パスワードを記載して送信を選択してください。
下記のように「ファイル wp-config.php に書き込めませんでした。」と言われた場合
/var/www/wordpress
配下にお好きな方法でwp-config.php
を作成し
テキストを張り付けるなどで作成してください。
また、wp-config-sample.php
の中身を編集し、wp-config.php
として保存する方法もあります。
作成出来たらインストール実行を選択してください。
サイトのタイトルやユーザ名、パスワード、メールアドレスを記載し、
WordPressをインストールを選択してください。
下記画面が出たらインストール完了になるので早速ログインしてみましょう。
無事WordPressへログインできました。
ただ見た目は問題ないのですが、画像のアップロードや
プラグインとテーマのインストールが上手くいかないので、
まだやらなければならないことがあります。
テーマとプラグインは下記のように接続情報を聞かれてします。
wp-contentの配下の設定
/var/www/wordpress/wp-content
に移動し、
wp-contentの配下の所有者をApacheにし権限を707に変更した。
配下を確認するとupgrade
とuploads
はないが
アップロードを行うとフォルダが作成されていた。
sudo chown -R apache:apache /var/www/wordpress
sudo chmod -R 707 wp-content
変更後、アップロードやテーマ、プラグインのインストールができるようになった。
番外編(ハマった部分)
今回パブリックIPを使用していたため
構築中にec2の停止し、起動するとIPが変わっていたため起動時にWordPressに
ログインできないという事態が発生したので解決方法を書いていこうと思います。
今回は固定IPを使用していないので
まず、インバウンドルールの変更をしサーバにログインします。
ログイン後、httpdとMySQLを自動起動にしていなかったの起動させます。
sudo service httpd start
sudo service mysqld start
ちなみに自動起動にしたい場合は下記コマンドでできます。
自動起動をやめてい場合はon
をoff
に変えてコマンド実行すれば変わります。
またmysql
をhttpd
に変えればhttpd
を自動起動に設定可能です。
sudo chkconfig mysqld on
確認コマンドは下記になります。
2~5がon
で自動起動設定になっています。
chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
起動状態を下記コマンドで確認し、起動されていたら
/etc/httpd/conf/httpd.conf
のServerName
を構築時と同様に現在のIPに変更し、
httpdをリスタートする。
sudo service httpd status
ここまでは構築時のに行っている作業の為、思いつくのだが
実はこれだけではまだWordPressに入れず、入ろうとするとMySQL接続エラーを起こす。
実は「ホーム」および「サイト URL」を
変更してあげなくてはいけないという落とし穴がありました。
・home
は、WordPress のコアファイルを配置する場所のアドレスで、
・siteurl
は、WordPress サイトに読者がアクセスするために使うアドレスです。
実際にMySQLにログインし、下記コマンドから確認してみてください。
select * from wp_options where option_name = 'siteurl';
select * from wp_options where option_name = 'home';
もしsiteurl
とhome
が現在のIPはと異なっていたら変更が必要なため
下記コマンドを書き換えて実施してください。
update wp_options set option_value = 'http://現在のIP' where option_name = 'siteurl';
update wp_options set option_value = 'http://現在のIP' where option_name = 'home';
これでようやくWordPressにログインが可能となります。