Nginx

nginxでphpMyAdminを動かす

nginx-phpmyadmin
o_wani
この記事は作成から5年以上経過しているため、内容が古くなっている可能性があります。

nginxでphpMyAdminを動かす方法をまとめました。
用途はローカルでの開発向けです。

今回はサブドメインを割り当ててアクセスする方法ではなく、既存ドメインにサブディレクトリでアクセスすることでphpMyAdminを表示します。

・nginx:1.12.2
・PHP:7.2
・MySQL:5.7
・CentOS:7

yum で phpMyAdminをインストール

PHPをインストールしたときと同じリポジトリを指定してphpMyAdminをインストールします。

$ sudo yum install -y --enablerepo=remi,remi-php72 phpMyAdmin

nginxの設定ファイル修正

$ sudo vi /etc/nginx/conf.d/dev.conf

もともとLaravelを動かしていたプロジェクトがありました。

もともとの設定

server {
listen 80;
listen [::]:80;
server_name dev.localhost;
root /vagrant/www/public;
index index.php index.html index.htm;
charset utf-8;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

今回の設定

既存のLaravelプロジェクトのURLにサブディレクトリでアクセスした際にphpMyAdminを表示するようにします。

phpmyadminの部分のみ追記します。

server {
listen 80;
listen [::]:80;
server_name dev.localhost;
root /vagrant/www/public;
index index.php index.html index.htm;
charset utf-8;
location / {
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location /phpmyadmin {
alias /usr/share/phpMyAdmin;
index index.php;
}
location ~ ^/phpmyadmin.+\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^/phpmyadmin(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/phpMyAdmin$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

設定ファイル修正後、nginxは再起動します。

$ sudo systemctl restart nginx

アクセスする

http://dev.localhost:8080/phpmyadmin

(※今回nginxは8080ポートでアクセスするように設定しています、適宜各自の環境に合わせて変更ください)

/var/lib/php/sessionのパーミッション変更

アクセスしてみると下記のようなエラーになるので、パーミッションを変更しておきます。

Alt text

$ sudo chmod -R 777 /var/lib/php/session

確認

再度アクセスしてみましょう。
http://dev.localhost:8080/phpmyadmin

Alt text

STAFF
o_wani
o_wani
スタッフ
大学卒業後、15年間WEB業界で働く。現在はマネジメントに従事していますが、ChatGPTの登場に触発され、このブログを再開。AIをパートナーに、自分で手を動かして実装する楽しさと喜びを再発見中。時代が変わりつつある中でも、陳腐化しない情報発信も目指しています。
記事URLをコピーしました