Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9318

M1 MacでHomebrewセットアップからLaravel でnom run watchするまで

$
0
0
知識レベルフロントエンドエンジニアがM1でLAMP環境整えるための覚書です。 Homebrew インストール M1の場合、「/opt/homebrew/」ディレクトリにインストールする必要がある。 ルートからoptへ移動 bash:~ cd /opt /opt sudo mkdir homebrew Password: sudo chown joraku:admin homebrew curl -L https://github.com/Homebrew/brew/tarball/master | tar xz --strip 1 -C homebrew optからルートへ移動 /opt cd ~ インストールが終わったら「.zshrc」ファイルでパスを通す。 ~ vim .zshrc .zshrc export PATH=$PATH:/opt/homebrew/bin escキー、:wqで保存して閉じる。 ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc 「brew -v」で動いてるか確認。 brew -v Homebrew >=2.5.0 (shallow or no git repository) Homebrew/homebrew-core N/A 上記のようにバージョンが表示されたらOK。 続けてwgetのインストール。 brew install wget wgetコマンドが実行できるようになる。 phpインストール インストール可能なphpを調べる brew search php ==> Formulae brew-php-switcher php-code-sniffer php@7.2 php@7.4 phplint phpmyadmin phpunit php php-cs-fixer php@7.3 phpbrew phpmd phpstan ==> Casks 現時点で最新のphp@7.4を入れます。 (というか7.3以下はM1ネイティブではないという記述を見た気がする) brew install php@7.4 インストール終わったらlink。 brew link php@7.4 Linking /opt/homebrew/Cellar/php@7.4/7.4.19... 25 symlinks created. If you need to have this software first in your PATH instead consider running: echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc パスを通すように指示が出るので、その通り実行。 echo 'export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"' >> ~/.zshrc echo 'export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH"' >> ~/.zshrc 念のため通っているかcatで確認。 cat ~/.zshrc export PATH=$PATH:/opt/homebrew/bin export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH" export PATH="/opt/homebrew/opt/php@7.4/sbin:$PATH" 先程のhomebrewに加えて、phpの設定も追加された。 ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc インストールしたphpが反映されているか確認。 which php /opt/homebrew/opt/php@7.4/bin/php Apacheインストール Macに元から入っているapacheを 止める。 sudo apachectl stop Password: /System/Library/LaunchDaemons/org.apache.httpd.plist: Could not find specified service Unload failed: 113: Could not find specified service が、「指定されたサービスが見つかりませんでした」となっているのでM1では動いていない? 何にしても動いてなければOK。 Homebrewでapacheを確認。 brew search httpd ==> Formulae darkhttpd httpd httpdiff libmicrohttpd lighttpd mighttpd2 確認できたのでインストール。 brew install httpd 「httpd.conf」「hosts」の設定 「httpd.conf」をvimとかで触るのはめんどくさいので、 「Visual Studio Code」などのエディタで触ります。 エディタがない場合(ある人はスキップ) 持っていない場合はインストールしておきます。 →Visual Studio Code Apple Silicon版またはUnivarsal版をダウンロードしてインストール。 M1の場合、「/opt/homebrew/etc/httpd/」に設定ファイルがある。 openでディレクトリを開きます。 open /opt/homebrew/etc/httpd/ 開きました。 httpd.confの編集 httpd.confを「Visual Studio Code」などのエディタで開く。 「Listen 8080」を書き換え。(8080のままでもOK、ポートの環境による) http.conf Listen 80 コメントアウト http.conf LoadModule userdir_module lib/httpd/modules/mod_userdir.so LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so 上記コメントアウトの下あたりに追記 http.conf LoadModule php7_module /opt/homebrew/opt/php@7.4/lib/httpd/modules/libphp7.so <IfModule php7_module> AddType application/x-httpd-php .php PHPIniDir "/opt/homebrew/opt/php@7.4/php.ini" </IfModule> 「#ServerName www.example.com:8080」を以下に書き換え。 (myhostにしているけど、これはお好みでOK) http.conf ServerName myhost 「AllowOverride none」を書き換え。 http.conf <Directory /> AllowOverride all Require all denied </Directory> 「DirectoryIndex index.html」にindex.phpを追記。 http.conf <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> コメントアウト http.conf # User home directories Include /opt/homebrew/etc/httpd/extra/httpd-userdir.conf # Virtual hosts Include /opt/homebrew/etc/httpd/extra/httpd-vhosts.conf httpd-vhosts.confの編集 http.confのあったディレクトリにある「extra」フォルダ内にttpd-vhosts.confはある。 こちらもエディタでサクッと編集してしまいます。 デフォルトで書いている内容は全部消してしまって大丈夫。 例として、phpMyAdminをローカルに置いて起動する想定で以下記述。 以下の設定の想定で記述 macのユーザー名:taroyamada phpmyadminの場所: taroyamada/web/phpMyAdmin ServerName: phpmyadmin.myhost 自身の環境に置き換えてよしなにやってください httpd-vhosts.conf <VirtualHost 127.0.0.1:80> ServerAdmin admin@phpmyadmin.myhost DocumentRoot "/Users/taroyamada/web/phpMyAdmin" ServerName phpmyadmin.myhost ErrorLog "/opt/homebrew/var/log/phpMyAdmin.localhost-error_log" CustomLog "/opt/homebrew/var/log/phpMyAdmin.localhost-access_log" common <Directory "/Users/taroyamada/web/phpMyAdmin"> Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted </Directory> </VirtualHost> M1じゃない環境から設定をコピペする場合、ログファイルのディレクトリが 「/opt/homebrew/var/log/」となるよう気をつける。 hostsの編集 ターミナルでの操作に戻り、hostsの設定をいじる。 管理者権限でhostsを開く。 ~ sudo vim /private/etc/hosts 開いたら「i」を押して編集モードにして 一番下に、例としてあげたphpmyadminのサーバーネームを追加。 hosts ## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## 127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 phpmyadmin.myhost 追加できたら「esc」キーからの「:wq」で閉じる。 パスを通す。 echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc whichでちゃんと切り替わったか確認。 which apachectl /opt/homebrew/bin/apachectl which httpd /opt/homebrew/bin/httpd /opt/homebrew/binになってたらOK。 Apacheの全設定が完了したので起動してみる。 hosts sudo apachectl start Password: 起動後ブラウザで以下にアクセス。 http://localhost/ 「It works!」と表示されたらOK。 MySQLのインストール HomebrewでMySQLを確認。 brew search mysql ==> Formulae automysqlbackup mysql++ mysql-client@5.7 mysql-sandbox mysql@5.6 mysqltuner mysql mysql-client mysql-connector-c++ mysql-search-replace mysql@5.7 qt-mysql ==> Casks homebrew/cask/mysql-connector-python homebrew/cask/mysql-shell homebrew/cask/mysql-utilities homebrew/cask/navicat-for-mysql homebrew/cask/sqlpro-for-mysql 確認できたのでmysql@5.7をインストール。 brew install mysql@5.7 mysql@5.7: stable 5.7.34 (bottled) [keg-only] Open source relational database management system https://dev.mysql.com/doc/refman/5.7/en/ /opt/homebrew/Cellar/mysql@5.7/5.7.34 (319 files, 234.0MB) Poured from bottle on 2021-05-21 at 00:12:20 From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/mysql@5.7.rb License: GPL-2.0-only ==> Dependencies Build: cmake ✘ Required: openssl@1.1 ✔ ==> Caveats 〜省略〜 If you need to have mysql@5.7 first in your PATH, run: echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc For compilers to find mysql@5.7 you may need to set: export LDFLAGS="-L/opt/homebrew/opt/mysql@5.7/lib" export CPPFLAGS="-I/opt/homebrew/opt/mysql@5.7/include" 〜省略〜 例の如くパスを通す必要があると出るので通す。 echo 'export PATH="/opt/homebrew/opt/mysql@5.7/bin:$PATH"' >> ~/.zshrc echo 'export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig"' >> ~/.zshrc ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc MySQLの設定 初期設定 mysql_secure_installationで初期設定を行う mysql_secure_installation mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD plugin? Press y|Y for Yes, any other key for No: Y VALIDATE PASSWORD PLUGIN を使うかどうか→Y There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 強度はローカルなのでLOWでOK→0 Please set the password for root here. New password: Re-enter new password: パスワードの設定→(rootパスワード、忘れないように) Estimated strength of the password: 50 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y パスワードの強度これで良いか聞かれる→Y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? (Press y|Y for Yes, any other key for No) : y 初期であるanonymousユーザー消すか→Y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y リモートでrootログインできなくするか→Y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y 初期であるtestデータベース消すか→Y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y 設定を今すぐ反映するか→Y Success. All done! 初期設定終了。 my.cnf設定 次はmy.cnfの確認。 mysql --help | grep my.cnf order of preference, my.cnf, $MYSQL_TCP_PORT, /etc/my.cnf /etc/mysql/my.cnf /opt/homebrew/etc/my.cnf ~/.my.cnf /opt/homebrew/etc/my.cnfがあるのでこれを編集 vim /opt/homebrew/etc/my.cnf 「character-set-server = utf8」と「default_password_lifetime = 0」を最終行に追加。 [mysqld] # Only allow connections from localhost bind-address = 127.0.0.1 character-set-server = utf8 default_password_lifetime = 0 起動テスト root、先ほど初期設定で入力したパスワードで起動。 mysql -u root -p 無事mysqlが立ち上がったら、まず新しくデータベースを作成する。 database_nameは目的のものを作ればOK。 mysql CREATE DATABASE database_name; ユーザーを作る。 your_name、your_passwordは好きに変えてOK。 mysql CREATE USER 'your_name'@'localhost' IDENTIFIED BY 'your_password'; 作成したユーザーに作成したデータベースの操作権限を付与する。 mysql GRANT ALL PRIVILEGES ON database_name.* TO 'your_name'@'localhost'; 設定を反映する mysql FLUSH PRIVILEGES; 完了、exitでmysqlを閉じる。 mysql exit; composerのインストール composerをインストール。 mysql brew install composer Node.jsのインストール nodebrewをインストール・・・としようとしたが、 2021年5月現在、どうもHomebrewからインストールするとうまくいかない模様。 調べるとM1ではnodebrew1.1.0から正式対応な模様。 とりま、以下のやり方でインストール。 curl -L git.io/nodebrew | perl - setup また例の如くパスを通す必要があると出るので通す。 echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zshrc ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc バージョンが最新であることを確認。 nodebrew -v nodebrew 1.1.0 インストール可能なnode.jsのバージョン確認。 nodebrew ls-remote v0.0.1 v0.0.2 v0.0.3 v0.0.4 v0.0.5 v0.0.6 〜省略〜 v14.16.1 v14.17.0 〜省略〜 v15.13.0 v15.14.0 v16.0.0 v16.1.0 v16.2.0 〜省略〜 Apple Siliconにネイティブ対応しているのはv15.x以降。 だけど既存プロジェクトで動作確認しているのはv14.x。 14も動くがせっかくなのでとりあえずv15.xを入れる。 が、ここでエラーが発生。 どうもarm版のデータが存在しない模様(なんでやねん)。 調べると、compileからだといけるとのこと。 なので一旦安定版を入れる。 nodebrew install-binary stable 過去の案件がstable版では動かないことがわかっているので、v15もコンパイルしていれる。 nodebrew compile v15.6.0 ↑おおよそ1時間近くかかります。 この後v14も同様に入れて、とりあえずインストールできているか確認。 nodebrew list v14.17.0 v15.6.0 v16.2.0 3つともインストールできた。 とりあえず、v15を動かすことにする。 nodebrew use v15.6.0 毎度お馴染み、パスを通す。 echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.zshrc ターミナルを再起動するか、sourseで.zshrc反映。 source ~/.zshrc バージョン確認できたら完了。 node -v v15.6.0 npm -v 7.4.0 プロジェクト環境を用意して移動、準備 クローンするなりしてプロジェクト環境を用意。 cdディレクトリに移動。 今回は、laravelのプロジェクト。 composerのインストール。 composer install Node.jsのパッケージをインストール。 npm install README.mdなどを読みながらプロジェクトに必要な設定を行う。 //envを設定したり cp .env.example .env //php artisanしたり php artisan key:generate php artisan migrate 〜他諸々設定〜 ↑↑↑ これらは一例であり、必須なものでもない。 不明な場合はプロジェクトの担当者に確認すること。 参考: https://zenn.dev/myb/articles/4b1dd3821703aa2ac95b https://qiita.com/shibukawa/items/797b7cbb7e530842e6f7 https://qiita.com/shibukawa/items/797b7cbb7e530842e6f7 https://zenn.dev/yaeda/scraps/4f17d8b2d3ad4c

Viewing all articles
Browse latest Browse all 9318

Trending Articles