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

サーバ移転したのでまた書きました。自分用に。

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

ユーザ作成

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

# passwd

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

ユーザーhoge作成
# useradd hoge

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

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

作業用アカウント(wheelグループ)以外ではrootになれないように制限します。元から設定済みでしたが念のため。

# visudo

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

suできるユーザを指定。

# vi /etc/pam.d/su

この行を有効化
auth required pam_wheel.so use_uid

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

# vi /etc/ssh/sshd_config

yesをnoに変更する
PermitRootLogin no

SSH再起動。

# systemctl restart sshd

CentOS8ではTCP wrapperが廃止されたので、SSHのアクセス制限はfirewalldでやる必要があります。未対応。

パッケージ管理

yumからdnfに変わりました。必要なリポジトリを追加します。

# dnf install epel-release
# dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

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

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

全部のアプリをアップデートする。

# dnf --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をインストールします。

# dnf 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 -u 0022

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

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

バーチャルホスト

ドメインごとに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をインストールします。

# dnf --enablerepo=remi install mod_ssl

証明書は無料のLet’s Encryptを使います。

インストール
# dnf install certbot

ドメイン追加
# certbot certonly --webroot -w /home/hoge/public_html -d hoge.com

バッチで証明書を自動更新します。

# crontab -e

00 04 01,15 * * certbot renew --post-hook "systemctl restart httpd"

SSLサーバの設定はドメインの設定ファイルに追加します。

vi /etc/httpd/conf.d/hoge.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
ErrorLog logs/hoge.com-error_log
CustomLog logs/hoge.com-access_log common
</VirtualHost>

設定できたら再起動。

# systemctl restart httpd

MySQL

以前はMariaDBを使ってたけど、色々あってMySQLに戻ってきました。

# dnf install mysql mysql-server

起動

# systemctl enable mysqld
# systemctl start mysqld

初期設定

# mysql_secure_installation
出てくる質問は以下の通り(英語)。
・パスワード検証ポリシー:0
・rootのパスワード:
・rootのパスワード確認用:
・いいですか?:y
・匿名ユーザの削除:y
・リモートからrootユーザ接続禁止:y
・テスト用データベースの削除:y
・変更を有効にする:y

デフォルト文字コードはutf8mb4だったので、そのままにしておきます。

終わったら再起動。

# systemctl restart mysqld

PHP

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

# dnf module install php:remi-7.4
# dnf install --enablerepo=remi php-devel php-mysqlnd php-pear php-mbstring php-pdo php-gd

各種設定。

# vi /etc/php.ini

post_max_size = 128M
upload_max_filesize = 128M
date.timezone = "Asia/Tokyo"
display_errors = On

変更を反映。
# systemctl restart php-fpm

phpMyAdmin

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

# dnf install --enablerepo=remi phpmyadmin

ここにもアクセス制限。

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

<Directory /usr/share/phpMyAdmin/>
の中の
Require local

Require host .jp
などに変更

メールサーバ

Postfix(送信)の設定。

# dnf install postfix cyrus-sasl cyrus-sasl-md5 cyrus-sasl-plain

# 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 = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination
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_relay_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(受信)の設定。

dnf 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/

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

メールアドレス作成

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

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

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

# vi /etc/postfix/virtual

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

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

# postmap /etc/postfix/virtual

まとめ

CentOS7からちょこちょこ変更が入ってるね。

追記
まさか移行1ヶ月でCentOS終了してしまうとは… 次はRocky Linuxかなあ。

コメントを残す

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


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