LoginSignup
10
5

More than 5 years have passed since last update.

(Mac)ローカルに手動でValet+Laravel環境を作成する手順

Last updated at Posted at 2019-04-08

この記事の概要、できること

  • Laravel開発をするにあたって、仮想環境、Homestead等は使用せず、軽量なValetを採用した
  • ローカルにValet環境を作成するにあたって、下記を行った
    • PHPをインストールする
    • Composer(ライブラリ依存管理ツール)をインストールする
    • PostgreSQLをインストールする
    • Valetをインストールする
    • Laravelのデフォルトページを表示する

Valetとは

  • 簡易Webサーバみたいなもの
  • ローカルPC内のwebサイト(アプリケーション)を"xxxxx.test"のURLでアクセスできるようにできる

各種バージョン

  • PHP:7.2
  • Composer:最新(記事作成時点で1.8.4)
  • PostgreSQL:11.2
  • Valet:最新(記事作成時点で2.2.1)

開発環境作成手順

Homebrewのインストール

  • 公式サイトの記載どおり実施した
    Homebrew

PHPのインストール

  • ターミナルより下記コマンドを実行
PHP7.2のインストール
brew install php@7.2 
  • バージョンとパスの確認
    バージョンは"PHP 7.2.nn"の様になっていればOK
バージョン確認
~ php -v
PHP 7.2.16 (cli) (built: Mar 22 2019 08:49:28) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.16, Copyright (c) 1999-2018, by Zend Technologies
パス確認
~ which php
/usr/local/opt/php@7.2/bin/php
  • バージョン、パスが反映されていなかったので、このサイトを参考に下記を実施した
環境変数を先頭に追加
~ export PATH=/usr/local/opt/php@7.2/bin:$PATH
環境変数の反映
~ source ~/.bash_profile 

Composerのインストール(要PHP)

~ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
~ php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
Installer verified
~ php composer-setup.php
All settings correct for using Composer
Downloading...

Composer (version 1.8.4) successfully installed to: /Users/yoshioka/work/booking/booking/composer.phar
Use it: php composer.phar

~ php -r "unlink('composer-setup.php');"
  • 上記だとLocallyな実行しかできないらしいので、Globallyに実行するために、下記コマンドを実行
$ mv composer.phar /usr/local/bin/composer
  • 確認
$ composer -V
Composer version 1.8.4 2019-02-11 10:52:10

PostgreSQLのインストール

アンインストール

既にmacにpostgresqlがインストールされていたので、下記手順でuninstallした

  • インストールファイルからインストールした場合
    /Library/PostgreSQL/nnにあるuninstall・・・を実行する →GUIが起動してアンインストールが実行される
$ open /Library/PostgreSQL/nn/uninstall-postgresql.app
  • homebrewでインストールしてある場合
    下記コマンドでアンインストール
brew uninstall postgresql
  • デフォルトで作成されたデーターベースクラスタを削除
    (initdbで初期化する際に、既に存在するとエラーになるらしい)
rm -rf /usr/local/var/postgres

インストール

  • homebrewでインストール
$ brew install postgresql
  • 確認
$ postgres -V
postgres (PostgreSQL) 11.2
$ psql -V
psql (PostgreSQL) 11.2

起動、停止

  • クラスタを新規に作成
    インストールした時点で/usr/local/var/postgresにクラスタが作成されているが、任意のフォルダに新規クラスタを作成(初期化)してみる
    英語、日本語圏ではlocale設定は意味がないらしい
    (ソート処理が遅くなる等の弊害があるらしいので--no-localeオプションをつける)
$ initdb /usr/local/var/postgres/db1 -E utf8 --no-locale
 :
Success. You can now start the database server using:
pg_ctl -D /usr/local/var/postgres/db1 -l logfile start  ← 当クラスタの起動コマンド
  • クラスタ作成の確認
    色々なファイル、フォルダが作成されている
$ ls /usr/local/var/postgres/db1
  • 起動(initdbした時に表示されるstartコマンドを実行)
$ pg_ctl -D /usr/local/var/postgres/db1 -l logfile start
waiting for server to start.... done
server started
  • 停止
$ pg_ctl -D /usr/local/var/postgres/db1 -l logfile stop 
waiting for server to shut down.... done
server stopped

Valetのインストール

  • Composerでパッケージ追加
$ composer global require laravel/valet
Changed current directory to /Users/xxxx/.composer
Using version ^2.2 for laravel/valet
  :
illuminate/support suggests installing vlucas/phpdotenv (Required to use the env helper (^3.3).)
Writing lock file
Generating autoload files
  • 環境変数を追加(パスを通す)
$ export PATH=~/.composer/vendor/bin:$PATH
  • Valet インストール → PHPがLinkされてないから失敗
$ valet install
Stopping nginx...
Installing nginx configuration...
Installing nginx directory...
Updating PHP configuration...

In Brew.php line 204:

  Homebrew PHP appears not to be linked.  

install
  • ここを参考にして
    homebrewでPHPへのリンクを貼る
$ brew link php72 --force
Linking /usr/local/Cellar/php@7.2/7.2.16... 25 symlinks created

If you need to have this software first in your PATH instead consider running:
  echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
  echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc
  • 「必要なら環境変数に追加しとけ」って出力されているので、追加しとく
    (PHPのインストール後にやったはずなんだけどな。。。)
$ echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
$ echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc
  • 再度Valetインストール → 成功
$ valet install
Password:
Stopping nginx...
Installing nginx configuration...
Installing nginx directory...
Updating PHP configuration...
Restarting php@7.2...
Installing dnsmasq...
[dnsmasq] is not installed, installing it now via Brew... 🍻
Restarting dnsmasq...
Valet is configured to serve for TLD [.test]
Restarting nginx...

Valet installed successfully!

Laravelプロジェクトの作成と画面表示

  • helloralavelフォルダにLaravelプロジェクトを生成
    (カレントフォルダ直下にhelloralavelフォルダが作成される)
$ laravel composer create-project laravel/laravel hellolaravel
Installing laravel/laravel (v5.8.3)
  - Installing laravel/laravel (v5.8.3): Loading from cache
  :
Application key set successfully.
  • hellolaravelのシンボリックリンクを生成(URL配信できるようにする)
$ cd hellolaravel  ← 先のコマンドで作成されたフォルダに移動
$ valet link  ← カレンドフォルダをURL配信する
A [hellolaravel] symbolic link has been created in [/xxx/xxx/.config/valet/Sites/hellolaravel].
  • valet起動
$ valet start
Restarting php@7.2...
Restarting nginx...
Valet services have been started.
  • Laravelデフォルトページの表示
    http://hellolaravel.testにアクセスして、下記が表示されればOK image.png

Laravelチュートリアルを実行して動作確認してみましょう

10
5
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
10
5