さくらVPS契約してみた やり直しその4

3. アプリ色々(続)

3.4. postfix

CentOS8 では、デフォルトで入っているメールサーバーは何もないので、postfix を入れていく。

dnf install -y postfix postfix-mysql
alternatives --display mta
mta -ステータスは自動です。
リンクは現在 /usr/sbin/sendmail.postfix を指しています。
/usr/sbin/sendmail.postfix - priority 60
 スレーブ mta-mailq: /usr/bin/mailq.postfix
 スレーブ mta-newaliases: /usr/bin/newaliases.postfix
 スレーブ mta-pam: /etc/pam.d/smtp.postfix
 スレーブ mta-rmail: /usr/bin/rmail.postfix
 スレーブ mta-sendmail: /usr/lib/sendmail.postfix
 スレーブ mta-mailqman: /usr/share/man/man1/mailq.postfix.1.gz
 スレーブ mta-newaliasesman: /usr/share/man/man1/newaliases.postfix.1.gz
 スレーブ mta-sendmailman: /usr/share/man/man1/sendmail.postfix.1.gz
 スレーブ mta-aliasesman: /usr/share/man/man5/aliases.postfix.5.gz
 スレーブ mta-smtpdman: /usr/share/man/man8/smtpd.postfix.8.gz
現在の「最適」バージョンは /usr/sbin/sendmail.postfix です。
postconf |grep mail_version
mail_version = 3.3.1
milter_macro_v = $mail_name $mail_version

postconf で下記エラーが出る場合は、hosts ファイルを編集して、IPv6 のループバックをコメントアウトしておく。

postconf |grep mail_version
postconf: fatal: parameter inet_interfaces: no local interface found for ::1
vi /etc/hosts
===
### 下記をコメントアウト
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
===

logwatch や ClamAV などからの通知メールを受け取るだけなら、特に設定は不要。

systemctl enable postfix
systemctl start postfix

ためしに、sendmail コマンドでメールを送信してみたが…メールが届かない。

postfix のログを見る限りだと、”status=sent” とあるので、送信自体は出来ていそうだが…

*メールアドレスを変えると送信できたので、受信側の問題。最初のメールの送り先は、普段使っているメインのドメインのもの。

3.5. Dovecot

Dovecot は、Linuxで多くのシェアを誇っている、オープンソースの POP/IMAP サーバー。postfix は送信サーバーなので、それだけではメールの受信はできない。ゆくゆくはレンサバで利用しているメールサーバーをこっちに移したいので、受信サーバーも立てておく。

sudo dnf -y install dovecot dovecot-mysql

さくら公式のスタートアップスクリプトを見る限り、設定はSSL証明書を取得した後で良い模様。データベース関連の設定やら、SSL関連の設定やらもあるので、そういう意味でも、DBやSSLの設定を行ってからのほうが良さそうだ。

3.6. MariaDB(MySQL)

DBMSとして、MariaDBをインストールしていく。

sudo dnf -y install mariadb-server
sudo systemctl enable mariadb.service
sudo systemctl start mariadb.service
sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

3.7. Apache

Webサーバーとしては Apache を入れていく。Nginx という選択肢もあるが…スタートアップスクリプトは Apache 前提で作ってあるし、Nginx はあまり慣れていないので、Apache をひとまず採用。

あわせて、mod_ssl のモジュールも入れておく。

sudo dnf -y install httpd mod_ssl

これも、設定やサービスの起動はSSL証明書を取得してから。

3.8. PHP

CentOS8 (8.2.2004) では、標準リポジトリからPHPをインストールすると、7.2が入る。

が、7系の最新は現状 7.4.12 で、レンサバでもちらほら対応しているところもあるので、今から立ち上げるサーバーなら、7系の最新である7.4 にしておきたいところ。

sudo dnf -y install php74 php74-php-mbstring php74-php-imap php74-php-mysql php74-php-fpm

これでインストールしようとすると、php74-php-imap のところで、「nothing provides libc-client.so.2007()(64bit) needed by php74-php-imap-7.4.12-1.el8.remi.x86_64」というエラーが出た。

情報を探すと、libc-client というのを入れておかないとダメらしい。

wget https://download-ib01.fedoraproject.org/pub/epel/8/Everything/x86_64/Packages/l/libc-client-2007f-24.el8.x86_64.rpm
sudo rpm -Uvh libc-client-2007f-24.el8.x86_64.rpm
sudo dnf -y install php74-php-imap

さくら公式のスタートアップスクリプトではlibc-clientをインストールしている形跡は無いようだったので、PHP7.4だったから必要だったのか…標準レポジトリじゃないから必要だったのか…イマイチわからん。

なお、このままだと、インストールしたPHPは「php74」というコマンドでないと実行できないので、「php」で呼べるようにしておく。

php -v
-bash: php: コマンドが見つかりません
sudo alternatives --install /usr/bin/php php /usr/bin/php74 1
php -v
PHP 7.4.12 (cli) (built: Oct 27 2020 15:01:52) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

3.9. Let’s Encrypt

さくら公式のスタートアップスクリプトに倣って、certbot をgitからダウンロードしてきて、証明書を取得してみる。

sudo git clone https://github.com/certbot/certbot /usr/local/certbot
sudo /usr/local/certbot/certbot-auto certonly -n --standalone --agree-tos -d [証明書を取得するドメイン] -m [管理者メルアド] --server https://acme-v02.api.letsencrypt.org/directory
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for virtual.hogehoge.com
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/virtual.hogehoge.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/virtual.hogehoge.com/privkey.pem
   Your cert will expire on 2021-02-21. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

Congratuation! とか出てるし、実際に証明書ファイルも生成されているようなので、問題なさそうだ。

あとは、証明書を定期的に更新するようにcronのジョブを追加する。

sudo vi /etc/cron.d/certbot-auto
0 4 5,15,25 * * root /usr/local/certbot/certbot-auto renew --pre-hook 'systemctl stop httpd' --post-hook 'systemctl reload postfix dovecot; systemctl start httpd'
コメントはまだありません

コメントを残す

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

CAPTCHA