WordPress構築

Bloggerを使っていたけれど,使い勝手がよいWiki形式へ切り替えたいと思い,勉強がてらWordpressを自前で立ててこちらへ引っ越し。

1発目はWordpress構築メモ。

構想

イメージ
構成概要

構成イメージはこのような感じ。GCP上にCentOS8の仮想マシンを1つたて,その上で完結させる。DBは分けた方がよいのだろうが,まずは無事に立ち上がるところまで。

ドメインはさくらインターネットで取得。.comドメインで年間1886円。GCPでグローバルIPアドレスを確保した後,設定画面でDNS登録をする。

大まかな流れ

  1. DBインストール,設定
  2. PHPインストール,設定
  3. HTTPDインストール,設定
  4. HTTPS化

いざゆかん。

とその前に,構築中は自宅からのみアクセス可能とするためGCPのファイアウォールでソースIPアドレスを限定する。

DBインストール,設定

オフィシャルサイトを参考。

$ sudo dnf install mariadb-server.x86_64

DBセキュア化

$ sudo 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.

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.

Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

wordpress用DB設定。

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


MariaDB [(none)]>


MariaDB [(none)]> GRANT ALL PRIVILEGES ON wordpress.* TO "DBアドミン名"@"localhost" IDENTIFIED BY "パスワード";
Query OK, 0 rows affected (0.004 sec)


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

ログに認証エラーが出た。ケルベロス認証などは使わないので無効化で対処する。

[ERROR] mysqld: Server GSSAPI error (major 851968, minor 2529639093) : gss_acquire_cred failed -Unspecified GSS failure. Minor code may provide more information. Keytab FILE:/etc/krb5.keytab is nonexistent or empty.
/var/log/mariadb/mariadb.log の内容
$ sudo mv /etc/my.cnf.d/auth_gssapi.cnf /etc/my.cnf.d/auth_gssapi.cnf.org

起動設定。

$ systemctl enable mariadb
$ systemctl start mariadb

PHPインストール,設定

$ sudo dnf install php php-mysqlnd php-mbstring.x86_64 php-json php-gd php-xml php-zip php-dom

php-gdがないとwordpress上で画像の編集ができない

推奨

参考) https://qiita.com/kogache/items/00dc37774dfe20f03a72

/etc/php-fpm.d/www.conf 編集。httpdとしてnginxを使うのでユーザとグループを修正する。

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
;user = apache
user = nginx
; RPM: Keep a group allowed to write in log dir.
;group = apache
group = nginx

nginxインストール,設定

$ sudo dnf install nginx

wordpress設定

wordpressもあわせてインストールをする。今回は /usr/share/nginx の下に WP というディレクトリを作成して,そこへwordpressを配置する。

$ wget https://ja.wordpress.org/latest-ja.tar.gz
$ tar xvfz latest-ja.tar.gz ./
$ sudo chown -R nginx:nginx /usr/share/nginx/WP
$ sudo mv wordpress/* /usr/share/nginx/WP/

nginx設定ファイル変更。 /etc/nginx/conf.d/wp.conf として編集。TLS1.2 と1.3に限定する。こちらを参考。

# HTTPSへリダイレクト
server {
        listen 80;
        server_name zassoul.com;
        return 301 https://$host$request_uri;
}
server {
        listen 443 ssl;
        server_name zassoul.com;
        root /usr/share/nginx/WP;
        index index.php;
        charset utf-8;

        ssl_certificate /etc/letsencrypt/live/zassoul.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/zassoul.com/privkey.pem;
        ssl_protocols TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

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

        # Deny to wp-config.php
        location ~* /wp-config.php {
                deny all;
        }

        #php-fpm
        location ~ \.php$ {
                try_files $uri = 404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include fastcgi_params;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass php-fpm;
        }

        location ~* \.(js|css|png|jpg|jpgeg|gif|ico)$ {
               expires max;
               log_not_found off;
        }
}

wp-config.php を編集する。(オフィシャルサイトを参考)

// ** MySQL 設定 - この情報はホスティング先から入手してください。 ** //
/** WordPress のためのデータベース名 */
define( 'DB_NAME', 'wordpress' );

/** MySQL データベースのユーザー名 */
define( 'DB_USER', '上で設定したユーザ名' );

/** MySQL データベースのパスワード */
define( 'DB_PASSWORD', '上で設定したパスワード' );

/** MySQL のホスト名 */
define( 'DB_HOST', 'localhost' );

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

/** データベースの照合順序 (ほとんどの場合変更する必要はありません) */
define( 'DB_COLLATE', '' );

/**#@+
 * 認証用ユニークキー
 */
define( 'AUTH_KEY',         'ジェネレートしたキー' );
define( 'SECURE_AUTH_KEY',  'ジェネレートしたキー' );
define( 'LOGGED_IN_KEY',    'ジェネレートしたキー' );
define( 'NONCE_KEY',        'ジェネレートしたキー' );
define( 'AUTH_SALT',        'ジェネレートしたキー' );
define( 'SECURE_AUTH_SALT', 'ジェネレートしたキー' );
define( 'LOGGED_IN_SALT',   'ジェネレートしたキー' );
define( 'NONCE_SALT',       'ジェネレートしたキー' );

/**#@-*/

HTTPS化

常時HTTPSもするのでLet’s EncryptでSSL証明書を発行する。サイト認証のため80番ポートでアクセスしてくる必要があるということで,一時的に80番ポート全解放をする。

こちらを参考にLet’s Encryptで証明書発行を実施。

Certbotのインストール。

$ sudo dnf install epel-release
$ sudo dnf install certbot

お試しdry-run。

$ sudo certbot certonly --dry-run --standalone -w /usr/share/nginx/WP/ -d zassoul.com -m "Email-Address"

本番実行。

$ sudo certbot certonly  --standalone -d zassoul.com -m "Email-Address" --agree-tos
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for zassoul.com
Using the webroot path /usr/share/nginx/WP for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Subscribe to the EFF mailing list (email: "Email-Address").


IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/zassoul.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/zassoul.com/privkey.pem
   Your cert will expire on 2020-12-07. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. 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

ここで80番を閉じる。nginx起動設定。

$ sudo systemctl enable nginx
$ sudo systemctl start nginx

完了。