Centos7にmysql5.7とLaravelを入れる


本編の内容

以前の記事でVagrantを使ってCentOS7にPHP7.1とnginxを入れることに成功しました。
今回はそれにmysqlとlaravelを入れて、画面表示するところまでをやりたいと思います。

mysqlをyumでインストールする

まずは、既にmariaDBがデフォルトで存在している可能性があるので、事前に消しておきましょう。

vagant
# rpm -qa | grep maria まずはこれで確認。あったら以下を実行。

# yum remove mariadb-libs
# rm -rf /var/lib/mysql/

次に、mysql5.7のリポジトリを追加します。
こちらのサイトからyumリポジトリの最新版のURLを確認してインストールします。
https://dev.mysql.com/downloads/repo/yum/
今回はCentOS7なので対応する最新URLをコピーして'http://dev.mysql.com/get/' のあとに貼り付けしてコマンド実行します。

# yum localinstall http://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

ただ、これだとmysql8.0が入ってしまっているように見えます。
欲しいのは5.7なのですが、実際どうなのかgrepして確認してみましょう。

# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community      無効
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 無効
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community      無効
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 無効
mysql-connectors-community/x86_64  MySQL Connectors Community       有効:     51
mysql-connectors-community-source  MySQL Connectors Community - Sou 無効
mysql-tools-community/x86_64       MySQL Tools Community            有効:     63
mysql-tools-community-source       MySQL Tools Community - Source   無効
mysql-tools-preview/x86_64         MySQL Tools Preview              無効
mysql-tools-preview-source         MySQL Tools Preview - Source     無効
mysql55-community/x86_64           MySQL 5.5 Community Server       無効
mysql55-community-source           MySQL 5.5 Community Server - Sou 無効
mysql56-community/x86_64           MySQL 5.6 Community Server       無効
mysql56-community-source           MySQL 5.6 Community Server - Sou 無効
mysql57-community/x86_64           MySQL 5.7 Community Server       無効
mysql57-community-source           MySQL 5.7 Community Server - Sou 無効
mysql80-community/x86_64           MySQL 8.0 Community Server       有効:     17
mysql80-community-source           MySQL 8.0 Community Server - Sou 無効

ちゃんとmysql57も入ってますね。ただ、mysql80が有効になってしまってます。
これを変えたいのですが、yum の設定を変更するために yum-utils パッケージが必要なのでインストールします。
そのあと、mysql80を無効にして、mysql57を有効にします。
(/x86_64という部分の指定は不要なことに注意)

# yum -y install yum-utils
# yum-config-manager --disable mysql80-community
# yum-config-manager --enable mysql57-community

では、本当に変わったかもう一度みてみます。

# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community      無効
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 無効
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community      無効
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 無効
mysql-connectors-community/x86_64  MySQL Connectors Community       有効:     51
mysql-connectors-community-source  MySQL Connectors Community - Sou 無効
mysql-tools-community/x86_64       MySQL Tools Community            有効:     63
mysql-tools-community-source       MySQL Tools Community - Source   無効
mysql-tools-preview/x86_64         MySQL Tools Preview              無効
mysql-tools-preview-source         MySQL Tools Preview - Source     無効
mysql55-community/x86_64           MySQL 5.5 Community Server       無効
mysql55-community-source           MySQL 5.5 Community Server - Sou 無効
mysql56-community/x86_64           MySQL 5.6 Community Server       無効
mysql56-community-source           MySQL 5.6 Community Server - Sou 無効
mysql57-community/x86_64           MySQL 5.7 Community Server       有効:    267
mysql57-community-source           MySQL 5.7 Community Server - Sou 無効
mysql80-community/x86_64           MySQL 8.0 Community Server       無効
mysql80-community-source           MySQL 8.0 Community Server - Sou 無効

OKですね!!
yum infoで二重チェックしてみましょう。

# yum info mysql-community-server
利用可能なパッケージ
名前                : mysql-community-server
アーキテクチャー    : x86_64
バージョン          : 5.7.22
リリース            : 1.el7
容量                : 165 M
リポジトリー        : mysql57-community/x86_64
要約                : A very fast and reliable SQL database server

このようにちゃんとmysql5.7になっているようなので、インストールをします。

# yum -y install mysql-community-server

うまく行きました。バージョンを調べておきましょう。

# mysqld --version
mysqld  Ver 5.7.22 for Linux on x86_64 (MySQL Community Server (GPL))

オッケーオッケー順調ですね!
あとはマシン起動時にmysqlも起動されるように設定だけしておきましょう。

# systemctl enable mysqld
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Multiple identities can be used for authentication:
 1.  veewee
 2.  vagrant
Choose identity to authenticate as (1-2): 2
Password: 
==== AUTHENTICATION COMPLETE ===

なにやら尋ねられましたが、vagrantを選んでパスワードもvagrantと打ち込めばOKでした。

mysql5.7では初期パスワードが自動で生成されてログに吐かれているので確認しておきます。

mysqld.log
# cat /var/log/mysqld.log 
2018-05-31T07:43:32.301323Z 1 [Note] A temporary password is generated for root@localhost: xxxxxxxxxxxx←ここにパスワードが書いてあった

初期設定をする

mysql_secure_installationを実行して初期設定を進めます。

# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: 先ほど確認した初期パスワードを打ち込みます。 

The existing password for the user account root has expired. Please set a new password.

New password: 新しいパスワードを入れてください
(パスワードが最低 1 つの数値文字を含み、1 つの小文字および大文字を含み、1 つの特殊文字 (英数字以外) を含む必要がある)

Re-enter new password: 再入力します
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : No 
パスワード強度が表示されやっぱりパスワードを変えますか?と聞かれたのでNoです

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
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
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
 - 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
Success.

All done! 

まあとりあえずほとんどYesと答えておけばOKでしたね。
あとは設定ファイルの文末に以下の記述を入れて文字コードあたりをやっておきましょう。

etc/my.cnf
文末に下記を追加

default-storage-engine=InnoDB
innodb_file_per_table

character-set-server = utf8
collation-server = utf8_general_ci

[mysql]
default-character-set = utf8 

[client]
default-character-set = utf8

再起動します。
一応その後 mysql -u root -p で入れることも確認しておくといいでしょう。

# systemctl restart mysqld

Laravel5.5を入れる

まずはnginxのドキュメントルートにlaravel_testというディレクトリを作りましょう。
その中でcomposerダウンロードしましょう。
その後、どこでもコマンドが使えるようにcomposerの場所を移動しておきましょう。

# cd /usr/share/nginx/html
# mkdir laravel_test
# cd laravel_test
# curl -sS https://getcomposer.org/installer | php
# mv ./composer.phar /usr/local/bin/composer
# composer

最後にcomposerと打って、例のcomposer画面が表示されればOKです。
どうでもいいけどこの画面好き。笑

   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.6.5 2018-05-04 11:44:59

そしたらlaravelをインストールするのにgit等が必要ですが、まだインストールしていなかったので先にしておきます。
以前これでエラーが出たことあったので・・・

# yum -y install git zip unzip

composerを使ってsampleという名前のプロジェクトを作ってあげましょう。
今回は5.5がいいのでそれを使います。

# composer create-project --prefer-dist laravel/laravel sample "5.5.*"

なんかエラー出た

エラー画面
Failed to decode response: zlib_decode(): data error
Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info

でもよく見てみるとプロジェクトはちゃんとできている。
こちらのサイトによるとcomposerがネットワーク速度に警鐘を鳴らしているだけで、コマンド自体はちゃんとできているとのこと。
・・・ならまあいいか。笑

めげずにプロジェクトフォルダに入り、一度composer updateをかけます。
これはlaravelで必要なパッケージがプロジェクト内のcomposer.jsonに書いてあるので、
一回updateしないと色々とエラーが起きるからです。

# cd sample
# composer update
# php artisan --version
Laravel Framework 5.5.40

ついでにバージョンも確認できましたね!

後は、以下のURLでアクセスできるように諸々の設定をします。
http://192.168.33.10/laravel_test/sample/public/

まずはnginx権限を設定(これは他の記事を参考に実行しました)

# chown nginx:nginx -R /usr/share/nginx/html/laravel_test/sample/storage
# chown nginx:nginx -R /usr/share/nginx/html/laravel_test/sample/bootstrap/cache

万を時してURLにアクセス!!
・・・No application encryption key has been specified.というエラーが発生

どうやらkeyを作っておいてあげないとダメらしい。

php artisan key:generate
Application key [base64:KU09OmnjlMaq1kvUP3ehGpK2V67aNgfcRgA4ExkM8i0=] set successfully.

これを実行して再度アクセス!!

長い戦いだった・・・。