CentOS7上でのWordPressの設定方法

CentOS7上でのWordPressの設定方法をまとめました。

設定環境

今回使用する環境は以下の通りです。

OS: CentOS7(7.7.1908)
Web server: Apache(2.4.6)
DB: MariaDB(5.5.64)

wordpressのセットアップ手順

  1. mariadb(DB)のインストール
  2. httpd(web server)のインストール
  3. phpのinstall
  4. 各設定
  5. メモ

1. mariadb(DB)のインストール

mariadbのインストールする。

# yum install mariadb-server

サーバ起動時に起動するように設定する。

# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service
 to /usr/lib/systemd/system/mariadb.service.

mariadbのステータスの確認
/usr/lib/systemd/system/mariadb.service; enabled;となっていればOK!

# systemctl status mariadb
mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor pres
et: disabled)
   Active: active (running) since Mon xxxx-xx-xx xx:xx:xx UTC; Xs ago
...

セキュリティ向上のため、以下コマンド(mysql_secure_installation)を実行し、新しいパスワードを設定する。

# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!

By default, MariaDB 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? [Y/n] 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? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

2. httpd(web server)のインストール

httpdをインストールする。

# yum install httpd

サーバ起動時にhttpdを起動するように設定する。

# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service t
o /usr/lib/systemd/system/httpd.service.

httpdのステータスを確認し、上記の設定が反映されているのかを確認する。
/usr/lib/systemd/system/httpd.service; enabled;となっていればOK!

# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset
: disabled)
   Active: inactive (dead)
...

httpdを起動する。

# systemctl start httpd

httpdのステータスを確認し、上記の設定が反映されているのかを確認する。
Active: active (running)となっていればOK!

# systemctl status httpd
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset
: disabled)
   Active: active (running) since Mon xxxx-xx-xx xx:xx:xx UTC; Xs ago

3. phpのinstall

EPEL repository&yum-utilsをインストールする。

# yum install epel-release yum-utils

REMI repositoryを追加する。

# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

php7.4をここでは使用する。

# yum-config-manager --enable remi-php74

wordpressを動作させるために必要なphpパッケージをインストールする。

# yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysqlnd

4. 各設定

DBの設定

wordpressで使用するDBを作成する。
<DB_NAME>, <DB_USER_NAME>, <DB_PASSWORD>へそれぞれ値を入れること!

# mysql -u root -p
MariaDB [(none)]> create database <DB_NAME>;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON <DB_NAME>.* TO <DB_USER_NAME>@localhost IDENTIFIED BY '<DB_PASSWORD>';
MariaDB [(none)]> FLUSH PRIVILEGES;

wordrepssの設置

wgetをインストールする。

# yum install wget

ここでは例としてsampleディレクトリ以下で、wgetでdownloadしてきた圧縮ファイルを解凍する。

# cd /var/www/sample
# wget http://wordpress.org/latest.tar.gz
# tar -xzvf latest.tar.gz

wp-config-sample.phpをコピーし、wp-config.phpファイルを修正する。<DB_NAME>, <DB_USER_NAME>, <DB_PASSWORD>へ設定した値を入れる。

# cp wp-config-sample.php wp-config.php
# vi wp-config.php
define('DB_NAME', '<DB_NAME>');
define('DB_USER', '<DB_USER_NAME>');
define('DB_PASSWORD', '<DB_PASSWORD>');

httpdのconfファイル設定

Virtual Hostの設定をするために以下へ移動する。

# cd /etc/httpd/conf.d

80ポートでwordpressのサイトがみれるように設定

# vi myhost.conf
Listen 80

ServerName sample.test

<VirtualHost XX.XX.XX.XX:80>
  DocumentRoot /var/www/sample/wordpress
  <Directory /var/www/sample/wordpress>
      Options FollowSymLinks
      AllowOverride All
      Require all granted
  </Directory>
</VirtualHost>

confファイル修正後、httpdを再起動する。

# systemctl restart httpd

5. メモ

SELinuxが有効な場合、サイトにアクセスできないことがある

# grep -r "Permission" /var/log/httpd/error_log
[Wed Jan 01 XX:XX:XX.XXXXXX 2020] [core:error] [pid 1278] (13)Permission denied:
 [client 192.168.XX.XX:XXXXX] AH00035: access to /index.php denied (filesystem pa
th '/var/www/vlog/wordpress/index.php') because search permissions are missing o
n a component of the path

SELinuxをPermissiveにする。

# getenforce
Enforcing
# setenforce 0
# getenforce
Permissive

次回サーバ起動時にもSELinuxをPermissiveにするために、以下ファイルを編集する。

# vi /etc/selinux/config
SELINUX=permissive

welcomeページの設定をOFFにする

welcome.confがある場合、welcomeページにいってしまうため、ここではファイル名を変更して読み込まれないようにする

# cd /etc/httpd/conf.d/
# mv welcome.conf welcome.conf.bak
タイトルとURLをコピーしました