ConoHa VPS Ver.3.0 の Ubuntu 24.04 の設定 (SSH、Nginx、Postfix、WordPress)
ConoHa VPS Ver.2.0 の Ubuntu 22.04 を使っていたのだが、ConoHa VPS Ver.3.0 の Ubuntu 24.04 を使うことにした。
ConoHa VPS のセキュリティグループ
ConoHa VPS Ver.3.0 のデフォルトでは、(Ubuntu のファイアウォールではなく) ネットワークで SSH のポートが閉じられている。 ConoHa コントロールパネルから、VPS の「詳細情報」「セキュリティグループ」で「IPv4v6-SSH」を加えておく。 ウェブやメールのサーバとして使う場合は、対応するセキュリティグループを加える。 また、標準的ではないポートを使うには、「セキュリティ」「セキュリティグループ」で設定を追加する。
SSH サーバ
ConoHa コントロールパネルからコンソールを起動し、SSH サーバをインストールする。
apt install openssh-server
SSH のポートを変更するには、/etc/ssh/sshd_config に
Port 22
Port 12345
などを加える。12345 ポートを開けるには
ufw allow 12345/tcp
ufw reload
とする必要がある。
PasswordAuthentication を禁止するので、ログインで使うユーザの SSH の鍵を事前に ~/.ssh に置いておく。 /etc/ssh/sshd_config を以下の部分だけ変更した。
PasswordAuthentication no
PermitRootLogin no
ClientAliveInterval 60
ClientAliveCountMax 3
ConoHa VPS の Ubuntu 24.04 だと /etc/ssh/sshd_config.d/50-cloud-init.conf に
PasswordAuthentication yes
と書いてあるので、このファイルを削除しておく。
以下は、SSH で接続して実行した。
パッケージのインストール
よく使うパッケージをインストールしておく。
apt update
apt full-upgrade
apt install net-tools zsh tmux fzf ripgrep etckeeper
etckeeper で /etc をバージョン管理しておく。
Nginx & Let’s Encrypt
ウェブサーバを移転する。
apt install nginx certbot python3-certbot-nginx
ポートを開ける。
ufw allow "Nginx FUll"
/etc/nginx/sites-available にこれまで使用していた設定ファイルをコピーし、 使用する設定ファイルを /etc/nginx/sites-enabled にシンボリックリンクを張る。
元のサーバの証明書を使うことにした。 /etc/letsencrypt の renewal、archive、live にある証明書ごとのファイルを移す。
mv /path_to_old/etc/letsencrypt/renewal/www.example.com.conf /etc/letsencrypt/renewal
mv /path_to_old/etc/letsencrypt/archive/www.example.com /etc/letsencrypt/archive
mv /path_to_old/etc/letsencrypt/live/www.example.com /etc/letsencrypt/live
元のサーバで証明書を削除するには
certbot certificates
で確認して
certbot delete --cert-name www.example.com
を行う。
新しいサーバでは Let’s Encryp のアカウントの登録が必要だった。
certbot register --agree-tos --email mail@example.com
証明書の更新がうまくいくかどうかを確認する。
certbot renew --dry-run
/home/www 以下にウェブサイトのファイルを置いたが、このディレクトリのパーミッションを変更する必要があった。
chmod 755 /home/www
nginx を再起動する。
systemctl restart nginx
Postfix
Postfix を移転する。
apt install postfix postfix-policyd-spf-python mailutils
/etc/postfix の中の必要なファイルを元のサーバから移す。 私の環境では次のファイルをコピーした。
cd OLD_SETTINGS/etc/postfix
cp main.cf master.cf virtual virtual.db /etc/postfix
cp OLD_SETTINGS/etc/mailname /etc
postfix を再起動する。
systemctl restart postfix
使用していたユーザが ~/.forward にしたがってメールを転送していたので、~/.forward を古いサーバから移した。
ポートを開ける。
ufw allow Postfix
ufw allow "Postfix Submission"
~/Maildir に保存されているメールを端末で読むには
mail -f ~/Maildir
とする。
WordPress
WordPress を移転する。
apt install nginx mariadb-server libsqlite3-dev libmysqlclient-dev php-mysql php-fpm php-gd
元のサーバで
mysqldump -u root -p wordpress > wordpress_database.sql
のようにしてデータベースをダンプする。 ここでは、データベース名は wordpress であり、 MySQL (Mariadb) のデータベースのパスワードは wp-config.php に記述されている。 wordpress_database.sql を新しいサーバにコピーする。
新しいサーバで以前と同じようにデータベースを作成する。
mysql -u root -p
として
create database wordpress;
grant all privileges on wordpress.* to "wpuser"@localhost identified by "wordpress-password";
flush privileges;
とする。
mysql -u wpuser -p wordpress < wordpress_database.sql
として新しいデータベースにデータを復元する。 Wordpress が保存されているディレクトリ /path/to/example.com に移動して、グループとパーミションを設定する。 Ubuntu の nginx では chown で www-data:www-data に変えれば良い。
cd /path/to/wordpress
chown -fR www-data:www-data *
chmod -fR g+w *
WordPress に合わせて Nginx を適切に設定する。