ウェブサイト検索

Nginx で phpMyAdmin をインストールし、Ubuntu 20.04 LTS で SSL を暗号化する方法


このチュートリアルは、これらの OS バージョン用に存在します

  • Ubuntu 11.04 (Natty Narwhal)

このページでは

  1. 前提条件
  2. Nginx、MariaDB、PHP をインストール
  3. phpMyAdmin をインストール
  4. MariaDB データベースを構成する
  5. phpMyAdmin 用に Nginx を構成する
  6. Let's Encrypt SSL で phpMyAdmin を保護
  7. phpMyAdmin にアクセス
  8. 結論

このチュートリアルでは、Ubuntu 20.04 に Nginx で phpMyAdmin をインストールし、無料の Let's Encrypt SSL 証明書で保護する方法を説明します。

前提条件

  • Ubuntu 20.04 を実行しているサーバー。
  • サーバーで指定された有効なドメイン名
  • サーバーに root パスワードが設定されている。

Nginx、MariaDB、PHP をインストールする

まず、Nginx Web サーバー、MariaDB、PHP、およびその他の必要な PHP 拡張機能をサーバーにインストールする必要があります。次のコマンドですべてをインストールできます。

apt-get install nginx mariadb-server php php-cli php-mysql php-mbstring php-zip php-gd php-json php-curl php-fpm -y

すべてのパッケージがインストールされたら、次のステップに進むことができます。

phpMyAdmin をインストールする

デフォルトでは、phpMyAdmin パッケージは Ubuntu 20.04 デフォルト リポジトリで利用できます。次のコマンドを実行するだけでインストールできます。

apt-get install phpmyadmin -y

Nginx Web サーバーを使用しているため、TAB を押してから ENTER を押すだけで、このプロンプトをバイパスできます。 phpMyAdmin が使用するデータベースを設定するよう求められます。

[はい] を選択し、Enter キーを押して続行します。以下に示すように、phpMyAdmin アプリケーションのパスワードを選択して確認するよう求められます。

希望のパスワードを入力し、Enter キーを押してインストールを終了します。

MariaDB データベースの構成

デフォルトでは、MariaDB は保護されていません。そのため、MariaDB を保護し、次のコマンドで MariaDB ルート パスワードを設定します。

mysql_secure_installation

以下に示すように、すべての質問に答えてください。

Enter current password for root (enter for none): 
Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Remove anonymous users? [Y/n] Y
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 を保護したら、別のユーザーを作成して phpMyAdmin に接続し、データベースを管理することをお勧めします。

これを行うには、次のコマンドを使用して MariaDB シェルにログインします。

mysql -u root -p

プロンプトが表示されたら root パスワードを入力し、次のコマンドで新しいユーザーを作成します。

MariaDB [(none)]> create user  identified by 'password';

次に、次のコマンドを使用して、すべての権限をユーザーに付与します。

MariaDB [(none)]> grant all privileges on *.* to  with grant option;

次に、特権をフラッシュし、次のコマンドを使用して MariaDB シェルを終了します。

MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

完了したら、次のステップに進むことができます。Advertisement

phpMyAdmin 用に Nginx を構成する

次のコマンドを使用して、phpMyAdmin 用の新しい Nginx 仮想ホスト構成ファイルを作成できます。

nano /etc/nginx/sites-available/phpmyadmin

次の行を追加します。

server {
  listen 80;
  listen [::]:80;
  server_name phpmyadmin.linuxbuz.com;
  root /usr/share/phpmyadmin/;
  index index.php index.html index.htm index.nginx-debian.html;

  access_log /var/log/nginx/phpmyadmin_access.log;
  error_log /var/log/nginx/phpmyadmin_error.log;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ ^/(doc|sql|setup)/ {
    deny all;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

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

終了したら、ファイルを保存して閉じます。次に、/etc/nginx/sites-enabled/ ディレクトリへのシンボリック リンクを作成します。

ln -s /etc/nginx/sites-available/phpmyadmin /etc/nginx/sites-enabled/

次に、Nginx のデフォルト構成ファイルで hash_bucket_size を設定します。

nano /etc/nginx/nginx.conf

「http {」行の下に次の行を追加します。

    server_names_hash_bucket_size 64;

ファイルを保存して閉じます。次に、次のコマンドを使用して Nginx の構文エラーを確認します。

nginx -t

次の出力が得られるはずです。

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

最後に、Nginx サービスを再起動して変更を適用します。

systemctl restart nginx

次のコマンドを使用して、Nginx サービスのステータスを確認することもできます。

systemctl status nginx

次の出力が表示されます。

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2020-05-15 06:24:03 UTC; 2s ago
       Docs: man:nginx(8)
    Process: 107736 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 107737 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 107738 (nginx)
      Tasks: 3 (limit: 2282)
     Memory: 3.7M
     CGroup: /system.slice/nginx.service
             ??107738 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ??107739 nginx: worker process
             ??107740 nginx: worker process

May 15 06:24:03 ubunt4 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 15 06:24:03 ubunt4 systemd[1]: Started A high performance web server and a reverse proxy server.

Let's Encrypt SSL で phpMyAdmin を保護する

開始する前に、Certbot クライアントをインストールして、Let's Encrypt SSL をダウンロードしてインストールする必要があります。

まず、次のコマンドで Certbot リポジトリを追加します。

add-apt-repository ppa:ahasenack/certbot-tlssni01-1875471

次に、リポジトリを更新し、次のコマンドを使用して Certbot クライアントをインストールします。

apt-get update -y
apt-get install certbot python3-certbot-nginx -y

Certbot がインストールされたら、次のコマンドを実行して、ドメインの Let's Encrypt SSL をダウンロードしてインストールします。

certbot --nginx -d phpmyadmin.linuxbuz.com

以下に示すように、電子メールを提供し、サービス条件に同意するよう求められます。

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): 

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y

Obtaining a new certificate
Performing the following challenges:
http-01 challenge for phpmyadmin.linuxbuz.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/phpmyadmin

次に、HTTP トラフィックを HTTPS にリダイレクトするかどうかを選択します。

Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

2 と入力して Enter キーを押し、インストールを終了します。

Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/phpmyadmin

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://phpmyadmin.linuxbuz.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=phpmyadmin.linuxbuz.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/phpmyadmin.linuxbuz.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/phpmyadmin.linuxbuz.com/privkey.pem
   Your cert will expire on 2020-08-12. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

phpMyAdmin にアクセスする

ここで、Web ブラウザーを開き、URL https://phpmyadmin.linuxbuz.com を入力します。 phpMyAdmin ログイン ページにリダイレクトされます。

管理者のユーザー名とパスワードを入力し、[Go] ボタンをクリックします。次のページに phpMyAdmin のデフォルト ダッシュボードが表示されます。

結論

おめでとう! phpMyAdmin を正常にインストールし、Ubuntu 20.04 で Let's Encrypt SSL で保護しました。 MariaDB と対話し、Web ブラウザーを介していくつかのタスクを実行できるようになりました。