概要
サーバの初期セットアップからwordpress環境構築まで記します。
沢山のwebサイトで手順は公開されていますが、それらから色々省いたり自分なりにまとめたものです。
まずはサーバの初期セットアップ編です。
これを実施すればwordpressを運営するサーバの基本的な環境が整います。
環境
さくらのVPS CentOS7(標準OS)
前提
さくらのVPSにCentOS7をインストールしてある
やる気を出す
サーバの初期セットアップ編
Login
まずはsshやVNCコンソールにてrootアカウントでサーバにログインする。
teratermやterminalを使ってsshでログインする時の書式は username@ipaddr なので接続先には下記のように指定する。
ssh root@XXX.XXX.XXX.XXX
yum update
何はともあれ、とりあえずyum updateです。
# yum update
作業用ユーザの作成
usernameのところを好きな名前にしてください。
# useradd username
作業用ユーザのPW設定
単純過ぎると怒られますのである程度複雑なものにしてください。
# passwd username
Changing password for user username.
New password: ←PW入力
Retype new password: ←PW再入力
passwd: all authentication tokens updated successfully.
sudo 設定の有効化
作成したユーザがsudoコマンドを実行できるようにする為に、wheelグループに先ほど作成した作業用ユーザを追加してあげます。
# usermod -G wheel username
※この手順はガンガンrootユーザで作業を行っていますが、基本は一般ユーザで作業し必要に応じてsudoコマンドを用いて管理者コマンドを実行するようにして下さい。
日本語の設定
まず下記のコマンドを実行してみて下さい。
# date
Sat Apr 29 00:08:41 JST 2017
で、日本語設定をします。
# localectl set-locale LANG=ja_JP.utf8
サーバの再起動後に設定が反映します。
後にサーバの再起動手順が出てくるので、再起動が済んだら改めて date コマンドを実行してみて下さい。
SElinuxの有効化する
SELinuxはLinux実装された強固なセキュリティ機構です。
さくらのVPS 標準OS CentOS7 の場合、最初はSELinuxが無効になっています。
※この手順では最終的にSELinuxは無効にしますが、いざ有効にした時に困らないように設定を行っておきます。
# vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disable ← ここを enforcing に変更する
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
ちなみに enforcing だと何かと不便な場合は permissive にすると、アクセス制限は行わず警告を出力する状態になります。好きにして下さい。
サーバの再起動
ここまで設定が済んだら以下の目的で再起動を行います。
1.SELinuxの設定を反映
2.日本語の設定を反映
下記のコマンドを実行して下さい。
# systemctl reboot
再起動するとsshクライアントは一度接続が切れるので、数分してから再接続して下さい。
再起動が済んだら日本語設定が反映してるかどうか確認する為に date コマンドを実行してみて下さい。
下記のようになればOKです。
# date
2017年 4月 29日 土曜日 00:39:36 JST
sshの設定
次にsshの接続ポートの変更等を行う為 etc/ssh/sshd_config の編集を行います。
いきなり編集せずに念のためssh_configファイルのバックアップを取っておいてください。
# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.org
/etc/ssh/sshd_configをviで編集しrootアカウントでのログインとパスワード認証の禁止、ssh接続ポートの変更を行います。
# vi /etc/ssh/sshd_config
# $OpenBSD: sshd_config,v 1.69 2004/05/23 23:59:53 dtucker Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
~ 省略 ~
上記のテキストの中の一部を下記のように編集する
①
# PermitRootLogin yes
↓
PermitRootLogin no
②
# PasswordAuthentication yes
↓
PasswordAuthentication no
③
# Port 22
↓
Port 3333 ※well-known ports以外で任意の番号
で、sshd.service を再起動する。
# systemctl restart sshd
続けて、firewalld の設定変更を行う
# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/
# vi /etc/firewalld/services/ssh.xml
<?xml version="1.0" encoding="utf-8"?><service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22"/> ←ここのportを先ほど指定した任意の番号に変更する
</service>
設定を有効にする為、リロードします。
# firewall-cmd --reload
success
あとは、変更後のポート番号でログインが出来れば設定完了
terminal等でログインする際は下記のようにポート番号を指定する。
$ ssh username@XXX.xXX.XXX.XXX -p 10022
SAKURA Internet [Virtual Private Server SERVICE]
[username@hosts ~]$
SELinuxの設定
SElinuxの設定用コマンドをインストールします。
※wordpressを運用するにあたりSElinuxは最終的に無効にしますが一応port番号の設定はしておきます。有効のまま運用したい方はTipsを参照してください。
# yum -y install policycoreutils-python
先ほどSSHの接続用ポート番号を変更したので、SElinux側にもそれを反映させてあげます。
# semanage port -a -t ssh_port_t -p tcp portnumber ← portnumber に先ほど変更したport番号を入力
# semanage port -l | grep ssh
ssh_port_t tcp portnumber,22 ← portnumber に変更後の番号が追加されていればOK
SELinuxの設定時の注意事項
SELinuxの設定にあたりSElinuxが無効になっていると以下のようなエラーになります。
さくらのVPSでCentOS7を使用する場合、デフォルトではSELinuxが無効になっているようなので有効に設定してあげる必要があります。
# semanage port -a -t ssh_port_t -p tcp 3333
SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.30: No such file or directory
/sbin/load_policy: Can't load policy: No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
SELinux: Could not downgrade policy file /etc/selinux/targeted/policy/policy.30, searching for an older version.
SELinux: Could not open policy file <= /etc/selinux/targeted/policy/policy.30: No such file or directory
/sbin/load_policy: Can't load policy: No such file or directory
libsemanage.semanage_reload_policy: load_policy returned error code 2. (No such file or directory).
OSError: No such file or directory
詳細はRedHat社の公式ドキュメント参照
SELinuxのステータス取得
SElinux のステータスをチェックするには、getenforce または sestatus コマンドを使います。
ステータスの意味はElinuxを一時的に有効・無効にする方法を参照して下さい。
# getenforce
Permissive
# sestatus
SELinux status: enabled
<省略>
SElinuxを一時的に有効・無効にする方法
有効化
# setenforce 1
一時的なステータス変更コマンド | 変更後のステータス | ステータスの意味 |
---|---|---|
setenforce 0 | permissive | ポリシーに違反するもののログが残る※ |
setenforce 1 | enforcing | 有効 |
※監査ログは /var/log/audit/audit.log に記録されるが ausearch -m avc で確認する方がわかりやすいです。詳細な確認方法はコチラを参照
Tips
SELinuxを有効にしてwordpressを運営する場合の設定
setsebool -p httpd_can_network_connect_db 1
setsebool -p httpd_tty_comm 1
setsebool -p httpd_unified 1
setsebool -p httpd_dbus_avahi 1
semanage fcontext -a -t httpd_sys_syscontent_t "/var/www/html/"
semanage fcontext -a -t httpd_sys_rw_syscontent_t "/var/www/html/wp-content(/.*)?"
restorecon -R -v /var/www/html
※ドキュメントルート以外にwordpressをインストールしている場合は指定のパスも任意のものに変わりますので通常wordpressをインストール後に実施すべきcommandです。
設定が済んだら次はCentOS7でwordpress環境の構築 -PHPのインストールとミドルウェアのセットアップ編-です。