CentOS7 を入れた Raspberry Pi に Laravel8 環境を構築する

こんにちは。
CentOS7 を入れたラズパイに Laravel8 環境を構築してみます。
デフォルトのリポジトリからだと、古い PHP と MariaDB が入っちゃうので、それぞれ工夫して導入します。

PHP

デフォルトのリポジトリだと PHP5 系が入っちゃいます。
armv7l 用の remi リポジトリがあるけども PHP7.2 なので、こちらもダメです。
ついては phpenv でソースから PHP7.4 をビルドします。

# EPEL リポジトリ導入
cat > /etc/yum.repos.d/epel.repo << EOF
[epel]
name=Epel rebuild for armhfp
baseurl=https://armv7.dev.centos.org/repodir/epel-pass-1/
enabled=1
gpgcheck=0
EOF

# 関連パッケージ 導入
yum groupinstall "Development tools"
yum install libicu libicu-devel oniguruma oniguruma-devel cmake cmake3
yum install gcc libxml2 libxml2-devel libcurl libcurl-devel libpng libpng-devel libmcrypt libmcrypt-devel libtidy libtidy-devel libxslt libxslt-devel openssl-devel bison libjpeg-turbo-devel readline-devel autoconf sqlite-devel bzip2-devel nginx

# phpenv 導入
cd /usr/local/src/
git clone https://github.com/CHH/phpenv.git
cd phpenv/bin/
./phpenv-install.sh
git clone https://github.com/CHH/php-build.git ~/.phpenv/plugins/php-build

vi ~/.bashrc
======================================
export PATH="/root/.phpenv/bin:$PATH"
eval "$(phpenv init -)"
======================================

source ~/.bashrc

# libzip 導入
cd /usr/local/src/
wget https://libzip.org/download/libzip-1.7.3.tar.gz
tar xzvf libzip-1.7.3.tar.gz 
cd libzip-1.7.3
cmake3 -DCMAKE_INSTALL_PREFIX=/usr/local/libzip
make
make install

export PKG_CONFIG_PATH="$PKG_CONFIG_PATH:/usr/local/libzip/lib/pkgconfig"

# PHP 導入
phpenv install --list
phpenv install 7.4.14
phpenv global 7.4.14

# PHP-FPM 設定
vi ~/.phpenv/versions/7.4.14/etc/php-fpm.d/www.conf
==========================================================
[www]
user = nginx
group = nginx
listen = /var/run/www.sock
listen.owner = nginx
listen.group = nginx
==========================================================

vi ~/.phpenv/versions/7.4.14/etc/php-fpm.conf
==========================================================
pid = /var/run/php-fpm.pid
==========================================================

ln -s /root/.phpenv/versions/7.4.14/sbin/php-fpm /usr/local/bin/php-fpm

# Unit ファイル作成
vi /usr/lib/systemd/system/php-fpm.service
==============================================================
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/var/run/php-fpm.pid
ExecStartPre=/usr/bin/rm -f /run/php-fpm.pid
ExecStart=/usr/local/bin/php-fpm -D
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
RuntimeDirectory=php-fpm
RuntimeDirectoryMode=0755

[Install]
WantedBy=multi-user.target
==============================================================

# PHP-FPM 起動
systemctl daemon-reload
mkdir -p /var/run/php
systemctl enable php-fpm
systemctl start php-fpm

MariaDB

こちらもデフォルトのリポジトリからだと 5.5 系が入っちゃうのでパスです。
一旦 yum で 5.5 系を導入して初期設定を行って、その後にソースから最新版をビルドし、Unit ファイルの書き換えで最新版にアップデートします。

# 5.5 系の MariaDB 導入
yum install mariadb-server
systemctl start mariadb
mysql_secure_installation

# 10.5.8 の MariaDB ソースをビルド
cd /usr/local/src/
wget https://downloads.mariadb.org/interstitial/mariadb-10.5.8/source/mariadb-10.5.8.tar.gz/from/https%3A//mirror.yongbok.net/mariadb/
mv index.html mariadb.tar.gz
tar -zxvf mariadb.tar.gz
cd mariadb-10.5.8/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb
make
make install

# Unit ファイル書き換え
systemctl stop mariadb
vi /usr/lib/systemd/system/mariadb.service
================================================
#ExecStart=/usr/bin/mysqld_safe --basedir=/usr
ExecStart=/usr/local/mariadb/bin/mysqld_safe
================================================

# 10.5.8 にて MariaDB 起動
systemctl daemon-reload
systemctl start mariadb

Composer

Composer は公式に則って普通に導入です。

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"

補足

EASE の Nginx 設定ファイル例も記載しておきます。
自分用の備忘録ですしお寿司。

upstream php-fpm-ease {
  ip_hash;
  server unix:/var/run/www.sock;
}

server {
     listen 80;
     server_name ease.local;
     root         /var/www/vhosts/ease/public;
     index        index.php index.html index.htm;
     access_log   /var/log/nginx/ease.local_access.log;
     error_log    /var/log/nginx/ease.local_error.log;

     fastcgi_read_timeout 999999;
     proxy_read_timeout 999999;

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

    location ~ \.php$ {
        fastcgi_pass  php-fpm-ease;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_buffer_size  128k;
        fastcgi_buffers  256 16k;
        fastcgi_busy_buffers_size  256k;
        fastcgi_temp_file_write_size  256k;
        include  fastcgi_params;
    }

}