phpMyAdminのインストール方法と公開手順【簡単ステップ】

MySQL

はじめに

phpMyAdminは、MySQLをブラウザ上で管理できる便利なツールです。本記事では、Ubuntu 22.04, Nginx, PHP 8.1, MySQL環境でphpMyAdminをインストールし、安全に公開する方法をわかりやすく解説します。


1. 必要なパッケージのインストール

まず、phpMyAdminをインストールするために必要な追加パッケージを準備します。

sudo apt update
sudo apt install -y phpmyadmin
  • インストール中に以下のようなプロンプトが表示されます:
    1. 「Webサーバーを選択」: apache2 と表示されますが、Nginxを使っているため選択せず進んでください。
    2. 「dbconfig-commonを使用しますか?」: はい を選択してください。

2. phpMyAdminをNginxで動かす設定

(1) シンボリックリンクを作成

NginxでphpMyAdminを公開するため、phpMyAdminのディレクトリを適切な場所にリンクします。

sudo ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

(2) Nginxの設定ファイルを編集

phpMyAdmin用の設定を追加します。

sudo vim /etc/nginx/sites-available/phpmyadmin.conf

以下を記述:

server {
    listen 80;
    server_name your-domain.com;

    root /var/www/html;

    index index.php index.html index.htm;

    location /phpmyadmin {
        alias /usr/share/phpmyadmin;
        index index.php;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|ttf|eot)$ {
            expires max;
            log_not_found off;
        }
    }

    location ~ /\.ht {
        deny all;
    }
}

(3) サイト設定の有効化とNginxの再起動

sudo ln -s /etc/nginx/sites-available/phpmyadmin.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

3. セキュリティ設定を強化

(1) HTTPS対応(Let’s Encrypt)

SSL証明書をインストールしてセキュアに公開します。

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com -m your-mail-address

HTTPS対応用Nginx設定例

ファイル名: /etc/nginx/sites-available/example.com

server {
    listen 80;
    server_name www.your-domain.com;

    # HTTPからHTTPSへのリダイレクト
    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    server_name www.your-domain.com;

    # SSL証明書の設定
    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    # 推奨SSL設定
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # HTTP/2の最適化
    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" always;
    add_header X-Frame-Options SAMEORIGIN;
    add_header X-Content-Type-Options nosniff;

    # コンテンツのルートディレクトリ
    root /var/www/html;
    index index.php index.html;

    # phpMyAdminの設定例
    location /phpmyadmin {
        alias /usr/share/phpmyadmin;
        index index.php;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }

        location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|ttf|eot)$ {
            expires max;
            log_not_found off;
        }
    }

    # PHPの処理
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # ログ設定
    access_log /var/log/nginx/your-domain.com.access.log;
    error_log /var/log/nginx/your-domain.com.error.log;

    # 隠しファイルへのアクセスを禁止
    location ~ /\.ht {
        deny all;
    }
}

設定を保存後、Nginxを再起動します。

sudo systemctl restart nginx

4. 動作確認

ブラウザで https://your-domain.com/phpmyadmin にアクセスし、以下を確認します


まとめ

これで、phpMyAdminのインストールと公開が完了です!Nginxの設定やSSL導入でセキュリティも強化しました。次のステップとして、phpMyAdminを使ってMySQLデータベースを効率的に管理してみましょう。

今回使用したVPS

コメント

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