本番環境のLamp環境にデプロイするためにまずはVirtualboxで環境構築など行いました。
本番環境はCentOS7.9がインストールされていて、そこにVirtualBoxでLamp環境の構築 CentOS7 MariaDBの手順で問題なくLamp環境を構築しました。
なので、この記事の内容としてはVirtualboxのLamp環境にLaravelアプリをgit cloneするの本番環境バージョンになります。
Lamp環境の確認
まずは確認
$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
$ httpd -v
Server version: Apache/2.4.6 (CentOS)
$ mysqld --version
mysqld Ver 10.6.5-MariaDB for Linux on x86_64 (MariaDB Server)
$ php --version
PHP 8.1.2 (cli) (built: Jan 18 2022 23:52:03) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
念のためupdate
$ yum update
gitなどのインストール
$ yum install git
$ yum install zip unzip
OSの設定変更
SELinuxの無効化
最初から無効状態でした。
$ getenforce
Disabled
タイムゾーンやロケールなども最初から日本仕様でした。
備忘録なのでコマンドのみ掲載。
$ timedatectl status
$ localectl status
git clone
$ cd /var/www
# ファイルがあるとgit cloneできないので削除
$ rm -r html
$ rm -r cgi-bin
# git clone
# 最後の . は、つけることによってカレントディレクトリにディレクトリを作らずにcloneすることができます
$ git clone https://github.com/アカウント名/リポジトリ名.git .
cloneでできた不要ファイルがあればそれも削除
composerのインストール
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# Composer の実行ファイル(phar)を作成
$ php composer-setup.php
# インストーラの削除
$ php -r "unlink('composer-setup.php');"
# パスの通っているディレクトリへ移動させる
$ mv composer.phar /usr/local/bin/composer
# パスが通っているか確認
$ composer -v
# domのインストール
$ sudo yum install php-xml
# composerのインストール
$ cd /var/www/git cloneしたフォルダ名
$ composer install
Virtualboxの時は、installでエラーがでて、updateしました。
本番環境はinstallが通りました。
DBの作成
# MariaDBに接続
$ mysql -u root -p
# DBの作成
# (例)create database db_name;
MariaDB [(none)]> create database DBの名前;
# DBが作成されているか確認
MariaDB [(none)]> show databases;
# HOSTのコピー
MariaDB [(none)]> SELECT User, Host FROM mysql.user;
+-------------+-------------------------------+
| User | Host |
+-------------+-------------------------------+
| | これをコピー |
# DBユーザーの作成(Hostをペースト)
# (例)GRANT USAGE ON db_name.* to db_user@localhost IDENTIFIED BY 'password';
MariaDB [(none)]> grant all privileges on db名.* to dbユーザー名@HOSTをペースト identified by 'パスワード';
# 権限確認
MariaDB [(none)]> show grants for [DBユーザー]@コピペしたHOST;
Apacheの設定
firewallの設定
ファイアウォールが走っていなかったので、【CentOS7】Apache2.4のインストール方法とhttpd.confの設定のApacheをインストールを参考に設定しました。
最終的にアクセスするサイトはhttpですが、一応httpsの設定も。
# ファイアウォールの設定をしようとしたら、走ってませんでした。
$ firewall-cmd --permanent --add-service=http
FirewallD is not running
# まずはファイアウォールの状態確認
# deadと表示されていたので、停止状態
$ systemctl status firewalld
# ファイアウォールの起動とサーバー再起動時にファイアウォールも起動するように設定
$ systemctl start firewalld.service
$ systemctl enable firewalld
# firewalldでポートが許可されているかを確認
$ firewall-cmd --list-services --zone=public --permanent
dhcpv6-client ssh
# HTTP(80)とHTTPS(443)を開放
$ firewall-cmd --permanent --zone=public --add-service=http
$ firewall-cmd --permanent --zone=public --add-service=https
# ファイアウォールの設定を反映と再起動
$ firewall-cmd --reload
$ systemctl restart firewalld
# firewalldでポートが許可されているかを確認
$ firewall-cmd --list-services --zone=public --permanent
dhcpv6-client ssh http https
httpd.confへ追記
VirtualHostの設定をするので下記を参考に編集しました。
まずはhttpd.confのバックアップ
$ cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bk
# 指定のIPアドレスを追記
# Listen 12.34.56.78:80
↓
Listen xxx.xxx.xxx.xx:80
# 管理者用のメールアドレスに変更
ServerAdmin root@localhost
↓
ServerAdmin xxx@xxx.com
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
# 下記に変更
<Directory />
AllowOverride All
Require all granted
</Directory>
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
# 下記に変更(※1)
DocumentRoot "/var/www/git cloneしたフォルダ名/public"
# 一番下に追記
<VirtualHost *:80>
DocumentRoot /var/www/git cloneしたフォルダ名/public
ServerName http://指定のアドレス/
<Directory "/var/www/git cloneしたフォルダ名/public">
AllowOverride All
Require all granted
Options FollowSymLinks
</Directory>
</VirtualHost>
# Apacheの再起動
$ systemctl restart httpd
VirtualHostを使うので、(※1)のところの変更は不要かと思っていましたが、再起動時にエラーがでたため変更。
Job for httpd.service failed because the control process exited with error code. See "systemctl status httpd.service" and "journalctl -xe" for details.
# 下記コマンドでエラー箇所を教えてくれる
$ service httpd configtest
Laravelの設定
キャッシュのクリア
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
$ php artisan route:cache
Route cache cleared!
パーミッションの変更
$ chmod 777 storage -R
$ chmod 777 bootstrap -R
.envの設定
下記を参考にしました。
$ cp .env.example .env
$ vi .env
APP_NAME=example ←自分のプロジェクト名を入れましょう
APP_ENV=production ←本番環境ならproductionに変更しましょう
APP_KEY= ←後ほど説明します
APP_DEBUG=false ←本番環境ならfalseにしましょう trueだと画面上にエラーが表示されます
APP_URL=http://hogehoge.com ←URLを入れましょう
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 ←DBサーバーが別れている場合はDBサーバーのIPを入れましょう
DB_PORT=3306
DB_DATABASE=hoge ←このプロジェクトでアクセスするデータベースです
DB_USERNAME=fugauser ←このプロジェクトのDBアクセスで使うユーザーです
DB_PASSWORD=passwordhoge ←このプロジェクトのDBアクセスで使うパスワードです
サイトを参考に以下のようにenvを編集
APP_NAME=git cloneしたフォルダ名に変更
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=http://指定のアドレス/
LOG_CHANNEL=stack
LOG_LEVEL=error
DB_CONNECTION=mysql
DB_HOST=db作った時のhostをペースト
DB_PORT=3306
DB_DATABASE=データベース名
DB_USERNAME=ユーザー名
DB_PASSWORD=パスワード
キーの作成
$ php artisan key:generate
$ php artisan config:clear
シンボリックリンクの作成
$ php artisan storage:link
The [/var/www/git cloneしたフォルダ名/public/storage] link has been connected to [/var/www/git cloneしたフォルダ名/storage/app/public].
The links have been created.
could not find driverの解決
php artisan migrate
を行うとcould not find driverが出たので下記コマンドで解決。
$ yum -y install --enablerepo=remi,remi-php81 php-mysqlnd
# appacheの再起動
$ sudo service httpd restart
開発環境だとClass "PDO" not foundがでたんですが、本番環境ではでませんでした。
php artisan migrate
$ php artisan migrate
# seedを作ってる場合はこちら
$ php artisan migrate:refresh --seed
無事にマイグレーションが通りました。
500 SERVER ERRORの解決
500 server error エラーの原因を確認する方法を参考にエラーの調査をします。
$ cd /var/www/git cloneしたフォルダ名
$ cd storage/logs
$ cat laravel.log
# laravel.log
production.ERROR: Your serialized closure might have been modified or it's unsafe to be unserialized.
# 解決方法
$ php artisan route:clear
$ php artisan cache:clear
$ php artisan config:clear
$ php artisan view:clear
上記エラーは、laravelで起こったトラブルシューティング(エラー解消)を参考に解決。
500 SERVER ERRORが解決しました。