Laravel5.5を8.58にアップグレードした際の手順メモ


PHP のWebアプリケーションフレームワーク Laravel を最新バージョンに更新しました。
次回また必要になったときの、ほぼ個人的なメモなので解説はありません。環境に合わせて読み替えて下さい。

環境

Red Hat Enterprise Linux のリビルド版だった CentOS のサポート終了に伴い、Laravel移行と同じタイミングで Ubuntu に乗り換えています。

旧環境
$ cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

$ php -v
PHP 7.2.3 (cli) (built: Feb 28 2018 08:39:13) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

$ php artisan --version
Laravel Framework 5.5.50
新環境
$ cat /etc/os-release
VERSION="20.04.3 LTS (Focal Fossa)"

$ php -v
PHP 7.4.3 (cli) (built: Jul  5 2021 15:13:35) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

$ php artisan --version
Laravel Framework 8.58.0

手順

一般的には、composer.json を編集し composer updateコマンドで更新という手順になると思いますが、バージョンが開き過ぎているので、最新の Laravel を新規インストールし、旧プロジェクトファイルを手作業でマージしていくことにしました。

composer で Laravel をインストール

cd /var/www/html
composer create-project laravel/laravel [プロジェクト名]
cd [プロジェクト名]

chown -R www-data:www-data .
find . -type d -exec chmod 770 {} \;
find . -type f -exec chmod 660 {} \;
chmod -R 777 ./storage ./bootstrap/cache

validation.php 言語ファイルの日本語化

php -r "copy('https://readouble.com/laravel/8.x/ja/install-ja-lang-files.php', 'install-ja-lang.php');"
php -f install-ja-lang.php
php -r "unlink('install-ja-lang.php');"

Laravel Fortify のインストール

composer require laravel/fortify
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
config/app.php
    'providers' => [
        App\Providers\FortifyServiceProvider::class,  // 追加
config/fortify.php
return [
    'username' => 'user_id',   // 必要に応じて修正
    :
    // 必要に応じてコメントアウト
    'features' => [
        Features::registration(),
        Features::resetPasswords(),
        //Features::emailVerification(),
        //Features::updateProfileInformation(),
        //Features::updatePasswords(),
        //Features::twoFactorAuthentication([
        //    'confirmPassword' => true,
        //]),
    ],

config の下を編集した場合は php artisan config:clear を忘れずに。

app/Providers/FortifyServiceProvider.php
public function boot()
{
    :
    Fortify::viewPrefix('auth.');  // 追加
app/Providers/RouteServiceProvider.php
//public const HOME = '/home';
public const HOME = '/xxxxx';  // 必要に応じて修正

protected $namespace = 'App\\Http\\Controllers';  // コメントアウトを外す

Laravel Debugbar のインストール

composer require barryvdh/laravel-debugbar
php artisan vendor:publish --provider="Barryvdh\Debugbar\ServiceProvider"

データベース設定

$ mysql -u root
CREATE DATABASE [データベース名];
CREATE USER '[ユーザ名]'@'localhost' IDENTIFIED BY '[パスワード]';
GRANT ALL PRIVILEGES ON [データベース名].* TO '[ユーザ名]'@'localhost';
FLUSH PRIVILEGES;
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=[データベース名]
DB_USERNAME=[ユーザ名]
DB_PASSWORD=[パスワード]

Apache設定

/etc/apache2/sites-available/virtual-host.conf
Alias /[プロジェクト名] /var/www/html/[プロジェクト名]/public
<Directory "/var/www/html/[プロジェクト名]/public">
    AllowOverride All
    Require all granted
</Directory>
/var/www/html/[プロジェクト名]/public/.htaccess
RewriteBase /[プロジェクト名]

Apache の再起動

apache2ctl configtest
systemctl restart apache2

ルーティング設定

routes/web.php
Route::get('/', function () {
  //return view('welcome');
  return view('auth.login');  // 必要に応じて修正
});
Route::group(['middleware' => 'auth'], function () {

最後に、コントローラやビュー、モデルの他、個別に修正していたファイルがあればマージして完成です。
モデルは\App\Modelsに置くのがデフォルトになりました。