AlmaLinuxでLaravel + MySQL + nginx + PHP-FPM 環境構築手順(自分メモ)
本ドキュメントは、AlmaLinuxの初期状態から、LaravelアプリケーションをMySQLに接続し、nginx + PHP-FPMで動作させ、データベース・キャッシュ・セッションを全て正常化する手順をまとめたものです。
1. AlmaLinuxの準備
最初にシステムを最新化します。
dnf clean all
dnf -y update
reboot
2. 基本ツールの導入
dnf -y install epel-release
dnf -y install wget curl unzip git vim policycoreutils-python-utils
3. nginx のインストールと設定
3.1 インストール
dnf -y install nginx
systemctl enable nginx
systemctl start nginx
systemctl status nginx -l --no-pager
3.2 ファイアウォール設定
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
3.3 動作確認
ブラウザで http://サーバIP/
にアクセスして nginx のデフォルトページが表示されればOKです。
4. PHP-FPM 8.x の導入
4.1 PHPリポジトリ追加
dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm
dnf module reset php -y
dnf module enable php:remi-8.2 -y
4.2 PHPと必要モジュール導入
dnf -y install php php-cli php-fpm php-mysqlnd php-opcache php-gd php-xml php-mbstring php-curl php-zip php-bcmath php-pdo php-intl
4.3 PHP-FPM起動
systemctl enable php-fpm
systemctl start php-fpm
systemctl status php-fpm -l --no-pager
4.4 nginxとPHP-FPM連携設定例
vi /etc/nginx/conf.d/laravel.conf
server {
listen 80;
server_name _;
root /var/www/api/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
nginx -t
systemctl restart nginx
5. MySQL 8.x の導入と設定
5.1 インストール
dnf -y install @mysql
systemctl enable mysqld
systemctl start mysqld
systemctl status mysqld -l --no-pager
5.2 初期設定
mysql_secure_installation
rootパスワード設定、匿名ユーザー削除、リモートroot接続無効化、テストDB削除を行います。
5.3 Laravel用DB・ユーザー作成
mysql -u root -p
CREATE DATABASE infiniteai_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'infiniteai_db_user'@'127.0.0.1' IDENTIFIED BY 'M6x$+.wr';
GRANT ALL PRIVILEGES ON infiniteai_db.* TO 'infiniteai_db_user'@'127.0.0.1';
FLUSH PRIVILEGES;
EXIT;
5.4 接続確認
mysql -u infiniteai_db_user -p'M6x$+.wr' -h 127.0.0.1 infiniteai_db -e "SELECT 1;"
6. Laravelの設置と設定
6.1 Laravel配置
cd /var/www
git clone https://github.com/example/api.git
cd api
composer install --no-dev --optimize-autoloader
cp .env.example .env
6.2 .env編集
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:xxxxxxxxxxxxxxxxxxx
APP_DEBUG=true
APP_URL=http://localhost
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=infiniteai_db
DB_USERNAME=infiniteai_db_user
DB_PASSWORD=M6x$+.wr
SESSION_DRIVER=database
CACHE_STORE=database
6.3 権限設定
mkdir -p storage bootstrap/cache
chown -R nginx:nginx storage bootstrap/cache
chmod -R ug+rwX storage bootstrap/cache
6.4 SELinuxコンテキスト設定
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/api/storage(/.*)?'
semanage fcontext -a -t httpd_sys_rw_content_t '/var/www/api/bootstrap/cache(/.*)?'
restorecon -Rv /var/www/api/storage /var/www/api/bootstrap/cache
7. キャッシュ・セッションテーブル準備
7.1 マイグレーション生成
sudo -u nginx php artisan cache:table
sudo -u nginx php artisan session:table
sudo -u nginx php artisan queue:table
7.2 マイグレーション実行
sudo -u nginx php artisan migrate --force
8. キャッシュ類のクリア
sudo -u nginx php artisan cache:clear
sudo -u nginx php artisan config:clear
sudo -u nginx php artisan route:clear
sudo -u nginx php artisan view:clear
9. 動作確認
9.1 DB疎通
sudo -u nginx php artisan tinker --execute="DB::select('SELECT 1')"
9.2 設定確認
sudo -u nginx php artisan about | grep -E 'Database|Cache|Session'
結果例:
Cache ...................................................................... database
Database ................................................................... mysql
Session ................................................................... database
10. 運用上の注意
- 本番では
APP_ENV=production
にしphp artisan config:cache
を実行 - MySQLユーザー権限は最小限に
- SELinuxを有効に保つ場合は必ずコンテキストを設定
- nginx + PHP-FPM のチューニングを行い性能向上
以上で、AlmaLinux上に Laravel + MySQL + nginx + PHP-FPM 環境が完成します。