サーバの初期設定する(CentOS7.2)

以前書いた記事は古くなったので、新しく書き直しました。自分用に。

さくらのVPSを使っているので、ところどころ www????.sakura.ne.jp として入力しています。

ユーザ作成

まずはrootのパスワードを変更します。

# passwd

作業用アカウントを作成します。

ユーザーhoge作成
# useradd hoge

hogeのパスワード設定
# passwd hoge

hogeをwheelグループに入れる
# usermod -G wheel hoge

作業用アカウント(wheelグループ)以外ではrootになれないように制限します。

sudoできるユーザを指定。

# visudo

この行を有効化
%wheel ALL=(ALL) ALL

suできるユーザを指定。

# vi /etc/pam.d/su

この行を有効化
auth required /lib/security/$ISA/pam_wheel.so use_uid

rootで直接ログインできないようにします。

# vi /etc/ssh/sshd_config

yesをnoに変更する
PermitRootLogin no

SSH再起動。

# systemctl restart sshd

TCP wrapperでsshにログインできるホストを制限します。

拒否リスト。

# vi /etc/hosts.deny

以下を追加
sshd : all

許可リスト。

# vi /etc/hosts.allow

以下を追加
sshd : localhost
sshd : hoge.com
sshd : .jp

これでサーバ自身と.jp以外の人はアクセスできなくなりました。ちゃんと自分のプロバイダに合致したホスト名を指定しないと繋がらなくなります。

yumでRemiリポジトリを使う

標準のyumだと古いパッケージばかりなので、新しいのがインストールできるRemiリポジトリを使用します。

CentOS7用
# wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# rpm -Uvh remi-release-7.rpm

Remiを指定してインストールする場合には以下のように記述します。

# yum --enablerepo=remi install (パッケージ名)

アップデートは新旧入り混じると面倒なので、Remiは入れたのは個別でやった方が良いのかな。

通常パッケージ全部アップデート
# yum update

Remiで入れたの個別アップデート
# yum --enablerepo=remi update (パッケージ名)

ファイアウォール

起動
# systemctl enable firewalld
# systemctl start firewalld
設定追加
# firewall-cmd --add-service=http --zone=public --permanent
# firewall-cmd --add-service=https --zone=public --permanent
# firewall-cmd --add-service=smtp --zone=public --permanent
# firewall-cmd --add-port=110/tcp --zone=public --permanent
# firewall-cmd --add-port=587/tcp --zone=public --permanent
# firewall-cmd --reload

現在の設定は以下で確認できます。

# firewall-cmd --list-all --zone=public

webサーバ

Apache 2.4をインストールします。

# yum install httpd

起動
# systemctl enable httpd
# systemctl start httpd

ドメインごとに管理するユーザを分けます。普通にユーザを作成すると自分のホームディレクトリより上の階層に行けてしまうので、chrootを使って行けないようにします。

グループ作成。

# groupadd sftpgroup

グループに所属するユーザにchrootを適用します。

# vi /etc/ssh/sshd_config

サブシステム変更
#Subsystem sftp /usr/libexec/openssh/sftp-server
Subsystem sftp internal-sftp

グループと行ける範囲を末尾に追加
Match group sftpgroup
ChrootDirectory ~
ForceCommand internal-sftp

sshdを再起動します。設定間違うとサーバにアクセスできなくなるので、念の為ターミナルをもう1つ開いておいたり。

# systemctl restart sshd

あとはユーザを作成していけば /home/hoge/ がsshのルートディレクトリになります。

ユーザ作成
# useradd -g sftpgroup hoge
# passwd hoge

ホームディレクトリの所有者をrootにする
# chown root:root /home/hoge
# chmod 755 /home/hoge

ディレクトリ作成とパーミッション変更
# mkdir /home/hoge/public_html
# chmod 777 /home/hoge/public_html

バーチャルホスト

1台のサーバで複数のドメインを運用したいので、バーチャルホストの設定をします。

# vi /etc/httpd/conf/httpd.conf
この行のコメントアウトを外す
NameVirtualHost *:80

ドメインごとにconfファイルを作成します。以下はhoge.com用です。

# vi /etc/httpd/conf.d/hoge.conf

<VirtualHost *:80>
ServerAdmin webmaster@hoge.com
DocumentRoot /home/hoge/public_html
ServerName hoge.com
ServerAlias www.hoge.com
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log common
</VirtualHost>

公開前にサイトを確認できるようにします。ユーザー名がhogeの場合、こんなチルダ付のURLで見れるようになります。
http://www????.sakura.ne.jp/~hoge/

# vi /etc/httpd/conf.d/userdir.conf

コメントアウト
#UserDir disabled

有効化
UserDir public_html

Allにする
AllowOverride All

必要なものだけ
Options IncludesNoExec ExecCGI FollowSymLinks

SSL

OpenSSLは既に入っているので、mod_sslをインストールします。

# yum --enablerepo=remi install mod_ssl

SSL証明書は無料のLet’s Encryptを使います。取得方法は省略。

SSLサーバの設定は以下にまとめて書いておきます。

vi /etc/httpd/conf.d/vhost.conf

<virtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/hoge.com/cert.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hoge.com/privkey.pem
SSLCACertificateFile /etc/letsencrypt/live/hoge.com/chain.pem
ServerAdmin webmaster@hoge.com
DocumentRoot /home/hoge/public_html
ServerName hoge.com
ServerAlias www.hoge.com
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log common
</VirtualHost>

設定できたら再起動。

# systemctl restart httpd

MySQL(MariaDB)

MariaDBインストールします。

# yum install mariadb mariadb-server

起動

# systemctl enable mariadb
# systemctl start mariadb

初期設定

# mysql_secure_installation
出てくる質問は以下の通り。
・MariaDBのrootにパスワード設定
・匿名ユーザ(anonymous)の削除
・リモートからrootユーザ接続禁止
・テスト用データベースの削除

文字コードをUTF-8にする。

vi /etc/my.cnf.d/server.cnf

[mysqld]
の後に以下を追加
character-set-server=utf8

終わったら再起動

# systemctl restart mariadb

PHP

PHP7とそれに関係するものをインストール。

# yum install --enablerepo=remi,remi-php70 php php-devel php-mysqlnd php-pear php-mbstring php-pdo php-gd

phpMyAdmin

せっかくなのでMySQL(MariaDB)の管理ページを入れる。

# yum --enablerepo=remi-php70 install phpmyadmin

ここにもアクセス制限。

vi /etc/httpd/conf.d/phpMyAdmin.conf

<RequireAny>
の下に
Require host .jp
など

メールサーバ

ない場合はインストール。

# yum install postfix cyrus-sasl cyrus-sasl-md5

Postfix(送信)の設定。

# vi /etc/postfix/main.cf

以下のように修正
myhostname = www????.sakura.ne.jp
mydomain = www????.sakura.ne.jp
myorigin = $mydomain
inet_interfaces = all
#inet_interfaces = localhost
inet_protocols = ipv4
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
smtpd_banner = $myhostname ESMTP unknown

以下を追加(スパム対策)
disable_vrfy_command = yes
smtpd_helo_required = yes

以下を追加(バーチャルドメイン用)
virtual_alias_maps = hash:/etc/postfix/virtual

以下を追加(SMTP-Auth用)
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = cyrus
smtpd_sasl_path = smtpd
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = smtpd_relay_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

以下を追加(メールへの添付可能サイズを無制限にする)
message_size_limit = 0
mailbox_size_limit = 0

メール送信時にサブミッションポート(587)を使用します。

# vi /etc/postfix/master.cf

以下の行のコメントアウトを外す
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING

再起動。

# systemctl restart postfix
# systemctl enable postfix

SMTP認証のためにSaslauthdを使用します。アプリと認証処理を分離してくれるものらしい。

# vi /etc/sysconfig/saslauthd

Linuxユーザのパスワード(/etc/shadow)を使う
MECH=shadow

起動します。

# systemctl enable saslauthd
# systemctl start saslauthd

Dovecot(受信)をインストールします。

yum install dovecot

設定していきます。

# vi /etc/dovecot/conf.d/10-mail.conf

以下の行のコメントアウトを外す
mail_location = maildir:~/Maildir
# vi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
listen = *
# vi /etc/dovecot/conf.d/10-auth.conf
パスワードを平文にする
disable_plaintext_auth = no
# vi /etc/dovecot/conf.d/10-ssl.conf
SSL接続しない
ssl = no

Dovecotを起動します。

# systemctl enable dovecot
# systemctl start dovecot

useradd時にメールボックス用のディレクトリを自動で作成されるようにします。

# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
# chmod -R 700 /etc/skel/Maildir/

メールアドレス作成

メールアカウント用のユーザをシェルログイン不可で作成します。

# useradd -s /sbin/nologin hoge-info
# passwd hoge-info

useraddしたらメールボックスはこのように作成されました。

/home/hoge-info/Maildir/new/
/home/hoge-info/Maildir/cur/
/home/hoge-info/Maildir/tmp/

メールアドレスとその振り分け処理を作成します。ドメインごとにanythingの行を入れないとメールが使用できません。

# vi /etc/postfix/virtual

hoge.com         anything
info@hoge.com hoge-info
web@hoge.com hoge-web

振り分けを反映させます。

# postmap /etc/postfix/virtual

まとめ

1記事にまとめてみたけど、やっぱ設定項目多いね。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です


The reCAPTCHA verification period has expired. Please reload the page.