Linux 用 Windows サブシステム2 (WSL2)のトップページ


Linux 用 Windows サブシステム2 (WSL2)

Laravel で Web システムを開発する

プロジェクトの作成

WSL をインストールした後にここの手順で各種ソフトウェアをインストールすれば既に Laravel で Web システムを開発するための機能が設定されています.

Laravel のコンポーザはユーザホームディレクトリの「Documents/laravel」フォルダの中にインストールされています.

rinsaka@XPS2019:~$ cd Documents/ ⏎
rinsaka@XPS2019:~/Documents$ ls ⏎
laravel
rinsaka@XPS2019:~/Documents$ cd laravel/ ⏎
rinsaka@XPS2019:~/Documents/laravel$ ls -l ⏎
total 2768
-rwxr-xr-x 1 rinsaka rinsaka 2831608 Mar 18 09:02 composer.phar
rinsaka@XPS2019:~/Documents/laravel$

myapp という名前(何でもよい)のプロジェクトを作成します.このとき,プロジェクトのフォルダが生成され,多くのファイルがインストールされます.

rinsaka@XPS2019:~/Documents/laravel$ php composer.phar create-project --prefer-dist laravel/laravel myapp ⏎
Creating a "laravel/laravel" project at "./myapp"
Info from https://repo.packagist.org: #StandWithUkraine
Installing laravel/laravel (v10.0.4)
  - Installing laravel/laravel (v10.0.4): Extracting archive
Created project in /home/rinsaka/Documents/laravel/myapp
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Lock file operations: 106 installs, 0 updates, 0 removals
  - Locking brick/math (0.10.2)
  - Locking dflydev/dot-access-data (v3.0.2)
  - Locking doctrine/inflector (2.0.6)
  - Locking doctrine/lexer (3.0.0)
  - Locking dragonmantank/cron-expression (v3.3.2)
  - Locking egulias/email-validator (4.0.1)
  - Locking fakerphp/faker (v1.21.0)
  - Locking filp/whoops (2.15.1)
  - Locking fruitcake/php-cors (v1.2.0)
  - Locking graham-campbell/result-type (v1.1.1)
  - Locking guzzlehttp/guzzle (7.5.0)
  - Locking guzzlehttp/promises (1.5.2)
  - Locking guzzlehttp/psr7 (2.4.4)
  - Locking guzzlehttp/uri-template (v1.0.1)
  - Locking hamcrest/hamcrest-php (v2.0.1)
  - Locking laravel/framework (v10.4.0)
  - Locking laravel/pint (v1.6.0)
  - Locking laravel/sail (v1.21.2)
  - Locking laravel/sanctum (v3.2.1)
  - Locking laravel/serializable-closure (v1.3.0)
  - Locking laravel/tinker (v2.8.1)
  - Locking league/commonmark (2.3.9)
  - Locking league/config (v1.2.0)
  - Locking league/flysystem (3.12.3)
  - Locking league/mime-type-detection (1.11.0)
  - Locking mockery/mockery (1.5.1)
  - Locking monolog/monolog (3.3.1)
  - Locking myclabs/deep-copy (1.11.1)
  - Locking nesbot/carbon (2.66.0)
  - Locking nette/schema (v1.2.3)
  - Locking nette/utils (v4.0.0)
  - Locking nikic/php-parser (v4.15.4)
  - Locking nunomaduro/collision (v7.1.2)
  - Locking nunomaduro/termwind (v1.15.1)
  - Locking phar-io/manifest (2.0.3)
  - Locking phar-io/version (3.2.1)
  - Locking phpoption/phpoption (1.9.1)
  - Locking phpunit/php-code-coverage (10.0.2)
  - Locking phpunit/php-file-iterator (4.0.1)
  - Locking phpunit/php-invoker (4.0.0)
  - Locking phpunit/php-text-template (3.0.0)
  - Locking phpunit/php-timer (6.0.0)
  - Locking phpunit/phpunit (10.0.16)
  - Locking psr/container (2.0.2)
  - Locking psr/event-dispatcher (1.0.0)
  - Locking psr/http-client (1.0.1)
  - Locking psr/http-factory (1.0.1)
  - Locking psr/http-message (1.0.1)
  - Locking psr/log (3.0.0)
  - Locking psr/simple-cache (3.0.0)
  - Locking psy/psysh (v0.11.12)
  - Locking ralouphie/getallheaders (3.0.3)
  - Locking ramsey/collection (2.0.0)
  - Locking ramsey/uuid (4.7.3)
  - Locking sebastian/cli-parser (2.0.0)
  - Locking sebastian/code-unit (2.0.0)
  - Locking sebastian/code-unit-reverse-lookup (3.0.0)
  - Locking sebastian/comparator (5.0.0)
  - Locking sebastian/complexity (3.0.0)
  - Locking sebastian/diff (5.0.0)
  - Locking sebastian/environment (6.0.0)
  - Locking sebastian/exporter (5.0.0)
  - Locking sebastian/global-state (6.0.0)
  - Locking sebastian/lines-of-code (2.0.0)
  - Locking sebastian/object-enumerator (5.0.0)
  - Locking sebastian/object-reflector (3.0.0)
  - Locking sebastian/recursion-context (5.0.0)
  - Locking sebastian/type (4.0.0)
  - Locking sebastian/version (4.0.1)
  - Locking spatie/backtrace (1.4.0)
  - Locking spatie/flare-client-php (1.3.5)
  - Locking spatie/ignition (1.4.5)
  - Locking spatie/laravel-ignition (2.0.0)
  - Locking symfony/console (v6.2.7)
  - Locking symfony/css-selector (v6.2.7)
  - Locking symfony/deprecation-contracts (v3.2.1)
  - Locking symfony/error-handler (v6.2.7)
  - Locking symfony/event-dispatcher (v6.2.7)
  - Locking symfony/event-dispatcher-contracts (v3.2.1)
  - Locking symfony/finder (v6.2.7)
  - Locking symfony/http-foundation (v6.2.7)
  - Locking symfony/http-kernel (v6.2.7)
  - Locking symfony/mailer (v6.2.7)
  - Locking symfony/mime (v6.2.7)
  - Locking symfony/polyfill-ctype (v1.27.0)
  - Locking symfony/polyfill-intl-grapheme (v1.27.0)
  - Locking symfony/polyfill-intl-idn (v1.27.0)
  - Locking symfony/polyfill-intl-normalizer (v1.27.0)
  - Locking symfony/polyfill-mbstring (v1.27.0)
  - Locking symfony/polyfill-php72 (v1.27.0)
  - Locking symfony/polyfill-php80 (v1.27.0)
  - Locking symfony/polyfill-uuid (v1.27.0)
  - Locking symfony/process (v6.2.7)
  - Locking symfony/routing (v6.2.7)
  - Locking symfony/service-contracts (v3.2.1)
  - Locking symfony/string (v6.2.7)
  - Locking symfony/translation (v6.2.7)
  - Locking symfony/translation-contracts (v3.2.1)
  - Locking symfony/uid (v6.2.7)
  - Locking symfony/var-dumper (v6.2.7)
  - Locking symfony/yaml (v6.2.7)
  - Locking theseer/tokenizer (1.2.1)
  - Locking tijsverkoyen/css-to-inline-styles (2.2.6)
  - Locking vlucas/phpdotenv (v5.5.0)
  - Locking voku/portable-ascii (2.0.1)
  - Locking webmozart/assert (1.11.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 106 installs, 0 updates, 0 removals
  - Installing doctrine/inflector (2.0.6): Extracting archive
  - Installing doctrine/lexer (3.0.0): Extracting archive
  - Installing symfony/polyfill-ctype (v1.27.0): Extracting archive
  - Installing webmozart/assert (1.11.0): Extracting archive
  - Installing dragonmantank/cron-expression (v3.3.2): Extracting archive
  - Installing symfony/deprecation-contracts (v3.2.1): Extracting archive
  - Installing psr/container (2.0.2): Extracting archive
  - Installing fakerphp/faker (v1.21.0): Extracting archive
  - Installing symfony/polyfill-mbstring (v1.27.0): Extracting archive
  - Installing symfony/http-foundation (v6.2.7): Extracting archive
  - Installing fruitcake/php-cors (v1.2.0): Extracting archive
  - Installing psr/http-message (1.0.1): Extracting archive
  - Installing psr/http-client (1.0.1): Extracting archive
  - Installing ralouphie/getallheaders (3.0.3): Extracting archive
  - Installing psr/http-factory (1.0.1): Extracting archive
  - Installing guzzlehttp/psr7 (2.4.4): Extracting archive
  - Installing guzzlehttp/promises (1.5.2): Extracting archive
  - Installing guzzlehttp/guzzle (7.5.0): Extracting archive
  - Installing symfony/polyfill-php80 (v1.27.0): Extracting archive
  - Installing guzzlehttp/uri-template (v1.0.1): Extracting archive
  - Installing laravel/pint (v1.6.0): Extracting archive
  - Installing symfony/yaml (v6.2.7): Extracting archive
  - Installing voku/portable-ascii (2.0.1): Extracting archive
  - Installing phpoption/phpoption (1.9.1): Extracting archive
  - Installing graham-campbell/result-type (v1.1.1): Extracting archive
  - Installing vlucas/phpdotenv (v5.5.0): Extracting archive
  - Installing symfony/css-selector (v6.2.7): Extracting archive
  - Installing tijsverkoyen/css-to-inline-styles (2.2.6): Extracting archive
  - Installing symfony/var-dumper (v6.2.7): Extracting archive
  - Installing symfony/polyfill-uuid (v1.27.0): Extracting archive
  - Installing symfony/uid (v6.2.7): Extracting archive
  - Installing symfony/routing (v6.2.7): Extracting archive
  - Installing symfony/process (v6.2.7): Extracting archive
  - Installing symfony/polyfill-php72 (v1.27.0): Extracting archive
  - Installing symfony/polyfill-intl-normalizer (v1.27.0): Extracting archive
  - Installing symfony/polyfill-intl-idn (v1.27.0): Extracting archive
  - Installing symfony/mime (v6.2.7): Extracting archive
  - Installing symfony/service-contracts (v3.2.1): Extracting archive
  - Installing psr/event-dispatcher (1.0.0): Extracting archive
  - Installing symfony/event-dispatcher-contracts (v3.2.1): Extracting archive
  - Installing symfony/event-dispatcher (v6.2.7): Extracting archive
  - Installing psr/log (3.0.0): Extracting archive
  - Installing egulias/email-validator (4.0.1): Extracting archive
  - Installing symfony/mailer (v6.2.7): Extracting archive
  - Installing symfony/error-handler (v6.2.7): Extracting archive
  - Installing symfony/http-kernel (v6.2.7): Extracting archive
  - Installing symfony/finder (v6.2.7): Extracting archive
  - Installing symfony/polyfill-intl-grapheme (v1.27.0): Extracting archive
  - Installing symfony/string (v6.2.7): Extracting archive
  - Installing symfony/console (v6.2.7): Extracting archive
  - Installing ramsey/collection (2.0.0): Extracting archive
  - Installing brick/math (0.10.2): Extracting archive
  - Installing ramsey/uuid (4.7.3): Extracting archive
  - Installing psr/simple-cache (3.0.0): Extracting archive
  - Installing nunomaduro/termwind (v1.15.1): Extracting archive
  - Installing symfony/translation-contracts (v3.2.1): Extracting archive
  - Installing symfony/translation (v6.2.7): Extracting archive
  - Installing nesbot/carbon (2.66.0): Extracting archive
  - Installing monolog/monolog (3.3.1): Extracting archive
  - Installing league/mime-type-detection (1.11.0): Extracting archive
  - Installing league/flysystem (3.12.3): Extracting archive
  - Installing nette/utils (v4.0.0): Extracting archive
  - Installing nette/schema (v1.2.3): Extracting archive
  - Installing dflydev/dot-access-data (v3.0.2): Extracting archive
  - Installing league/config (v1.2.0): Extracting archive
  - Installing league/commonmark (2.3.9): Extracting archive
  - Installing laravel/serializable-closure (v1.3.0): Extracting archive
  - Installing laravel/framework (v10.4.0): Extracting archive
  - Installing laravel/sail (v1.21.2): Extracting archive
  - Installing laravel/sanctum (v3.2.1): Extracting archive
  - Installing nikic/php-parser (v4.15.4): Extracting archive
  - Installing psy/psysh (v0.11.12): Extracting archive
  - Installing laravel/tinker (v2.8.1): Extracting archive
  - Installing hamcrest/hamcrest-php (v2.0.1): Extracting archive
  - Installing mockery/mockery (1.5.1): Extracting archive
  - Installing filp/whoops (2.15.1): Extracting archive
  - Installing nunomaduro/collision (v7.1.2): Extracting archive
  - Installing sebastian/version (4.0.1): Extracting archive
  - Installing sebastian/type (4.0.0): Extracting archive
  - Installing sebastian/recursion-context (5.0.0): Extracting archive
  - Installing sebastian/object-reflector (3.0.0): Extracting archive
  - Installing sebastian/object-enumerator (5.0.0): Extracting archive
  - Installing sebastian/global-state (6.0.0): Extracting archive
  - Installing sebastian/exporter (5.0.0): Extracting archive
  - Installing sebastian/environment (6.0.0): Extracting archive
  - Installing sebastian/diff (5.0.0): Extracting archive
  - Installing sebastian/comparator (5.0.0): Extracting archive
  - Installing sebastian/code-unit (2.0.0): Extracting archive
  - Installing sebastian/cli-parser (2.0.0): Extracting archive
  - Installing phpunit/php-timer (6.0.0): Extracting archive
  - Installing phpunit/php-text-template (3.0.0): Extracting archive
  - Installing phpunit/php-invoker (4.0.0): Extracting archive
  - Installing phpunit/php-file-iterator (4.0.1): Extracting archive
  - Installing theseer/tokenizer (1.2.1): Extracting archive
  - Installing sebastian/lines-of-code (2.0.0): Extracting archive
  - Installing sebastian/complexity (3.0.0): Extracting archive
  - Installing sebastian/code-unit-reverse-lookup (3.0.0): Extracting archive
  - Installing phpunit/php-code-coverage (10.0.2): Extracting archive
  - Installing phar-io/version (3.2.1): Extracting archive
  - Installing phar-io/manifest (2.0.3): Extracting archive
  - Installing myclabs/deep-copy (1.11.1): Extracting archive
  - Installing phpunit/phpunit (10.0.16): Extracting archive
  - Installing spatie/backtrace (1.4.0): Extracting archive
  - Installing spatie/flare-client-php (1.3.5): Extracting archive
  - Installing spatie/ignition (1.4.5): Extracting archive
  - Installing spatie/laravel-ignition (2.0.0): Extracting archive
50 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi

   INFO  Discovering packages.

  laravel/sail ............................................................... DONE
  laravel/sanctum ............................................................ DONE
  laravel/tinker ............................................................. DONE
  nesbot/carbon .............................................................. DONE
  nunomaduro/collision ....................................................... DONE
  nunomaduro/termwind ........................................................ DONE
  spatie/laravel-ignition .................................................... DONE

80 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force

   INFO  No publishable resources for tag [laravel-assets].

No security vulnerability advisories found
> @php artisan key:generate --ansi

   INFO  Application key set successfully.

rinsaka@XPS2019:~/Documents/laravel$

データベースの作成

バックエンドのデータベースには SQLite や MySQL などを利用できます.

データベースに SQLite を利用する場合は,database フォルダに空のデータベースファイルを作成します.

rinsaka@XPS2019:~/Documents/laravel$ cd myapp ⏎
rinsaka@XPS2019:~/Documents/laravel/myapp$ ls ⏎
README.md  artisan    composer.json  config    package.json  public     routes   tests   vite.config.js
app        bootstrap  composer.lock  database  phpunit.xml   resources  storage  vendor
rinsaka@XPS2019:~/Documents/laravel/myapp$ touch database/database.sqlite ⏎
rinsaka@XPS2019:~/Documents/laravel/myapp$

MySQL を使う場合は,データベースとユーザを作成します.具体的には,(1) 管理者 (root) でログインする,(2) myappDB という名前のデータベースを作成する,(3) ユーザ myapp_user を作成しパスワードを「hogehogehoge」にする,(4) データベース myappDB に対する権限をユーザ myapp_user 付与する,という手順を行います.

rinsaka@XPS2019:~/Documents/laravel$ mysql -u root -p ⏎ # (1) 管理者 (root) でログイン
Enter password: # パスワードを入力(表示されない)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES; ⏎ # データベースの一覧を確認
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> CREATE DATABASE myappDB; ⏎ # (2) データベースを作成
Query OK, 1 row affected (0.09 sec)

mysql> SHOW DATABASES; ⏎ # データベースの一覧を確認
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myappDB            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> CREATE USER 'myapp_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'hogehogehoge'; ⏎ # (3) ユーザを作成
Query OK, 0 rows affected (0.02 sec)

mysql> GRANT ALL PRIVILEGES ON myappDB.* TO 'myapp_user'@'localhost'; ⏎ # (4) 権限を付与
Query OK, 0 rows affected (0.02 sec)

mysql> FLUSH PRIVILEGES; ⏎ # 権限変更を有効化
Query OK, 0 rows affected (0.01 sec)

mysql> EXIT ⏎
Bye
rinsaka@XPS2019:~/Documents/laravel$

いま作成した myapp_user で MySQL にログインでき,myappDB がそのユーザに見えている(mysql と sys データベースは見えていない)ことを念のために確認しておきます.

rinsaka@XPS2019:~/Documents/laravel$ mysql -u myapp_user -p ⏎
Enter password: # パスワードを入力(表示されない)
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.32-0ubuntu0.22.04.2 (Ubuntu)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SHOW DATABASES; ⏎
+--------------------+
| Database           |
+--------------------+
| information_schema |
| myappDB            |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)

mysql> EXIT ⏎
Bye
rinsaka@XPS2019:~/Documents/laravel$

なお,システムの再起動などの影響で MySQL が起動していない場合はここに示す手順で MySQL を起動する必要があります.

プロジェクトの初期設定

Laravel プロジェクト内のファイルを編集して初期設定を行います.次のように Visual Studio Code で編集すると良いでしょう.

rinsaka@XPS2019:~/Documents/laravel$ cd myapp/ ⏎
rinsaka@XPS2019:~/Documents/laravel/myapp$ ls -la ⏎
total 376
drwxr-xr-x 12 rinsaka rinsaka   4096 Mar 18 09:35 .
drwx------  3 rinsaka rinsaka   4096 Mar 18 09:35 ..
-rw-r--r--  1 rinsaka rinsaka    258 Feb 28 03:37 .editorconfig
-rw-r--r--  1 rinsaka rinsaka   1120 Mar 18 09:35 .env
-rw-r--r--  1 rinsaka rinsaka   1069 Feb 28 03:37 .env.example
-rw-r--r--  1 rinsaka rinsaka    186 Feb 28 03:37 .gitattributes
-rw-r--r--  1 rinsaka rinsaka    243 Feb 28 03:37 .gitignore
-rw-r--r--  1 rinsaka rinsaka   4158 Feb 28 03:37 README.md
drwxr-xr-x  7 rinsaka rinsaka   4096 Feb 28 03:37 app
-rwxr-xr-x  1 rinsaka rinsaka   1686 Feb 28 03:37 artisan
drwxr-xr-x  3 rinsaka rinsaka   4096 Feb 28 03:37 bootstrap
-rw-r--r--  1 rinsaka rinsaka   1852 Feb 28 03:37 composer.json
-rw-r--r--  1 rinsaka rinsaka 285924 Mar 18 09:35 composer.lock
drwxr-xr-x  2 rinsaka rinsaka   4096 Feb 28 03:37 config
drwxr-xr-x  5 rinsaka rinsaka   4096 Mar 18 09:36 database
-rw-r--r--  1 rinsaka rinsaka    226 Feb 28 03:37 package.json
-rw-r--r--  1 rinsaka rinsaka   1146 Feb 28 03:37 phpunit.xml
drwxr-xr-x  2 rinsaka rinsaka   4096 Feb 28 03:37 public
drwxr-xr-x  5 rinsaka rinsaka   4096 Feb 28 03:37 resources
drwxr-xr-x  2 rinsaka rinsaka   4096 Feb 28 03:37 routes
drwxr-xr-x  5 rinsaka rinsaka   4096 Feb 28 03:37 storage
drwxr-xr-x  4 rinsaka rinsaka   4096 Feb 28 03:37 tests
drwxr-xr-x 39 rinsaka rinsaka   4096 Mar 18 09:35 vendor
-rw-r--r--  1 rinsaka rinsaka    263 Feb 28 03:37 vite.config.js
rinsaka@XPS2019:~/Documents/laravel/myapp$ code . & ⏎
[2] 24990
rinsaka@XPS2019:~/Documents/laravel/myapp$

まずは .env ファイルを編集します.データベースに SQLite を使う場合は DB_CONNECTIONsqlite に変更し,DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD の行を削除するか,先頭に # を付けてコメントアウトするとよいでしょう.

.env (抜粋)
DB_CONNECTION=sqlite
# DB_CONNECTION=mysql
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=

データベースに MySQL を使う場合は上で設定した情報を入力します.

.env (抜粋)
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myappDB
DB_USERNAME=myapp_user
DB_PASSWORD=hogehogehoge

タイムゾーンと言語の設定

プロジェクトの設定ファイル config/app.php を編集し,タイムゾーンを Asia/Tokyo に,言語を ja に変更します.

config/app.php (抜粋)
    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Tokyo',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'ja',

Ubuntu の IP アドレスを調べる

Ubuntu に設定されている IP アドレスを調べます.このためには ip a というコマンド入力し IP アドレスを探します.すると,この環境では172.23.116.166 というアドレスが割り当てられていることがわかりました.このアドレスを控えておきます.

rinsaka@XPS2019:~$ ip a ⏎
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: bond0:  mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 7e:18:44:8e:94:2e brd ff:ff:ff:ff:ff:ff
3: dummy0:  mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 6e:b3:63:d5:14:d8 brd ff:ff:ff:ff:ff:ff
4: tunl0@NONE:  mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/ipip 0.0.0.0 brd 0.0.0.0
5: sit0@NONE:  mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
6: eth0:  mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 00:15:5d:5f:2d:f0 brd ff:ff:ff:ff:ff:ff
    inet 172.23.116.166/20 brd 172.23.127.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::215:5dff:fe5f:2df0/64 scope link
       valid_lft forever preferred_lft forever
rinsaka@XPS2019:~$

もしも大量の出力から目的の IP アドレスを探すのが面倒であれば,次のように grep で出力を絞り込む方法もあります.なお | はパイプ記号と呼ばれます.

rinsaka@XPS2019:~$ ip a | grep inet ⏎ # 「inet」が含まれる行だけを出力すると「inet6」の行も出力される
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
    inet 172.23.116.166/20 brd 172.23.127.255 scope global eth0
    inet6 fe80::215:5dff:fe5f:2df0/64 scope link
rinsaka@XPS2019:~$ ip a | grep "inet " ⏎ # 「inet」の後ろに半角空白が含まれる行だけを出力するとさらに絞り込まれる
    inet 127.0.0.1/8 scope host lo
    inet 172.23.116.166/20 brd 172.23.127.255 scope global eth0
rinsaka@XPS2019:~$

Web サーバの起動と終了

Web サーバを起動します.このとき,IP アドレスは上で調べた IP アドレス (172.23.116.166) を指定してください.ポート番号には 8000 を指定します.ゼミサーバを利用する場合は他の学生と重複しないポート番号を使う必要がありましたが,WSL 環境では特に気にする必要はありません.

rinsaka@XPS2019:~/Documents/laravel/myapp$ pwd
/home/rinsaka/Documents/laravel/myapp
rinsaka@XPS2019:~/Documents/laravel/myapp$ ls
README.md  artisan    composer.json  config    package.json  public     routes   tests   vite.config.js
app        bootstrap  composer.lock  database  phpunit.xml   resources  storage  vendor
rinsaka@XPS2019:~/Documents/laravel/myapp$ php artisan serve --host=172.23.116.166 --port 8000

   INFO  Server running on [http://172.23.116.166:8000].

  Press Ctrl+C to stop the server


Web サーバが起動できたら Windows の Web ブラウザのアドレスバーに http://172.23.116.166:8000 を入力して接続します.次のような画面が出れば OK です.

wsl-laravel-01

Webサーバを停止するには Ctrl + C を押します.