ホーム » 技術 » WordPressをCentOS7/nginxでインストール

WordPressをCentOS7/nginxでインストール

WordPressを、CentOS 7.5/nginxで動くサーバにインストールする手順をまとめてみた。ようするにこのサイトを作る手順のメモ。

サーバの準備

大したアクセスはない、という仮定でさくらのクラウドの 1core 2GB サーバを作る。アーカイブから CentOS 7.5 を選んで作成する。

個人的な好みで、/etc/locale.conf を書き換えて英語にしておく。

LANG="en_US.UTF-8"

それからアップデートして最新にしておく。

yum -y update
reboot & exit

リブートのついでに、nameserverを設定してIPアドレスを引けるようにしておく。この辺は持っているドメインの管理コンパネを使う。IPアドレスの逆引きは、サーバの管理画面の方でできる。

DB (Percona Server) のインストール

DBは趣味で Percona Server を入れる。特に冗長にする気もないので、PXCではなくシングルのサーバを入れる。次の通り。

yum -y install https://repo.percona.com/yum/percona-release-latest.noarch.rpm
yum -y install Percona-Server-server-57

インストールが終わったら、起動前に設定ファイルを準備する。/etc/my.cnf.d/mysqld.conf ぐらいが適当。

[mysqld]
innodb_file_per_table=ON
innodb_buffer_pool_size=1024M
innodb_log_file_size=256M

innodb_flush_method=O_DIRECT

character-set-server=utf8mb4

できたらまずは起動する。パスワードがログファイルに書かれるので、それを使って最初のログインをし、パスワードを設定する。

systemctl start mysql
grep password /var/log/mysqld.log
  # 初期パスワードをメモ
mysql -u root -p
  # メモした初期パスワードでログイン
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password-for-root';

ついでにDBを作っておく。

CREATE DATABASE northpage;
\q

nginx/php-fpmのインストール

firewall に穴をあけておく。

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

nginxと、remi経由でphp73をインストールする。

rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y install nginx
yum -y install php73 php73-php-fpm php73-php-mbstring php73-php-pear php73-php-mcrypt php73-php-mysql

/etc/nginx/nginx.conf を設定。WordPressの本家などを参考に、WordPressが動くように設定する。ここはサイトによりけりなので省略。

/etc/opt/remi/php73/php.ini を書き換える。主に timezone とか。

/etc/opt/remi/php73/php-fpm.d/www.conf を書き換える。こんな感じ。

; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

できたら起動する。

systemctl enable nginx php73-php-fpm
systemctl start nginx php73-php-fpm

Let’s Encryptの設定 (certbotのインストール)

最後に Let’s Encrypt で HTTPS 化する。

yum -y install certbot python2-certbot-nginx

certbot を実行して、対話的に実行すれば、なんとなくインストールできる。/etc/nginx/nginx.conf を勝手に書き換えるので注意。

/etc/cron.d/certbot-renew に証明書のチェックを書く。

MAILTO=
13 9 * * mon root /usr/bin/certbot renew --post-hook "systemctl restart nginx" | logger -t certbot-renew -p local0.info

WordPressインストール

有名なWordPressインストールの画面へ。あとはお好みにあわせて。