サーバ移転したのでまた書きました。自分用に。
さくらの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) ALLsuできるユーザを指定。
# vi /etc/pam.d/suこの行を有効化
auth required pam_wheel.so use_uidrootで直接ログインできないようにします。
# vi /etc/ssh/sshd_configyesをnoに変更する
PermitRootLogin noSSH再起動。
# systemctl restart sshdCentOS8ではTCP wrapperが廃止されたので、SSHのアクセス制限はfirewalldでやる必要があります。未対応。
パッケージ管理
yumからdnfに変わりました。必要なリポジトリを追加します。
# dnf install epel-release
# dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpmRemiを指定してインストールする場合には以下のように記述します。
# 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=publicwebサーバ
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 -e00 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 httpdMySQL
以前は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 mysqldPHP
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.inipost_max_size = 128M
upload_max_filesize = 128M
memory_limit = 128M
date.timezone = "Asia/Tokyo"
display_errors = On変更を反映。
# systemctl restart php-fpmphpMyAdmin
せっかくなので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 postfixSMTP認証のためにSaslauthdを使用します。アプリと認証処理を分離してくれるものらしい。
# vi /etc/sysconfig/saslauthdLinuxユーザのパスワード(/etc/shadow)を使う
MECH=shadow起動します。
# systemctl enable saslauthd
# systemctl start saslauthdDovecot(受信)の設定。
dnf install dovecot# vi /etc/dovecot/conf.d/10-mail.conf以下の行のコメントアウトを外す
mail_location = maildir:~/Maildir# vi /etc/dovecot/dovecot.confprotocols = imap pop3 lmtp
listen = *# vi /etc/dovecot/conf.d/10-auth.confパスワードを平文にする
disable_plaintext_auth = no# vi /etc/dovecot/conf.d/10-ssl.confSSL接続しない
ssl = noDovecotを起動します。
# systemctl enable dovecot
# systemctl start dovecotuseradd時にメールボックス用のディレクトリを自動で作成されるようにします。
# 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/virtualhoge.com anything
info@hoge.com hoge-info
web@hoge.com hoge-web振り分けを反映させます。
# postmap /etc/postfix/virtualまとめ
CentOS7からちょこちょこ変更が入ってるね。
追記
まさか移行1ヶ月でCentOS終了してしまうとは… 次はRocky Linuxかなあ。

