はじめに
- 最終目標はオンプレミス環境でブログ投稿環境を作ることです
- この投稿はGMKtec G5(Intel N97搭載ミニPC)を使って、WSL2(Ubuntu)上にWordPressの開発環境を構築した記録です
- 最終的には別PC(大学時代に使い古したSurface Laptop)にUbuntuを入れて本番環境として外部公開し、G5はローカル開発環境として運用予定です
使用環境
1. PC構成
用途 | PC | OS | IPアドレス |
---|---|---|---|
本番環境(予定) | Surface Laptop | Ubuntu(予定) | 192.168.3.XXX(予定) |
開発環境 | GMKtec G5 | Windows11 + WSL2 Ubuntu | Windows: 192.168.3.92(固定) / WSL: 172.x.x.x(可変) |
作業内容(G5開発環境構築)
1. WSL2 + Ubuntu インストール
Power Shell(管理者権限) で以下のコマンドを実行
wsl --install
これで、WSLとUbuntuが自動でインストールされます
インストール完了後、PCを再起動
スタートメニューからUbuntuを起動しユーザー名・パスワードを設定(初回のみ)
Ubuntuで以下のコマンドでパッケージを更新
sudo apt update && sudo apt upgrade -y
ネットワーク接続がうまくいかない場合 はWindowsの Power Shell(管理者権限) で以下のコマンドでネットワーク設定をリセットするとよいかも
netsh winsock reset
netsh int ip reset
ipconfig /flushdns
ちなみに今回インストールされたUbuntu情報は下記を展開
OS
$ cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 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
kernel
$ uname -r
5.15.167.4-microsoft-standard-WSL2
そのほか Vim のインストールや ネットワーク設定などを済ましておく
2. Apache, MySQL, PHP(LAMP環境)セットアップ
Apacheインストール&起動
Ubuntuで以下のコマンドを実行
sudo apt install apache2 -y
sudo service apache2 start
起動後、以下のコマンドで Apache のステータスを確認できます
sudo service apache2 status
「* apache2 is running」と出力されれば動作している
Windowsで http://localhost/ にアクセスし、「Apache2 Ubuntu Default Page」が表示されれば問題なし
Ubuntuを起動時に自動でApachを起動するよう~/.bashrc
に以下を追記する
echo "sudo service apache2 start" >> ~/.bashrc
起動時に毎回パスワードを聞かれるが、回避する方法もある(今回は省略)
MySQLインストール&起動
Ubuntuで以下のコマンドを実行
sudo apt install mysql-server -y
sudo service mysql start
以下のコマンドを実行することで、rootパスワードの設定、強度、匿名ユーザーの削除、リモート root ログイン、test データベースの削除、権限のリロードが設定可能
sudo mysql_secure_installation
実行結果
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: n
Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL 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? (Press y|Y for Yes, any other key for No) : 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? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
続いてMySQL のソケットディレクトリの所有者が mysql:mysql
になっているか確認
ls -ld /var/run/mysqld
もし root などになっていたら、権限を修正
sudo chown -R mysql:mysql /var/run/mysqld
sudo chmod 755 /var/run/mysqld
PHPインストール
sudo apt install php php-mysql libapache2-mod-php -y
3. WordPress インストール
ダウンロード・配置
cd /var/www/html
sudo rm -rf *
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xvzf latest.tar.gz
sudo mv wordpress/* .
sudo rm -rf wordpress latest.tar.gz
パーミッション設定
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 755 /var/www/html
4. MySQL データベース作成
MySQLにログインする
sudo mysql -u root -p
データベースとユーザーを作成
-- WordPress用のデータベースを作成
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- WordPress用のユーザーを作成("wp_user" は適宜変更可能)
CREATE USER 'wp_user'@'localhost' IDENTIFIED WITH mysql_native_password BY 'wp_pass';
-- 作成したユーザーにデータベースへのアクセス権限を付与
GRANT ALL PRIVILEGES ON wordpress.* TO 'wp_user'@'localhost';
-- 権限を適用
FLUSH PRIVILEGES;
EXIT;
作成されたデータベースはMySQLにログインして以下のコマンドで確認可能
SHOW DATABASES;
実行結果
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5. WordPress設定ファイル(wp-config.php)
WordPressの設定ファイルwp-config.php
を作成する
sudo cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sudo vim /var/www/html/wp-config.php
以下の部分を MySQLで作成したデータベースに沿って編集する
define('DB_NAME', 'wordpress');
define('DB_USER', 'wp_reok');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
6. WordPress用のApache設定
.htaccess有効化(mod_rewrite)
sudo a2ensite wordpress
sudo a2enmod rewrite
sudo service apache2 reload
sudo service apache2 restart
index.html削除
sudo rm /var/www/html/index.html
7. WordPress初期設定
- ブラウザで
http://localhost/
にアクセス - 初期セットアップ(サイト名、管理者ユーザー作成など)
- 管理画面にログイン
注意点
WSL2特有の事項
-
systemctl
は使えない →service
コマンド使用
今後の方針
用途 | PC | OS | 状態 |
---|---|---|---|
本番環境 | Surface Laptop | Ubuntu(予定) | 未着手 |
開発環境 | GMKtec G5 | Windows11 + WSL2 Ubuntu | WordPress環境構築完了 |
おわりに
はじめてWSL2上でWordPress環境を構築しました。同じようにミニPC(GMKtec G5)やWSL2でブログを始めようとしている方の参考になれば幸いです。
誤りや改善点があれば、コメントいただけると助かります!