LoginSignup
2
3

More than 1 year has passed since last update.

EC2にDockerをインストールし、WordPressを構築 (DBはRDS)

Last updated at Posted at 2021-10-13

はじめに

表題の通りです。DBをRDSにした場合のやり方になります。

前提条件

・DBは、RDSのMySQLを使用します。

EC2にDockerをインストール

こちらの記事↓と全く同じコマンドで、dockerでWordPressが構築できます。
AWS EC2 docker + wordpress インストール
ただし、docker-compose.ymlファイルは以下のコードをコピペしてください。
Dockerfileも同じディレクトリ内に入れておいてください。

FROM wordpress:5.8.0-php7.4-apache

RUN set -ex; \
    apt-get update && apt-get install -y \
        wget \
        unzip \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# テーマのダウンロード
WORKDIR /tmp/wp-themes
RUN wget https://wp-cocoon.com/download/791/cocoon-master-2.1.9.1.zip

# テーマを配置
RUN unzip './*.zip' -d /usr/src/wordpress/wp-content/themes

# 不要ファイル削除
RUN apt-get clean
RUN rm -rf /tmp/*

WORKDIR /usr/src/wp-plugins

RUN set -ex; \
    wget -q -O amazon-s3-and-cloudfront.zip https://downloads.wordpress.org/plugin/amazon-s3-and-cloudfront.1.4.3.zip \
    && unzip -q -o '*.zip' -d /usr/src/wordpress/wp-content/plugins \
    && chown -R www-data:www-data /usr/src/wordpress/wp-content/plugins \
    && rm -f '*.zip'

# 所有者の変更
RUN chown -R www-data:www-data /usr/src/wordpress/wp-content/themes

WORKDIR /var/www/html
docker-compose.yml
version: '3'
services:
  wordpress:
    build: . # カレントディレクトリのDockerfileでイメージをビルド
    image: wordpress
    ports:
      - '80:80' # ローカルのport80でコンテナのport80にアクセスできるようにする
    environment:
      WORDPRESS_DB_HOST: xxxxxxx.ap-northeast-1.rds.amazonaws.com # rdsのエンドポイント
      WORDPRESS_DB_NAME: wordpress
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: wp_pass
    volumes:
      - .:/var/www/html
    restart: always

以下の3点は、EC2にSSH接続後、RDSに接続し、RDS内に作成していきます。
・WORDPRESS_DB_NAME: wordpress
・WORDPRESS_DB_USER: wp_user
・WORDPRESS_DB_PASSWORD: wp_pass

RDSに接続し、DB名等を作成

EC2を作成し、ssh接続後、RDSにwordpressのDBを作成しますので、下記のコマンドを入力します。
DB名とユーザー名とパスワードを作成します。

$ sudo yum update
$ sudo yum install -y mysql
$ mysql -h [RDSエンドポイント] -u RDS作成時のユーザー名 -p
Enter password: RDS作成時のパスワード

> create database wordpress;

Query OK, 1 row affected (0.01 sec)

> create user wp_user@'%' identified by 'wp_pass';


> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, RELOAD, PROCESS, REFERENCES, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER ON *.* TO 'wp_user'@'%' WITH GRANT OPTION;

Query OK, 0 rows affected (0.01 sec)

> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| innodb             |
| mysql              |
| performance_schema |
| sys                |
| wordpress          |
+--------------------+

>exit

#wp_userでログインできることを確認します。

$ mysql -h [RDSエンドポイント] -u wp_user -p

EC2のパプリックIPにアクセスすると、WordPressの画面が表示されました!

Dockerの自動起動設定

EC2が再起動した際、Dockerが自動で起動するよう設定します。

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; vendor preset: disabled)

service; disabledと表示され、自動起動設定されていませんので、設定しましょう。

$ sudo systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

$ sudo systemctl status docker
● docker.service - Docker Application Container Engine
   Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)

enabledと表示されていますので、自動起動設定されていることがわかります。

ちなみに自動起動設定をOFFもできます。

$ sudo systemctl disable docker
Removed symlink /etc/systemd/system/multi-user.target.wants/docker.service.

Docker起動中に、EC2を停止させた場合

Docker起動中に、EC2を停止させ、再度起動させた状態で、docker-composeコマンドを使用すると以下のエラー文がでます。

$ docker-compose up -d
Traceback (most recent call last):
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "requests/adapters.py", line 449, in send
  File "urllib3/connectionpool.py", line 727, in urlopen
  File "urllib3/util/retry.py", line 410, in increment
  File "urllib3/packages/six.py", line 734, in reraise
  File "urllib3/connectionpool.py", line 677, in urlopen
  File "urllib3/connectionpool.py", line 392, in _make_request
  File "http/client.py", line 1277, in request
  File "http/client.py", line 1323, in _send_request
  File "http/client.py", line 1272, in endheaders
  File "http/client.py", line 1032, in _send_output
  File "http/client.py", line 972, in send
  File "docker/transport/unixconn.py", line 43, in connect
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "docker/api/client.py", line 214, in _retrieve_server_version
  File "docker/api/daemon.py", line 181, in version
  File "docker/utils/decorators.py", line 46, in inner
  File "docker/api/client.py", line 237, in _get
  File "requests/sessions.py", line 543, in get
  File "requests/sessions.py", line 530, in request
  File "requests/sessions.py", line 643, in send
  File "requests/adapters.py", line 498, in send
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "docker-compose", line 3, in <module>
  File "compose/cli/main.py", line 80, in main
  File "compose/cli/main.py", line 189, in perform_command
  File "compose/cli/command.py", line 70, in project_from_options
  File "compose/cli/command.py", line 153, in get_project
  File "compose/cli/docker_client.py", line 43, in get_client
  File "compose/cli/docker_client.py", line 170, in docker_client
  File "docker/api/client.py", line 197, in __init__
  File "docker/api/client.py", line 222, in _retrieve_server_version
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
[3185] Failed to execute script docker-compose

焦らず、docker ps -aコマンドをしてみましょう

$ docker ps -a
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

dockerが起動されていないことが原因です。

$ systemctl start docker
Failed to start docker.service: The name org.freedesktop.PolicyKit1 was not provided by any .service files
See system logs and 'systemctl status docker.service' for details.

だめですね。原因は、実行権限がないことです。sudoを足しましょう。

$ sudo systemctl start docker
[ec2-user@ip-10-0-1-94 wordpress]$ docker ps -a
CONTAINER ID   IMAGE      COMMAND                  CREATED          STATUS          PORTS                               NAMES
xxxxxx   kyt-prod   "docker-entrypoi"   43 minutes ago   Up 31 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp   wordpress_wordpress_1

コンテナが起動しました!!
WordPressが使用できます!!

参考

AWS EC2 docker + wordpress インストール

2
3
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
2
3