Ubuntu Server+LAMPスタック+Wordpress(修正版)

Ubuntuサーバー上でWordpressがうまく構築できなくなった。(Ubuntuサーバー4
Wordpress用のDatabaseに正しく接続できなくなった。Databaseに問題があるかと思い、mysql-serverからMariaDBに変更してみた。
導入手順については、RaspbianサーバーにWordpressを導入した手順を参考にした。

参考資料:
Ubuntu 20.04にLAMPスタックでWordPressをインストールする方法
……https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-20-04-w
Raspberry Pi WordPressサーバの構築
……https://www.ingenious.jp/articles/howto/raspberry-pi-howto/wordpress-server-setup/

実行結果:
1.Ubuntuサーバーの構築
・Ubuntu 20.04 LTS (Arm64用) 64bit を、microSDカードに書き込む…Raspberry Pi Imagerを使用
・Raspberry Pi4にセットし、起動する。
・ログインし、パスワードを変更する。…初期ログインは、user:ubuntu、pw:ubuntu、pwを変更してログイン完了。
・ホスト名を変更。…詳細は、”Ubuntu Server設定”参照
・OSのUpdate、Upgrade 実行
・サーバーのIPアドレスを固定する…詳細は、”Ubuntu Server設定”参照

2.Apache2インストールとVersion確認
・Apache2インストール
$ sudo apt install apache2
・Version確認
$ apache2 -v
Server version: Apache/2.4.41 (Ubuntu)
Server built: 2022-01-05T14:49:56

3.PHP7.4インストールとVersion確認及び各種設定
・PHP7.4インストール
$ sudo apt install php7.4
・Version確認
$ php -v
PHP 7.4.3 (cli) (built: Nov 25 2021 23:16:22) ( 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モジュールのインストール…Wordpressサーバーの動作上必要なモジュール
$ sudo apt install php7.4-curl php7.4-json php7.4-mbstring php7.4-mysql php7.4-imagick php7.4-xml php7.4-zip
・PHPの設定 /etc/php/7.4/apache2/php.ini
$ sudo nano /etc/php/7.4/apache2/php.ini
【編集内容】
~(略)~
[mbstring]
; language for internal character representation.
; This affects mb_send_mail() and mbstring.detect_order.
; http://php.net/mbstring.language
mbstring.language = Japanese <== コメントアウト解除
~(略)~
memory_limit = 128M
post_max_size = 40M
upload_max_filesize = 30M
・php-fpmのインストール…Webサーバー(Apache2)からPHPを使用するため
$ sudo apt install php7.4-fpm
・Apache2の再起動
$ sudo systemctl restart apache2

4.PHP環境の動作確認
・テスト用ファイルの作成
$ sudo nano /var/www/html/test.php
【記載内容】
<?php
phpinfo();
?>
・PHPの各種情報の表示確認
Webブラウザから、URL:http://<IPアドレス または、ドメイン名>/test.phpにアクセス

<PHP各種情報の表示>

5.Apache Webサーバの設定
・ServerNameディレクティブの設定
$ sudo nano /etc/apache2/apache2.conf
【記載内容】
# Global configuration
#

ServerName www.takofuji.jp <-- 特に決まってないので、適当に決定
・DirectoryIndexディレクティブの設定変更
$ sudo nano /etc/apache2/mods-available/dir.conf
【編集後】:「index.php」を一番最初に記載します。
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
・Rewriteモジュールの有効化
$ sudo a2enmod rewrite
・Apache2の再起動
$ sudo systemctl restart apache2
・Rewriteモジュールの有効化確認
$ apache2ctl -t -D DUMP_MODULES | grep rewrite
rewrite_module (shared)
・AllowOverrideの設定変更
$ sudo nano /etc/apache2/apache2.conf

【変更前】
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

【変更後】
<Directory /var/www/>
Options FollowSymLinks <-- 「Indexes」を削除
AllowOverride All <-- 「All」に修正
Require all granted
</Directory>

6.MariaDBのインストール
・MariaDBインストール
$ sudo apt install mariadb-server-10.3
・Version確認
$ sudo mysql -u root -p
Enter password: <-- [Enter]を押します。
Your MariaDB connection id is 142
Server version: 10.3.32-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

7.MariaDBの初期設定
「mysql_secure_installation」スクリプトを使用して、MariaDBのセキュリティオプション等の設定を行います。
$ sudo mysql_secure_installation
---ポイントのみ表示する---
Enter current password for root (enter for none): <== [Enter]を押します。

Set root password? [Y/n] Y <== rootパスワード設定するため、[Y]を入力します。
New password: <== パスワードを入力します。
Re-enter new password: <== 再度パスワードを入力します。

Remove anonymous users? [Y/n] Y <== 匿名ユーザーを削除するため、[Y]を入力します。

Disallow root login remotely? [Y/n] Y <== リモート接続を拒否するため、[Y]を入力します。

Remove test database and access to it? [Y/n] Y <== 不要なテストデータベースを削除するため、[Y]を入力します。

8.MariaDBの初期設定の変更
「mysql -u root -p」コマンドで接続できるようにする
$ sudo mysql -u root -p
Enter password: <== パスワードを入力します。
MariaDB [(none)]> grant all privileges on *.* to root@localhost identified by 'password' with grant option;
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> quit
・「mysql -u root -p」で接続できることを確認
$ mysql -u root -p
Enter password: <== パスワードを入力し、データベースにログインできることを確認

9.WordPressのダウンロードと解凍
$ wget https://ja.wordpress.org/latest-ja.tar.gz
$ tar -xzvf latest-ja.tar.gz
wordpressディレクトリが作成される

10. WordPress用のデータベースとユーザーの作成
データベース名を「db_wordpress」、ユーザー名を「wp_user」、パスワードを「password」とした時を表示します。
$ mysql -u root -p
Enter password: <== パスワードを入力します。
MariaDB [(none)]> CREATE DATABASE db_wordpress;
MariaDB [(none)]> GRANT ALL PRIVILEGES ON db_wordpress.* TO "wp_user"@"localhost" IDENTIFIED BY "password";
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> quit

11. wp-config.phpファイルの作成
本記事では、インストール・スクリプトを使用して設定する方法を行います。(後述)

12. WordPressのファイルのアップロードと権限設定
・WordPressのファイルのアップロード
$ sudo cp -R ./wordpress/* /var/www/html/
・権限設定
$ sudo chown -R www-data:www-data /var/www/html/

13. WordPressサーバのセットアップ
インストール・スクリプトを使用して、初期セットアップを行います。
Webブラウザを使用して、以下のURLにアクセスします。

URL:http://<IPアドレス または、ドメイン名>/wp-admin/install.php

下記の画面で順次入力していきます。
・インストールへようこそ…「さあ、始めましょう」
・データベース設定…10. 項で設定した「データベース名」、「ユーザー名」、「パスワード」以下はデフォルトでよい。「送信」
・インストール完了…「インストール実行」
・Wordpressへようこそ…「サイトのタイトル」、「ユーザー名」(サイトユーザーの名前です)、「パスワード」、「メールアドレス」、「検索エンジンでの表示」(レ点をつける)。「WordPressをインストール」
・WordPressインストール完了画面…「ログイン」

WordPressのログイン画面に変わるので、ユーザー名、パスワードを入力。