2020年10月18日最終更新
1.Laravelをインストールするための事前準備
Laravelをインストールするためにあらかじめ必要な物はPHP、ApacheとComposerになります。下記の記事でそれぞれのやり方について詳しく解説していますので、もしインストールされていない場合は先にそちらをご覧ください。
1.1.PHPのインストール
1.2.Apachのインストール
1.3.Composerのインストール
2.Laravelのインストール
インストールしたComposerコマンドを利用して、Laravelのインストーラを導入します。
composer global require laravel/installer
インストールが完了すると下記のメッセージが流れます。
[centos@ip-xx-xx-xx-xx html]$ composer global require laravel/installer
Changed current directory to /home/centos/.config/composer
Using version ^3.0 for laravel/installer
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 16 installs, 0 updates, 0 removals
- Installing symfony/process (v5.0.8): Downloading (100%)
- Installing symfony/polyfill-ctype (v1.16.0): Downloading (100%)
- Installing symfony/filesystem (v5.0.8): Downloading (100%)
- Installing psr/container (1.0.0): Downloading (100%)
- Installing symfony/service-contracts (v2.0.1): Downloading (100%)
- Installing symfony/polyfill-php73 (v1.16.0): Downloading (100%)
- Installing symfony/polyfill-mbstring (v1.16.0): Downloading (100%)
- Installing symfony/console (v5.0.8): Downloading (100%)
- Installing ralouphie/getallheaders (3.0.3): Downloading (100%)
- Installing psr/http-message (1.0.1): Downloading (100%)
- Installing guzzlehttp/psr7 (1.6.1): Downloading (100%)
- Installing guzzlehttp/promises (v1.3.1): Downloading (100%)
- Installing symfony/polyfill-php72 (v1.16.0): Downloading (100%)
- Installing symfony/polyfill-intl-idn (v1.16.0): Downloading (100%)
- Installing guzzlehttp/guzzle (6.5.3): Downloading (100%)
- Installing laravel/installer (v3.0.1): Downloading (100%)
symfony/service-contracts suggests installing symfony/service-implementation
symfony/console suggests installing symfony/event-dispatcher
symfony/console suggests installing symfony/lock
symfony/console suggests installing psr/log (For using the console logger)
guzzlehttp/psr7 suggests installing zendframework/zend-httphandlerrunner (Emit PSR-7 responses)
symfony/polyfill-intl-idn suggests installing ext-intl (For best performance)
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
Writing lock file
Generating autoload files
8 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
インストール出来たら、パスを通します。
export PATH=$PATH:/laravelのインストール先ディレクトリ/.config/composer/vendor/bin
例えばドキュメントルート配下に
Laravelのコマンドを使ってプロジェクトを作成できます。
cd /var/www/html/test
laravel new "プロジェクト名"
でプロジェクトが作成できます。
作成したディレクトリに書き込み権限がないと下記のエラーメッセージが出てきたりしますので気をつけましょう。
Crafting application...
PHP Warning: file_put_contents(/var/www/html/laravel_8364782359246e8ae9f4d45bb28ea65d.zip): failed to open stream: Permission denied in /home/centos/.config/composer/vendor/laravel/installer/src/NewCommand.php on line 147
PHP Warning: ZipArchive::extractTo(): Permission denied in /home/centos/.config/composer/vendor/laravel/installer/src/NewCommand.php on line 169
PHP Warning: ZipArchive::close(): Invalid or uninitialized Zip object in /home/centos/.config/composer/vendor/laravel/installer/src/NewCommand.php on line 171
You should verify that the "storage" and "bootstrap/cache" directories are writable.
In Process.php line 332:
The provided cwd "/var/www/html/test2" does not exist.
インストール後プロジェクトディレクトリを展開してみると下記の様になっています。public配下がLaravelのルートディレクトリになります。
README.md artisan composer.json config package-lock.json phpunit.xml resources server.php tests webpack.mix.js
app bootstrap composer.lock database package.json public routes storage vendor
3.ドキュメントルートの変更
インストールしただけでは、Laravelの画面にアクセスできません。
Apacheの設定ファイル/etc/httpd/conf/httpd.conf
でドキュメントルートをLaravelのインストール先に変更する必要があります。
Overrideを許可します。
#DocumentRoot "/var/www/html"
DocumentRoot "/var/www/html/Laravelのインストール先/public"
<Directory "/var/www/html/Laravelのインストール先/public">
#AllowOverride none
AllowOverride ALL #Overrideを許可します
# Allow open access:
Require all granted
</Directory>
設定を書き換えたら、Apacheを再起動します。
sudo systemctl restart httpd
4.ディレクトリ 権限の変更
ここまでで、ブラウザからアクセスすることはできるのですが、例えば下記の様なエラーが出ているかと思います。

インストール直後は、いくつかのディレクトリ の権限を変更する必要があります。
chmod -R 777 ./storage/logs
chmod -R 777 ./bootstrap/cache
もし、上記を利用してもエラーメッセージが変化しない場合は、SELinuxが有効化されている可能性があるので、無効化して再起動してみましょう。
sudo setsebool httpd_can_network_connect 0
sudo systemctl restart httpd
成功するとLaravelのWelcomeページにアクセスできます。
