LoginSignup
9

More than 3 years have passed since last update.

AWS EC2上で php8 + laravel8 + MySQL8 の環境を構築する

Last updated at Posted at 2021-02-19

8が揃って末広がりだったり無限大だったり

ちょうど新規開発のお話があったので、これ実現できんの?を試したログ。

環境

EC2 : t2.micro
AMI : Amazon Linux2 (ami-0992fc94ca0f1415a)
EBS : 8GB/IOPS100

実験機なので root でゴリゴリ作業しちゃう。
vi 使ってるのはそういう世代だからです。

下準備として epel と remi の yum リポジトリ追加

[root@host ~] amazon-linux-extras install epel
[root@host ~] yum install https://rpms.remirepo.net/enterprise/remi-release-7.rpm

php 8.0

php80 のインストール

[root@host ~] yum -y install php80 php80-php php80-php-mbstring php80-php-pdo php80-php-xml php80-php-fpm php80-php-mysqlnd php80-php-gd
[root@host ~] systemctl enable php80-php-fpm
[root@host ~] systemctl start php80-php-fpm

php80 を php コマンドで使えるようにする

[root@host ~] alternatives --install /usr/bin/php php /usr/bin/php80 1
[root@host ~] php -v
PHP 8.0.2 (cli) (built: Feb  2 2021 19:28:42) ( NTS gcc x86_64 )
Copyright (c) The PHP Group
Zend Engine v4.0.2, Copyright (c) Zend Technologies

ヽ(=´▽`=)ノ

nginx

nginx をインストール

[root@host ~] amazon-linux-extras install nginx1
[root@host ~] systemctl enable nginx

設定ざっくり

[root@host ~] vi /etc/nginx/conf.d/dev.conf

[ 2021/02/25更新 ] 設定内容がぜんぜん足りてないものだったので修正しました

/etc/nginx/conf.d/dev.conf
server {
    listen 80;
    server_name 任意のホスト名;

    charset UTF-8;
    root  /var/www/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~* /\. {
        deny all;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
                return 404;
        }
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffers 256 128k;
        fastcgi_buffer_size 128k;
        fastcgi_intercept_errors on;
        fastcgi_read_timeout 120s;
    }

}

conf のファイル名やホスト名はお好みで。

とりあえず index.php 置く

[root@host ~] vi /var/www/html/index.php

あとで退避するので動作確認できればOK。

/var/www/html/index.php
<?php
phpinfo();

起動

[root@host ~] nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@host ~] systemctl start nginx

一度ブラウザでアクセスして確認しておく。

MySQL 8

参考 : https://qiita.com/zaburo/items/7518a432d915c061983f
[ 2021/02/25更新 ] なぜかmariadbの記述があったので修正しました

yum リポジトリ追加

[root@host ~] rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

リポジトリのURLはオフィシャルを参照しとく。
Amazon Linux 2 は CentOS 7 ベースらしいので、
Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package
のURLを取得する。
(ダウンロードボタン押すとログインしろや画面になるのでノーサンキューのとこのリンク先を右クリックでコピー)

インストール

[root@host ~] yum install mysql-community-server
[root@host ~] systemctl enable mysqld

起動と初期設定

[root@host ~] systemctl start mysqld
[root@host ~] grep password /var/log/mysqld.log
2021-02-19T07:38:18.176107Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 自動で設定された初期パスワード

初期パスワードをメモる。

mysql_secure_installation とログイン確認

[root@host ~] mysql_secure_installation

なんやかんや質問に答える。
メモったパスワードを入力すると、新しいrootパスワードを求められるのでお好みで入力。
ちなみに脆弱なパスワードは弾かれるのでちゃんとせねばあかん。

[root@host ~] mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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>

ヽ(=´▽`=)ノ

Laravel 8

composer をインストール

[root@host ~] curl  -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer

Laravel をインストール

[root@host ~] chown nginx /var/www
[root@host ~] cd /var/www
[root@host www] mv html default
[root@host www] sudo -u nginx composer create-project laravel/laravel html
[root@host www] cd html
[root@host html] sudo -u nginx composer update

ここによるとcomposer.jsonを編集しる、みたいな情報があるものの、記事公開時点ではアップデート済みなのかすでに編集不要だった

nginx の設定とパーミッション変更

[root@host html] vi /etc/nginx/conf.d/dev.conf
/etc/nginx/conf.d/dev.conf
root /var/www/html/public;
[root@host html] nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@host html] nginx -s reload
[root@host html] chmod -R 777 {storage,bootstrap/cache}

ブラウザでアクセスしてエラーなくデフォルトページが表示されればOK。

php8 + laravel8 + MySQL8

ここで終わると、まだ Laravel と MySQL がつながってなくて本来の目的は果たせないので。

.env 変更

[root@host html] vi .env
/var/www/laravel/.env
DB_USERNAME=root
DB_PASSWORD=mysql_secure_installationで設定したパスワード

データベース作成

[root@host html] mysql -uroot -p
Enter password:

mysql> CREATE DATABASE laravel;
Query OK, 1 row affected (0.00 sec)

Laravel-admin インストール

[root@host html] sudo -u nginx composer require encore/laravel-admin:1.*
[root@host html] sudo -u nginx php artisan vendor:publish --provider="Encore\Admin\AdminServiceProvider"
[root@host html] sudo -u nginx php artisan admin:install

これで /admin にアクセスし、ID/PW ともに admin でログインできれば、無事にnginx → Laravel8 → php-fpm → php8 → MySQL8 という流れが繋がってることが確認できる。

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
9