どうも、こっこです。
今回はブラウザ上でデータベースの処理を視覚的に行うことができるphpMyAdminをインストールした後に403エラーが表示される問題が起きた時の解決方法をご紹介します。
WebサーバーをFedoraで新設するときに毎回エラーが出て面倒なので、記事としてまとめておきます。
phpMyAdminに必要なインストール
まずは当たり前ですが、phpMyAdminを使用する前に必要なものをインストールさせておきましょう。すでに済んでいる人はエラーの箇所までスキップ。
Apacheをインストール
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#HTTPをインストール [root@www ~]# dnf -y install httpd #ウェルカムページ削除 [root@www ~]# rm -f /etc/httpd/conf.d/welcome.conf #httpd.confファイル編集 [root@www ~]# vi /etc/httpd/conf/httpd.conf #147行目:変更 (Indexes 削除) Options FollowSymLinks #154行目:NoneをAllに変更 AllowOverride All #167行目:ディレクトリ名のみでアクセスできるファイル名を追記 DirectoryIndex index.html index.php # 最終行に追記 ServerTokens Prod KeepAlive On #HTTP起動 [root@www ~]# systemctl start httpd [root@www ~]# systemctl enable httpd #ファイアウォールにHTTPを許可 [root@www ~]# firewall-cmd --add-service=http --permanent [root@www ~]# firewall-cmd --reload |
PHPをインストール
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#PHPインストール [root@www ~]# dnf -y install php php-mbstring php-pear #php.iniの設定 [root@www ~]# vi /etc/php.ini #922行目:コメント解除し自身のタイムゾーンを追記 date.timezone = "Asia/Tokyo" #HTTP再起動 [root@www ~]# systemctl restart httpd |
DBをインストール
※MariaDBを例にインストールする
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#MariaDBをインストール [root@www ~]# dnf -y install mariadb-server #mariadb-server.confファイルを編集 [root@www ~]# vi /etc/my.cnf.d/mariadb-server.cnf # 21行目:追記 [mysqld] character-set-server=utf8 #MariaDBを起動 [root@www ~]# systemctl start mariadb [root@www ~]# systemctl enable mariadb #DBの初期設定 [root@www ~]# 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. #rootパスワードを入力 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. # root パスワードを設定 Set root password? [Y/n] y New password: #任意のパスワードを入力 Re-enter new password: #任意のパスワードを再入力 # 匿名ユーザーは削除 Remove anonymous users? [Y/n] y # root のリモートログインは無効 Disallow root login remotely? [Y/n] y # テストデータベースは削除 Remove test database and access to it? [Y/n] y # 特権情報リロード Reload privilege tables now? [Y/n] y #ファイアウォールにMariaDBを許可する [root@www ~]# firewall-cmd --add-service=mysql --permanent [root@www ~]# firewall-cmd --reload |
phpMyAdminをインストールする
phpMyAdminをインストールしていきます。こちらもインストールが済んでいる場合は次に進みましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#phpMyAdminをインストール [root@www ~]# dnf -y install phpMyAdmin php-mysqlnd php-mcrypt php-php-gettext #phpMyAdmin.confファイルを編集 [root@www ~]# vi /etc/httpd/conf.d/phpMyAdmin.conf # 17行目:アクセス許可IP追記 Require ip 127.0.0.1 10.0.0.0/24 # 34行目:アクセス許可IP追記 Require ip 127.0.0.1 10.0.0.0/24 #Apacheを再起動 [root@www ~]# systemctl restart httpd |
本来これだけの設定をするだけで「http://(ホスト名またはIPアドレスまたはドメイン名)/phpmyadmin/」にアクセスすると正常にphpMyAdminのTOPページが開かれます。以下の画像のような表示が出れば正常です。
エラー表示の意味
さて問題のエラーの解消に取り掛かります。
アクセスできないエラー表示はこのようになっていました。
「You don’t have permission to access this resource.」とのことなので、翻訳すると「このリソースにアクセスする権限がありません。」らしいです。そもそもこのページにアクセスする権限を持っていないらしいですね。
ページタイトルに403とあるので、やはりページへのアクセス権が無いということですね。
このエラーが表示されているのはphpMyAdmin.confの設定ミスが原因です。
正常に動作する.confファイルソース
では正常に動作するための編集を行います。
あくまで先程の403エラーが無くなり正常にphpMyAdminが表示される、ということであり各設定が正しいかどうかは自己責任でお願いします。
phpMyAdmin.conf変更箇所
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#12行目のAddDefaultCharset UTF-8の下に2列追加 AllowOverride all Require all granted #confファイル内2箇所の後に以下IPアドレスが追加されているか確認 Require ip 127.0.0.1 10.0.0.0/24 ←追加 #!mod_authz_core.cの内部を以下の状態に書き換え <IfModule !mod_authz_core.c> # Apache 2.2 # Order Deny,Allow # Deny from All # Allow from 127.0.0.1 # Allow from ::1 Order Allow,Deny Allow from All </IfModule> #保存できたら念のためにApacheを再起動 systemctl restart httpd |
これでphpMyAdminが正常に表示されれば完了です。もしもこれで直らなければphpMyAdminを入れ直すか、そもそも違うエラーなのかもしれません。その場合はしっかりhttpのエラーログを確認して原因から見つめ直しましょう。
正常動作phpMyAdmin.confソース
改めてphpMyAdmin.confの正常に動作する全体のソースを載せておきます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# phpMyAdmin - Web based MySQL browser written in php # # Allows only localhost by default # # But allowing phpMyAdmin to anyone other than localhost should be considered # dangerous unless properly secured by SSL Alias /phpMyAdmin /usr/share/phpMyAdmin Alias /phpmyadmin /usr/share/phpMyAdmin <Directory /usr/share/phpMyAdmin/> AddDefaultCharset UTF-8 AllowOverride all Require all granted <IfModule mod_authz_core.c> # Apache 2.4 <RequireAny> Require ip 127.0.0.1 10.0.0.0/24 Require ip ::1 </RequireAny> </IfModule> <IfModule !mod_authz_core.c> # Apache 2.2 # Order Deny,Allow # Deny from All # Allow from 127.0.0.1 # Allow from ::1 Order Allow,Deny Allow from All </IfModule> </Directory> <Directory /usr/share/phpMyAdmin/setup/> <IfModule mod_authz_core.c> # Apache 2.4 <RequireAny> Require ip 127.0.0.1 10.0.0.0/24 Require ip ::1 </RequireAny> </IfModule> <IfModule !mod_authz_core.c> # Apache 2.2 Order Deny,Allow Deny from All Allow from 127.0.0.1 Allow from ::1 </IfModule> </Directory> # These directories do not require access over HTTP - taken from the original # phpMyAdmin upstream tarball # <Directory /usr/share/phpMyAdmin/libraries/> Order Deny,Allow Deny from All Allow from None </Directory> <Directory /usr/share/phpMyAdmin/setup/lib/> Order Deny,Allow Deny from All Allow from None </Directory> <Directory /usr/share/phpMyAdmin/setup/frames/> Order Deny,Allow Deny from All Allow from None </Directory> # This configuration prevents mod_security at phpMyAdmin directories from # filtering SQL etc. This may break your mod_security implementation. # #<IfModule mod_security.c> # <Directory /usr/share/phpMyAdmin/> # SecRuleInheritance Off # </Directory> #</IfModule> |
まとめ
いかがだったでしょうか?ちゃんとphpMyAdminは表示されていますか?
そもそも毎回サーバーを新設する時に自分なりのテンプレがあり、phpMyAdminの導入の部分にどこを書き足すかを正常なものに変更しておけば毎回エラーが表示されることはないんですけどね^^;
そのサーバー新設する自分のテンプレはまた今度まとめて投稿しておきますね。ssh接続やユーザー追加の方法もまとめておきたいと思っています。
ではまた!