WindowsのWSLにLAMP環境をインストールしてLaravelを動かした時のメモ


LAMP環境とは

Linux + Mysql + Apache + PHP の、webの一般的な構成。今回はLinux以外をインストールした。

macはこちら
Macに(L)AMP環境を構築してlaravelを動かした時のメモ - Qiita

参考

https://qiita.com/mollinaca/items/60c54b7767a49c1e3e51
https://codeaid.jp/blog/wsl-lamp-wp1/
https://qiita.com/diconran/items/1eca0e0792f4ce1930c5

PHPのインストール

$ sudo apt install php

ここで、aptが404エラーを起こしていた。サーバー情報が古かったようで、

$ sudo apt update
$ sudo apt upgrade

を実行して再試行したらインストールできた。

Apacheのインストール

$ sudo apt install apache2 libapache2-mod-php
$ sudo apt install php-mbstring php-gettext

phpモジュールのインストールも忘れずに。

MySQLのインストール

$ sudo apt install mysql-server
$ sudo apt install php-mysql

mysqlの設定

MySQLを起動

$ sudo service mysql start

パスワードを変更

$ sudo mysqladmin -u root password 'パスワード'

セキュリティ設定

$ sudo mysql_secure_installation

Y/nで選択する。1つ目(パスワードチェック)、2つ目(rootのパスワード)以外はY。

VALIDATE PASSWORD COMPONENT 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 component?

Press y|Y for Yes, any other key for No: n
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n 
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

apacheの設定

/etc/apache2/sites-available/000-default.conf
DocumentRoot {ふぁいるぱす} #変更
/etc/apache2/apache.conf
DirectoryIndex index.php index.html
/etc/apache2/apache.conf
<Directory {ふぁいるぱす}> #変更
Options Indexes FollowSymLinks
AllowOverride All #変更
Require all granted
</Directory>

rewrireを有効にするために実行

$ sudo a2enmod rewrite

おまけ1

nodeのインストール
GitHubにしたがう。

$ curl -L git.io/nodebrew | perl - setup

$HOME/.nodebrew/current/bin にパスを通す。

あとはmacの時とおなじ。

$ nodebrew install-binary stable
$ nodebrew ls #インストールされているnodeを表示
$ nodebrew use {使いたいバージョン} #有効化

おまけ2

composer installしたらzipがないと怒られたので。

$ sudo apt install zip unzip

つかいかた

Apache

$ sudo service apache2 start
$ sudo service apache2 restart
$ sudo service apache2 stop

mysql

$ sudo service mysql start
$ sudo service mysql restart
$ sudo service mysql stop

おまけ3

https://dev.to/oneearedmusic/access-denied-reset-mysql-root-user-password-2hk4
https://qiita.com/ucan-lab/items/3ae911b7e13287a5b917

php artisan migrate しようとしたらエラーが発生した。認証方式によるもののよう。
mysqlのコンソールにsudoで入って以下を実行すればよい。

> use mysql;
> UPDATE user SET authentication_string=PASSWORD("securepassword") where User='root';
> UPDATE user SET plugin="mysql_native_password";
> FLUSH PRIVILEGES;
> quit;