LoginSignup
1
1

More than 1 year has passed since last update.

VirtualboxのLamp環境にLaravelアプリをgit cloneする

Last updated at Posted at 2022-02-11

この記事の続きです。

「Laravelアプリをgit cloneする」というのは、以前作っていたLaravelアプリをVirtualboxのLamp環境にcloneするからです。

環境

  • Mac
  • VirtualBox
  • Lamp環境(CentOS7.9, MariaDB)

参考記事

パッケージのアップデート

環境構築の記事ではCentOS7.8にしていましたが、7.9にアップデートします。

$ yum update

$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

OSの設定

SELinuxの無効化

# SELinuxの状態の確認(Enforcingは有効)
$ getenforce
Enforcing

$ vi /etc/selinux/config
SELINUX=disabled

# 再起動
$ reboot

# SELinuxの状態の確認(Disabledは無効)
$ getenforce
Disabled

git clone

# gitのインストール 
$ yum install git

# ついでに
$ yum install zip unzip

$ cd /var/www

# ファイルがあるとgit cloneできないので削除
$ rm -r html
$ rm -r cgi-bin

# git clone
# 最後の . は、つけることによってカレントディレクトリにディレクトリを作らずにcloneすることができます
$ git clone https://github.com/アカウント名/リポジトリ名.git .

元々Dockerで作っていたので、clone後にymlなども削除。

composerのインストール

# コマンド入力すると何も起きませんが、lsやllで確認するとコピーされたのがわかります
$ 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

# composerのインストール
$ cd /var/www/git cloneしたフォルダ名
$ composer install

composer installのエラー

composer installをするとエラーが出ました。
一回目の構築中は雑なメモを取っていたので、記事を書くのための二回目の構築中でこんなエラーでたっけ?みたいになりました。
この記事を参考にすると、vendorがないのでcomposer updateをするといいみたいです。
たしかにvendorなくてエラーでてあれこれ探してコマンド打った記憶が・・・。
domインストールで参考にしたサイト

$ composer update
  Problem 1
    - phpunit/phpunit[9.3.3, ..., 9.5.x-dev] require ext-dom * -> it is missing from your system. Install or enable PHP's dom extension.
    - Root composer.json requires phpunit/phpunit ^9.3.3 -> satisfiable by phpunit/phpunit[9.3.3, ..., 9.5.x-dev].
    ・
    ・
    ・

# domのインストール 
$ sudo yum install php-xml

# domをインストールしたことによってcomposer updateが通りました。
$ composer update

DBの作成

実は一番躓いたところ。
Dockerで作っている時は、ymlに書けばdbもユーザーも作られていました。
そのためdbを作ることをすっかり忘れていて、dbが認識されないのは何故なのかと悩んでいました。

# MariaDBに接続
$ mysql -u root -p

# DBの作成
# (例)create database db_name;
MariaDB [(none)]> create database DBの名前;

# DBが作成されているか確認
MariaDB [(none)]> show databases;

# DBユーザーの作成
# (例)GRANT USAGE ON db_name.* to db_user@localhost IDENTIFIED BY 'password';
MariaDB [(none)]> grant all privileges on db名.* to dbユーザー名@localhost identified by 'パスワード';

# 権限確認
MariaDB [(none)]> show grants for [DBユーザー]@localhost;

メモ:
最初は、ユーザーをdbユーザー名@"%"で作成しましたが、migration通りませんでした。
そのため、dbユーザー名@localhostで作り直しました。

Apacheの設定

ファイヤウォールの設定

$ firewall-cmd --permanent --add-service=http
$ firewall-cmd --reload
$ systemctl restart firewalld

httpd.confへ追記

$ vi /etc/httpd/conf/httpd.conf

# ServerName www.example.com:80
ServerName localhost:80

# 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 none
    Require all denied
</Directory>
↓
<Directory />
    AllowOverride All
</Directory>
# こういう変更をしている参考サイトはなかったのですが、こうしないとtesting 123にとばされてしまいます


# 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.
#
DocumentRoot "/var/www/html"
↓
DocumentRoot "/var/www/git cloneしたフォルダ名/public"

# Relax access to content within /var/www.
# 以下に変更
<Directory "/var/www/git cloneしたフォルダ名/public">
    AllowOverride All
</Directory>

# Apacheの再起動
$ systemctl restart httpd

Apacheの再起動時のエラー

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
$ php artisan route:cache

# パーミッションの変更
$ chmod 777 storage -R
$ chmod 777 bootstrap -R

.env設定

$ cp .env.example .env
$ vi .env

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dbの名前
DB_USERNAME=userの名前
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.

Class "PDO" not foundが出た場合

Laravel で Class 'PDO' not found というエラーが出た場合の対処(CentOS7)

$ yum install --enablerepo=remi,remi-php81 php-pdo

php artisan migrate

$ php artisan migrate

# seedを作っていたので
$ php artisan migrate:refresh --seed

could not find driverが出た場合

Laravel +Vagrant + CentOS環境でphp artisan migrate時のcould not find driverエラーの解決方法

$ yum -y install --enablerepo=remi,remi-php81 php-mysqlnd

# appacheの再起動
$ sudo service httpd restart

以下エラーがでたので実行
再度chmodをするのが謎です

UnexpectedValueException
The stream or file "/var/www/git cloneしたフォルダ名/storage/logs/laravel.log" could not be opened in append mode: Failed to open stream: Permission denied

$ chmod 777 -R storage
Your serialized closure might have been modified and it's unsafe to be unserialized. Make sure you use the same security provider, with the same settings, both for serialization and unserialization.

$ php artisan route:clear
$ php artisan cache:clear

解決していないエラー

php artisan migrateは問題ないのですが、php artisan migrate:refresh --seedしてアクセスするとタイムアウトに・・・。
1回目の構築時のメモが雑だったので、2回目の構築の時にそのメモを見ながらやったら何が原因かわからずタイムアウト状態に。

1
1
0

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
1
1