LoginSignup
0
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

WSL2 + Ubuntu 24.04 をインストールして WordPress 起動できるまで実施してみる

Posted at

WSL2 + Ubuntu で検証環境を用意する

使っている端末をリニューアルしてから検証環境を用意していなかったので、今後の検証用に WordPress が動作する環境を用意してみようと思います。
以前の端末で WSL2 環境使っていましたが、改めて使い始めるのに手順を残しておくのが目的です。個人的な内容で環境を用意するため、最新の環境を用意したいとか特定のバージョンで用意したい等の場合にはあまり参考にならないかもしれませんのでご注意ください。

WSL2 の環境にバージョンを指定して Ubuntu をインストール

今回 WSL2 に Ubuntu をインストールするのに参考にしたサイトは以下です。

ターミナルを管理者で起動してまずはどのディストリビューションがインストール可能か確認してみます。

> wsl --list --online

記事を書いている時点での結果は以下の通り

インストールできる有効なディストリビューションの一覧を次に示します。
'wsl.exe --install <Distro>' を使用してインストールします。

NAME                                   FRIENDLY NAME
Ubuntu                                 Ubuntu
Debian                                 Debian GNU/Linux
kali-linux                             Kali Linux Rolling
Ubuntu-18.04                           Ubuntu 18.04 LTS
Ubuntu-20.04                           Ubuntu 20.04 LTS
Ubuntu-22.04                           Ubuntu 22.04 LTS
Ubuntu-24.04                           Ubuntu 24.04 LTS
OracleLinux_7_9                        Oracle Linux 7.9
OracleLinux_8_7                        Oracle Linux 8.7
OracleLinux_9_1                        Oracle Linux 9.1
openSUSE-Leap-15.5                     openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4    SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5           SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed                    openSUSE Tumbleweed

意外といろいろなディストリビューションが使えるのだなぁと感心します。
今回は Ubuntu-24.04 を指定してインストールしてみようと思います。単純に Ubuntu は情報が多いし使いやすそうなので個人的な趣味で決めています。

> wsl --install Ubuntu-24.04

ちなみに一度インストールを試みたのですが、インストール後の環境設定に失敗したので一度アンインストールしてからの再挑戦です。もしかしたらまったく最初だと少し画面等が違うかもしれませんのでご注意ください。
インストールが進行して以下のように username を聞いてきましたので適切に入力します。

インストール中: Ubuntu 24.04 LTS
Ubuntu 24.04 LTS がインストールされました。
Ubuntu 24.04 LTS を起動しています...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username:

username 設定後に New password も聞いてきました。適切に設定すると以下のようになりました。

New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

Welcome to Ubuntu 24.04 LTS (GNU/Linux 5.15.153.1-microsoft-standard-WSL2 x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/pro

 System information as of Mon Jun 17 12:36:08 JST 2024

  System load:  0.0                 Processes:             50
  Usage of /:   0.1% of 1006.85GB   Users logged in:       0
  Memory usage: 12%                 IPv4 address for eth0: 172.27.157.110
  Swap usage:   0%


This message is shown once a day. To disable it please create the
/home/shokun/.hushlogin file.

入力した username のディレクトリが生成されているのが分かりますね。
インストール完了後は Windows のスタートにも Ubuntu のアイコンが確認できます。

Ubuntu を起動して環境を構築

まずは Ubuntu を起動してアップデート等を実施してみましょう。
Windows のスタートから起動でもいいのだと思いますが、個人的にはターミナルを起動してタブで開くのが好きなのでこちらを多用しています。
image.png
起動が確認できたら Ubuntu のアップデートを実行してみます。

$ sudo apt update
[sudo] password for shokun:
Get:1 http://archive.ubuntu.com/ubuntu noble InRelease [256 kB]
Get:2 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
略
Get:51 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:52 http://archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 c-n-f Metadata [116 B]
Fetched 29.1 MB in 7s (4173 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
58 packages can be upgraded. Run 'apt list --upgradable' to see them.

アップグレードがあるよというのでアップグレードも実施しておきます。

$ sudo apt upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
略

Ubuntu 24.04 の apt の機能には sudo apt upgrade -Uだけで、⁠sudo apt update相当の処理も実行してくれるようになるとの情報もありましたが apt のバージョンで違いがありそうなのでとりあえず今まで通りの手順でアップデートを確認しています。

念のため現在のバージョン情報を確認しておきます。

$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo

MariaDB インストール

今回参考にしているサイトは以下です。

$ sudo apt install mariadb-server

無事にインストールできたら設定も実行してみます。

$ 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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

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!

MariaDB にログインして確認してみます。

$ sudo mysql

バージョンの確認もできましたね。

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 37
Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.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.

MariaDB [(none)]>

適当に確認したら抜けます。

MariaDB [(none)]> exit
Bye

Nginx をインストール

今回参考にしているサイトは以下です。

Nginx をインストールしてみます。

$ sudo apt install nginx

バージョンを確認してみましょう。

$ nginx -v
nginx version: nginx/1.24.0 (Ubuntu)

ここまできてhttp://localhost/を確認したら Apache2 がインストールされて実行されていることに気が付いたので、 Apache2 をアンインストールしてしまいます。

$ sudo apt-get remove apache2

アンインストールできたらパッケージごと削除してしまいます。

$ sudo apt-get purge apache2

改めて Nginx を起動します。

$ sudo systemctl start nginx

Nginx の起動状態を確認してみます。

$ sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-06-17 14:29:57 JST; 8s ago
       Docs: man:nginx(8)
    Process: 14240 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status>
    Process: 14241 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCES>
   Main PID: 14243 (nginx)
      Tasks: 21 (limit: 4572)
     Memory: 14.2M ()
     CGroup: /system.slice/nginx.service
略

起動を確認したのでhttp://localhost/で確認してみました。
image.png
大丈夫そうです。

PHP をインストール

今回参考にしているサイトは以下です。

まずは PHP 8.3 をインストールします。

$ sudo apt install php8.3 php8.3-mbstring php-pear

無事にインストールできたらバージョンを確認しておきましょう。

$ php -v
PHP 8.3.6 (cli) (built: Apr 15 2024 19:21:47) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

次に PHP-fpm をインストールします。

$ sudo apt install php8.3-fpm

PHP-fpm の設定を行います。

$ sudo vim /etc/nginx/sites-available/default

今回は以下のように設定しています。

 54         # pass PHP scripts to FastCGI server
 55         #
 56         #location ~ \.php$ {
 57         #       include snippets/fastcgi-php.conf;
 58         #
 59         #       # With php-fpm (or other unix sockets):
 60         #       fastcgi_pass unix:/run/php/php7.4-fpm.sock;
 61         #       # With php-cgi (or other tcp sockets):
 62         #       fastcgi_pass 127.0.0.1:9000;
 63         #}
 64         location ~ \.php$ {
 65                 include snippets/fastcgi-php.conf;
 66                 fastcgi_pass unix:/run/php/php8.3-fpm.sock;
 67         }

ついでにindexの設定にindex.phpを追加しておきましょうか。

 43         # Add index.php to the list if you are using PHP
 44         index index.php index.html index.htm index.nginx-debian.html;

参考サイトに倣って設定をリロードします。

$ sudo systemctl reload php8.3-fpm nginx

サイトルートに移動して PHP の確認用ファイルを作成します。

$ cd /var/www/html/
/var/www/html$ sudo vim info.php

内容はphpinfo()のみ記載しています。

<?php phpinfo(); ?>

サイトルートはこんな感じ。

/var/www/html$ ll
total 16
drwxr-xr-x 2 root root 4096 Jun 17 14:45 ./
drwxr-xr-x 3 root root 4096 Jun 17 14:14 ../
-rw-r--r-- 1 root root  615 Jun 17 14:19 index.nginx-debian.html
-rw-r--r-- 1 root root   20 Jun 17 14:45 info.php

http://localhost/info.phpを表示して確認してみました。
image.png
問題なさそう。
WordPress のインストールの前に必要となる PHP の拡張機能をインストールしておきます。

$ sudo apt-get install php-mysqli

PHP の設定を編集します。

$ sudo vim /etc/php/8.3/fpm/php.ini

今回編集したのは以下のみです。先頭の;を削除しています。

- 914 ;   extension=mysqli
+ 914    extension=mysqli

再度設定を読み込みます。

$ sudo systemctl reload php8.3-fpm nginx

WordPress をインストール

主な流れはプライム・ストラテジー在籍時代にマーケティング部でも頻繁にハンズオンとして主催していた際に使っていた WordPress の高速化手法を説明した「とにかく速いWordPress」の手法を用いようと思います。

データベースにユーザーを登録しデータベースを新規作成

まずは MariaDB にログインしてデータベースとユーザーを作成してみます。

$ sudo mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 38
Server version: 10.11.7-MariaDB-2ubuntu2 Ubuntu 24.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.

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.024 sec)

MariaDB [(none)]> grant all privileges on wordpress.* to wpuser@localhost identified by 'wppass';
Query OK, 0 rows affected (0.042 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.012 sec)

MariaDB [(none)]> exit;
Bye

WordPress のインストール

次に最新版の WordPress をダウンロードしてインストールを実行してみます。

$ cd /var/www/
/var/www$ sudo wget https://ja.wordpress.org/latest-ja.zip

ここにきて unzip がインストールされていないことに気が付いたのでついでにインストールします。

$ sudo apt install unzip

ダウンロードしてきた zip ファイルを解凍します。

/var/www$ sudo unzip latest-ja.zip

中のファイルを/htmlにコピーします。

/var/www$ sudo cp -rp wordpress/* html/

ディレクトリの権限を修正します。

/var/www$ sudo chmod 0777 html html/wp-content

ブラウザに戻ってリロードして確認してみます。
image.png
問題なさそう。
いつも通りインストールします。
WordPress のプラグイン等のアップデートが実行できない場合には実行ユーザーを変えてあげるとよいかも。

/var/www/html$ sudo chown -R www-data:www-data wp-*

一応これで翻訳のアップデートは確認しています。
WordPress の動作確認ができたのでとりあえず完了です。

まとめ

WSL2 ではいろいろとディストリビューションを指定してインストールできることが分かりました。以前の端末で実施したときはそこまでなかった気がする…便利になりましたね。
個人的に最近は Ubuntu を利用していたので最新版をインストールしましたが、初期設定でも Apache は動いているようですしそれなりにすぐ使用できる環境ではあるみたいですね。 Ubuntu 22.04 LTS のインストールについて書かれたサイトを見ていると、 PHP 8 系や Nginx はリポジトリの追加での手順が多かったように思いますが、 Ubuntu 24.04 LTS では比較的新しいバージョンをリポジトリの追加なしで用意できているので非常に助かりました。
これでようやく他の検証もできるようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0