はじめに
CentOSでのサーバ構築は6までしか経験してなかったのですが、今後建てるなら7でしょ、ということで手順をまとめてみます。
設定内容
VPSでCentOS7を選び、webサーバとして最低限の機能を構築することを想定しています。
- 必要なパッケージをインストール
- SSH設定
- httpd2.4 + let’s encryptでhttps接続
必要なパッケージをインストール
まずはカーネルその他諸々アップデート
yum -y install yum-priorities yum-fastestmirror
 
yum update
dnf
yumの後継として、Fedoraでは22以降で標準になっているパッケージ管理システムです。
 CentOS7でも使えますが、標準では入っておらず、yumでインストールすることもできないようです。
| 1 2 3 4 5 6 7 8 9 | $ yum install --enablerepo=epel dnf Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile  * base: ftp.riken.jp  * epel: ftp.riken.jp  * extras: ftp.riken.jp  * updates: ftp.riken.jp No package dnf available. Error: Nothing to do | 
ということで、RPMを直接ダウンロードしてインストールします。
 参考:CentOS 7.2でdnfを使う方法
| 1 2 3 4 | $ wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/dnf-conf-0.6.4-2.sdl7.noarch.rpm $ wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/dnf-0.6.4-2.sdl7.noarch.rpm $ wget http://springdale.math.ias.edu/data/puias/unsupported/7/x86_64/python-dnf-0.6.4-2.sdl7.noarch.rpm $ yum install python-dnf-0.6.4-2.sdl7.noarch.rpm dnf-0.6.4-2.sdl7.noarch.rpm dnf-conf-0.6.4-2.sdl7.noarch.rpm | 
以後はdnfを使っていきます。yumと同じような感じで使えます。
SSH設定
ユーザー作成・設定
| 1 2 3 4 5 6 7 8 9 10 | # ユーザー作成 useradd testuser # パスワード設定 passwd testuser # sudoできるように usermod -G wheel testuser # wheelグループであれば全コマンドが使えるようにする visudo ## Allows people in group wheel to run all commands # %wheel        ALL=(ALL)       ALL <= この行がコメントアウトされている場合は、コメントアウトを外す | 
ローカル側で鍵作成
| 1 2 | cd ~/.ssh/ ssh-keygen -t rsa -b 4096 -f hoge_rsa | 
パスなしで作成します(何も入力せずEnterでOK)。
 hoge_rsaとhoge_rsa.pubが作成されるので.pubの方をリモートにアップロードします。
 
scp -P ポート番号 ローカルからアップロードしたいファイルのパス ユーザ名@リモートのホスト名:リモートのコピー先ディレクトリ
サーバにアップロードした公開鍵をリネーム
| 1 2 3 4 5 6 7 | su testuser mkdir ~/.ssh cd ~/.ssh cat hoge_rsa.pub > authorized_keys rm -rf hoge_rsa.pub chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys | 
sshd_config編集
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | sudo vi /etc/ssh/sshd_config # ポートを変えておく # ウェルノウンポート(0〜1023)と、サーバに入っているサービスが使っているポート(例:MySQLなら3306)を避けます。 # 私は大体いつも20000番台で設定しています。 #Port 22 Port 23456 # 以下の行のコメントアウトを外して有効化する #PubkeyAuthentication yes #AuthorizedKeysFile     .ssh/authorized_keys # 以下は無効化する PermitRootLogin no PasswordAuthentication no | 
ここまでやったらsshdを再起動します。
 
sudo systemctl restart sshd
サービスの自動起動は最後にまとめて設定します。
ファイアウォールの設定変更
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | # firewalldの設定からSSHを削除 $ sudo firewall-cmd --permanent --remove-service=ssh # firewalldに新しいssh設定を追加する $ sudo cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/ssh.xml # ファイル内のポート番号を変更する $ sudo vi /etc/firewalld/services/ssh.xml <port protocol="tcp" port="22"/> ↓ <port protocol="tcp" port="23456"/> # 新しいssh設定を追加して、ファイアウォール再起動 $ sudo firewall-cmd --permanent --add-service=ssh $ sudo firewall-cmd --reload | 
ローカルから鍵認証でSSHログインできるかを確認します。
 
ssh -p 23456 -i ~/.ssh/hoge_rsa xxx@xxx.xxx.xxx.xxx
httpd2.4
インストール
Baseリポジトリには2.4系の古いバージョンしかないので、最新バージョンを扱うリポジトリを登録します。
 参考:CentOS7にApache2.4最新版をyum installする
 
dnf -y install https://centos7.iuscommunity.org/ius-release.rpm
iusリポジトリに無いが、httpdインストール時に必要となるパッケージをBaseリポジトリから入れておきます。
 
dnf install expat-devel libdb-devel cyrus-sasl-devel
これで準備完了です。httpdの古いバージョンが入ることを防ぐため、Baseリポジトリを無効化してインストールします(ついでにopensslとmod_sslも)。
 
dnf install --disablerepo=base --enablerepo=ius httpd-devel openssl-devel mod_ssl
ファイアウォール設定
httpで80番、httpsは443番を開放します。
| 1 2 3 4 5 6 | $ sudo firewall-cmd --permanent --add-service=http $ sudo firewall-cmd --permanent --add-service=https $ sudo firewall-cmd --reload # httpd起動 $ systemctl start httpd | 
ここまででhttp接続はできるようになっているはずです。ブラウザでアクセスして、おなじみの
It works!
が表示されればOKです。

let’s encryptでhttps化
certbotインストール&実行
| 1 2 3 | $ sudo curl https://dl.eff.org/certbot-auto -o /usr/bin/certbot-auto $ sudo chmod 700 /usr/bin/certbot-auto $ sudo certbot-auto certonly --webroot -w /var/www/html -d test.com --email 任意のメールアドレス | 
let’s encryptで取得した証明書の期限は3ヶ月なので、定期的(ここでは毎月1日の04:00)に更新するようにcronを設定します。
| 1 2 | $ sudo crontab -e 00 04 01 * * /usr/bin/certbot-auto renew --force-renew && systemctl reload httpd | 
正常に終了していれば、公開鍵ファイルが生成されています。
| 1 2 3 4 5 | $ sudo ls -l /etc/letsencrypt/live/test.com/ lrwxrwxrwx 1 root root  40 42  6月 25 01:05 cert.pem -> ../../archive/test.com/cert1.pem lrwxrwxrwx 1 root root  41 42  6月 25 01:05 chain.pem -> ../../archive/test.com/chain1.pem lrwxrwxrwx 1 root root  45 42  6月 25 01:05 fullchain.pem -> ../../archive/test.com/fullchain1.pem lrwxrwxrwx 1 root root  43 42  6月 25 01:05 privkey.pem -> ../../archive/test.com/privkey1.pem | 
ssl.conf編集
以前にも紹介した「Mozilla SSL Configuration Generator」が便利です。
httpd2.4.33 + openssl1.0.2k + Modern(セキュリティ強め設定、要件次第では不要かも)
| 1 2 3 4 5 | $ sudo vi /etc/httpd/conf.d/ssl.conf # certbotが生成した公開鍵のパスを記述します SSLCertificateFile /etc/letsencrypt/live/test.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/test.com/fullchain.pem | 
※ 
SSLProtocol
、
SSLCipherSuite
はVirtualHostディレクティブの外で設定しましょう。
サービスの自動起動設定
サーバが再起動した際、自動でサービスが立ち上がるようにします。
- CentOS6まで:
chkconfig サービス名 on
- CentOS7:
systemctl enable サービス名.service
.serviceは省略してもいいようです。
| 1 2 3 | $ sudo systemctl enable sshd $ sudo systemctl enable httpd $ sudo systemctl enable crond | 
確認コマンドは下記の通りです。
enabled
になっていればOKです。
| 1 2 3 4 | $ sudo systemctl list-unit-files -t service crond.service                                 enabled httpd.service                                 enabled sshd.service                                  enabled | 
httpsで接続確認
ブラウザで再度アクセスしてみます。
 ssl.confで
Strict-Transport-Security
が有効になっていれば、http接続しても強制的にhttpsにリダイレクトされます。
 
It works!
が出てくればOKです。お疲れ様でした。
さいごに
いかがでしたしょうか。元々あったCentOS6向けの自分用メモを更新して、その内容を一部改変し、説明を付け足した感じです。ある程度は定型的な作業になると思いますので、こうやって手順を整理して、サクサクできるように心がけています。
 
  
 
 
 
 
