ConoHaのVPSでMariaDBとphpMyAdminをインストールして設定する。
以下の環境でセットアップしていく。
〜VPS 1Gプラン〜
メモリ:1G
CPU:仮想2Core
ストレージ:HDD 100GB
OS:AlmaLinux 9.4
おことわり
筆者はCentOS 7からAlmaLinux 9へ移行したため、CentOS 7のコマンドで利用できる部分はそれらを用いる。
1.MariaDBのインストール
次のコマンドを実行し、MariaDBをインストールする。
[root@abc ~]# yum -y install mariadb-server
Last metadata expiration check: 0:22:46 ago on Mon 13 Jan 2025 10:48:50 AM JST.
Dependencies resolved.
======================================================================================================
Package Architecture Version Repository Size
======================================================================================================
Installing:
〜〜〜〜省略〜〜〜〜
perl-overloading-0.02-481.el9.noarch
perl-parent-1:0.238-460.el9.noarch
perl-podlators-1:4.14-460.el9.noarch
perl-subs-1.03-481.el9.noarch
perl-vars-1.05-481.el9.noarch
Complete!
[root@abc ~]#
次のコマンドを実行し、MariaDBを起動する。
[root@abc ~]# systemctl start mariadb
次のコマンドを実行し、MariaDBの自動起動を有効化する。
[root@abc ~]# systemctl enable mariadb
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
2.MariaDBのセットアップ
次のコマンドを実行し、MariaDBをセットアップする。
[root@abc ~]# 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
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
この時点ではパスワードが未設定のため、何も入力せずreturnキーを押す。
OK, successfully used password, moving on...
unix_socket 認証に切り替えるか聞かれるため、"Y"を入力する。
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]
↓
Switch to unix_socket authentication [Y/n] Y
Enabled successfully!
Reloading privilege tables..
... Success!
rootのパスワードを変更するか聞かれるため、"Y"を入力する。
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n]
↓
Change the root password? [Y/n] Y
New password:
設定したいパスワードを入力する。
New password:[パスワード]
設定したいパスワードを再度入力する。
Re-enter new password: [パスワード]
Password updated successfully!
Reloading privilege tables..
... Success!
匿名ユーザを削除するか聞かれるため、"Y"を入力する。
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]
↓
Remove anonymous users? [Y/n] Y
... Success!
リモートからのログインを許可するか聞かれるため、"Y"を入力する。
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]
↓
Disallow root login remotely? [Y/n] Y
... Success!
テスト関連のデータの削除を聞かれるため、"Y"を入力する。
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]
↓
Remove test database and access to it? [Y/n] Y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
権限テーブルリロードするか聞かれるため、"Y"を入力する。
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
↓
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!
[root@abc ~]#
3.phpMyAdminのインストール
次のコマンドを実行し、phpMyAdminをインストールする。
[root@abc ~]# yum -y install --enablerepo=epel phpMyAdmin php-mysqlnd
Last metadata expiration check: 1:49:15 ago on Mon 13 Jan 2025 10:48:50 AM JST.
Dependencies resolved.
===========================================================================================================
Package Architecture Version Repository Size
===========================================================================================================
Installing:
〜〜〜〜省略〜〜〜〜
php-pecl-zip-1.20.1-1.module_el9.1.0+15+94ba28e4.x86_64
php-process-8.1.30-1.module_el9.5.0+131+62ecd687.x86_64
phpMyAdmin-5.2.1-1.el9.noarch
Complete!
[root@abc ~]#
4.phpMyAdminのセットアップ
次のコマンドを実行し、設定ファイルを編集する。
[root@abc ~]# vim /etc/httpd/conf.d/phpMyAdmin.conf
8行目の"phpMyAdmin"を適当な文字列にする。
Alias /phpMyAdmin /usr/share/phpMyAdmin
↓
Alias /phpMyAdmin2ekS8nA /usr/share/phpMyAdmin
9行目の"phpmyadmin"を適当な文字列にする。
Alias /phpmyadmin /usr/share/phpMyAdmin
↓
Alias /phpmyadmin2ekS8nA /usr/share/phpMyAdmin
13行目に"SSLRequireSSL"を入力する。
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
Require local
↓
<Directory /usr/share/phpMyAdmin/>
AddDefaultCharset UTF-8
SSLRequireSSL
Require local
14行目の"Require local"を"Require all granted"にする。
SSLRequireSSL
Require local
</Directory>
↓
SSLRequireSSL
Require all granted
</Directory>
5.mod_sslのインストール
次のコマンドを実行し、mod_sslをインストールする。
[root@abc ~]# yum -y install mod_ssl
Last metadata expiration check: 2:06:14 ago on Mon 13 Jan 2025 10:48:50 AM JST.
Dependencies resolved.
===========================================================================================================
Package Architecture Version Repository Size
===========================================================================================================
Installing:
〜〜〜〜省略〜〜〜〜
Installed:
mod_ssl-1:2.4.62-1.el9.x86_64 sscg-3.0.0-7.el9.x86_64
Complete!
[root@abc ~]#
Apacheを再起動する。
[root@abc ~]# systemctl restart httpd
6.動作確認
ブラウザでhttps://[サーバーのIPアドレス]/phpMyAdmin[設定した文字列]にアクセスし、phpMyAdminの画面が表示されることを確認する。
SSL証明書がないためエラーメッセージが表示されるが、許可の操作をして確認する。
もしもログインできなかったり、403エラーなどが表示されたら最初からやり直す。