CentOS8でPHP、Laravelの開発環境を構築する

Laravel,Linux,PHP,技術CentOS,CentOS8,Firewall,PHP 7,さくらVPS,備忘録,開発環境構築

こんばんわ。

白でも黒でもない世界で笑わないどうもみけぽんです。

 

従量課金制のAWSは使えないビビりの私はさくらVPSでアプリの公開環境を運用していたのですが、初期構築時は最新のCentOS7で運用していました。

しかし2019年にCentOS8がリリースされ職場でも新規サーバーはCentOS8で構築するようになったので、思い立ってCentOS7→8へのリプレイスを行うことにしました。
CentOS8でPHPとLaravelの開発環境を構築した際の備忘録になります。

 

 

CentOSバージョン

バージョンは下記のとおりです。

$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)

 

まずはユーザーを追加

私の場合は/home/ユーザー配下を公開ディレクトリにしているのでまずはユーザーの作成

$ useradd hoge

# パスワードの変更
$ passwd hoge

# sudoコマンドを使用可にする
$ usermod -G wheel appuser 

 

apacheのインストール、有効化

業務を通して慣れているので私はNginXよりapache派です。

$ dnf -y install httpd httpd-tools httpd-devel httpd-manual

# ステータスを確認すると停止中になっている
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           mqphp-fpm.conf
   Active: inactive (dead)
     Docs: man:httpd.service(8)

# 起動
$ systemctl start httpd

# サーバーを再起動しても起動するよう有効化
$ systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

# 再度ステータスを確認すると起動しenable状態になっている。
$ systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           mqphp-fpm.conf
   Active: active (running) since Sun 2020-07-05 23:23:55 JST; 1min 27s ago
     Docs: man:httpd.service(8)
 Main PID: 18120 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 2857)
   Memory: 16.8M
   CGroup: /system.slice/httpd.service
           tq18120 /usr/sbin/httpd -DFOREGROUND
           tq18121 /usr/sbin/httpd -DFOREGROUND
           tq18122 /usr/sbin/httpd -DFOREGROUND
           tq18123 /usr/sbin/httpd -DFOREGROUND
           mq18124 /usr/sbin/httpd -DFOREGROUND

 

firewalld設定

パケットフィルタは使わずにFirewallで制御。

# httpとhttpsを許可する
$ firewall-cmd --add-service=http --zone=public --permanent
success
$ firewall-cmd --add-service=https --zone=public --permanent
success

# 設定の反映
$ firewall-cmd --reload

 

PHP7.4のインストール

現時点では最新のPHP7.4をインストール。

処理速度が向上するPHP8のリリースが待ち遠しいです。

# EPEL、Remiリポジトリを追加
$ dnf -y install epel-release
$ dnf -y install http://rpms.famillecollet.com/enterprise/remi-release-8.rpm

# PHP7.4のインストール
$ dnf -y module install php:remi-7.4

# バージョンの確認
$ php -v
PHP 7.4.7 (cli) (built: Jun  9 2020 10:57:17) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.7, Copyright (c), by Zend Technologies
    with Xdebug v2.9.6, Copyright (c) 2002-2020, by Derick Rethans 

# その他PHPの拡張をインストール
$ dnf install -y php-bcmath php-mcrypt php-pdo php-xml php-tokenizer php-mysqlnd php-pecl-xdebug php-gd php-intl php-zip php-opcache

 

wgetのインストール

composerはwgetコマンドでダウンロードする必要があるのでインストール。

$ dnf install -y wget

 

composerのインストール

Laravelをインストールするcomposerをインストール。

# インストーラーのダウンロード
$ wget https://getcomposer.org/installer -O composer-installer.php

# インストール
$ php composer-installer.php --filename=composer --install-dir=/usr/local/bin

# アップデート
$ composer self-update

# バージョンの確認
$ composer -v 

 

Laravelのインストール

ようやくLaravelをインストール。

# プロジェクトを作成するディレクトリへ移動
$ cd /home/hoge/public_html

# インストールの実行
$ composer create-project laravel/laravel --prefer-dist project_name

 

Virtualhostの設定

WEBブラウザからサーバーを参照した際、指定のディレクトリを参照するようapacheのvirtualhostを設定。

$ vi /etc/httpd/conf.d/vhost.conf
<VirtualHost *:80>
   DocumentRoot /home/hoge/public_html/project_name/public
   ServerName サーバーのipアドレスを記載
   ErrorLog /home/hoge/logs/project_name/error_log
   CustomLog /home/hoge/logs/project_name/access_log combined
   AddDefaultCharset UTF-8
   <Directory "/home/hoge/public_html/project_name/public">
      AllowOverride All
   </Directory>
</VirtualHost> 

# 構文エラーがないか確認。「Syntax OK」が表示されればOK。
$ httpd -t
Syntax OK

# 構文エラーがなければapache再起動
$ systemctl restart httpd

 

apacheの再起動後にvirtualhostで指定したIPアドレスにwebブラウザでアクセス。

権限やSELinuxの設定で間違わなければLaravelの画面が表示されます。

 

私はユーザーディレクトリの権限を間違えていて403エラーが表示され続け、若干詰まりました。

エラーが起こったらvirtualhostで設定したエラーログを確認。

 

MySQLの設定

MySQLのインストール

インストールするパッケージ名は公式サイトを確認
https://dev.mysql.com/downloads/repo/yum/

$ dnf localinstall -y https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm

# 既存MySQLモジュールを無効化
$ dnf module disable -y mysql

# インストール
$ dnf install -y mysql-community-server

# バージョンの確認
$ mysqld --version
/usr/sbin/mysqld  Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)

# MySQLの起動と有効化
$ systemctl start mysqld
$ systemctl enable mysqld

# 起動中かつenable状態になっていることを確認。
$ systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor pres>
   Active: active (running) since Mon 2020-07-06 22:25:56 JST; 2min 11s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 25393 (mysqld)
   Status: "Server is operational"
    Tasks: 38 (limit: 2857)
   Memory: 285.0M
   CGroup: /system.slice/mysqld.service
           mq25393 /usr/sbin/mysqld

 

初期設定

初期状態だとパスワードポリシーが強めなので個人的にゆるくします。

強め状態だと英大文字小文字、半角数字、記号の全てを入力しなければなりません。

# パスワードポリシーをLOWにする
$ vi /etc/my.conf
validate_password.policy=LOW

# 設定の反映のためmysqlを再起動
$ systemctl restart mysqld

 

あとはmysql_secure_installationコマンドで初期設定を行います。

# まずは初期パスワードを確認
$ grep 'temporary password' /var/log/mysqld.log
2020-07-06T13:25:50.631066Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: HogeHoge

$ 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:

Re-enter new password:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
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) : y

New password:

Re-enter new password:

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

# リモートからのrootユーザーのログインを禁止
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.

# testデータベースの削除
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!

 

ログイン後の設定

MySQLコマンドを実行し、先ほど設定したrootパスワードでMySQLへログイン。

$ mysql -u root -p
Enter password:

# Laravel用データベースを作成。
> create database hogepiyo;

# Laravel用のユーザー作成
> create user 'user_name'@'localhost' identified by 'password';

# 権限付与
> grant all on hogepiyo.* to 'user_name'@'localhost' with grant option;

# 反映
> flush privileges;

 

設定不要箇所

timezone設定は必要なし

さくらの場合は最初からAsia/Tokyoに設定されています。

 

まとめ

私自身環境構築はあまり得意ではないので、備忘録をつけることが学習になると考えました。

この備忘録が同じように環境構築で躓いている方の助けになれば幸いです。

 

参照サイト