【Ubuntu】Nginx+MariaDB+PHP7.4+php-fpmでwordpressをインストール

Ubuntu Server 20.04にnginx + MariaDB + PHP7.4 + php-fpmの環境でwordpressをインストールする方法です。

作業工程

下記の作業工程でUbuntu Server 20.04 LTSにwordpressをインストールしていきます。

  • 80,443ポート通信許可設定
  • nginxインストール
  • MariaDBインストール
  • wordpress用データベース作成
  • phpインストール
  • php-fpm設定
  • wordpressインストール
  • nginx設定
  • wordpress設定
  • Let's Encryptでhttps対応

wordpressをインストールするサーバのホスト名は、事前にDNSに登録してインターネット上で名前解決が出来るようにしておいてください。

80,443ポート通信許可設定

ufwコマンドを使用して、wordpressで使用する「80」「443」番ポートの通信許可設定をファイアウォールに行っていきます。

$ sudo ufw allow 80/tcp
$ sudo ufw allow 443/tcp
$ sudo ufw reload

「ufw status」コマンドで設定の確認を行えます。

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----                 
80/tcp                     ALLOW       Anywhere                  
443/tcp                    ALLOW       Anywhere                             
80/tcp (v6)                ALLOW       Anywhere (v6)             
443/tcp (v6)               ALLOW       Anywhere (v6)  

「Action」部分が「ALLOW」となっていれば通信が許可されています。

nginx

「nginx」の公式サイトにあるリポジトリを使用してインストールを行っていきます。

インストール手順は下記ページにまとめてありますので、そちらを参照願います。

【Ubuntu】nginxリポジトリを使用してインストール
【Ubuntu Server 22.04】apt-key addの非推奨に関わるインストール手順変更 apt-key addが非推奨(廃止予定)となったので、Ubuntu Server 22.04からはnginx公式のリポジトリを使用したイ...

MariaDB

MariaDBの公式サイトで提供されているリポジトリを使用してMariaDBをインストールしていきます。

インストール手順は下記ページにまとめてありますので、「インストール」「mysql_secure_installation」の実行、「文字コードの設定」作業を行ってください。

【Ubuntu】MariaDBリポジトリを使ったインストール
UbuntuにMariaDBインストール(MariaDB公式リポジトリを使用) 「Ubuntu Server 20.04」のデフォルトリポジトリにあるMariaDBはバージョンが少し古いので、MariaDB公式リポジトリを使用して新しいバー...

wordpress用データベース作成

wordpressで使用するデータベースを作成します。

MariaDBに「root」ユーザで接続して作業を行います。

$ mysql -u root -p

データベースは下記の書式で作成することが出来ます。

CREATE DATABASE データベース名;

今回は「wordpress」という名前のデータベースを作成していきます。

MariaDB [(none)]> CREATE DATABASE wordpress ;
Query OK, 1 row affected (0.000 sec)

複数のwordpressをインストールしていく予定がある場合は、どのサイトのデータベースなのか判別がつく名前で作成しておくと良いでしょう。

ユーザ作成

下記の書式でwordpress用に作成したデータベースの管理ユーザを作成し、データベースに権限を設定します。

GRANT ALL PRIVILEGES ON データベース名.* TO "作成ユーザ"@"localhost" IDENTIFIED BY "パスワード";

本手順では作成するユーザ名とパスワードは下記の内容で設定を行っていきますが、実際に作業を行う場合はお好きなユーザ名とパスワードを設定してください。

  • ユーザ名 blogadmin
  • パスワード dbpassword

データベースは先ほど作成した「wordpress」を指定しています。

MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO "blogadmin"@"localhost" IDENTIFIED BY "dbpassword";
Query OK, 0 rows affected (0.001 sec)

「FLUSH PRIVILEGES」で設定を反映させ、exitコマンドでMariaDBへの接続を終了します。

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> exit
Bye

phpインストール

phpがインストールされていない場合は、インストールを行っていきます。

$ sudo apt update
$ sudo apt install php7.4-cli php7.4-mbstring php-pear php7.4-fpm php7.4-mysql php7.4-curl php7.4-dom php7.4-imagick php7.4-zip php7.4-intl

phpがインストールされているかどうかの確認は「php -v」コマンドで確認することが出来ます。

$ php -v
PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

php-fpm 設定

nginxでphp-fpmを使用できるように「/etc/php/7.4/fpm/pool.d/www.conf」を編集します。

$ cd /etc/php/7.4/fpm/pool.d/
$ sudo cp -p www.conf www.conf_`date +%Y%m%d-%H%M%S`
$ sudo vi www.conf

編集内容

下記の項目を初期値のwww-dataからnginxに変更し、listen.modeを0660に編集します。

  • user
  • group
  • listen.owner
  • listen.group
変更前
user = www-data
group = www-data

listen.owner = www-data
listen.group = www-data
;listen.mode = 0660
変更後
user = nginx
group = nginx

listen.owner = nginx
listen.group = nginx
listen.mode = 0660
Listen項目を確認

「www.conf」内の「listen」の項目は「nginx」の設定ファイルを作成する際に必要となるため、設定内容を確認をしておいてください。

listen = /run/php/php7.4-fpm.sock

設定反映

pfp-fpmを再起動して設定の反映を行います。

$ sudo systemctl restart php7.4-fpm

nginx設定

「nginx」で「wordpress」用の設定を作成していきます。

まずはhttpでの接続用設定を作成して、wordpressのインストールと動作確認を行い、その後に「Let's Encrypt」でSSLの証明書の取得とhttps用の設定を追加していきます。

今回は作成する設定ファイルの名前をwordpress.confとしていますが、拡張子を.confとする以外は特に決まりはないので自分でわかりやすい名前で作成してください。

$ cd /etc/nginx/conf.d
$ sudo vi wordpress.conf

設定内容

「fastcgi_pass unix:」部分の設定は「php-fpm」の設定ファイル「www.conf」で設定されている「listen = /run/php/php7.4-fpm.sock」に合わせてください。

server {
    listen 80;
    server_name ホスト名;
    root /usr/share/nginx/wordpress;
    index index.php;
   
    charset utf-8;
   
# wordpress パーマネントリンク設定
  try_files $uri $uri/ /index.php?q=$uri&$args;
   
# wp-config.phpへのアクセス拒否設定
  location ~* /wp-config.php {
    deny all;
  }
   
# php-fpm用設定
  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
  }
}

設定反映

nginxの再起動し設定反映を行います。

$ sudo nginx -t
$ sudo systemctl restart nginx

Wrodpressインストール

wordpressのインストールを行っていきます。

ダウンロードと解凍

「/usr/share/nginx/」ディレクトリに最新のwordpress(latest-ja.tar.gz)をダウンロードし解凍します。

ダウンロードしたlatest-ja.tar.gzの解凍を行うと、wordpressというディレクトリが作成されるので所有者の情報をnginxに変更します。

$ cd /usr/share/nginx/
$ sudo curl -O https://ja.wordpress.org/latest-ja.tar.gz
$ sudo tar xzfv latest-ja.tar.gz
$ sudo chown -R nginx:nginx wordpress

wordpressを複数インストールする予定の場合は、どのサイトのwordpressなのかを判断しやすように、「wordpress」というディレクトリの名前を適宜変更しておきましょう。

wp-config.php設定

wordpressの設定ファイルであるwp-config.phpを作成して、下記の設定を行います。

  • データベースへの接続情報
  • テーブル作成時の文字コード
  • 認証用ユニークキー
$ cd wordpress
$ sudo cp -p wp-config-sample.php wp-config.php
$ sudo vi wp-config.php 

設定項目

今回の手順では下記のような設定となります。

  • データベース名: wordpress
  • データベースの管理ユーザ名: blogadmin
  • パスワード: dbpassword
  • テーブル作成時の文字セット: utf8mb4

認証用ユニークキーの部分は下記の自動生成用URLにアクセスして表示されたものをコピペします。

https://api.wordpress.org/secret-key/1.1/salt/

設定内容

実際に設定を行う部分は下記と項目なります。

/** WordPress のためのデータベース名 */
define( 'DB_NAME', 'wordpress' );

/** MySQL データベースのユーザー名 */
define( 'DB_USER', 'blogadmin' );

/** MySQL データベースのパスワード */
define( 'DB_PASSWORD', 'dbpassword' );

/** データベースのテーブルを作成する際のデータベースの文字セット */
define( 'DB_CHARSET', 'utf8mb4' );

/**#@+
 * 認証用ユニークキー
 *
 * それぞれを異なるユニーク (一意) な文字列に変更してください。
 * {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org の秘密鍵サービス} で自動生成することもできます。
 * 後でいつでも変更して、既存のすべての cookie を無効にできます。これにより、すべてのユーザーを強制的に再ログインさせることになります。
 *
 * @since 2.6.0
 */
自動生成URLにアクセスし生成されたものをコピペします

初期セットアップ

wp-config.phpの設定が終わりましたら、下記のURLにアクセスしてwordpressの初期設定を行います。

http://ホスト名/wp-admin/install.php

「ようこそ」画面が表示されるので、下記の項目を設定したあとにwordpressをインストールをクリックします。

  • サイトのタイトル
  • ユーザ名
  • パスワード
  • メールアドレス
  • インストール完了画面が表示されるので、作成したユーザ名を確認してログインをクリックします。

    動作確認

    wordpress管理画面へのログイン画面が表示されていると思いますので、初期セットアップで設定したユーザ名とパスワードを入力してログインボタンをクリックしてログインします。

    ログイン画面は下記のURLにアクセスすることでも表示させることができます。

    http://ホスト名/wp-admin
    

    ダッシュボードが表示されるので、左側のメニューから「ツール」-「サイトヘルス」と選択して、サイトヘルスのステータスを確認し良好と表示されているか確認します。

    これでwordpressのインストールと動作確認は完了となりますので、次はhttpsで接続できるように設定を行っていきます。

    Let's Encryptでhttps対応

    httpでwordpressの動作確認ができたら、Let's Encryptで無料のSSL証明書を発行してもらいサイトをhttps対応していきます。

    certbotインストール

    Let's EncryptでSSL証明書を発行してもらうには、certbotというアプリケーションを使用します。

    今回はnginxをwebサーバとして使用するので、nginx用のcertbotインストールしていきます。

    $ sudo apt install certbot python3-certbot-nginx
    

    証明書の発行依頼

    SSL証明書の発行依頼は下記のコマンドで行います。

    • ホスト名 wordpressを公開するサーバ名を設定
    • メールアドレス Let's Encryptからの通知を受け取るメールアドレスを設定
    $ sudo certbot --nginx -d ホスト名 -m メールアドレス --agree-tos
    

    コマンドを実行してSSL証明書の発行依頼を行うと、SSL証明書を取得すると同時にnginxの設定ファイルにhttps通信用の設定が自動で追記されます。

    certbotコマンド実行

    今回は下記の内容でcertbotを実行し、SSL証明書の取得を行っていきます。

    • ホスト名: sandbox.server-memo.net
    • メールアドレス: tamohiko@server-memo.net

    certbot実行すると途中で下記の内容について質問されますので、どのような設定をするのかを回答する必要があります。

    メールアドレスの共有許可

    Lets EncryptのパートナーであるElectronic Frontierとメールアドレスを共有しても良いかの質問されますが、共有してもしなくてもどちらでも特に問題はありませんので、好きな方を入力してください。

    • 共有させる場合は「y」を入力
    • 共有させたくない場合は「n」を入力
    httpsへのリダイレクト設定

    httpでの接続をhttpsへリダイレクトする設定にするかどうかの質問があります。

    • リダイレクトさせたくない場合は「1」を入力
    • リダイレクトさせる場合は「2」を入力

    特別な理由がない限りリダイレクトさせてhttps接続させる方が良いので、リダイレクトさせるようにしましょう。

    $ sudo certbot --nginx -d sandbox.server-memo.net -m tamohiko@server-memo.net --agree-tos
    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    Plugins selected: Authenticator nginx, Installer nginx
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    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: n   # メールアドレスの共有は許可しないのでnを入力
    Obtaining a new certificate
    Performing the following challenges:
    http-01 challenge for sandbox.server-memo.net
    Waiting for verification...
    Cleaning up challenges
    Deploying Certificate to VirtualHost /etc/nginx/conf.d/wordpress.conf
    
    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を入力
    Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/wordpress.conf
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Congratulations! You have successfully enabled https://sandbox.server-memo.net
    
    You should test your configuration at:
    https://www.ssllabs.com/ssltest/analyze.html?d=sandbox.server-memo.net
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    IMPORTANT NOTES:
     - Congratulations! Your certificate and chain have been saved at:
       /etc/letsencrypt/live/sandbox.server-memo.net/fullchain.pem
       Your key file has been saved at:
       /etc/letsencrypt/live/sandbox.server-memo.net/privkey.pem
       Your cert will expire on 2022-02-20. 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"
     - Your account credentials have been saved in your Certbot
       configuration directory at /etc/letsencrypt. You should make a
       secure backup of this folder now. This configuration directory will
       also contain certificates and private keys obtained by Certbot so
       making regular backups of this folder is ideal.
     - 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
    

    これでSSLの証明書が発行され、nginxの設定ファイルにhttps接続用の設定が追記されます。

    証明書の保管場所

    発行されたSSL証明書は下記の場所に保管されます。

    • 証明書:/etc/letsencrypt/live/ホスト名/fullchain.pem
    • 秘密鍵:/etc/letsencrypt/live/ホスト名/privkey.pem

    nginx設定ファイルに追記されたhttps用設定について

    nginxのwordpress用設定ファイルを確認すると、certbotが追記したhttps用の設定部分には「# managed by Certbot」と表示されているので確認してみてください。

    $ sudo cat /etc/nginx/conf.d/wordpress.conf
    server {
        server_name sandbox.server-memo.net;
        root /usr/share/nginx/wordpress;
        index index.php;
       
        charset utf-8;
       
    # wordpress パーマネントリンク設定
      try_files $uri $uri/ /index.php?q=$uri&$args;
       
    # wp-config.phpへのアクセス拒否設定
      location ~* /wp-config.php {
        deny all;
      }
       
    # php-fpm用設定
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_script_name;
        include fastcgi_params;
      }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/sandbox.server-memo.net/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/sandbox.server-memo.net/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }
    server {
        if ($host = sandbox.server-memo.net) {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    
        listen 80;
        server_name sandbox.server-memo.net;
        return 404; # managed by Certbot
    
    
    }
    

    設定反映

    設定ファイルが作成されたら、nginxを再起動して設定を反映させます。

    $ sudo nginx -t
    $ sudo systemctl restart nginx
    

    あとはhttpsでwordpressに接続できるかの確認を行ってみて、問題がなければhttps対応作業は完了となります。

    wordpressアドレス設定変更

    wordpressの管理画面にログインし、左側のメニューから「設定」-「一般」とクリックすると、一般設定の画面が表示されるので、「WordPress アドレス (URL)」と「サイトアドレス (URL)」に設定されているURLの「http://」部分を「https://」に変更して、画面の下にある「変更を保存」ボタンをクリックしてください。

コメント

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