CentOS8でのWordPress構築

はじめに

CentOS8でのWordPres構築手順になります。CentOS8は、CentOS7と色々と異なる部分が多く、かなりハマりましたので参考になればうれしいです。

前提

VagrantおよびVirtualBoxがインストールされていること

構築手順

VirtualboxでCentOS8サーバーを構築する

VagrantにCentOS8のBoxを追加する

まず最初にVagrantでCentOS8を利用可能とする為、Boxを追加する操作を行います。

vagrant box add generic/centos8

==> box: Loading metadata for box 'generic/centos8'
    box: URL: https://vagrantcloud.com/generic/centos8
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.

1) docker
2) hyperv
3) libvirt
4) parallels
5) virtualbox
6) vmware_desktop

Enter your choice: 5
==> box: Adding box 'generic/centos8' (v2.0.6) for provider: virtualbox
    box: Downloading: https://vagrantcloud.com/generic/boxes/centos8/versions/2.0.6/providers/virtualbox.box
    box: Download redirected to host: vagrantcloud-files-production.s3.amazonaws.com
    box: Progress: 100% (Rate: 18.5M/s, Estimated time remaining: --:--:--)
==> box: Successfully added box 'generic/centos8' (v2.0.6) for 'virtualbox'!

次に作業フォルダを作成し、ターミナルで該当ディレクトリへ移動します。なお、以下は注意事項となります。

パスに日本語を含む場合、後続の作業でURIエンコーディングが必要となる為、ASCIIのみを推奨

日本語を含まなくても途中のパスが問題でURIエラーになるケースがある為、windowsの場合はcドライブ直下にフォルダ作成することを推奨

vagrant init generic/centos8

vagrantfileの編集

以下の通りvagrantfileを修正してください。

  # サーバー定義設定
  config.vm.define :wp do |wpcent8|
    wpcent8.vm.hostname = "wpserver"
    wpcent8.vm.network :private_network, ip:"192.168.0.11"
  end

Vagrantを起動

vagrant up

 仮想マシン(CentOS8)の設定

仮想マシンへログイン

vagrant ssh wp

ルートユーザーへ切り替え

sudo su

selinux無効化

getenforce Enforcing
vi /etc/selinux/config
SELINUX=enforcing → SELINUX=disabled
getenforce Disabled

時刻同期設定

vi /etc/chrony.conf
#pool 2.centos.pool.ntp.org iburst → pool ntp.jst.mfeed.ad.jp iburst
systemctl restart chronyd

タイムゾーン設定

timedatectl set-timezone Asia/Tokyo

Apacheのインストール

Apacheのインストール

 dnf -y install httpd

Error: Failed to synchronize cache for repo 'epel’が発生する場合、以下のように
/etc/yum.repos.d/epel.repoファイルを編集してepelを無効化(enabledを"1″から"0″へ変更)する

[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
#baseurl=https://download.fedoraproject.org/pub/epel/$releasever/Everything/$basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch&infra=$infra&content=$contentdir
failovermethod=priority
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever

Apache自動起動設定

systemctl enable httpd.service

Apache起動

systemctl start httpd.service

Apache起動確認

systemctl status httpd.service

httpのポート開放(80)

firewall-cmd --permanent --zone=public --add-service=http

firewallのリロード

firewall-cmd --reload

ブラウザからApache起動確認 (192.168.0.11をブラウザのURLに入力)

phpおよびMySQLのインストール

phpインストール

dnf module install -y php

dnf install php-mysqlnd

mysqlインストール

dnf install php mysql-server

mysql自動起動設定

systemctl enable mysqld.service

mysql起動

systemctl start mysqld.service

mysql起動確認

systemctl status mysqld.service

WordPressのインストール

カレントディレクトリ移動

cd /var/www/html

WordPressダウンロード

wget https://ja.wordpress.org/latest-ja.tar.gz

ファイルの解凍

tar xvf latest-ja.tar.gz

権限の変更

chown -R apache:apache *

chmod -R 777 /var/www/html

httpd.confの編集

DocumentRoot "/var/www/html"

#
# Relax access to content within /var/www.
#
<Directory "/">
    AllowOverride None
    # Allow open access:
    Require all granted
</Directory>

# Further relax access to the default document root:
<Directory "/var/www/html">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All★
    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

DBの作成

[root@wpserver html]# mysql -u root

Welcome to the MySQL monitor.  Commands end with ; or ¥g.
Your MySQL connection id is 8
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '¥h' for help. Type '¥c' to clear the current input statement.

// DBを作成
mysql> create database wordpress default character set utf8;
Query OK, 1 row affected, 1 warning (0.01 sec)

 create user 'wp_user' identified by 'password';
 grant all on wordpress.* to 'wp_user';

// 終了
 exit

Apache再起動

systemctl stop httpd

systemctl start httpd

WordPress起動確認

ブラウザから(http://192.168.0.11/wordpress)へアクセスし、WordPressの画面が表示されれば完了です。

おわりに

今回の手順は最低限の操作でWordPressをCentOS8で構築する為の手順となりますので、例えばWordPressの権限設定等は適宜、見直しください。