【さくらのVPS】CentOS7からrocky Linux9にしてWordPress(MySQL含む)も移行をしました

Blog
スポンサーリンク

概要

さくらVPCで数年間稼働していたサーバのOSバージョンが古くなり、不要なファイルも増えてきたため、CentOS7からRocky Linux9に移行しました。
その際の手順を備忘録としてまとめました。

注意事項

  • 十分に検証して作業を行ってください。
  • Let’s Encryptの証明書は一旦バックアップを取ります。(新サーバで再発行予定です)
  • 新サーバのRockyLinux9ではMariaDB10.5以降しか導入できないため、SQLリストアが必要です。(旧サーバはMariaDB10.2)

事前作業 旧サーバ

以下は旧サーバでの事前作業内容です。

Apacheバックアップ 対象ディレクトリとファイル

/etc/httpd/conf/*
/etc/httpd/conf.d/*

DBバックアップ(SQLバックアップ)

参考
mysqldump --single-transaction -u  -p  > <任意>.sql

Let’s Encrypt バックアップ 対象ディレクトリとファイル

/etc/letsencrypt/archive/*

事前作業 インストール 新サーバ

パッケージ インストール

以下は新サーバでの作業内容です。

各パッケージをインストールします。

お好みで

# dnf -y update
# dnf -y install epel-release jq net-tools wget gcc gcc-c++ make vim tar unzip

httpdインストール

初回の試行で失敗する場合があります。

# yum -y install httpd
Killed
??? なぜか失敗

スワップ領域の確認と追加

スワップ領域がない場合は追加します。

# sudo swapon --show
# df -h /
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        25G  1.4G   22G   6%
# dd if=/dev/zero of=/swapfile bs=1M count=2048
# chmod 600 /swapfile
# mkswap /swapfile
# swapon /swapfile
# cp -p /etc/fstab /etc/fstab.org
# echo "/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab

再度httpdをインストール

httpdをインストールし直します。

# dnf -y install httpd
# dnf -y install mod_ssl

Let’s Encryptのインストール

Let’s Encryptをインストールします。

# dnf -y --enablerepo=epel install certbot
# sudo dnf install -y certbot
# sudo dnf install -y python3-certbot-apache

MariaDB 11のインストール

MariaDB 11をインストールします。

# wget https://dlm.mariadb.com/3968456/MariaDB/mariadb-11.6.2/yum/centos/mariadb-11.6.2-rhel-9-x86_64-rpms.tar
# mv mariadb-11.6.2-rhel-9-x86_64-rpms.tar /tmp
# cd /tmp
# tar xvf mariadb-11.6.2-rhel-9-x86_64-rpms.tar
# cd mariadb-11.6.2-rhel-9-x86_64-rpms
# dnf localinstall -y \
 galera-4-26.4.20-1.el9.x86_64.rpm      \
 MariaDB-client-11.6.2-1.el9.x86_64.rpm \
 MariaDB-common-11.6.2-1.el9.x86_64.rpm \
 MariaDB-server-11.6.2-1.el9.x86_64.rpm \
 MariaDB-shared-11.6.2-1.el9.x86_64.rpm \
 MariaDB-backup-11.6.2-1.el9.x86_64.rpm

事前準備 初期セットアップ 新サーバ

MariaDB 11の初期セットアップ

以下の手順でMariaDB 11を初期セットアップします。

MariaDB 設定ファイル変更

# cp -p /etc/my.cnf.d/server.cnf /etc/my.cnf.d/server.cnf.org
# vi /etc/my.cnf.d/server.cnf

`[mysqld]`セクション内に以下を追記します。

[mysqld]
character-set-server = utf8mb4

次に、以下の設定を行います。

# cp -p /etc/my.cnf.d/client.cnf /etc/my.cnf.d/client.cnf.org
# vi /etc/my.cnf.d/client.cnf

`[client]`セクション内に以下を追記します。

[client]
default-character-set = utf8mb4

MariaDBサービスを起動し、有効化します。

# systemctl start mariadb
# systemctl status mariadb
# systemctl enable mariadb

MariaDB 初期設定(mariadb-secure-installation)

以下のコマンドを実行し、MariaDBの初期設定を行います。

# mariadb-secure-installation

以下のように対話式で回答を行います。

1. **rootユーザーの現在のパスワード入力**
   初期状態ではパスワードが未設定のため、そのままEnterを押します。

2. **unix_socket認証の有効化の確認**  
   「Switch to unix_socket authentication [Y/n]」と表示されるので、`n`を入力します。

3. **rootパスワードの設定**  
   「Change the root password? [Y/n]」と表示されたら、`y`を入力し、任意のパスワードを設定します。

4. **匿名ユーザーの削除**  
   「Remove anonymous users? [Y/n]」で、`y`を入力します。

5. **リモートからのrootログイン禁止**  
   「Disallow root login remotely? [Y/n]」で、`y`を入力します。

6. **testデータベースの削除**  
   「Remove test database and access to it? [Y/n]」で、`y`を入力します。

7. **権限テーブルのリロード**  
   「Reload privilege tables now? [Y/n]」で、`y`を入力します。

すべての設定が完了すると、以下のメッセージが表示されます。

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

参考の作業結果

# mariadb-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
haven't set the root password yet, you should just press enter here.

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

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n	★n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] y	★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	★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	★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	★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	★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!

### MariaDBログイン確認

設定が完了したら、以下のコマンドでMariaDBにログインできるか確認します。

# mariadb -u root -p
Enter password: ★設定したパスワードを入力

ログインに成功したら、以下のコマンドでMariaDBを終了します。

mariadb> \q

PHPセットアップ

PHPをバージョン7.4でセットアップしました。将来的にバージョンを切り替えられるよう、8系もインストールしています。

PHPセットアップ事前準備

# dnf install -y http://rpms.famillecollet.com/enterprise/remi-release-9.rpm
# dnf install -y yum-utils
# yum-config-manager --enable remi

PHPのインストール

# for v in 74 ; do yum -y install php$v php$v-php-{gd,xml,mbstring,mysqlnd} ; done
# for v in 74 ; do yum -y install php$v-php-{opcache,pecl-apcu,pecl-memcached} ; done

# for v in 81 82 ; do yum -y install php$v php$v-php-{gd,xml,mbstring,mysqlnd} ; done
# for v in 81 82 ; do yum -y install php$v-php-{opcache,pecl-apcu,pecl-memcached} ; done

PHPバージョンの確認

# for v in 74 ; do php$v -v ; echo \ ; done
# for v in 81 82 ; do php$v -v ; echo \ ; done

php.iniの配置場所の確認

# for v in 74 ; do php$v --ini | grep Loaded | awk '{print $4}' ; done
# for v in 81 82 ; do php$v --ini | grep Loaded | awk '{print $4}' ; done

php-fpmのインストールと設定

以下の手順でphp-fpmをインストールし、設定を行います。

# for v in 74 ; do yum -y install php$v-php-fpm ; done
# for v in 81 82 ; do yum -y install php$v-php-fpm ; done

php-fpmサービスを起動し、有効化します。

# systemctl start php74-php-fpm.service
# systemctl enable php74-php-fpm.service

設定ファイルをバックアップし、編集します。

# cp -p /etc/opt/remi/php74/php-fpm.d/www.conf /etc/opt/remi/php74/php-fpm.d/www.conf.org
# vi /etc/opt/remi/php74/php-fpm.d/www.conf

デフォルトではソケット通信になっていますが、以下のようにポート通信に変更します。

listen = 127.0.0.1:9000

リストア 新サーバ

Apacheリストア

バックアップしていたディレクトリとファイルを新しいサーバにリストアします。
もし旧サーバでhttpdの動作に必要なディレクトリや設定がある場合は、作成しておいてください。

# systemctl enable httpd
# systemctl start httpd

データベースリストア

バックアップしていたSQLファイルを新サーバでリストアします。

参考コマンド

mariadb -u  -p  <任意>.sql

Let’s Encryptリストア

Let’s Encryptの証明書を新しいサーバにリストアします。

1. 必要なディレクトリを作成します。

# mkdir -p /etc/letsencrypt/live

2. バックアップしていた`/etc/letsencrypt/archive/`配下のディレクトリとファイルを新しいサーバにリストアします。

3. `live`ディレクトリに対象ドメイン名のディレクトリを作成します。

# cd /etc/letsencrypt/live
# mkdir <対象ドメイン名>

4. 以下のようにシンボリックリンクを作成します。`<番号>`には`/etc/letsencrypt/archive/<対象ドメイン名>/`配下の最新の番号を指定します。

# cd /etc/letsencrypt/live/<対象ドメイン名>
# ln -s ../../archive/<対象ドメイン名>/cert<番号>.pem cert.pem
# ln -s ../../archive/<対象ドメイン名>/chain<番号>.pem chain.pem
# ln -s ../../archive/<対象ドメイン名>/fullchain<番号>.pem fullchain.pem
# ln -s ../../archive/<対象ドメイン名>/privkey<番号>.pem privkey.pem

※今回は、証明書を移行しています。また再発行をする予定です。

再起動

システムを再起動します。

# reboot

接続確認

新サーバのwordpressに接続して動作確認を行いましょう。
※さくらのVPSは再インストールしてもGIPが変わらなかったのでAレコード変更等はしなかったです。

参考リンク

mysqldump — データベースバックアッププログラム
※そのほかは本文に記載をしています。

ありがとうございます

BlogInfrawordpressさくらのVPS
スポンサーリンク
インフラエンジニア|パイナップル星人 ブログ

コメント

タイトルとURLをコピーしました