個人開発で EC-CUBE のプラグインを開発している Tirol と申します。
独自でプラグインを開発する過程で利用する動作確認等のテスト用のサーバとして、以前は Heroku を使っていたのですが、
いつぞやからかプラグインのインストールに失敗するようになりました。 → 該当 issue
ローカルで動かしても失敗するため、この度 EC2 インスタンス上で EC-CUBE を構築する方法を調査しまとめました。
EC2 インスタンスのスペック
- t2.medium (t2.micro 以下だとメモリが足らなくてプラグインインストールに失敗します)
- Amazon Linux 2
参考にした記事
本記事は下記の記事を参考に、データベースをアプリケーションと同一のサーバ上で動かす場合に必要な設定をまとめたものになります。
環境構築手順
各パッケージのインストール
この章に書いてあるコマンドを自動で実行したい方はこちらの gist のシェルスクリプトをコピペして実行してください。
root への切り替え
$ sudo su -
日本時間や文字コードの設定
$ timedatectl set-timezone Asia/Tokyo
$ sed -i "s/en_US\.UTF-8/ja_JP\.UTF-8/g" /etc/sysconfig/i18n
yum のアップデート
$ yum update -y
各パッケージのインストール
# Install apache
$ yum install -y httpd
# Install php
$ amazon-linux-extras install -y php7.2
$ yum install -y php-mbstring php-xml php-intl
# Install git
$ yum install -y git
# Install MySQL server and client
$ systemctl start mariadb
$ sudo systemctl enable mariadb
ec-cube 本体のためのパーミッション変更
$ usermod -a -G apache ec2-user
$ chown -R ec2-user:apache /var/www
$ chmod 2775 /var/www
$ find /var/www -type d -exec sudo chmod 2775 {} \;
$ find /var/www -type f -exec sudo chmod 0664 {} \;
composer のインストール
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php composer-setup.php
$ rm composer-setup.php
$ sudo mv composer.phar /usr/local/bin/composer
各種 config ファイルの編集
Apache と php の設定を行います。
vim で /etc/httpd/conf/httpd.conf を開き、 下記のように設定します。
<IfModule mime_module>
AddType application/x-httpd-php .php
</IfModule>
<Directory "/var/www/html">
AllowOverride All
</Directory>
次に、 プラグインインストール時に memory_limit
が原因で失敗することがあるため、 vim で /etc/php.ini を開き、下記のように設定します。
memory_limit = 512M
最後に、下記コマンドで php.ini の変更を反映させるため、 Apache を再起動します。
$ systemctl restart httpd.service
MariaDB の設定
MariaDB にログインし、 EC−CUBE で利用する database と その database へのアクセス権を持つ user の作成を行います。
mysql_secure_installation コマンドによる初期設定
root user の初期パスワードや、テスト用の database を削除するかどうか等が選べます。お好みで設定してください。
$ mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, 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 MariaDB
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 MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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] Y
... Success!
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] Y
... Success!
By default, MariaDB 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] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
EC-CUBE が利用する MySQL database と user の作成
よしなに作ります。
今回は ec_cube_database
という database を作り、それに対する全ての操作権限を持つ ec_cube_user
というユーザを作りました。
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE 'ec_cube_database';
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> CREATE USER 'ec_cube_user'@'localhost' IDENTIFIED BY 'password';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> GRANT ALL on ec_cube_database.* to 'ec_cube_user'@'localhost';
Query OK, 0 rows affected (0.00 sec)
EC-CUBE のインストール
$ cd /var/www/html/
$ git clone https://github.com/EC-CUBE/ec-cube.git
$ chmod 764 ec-cube/bin/console
$ cd ec-cube/
$ composer install
$ bin/console eccube:install
bin/console eccube:install
では、各環境変数を標準入力から入力するように求められます。
最低限設定が必要なのは、 Database Url
です。 Connection URL Syntax を参考に設定してください。
自分の場合は下記のようになります。
Database Url [sqlite:///var/eccube.db]:
> mysql://ec_cube_user:password@localhost/ec_cube_database
動作確認
<パブリック DNS>/ec-cube/
にアクセスしてサイトが表示されれば OK です。