Google Compute Engine を CLI で作成
## Google Cloud プロジェクトを選択
gcloud config set project mnrst-dev
## Always Free のリージョンを選択
gcloud config set compute/region us-west1
## リージョンの中のゾーンを選択
gcloud config set compute/zone us-west1-a
## ディスクサイズ 30GB の Debian 11 を e2-micro インスタンスで作成
gcloud compute instances create wp-instance \
--machine-type=e2-micro \
--boot-disk-size=30GB
## VPC のファイアウォールで HTTP/HTTPS を開ける
gcloud compute firewall-rules create default-allow-http \
--allow=tcp:80,tcp:443 \
--source-ranges="0.0.0.0/0" \
--priority=1000
## VPC のファイアウォールで SSH を自分の IP アドレスだけに制限
gcloud compute firewall-rules update default-allow-ssh \
--source-ranges=$(curl -s inet-ip.info)
## ついでに RDP も制限
gcloud compute firewall-rules update default-allow-rdp \
--source-ranges=$(curl -s inet-ip.info)
## インスタンスに SSH 接続
gcloud compute ssh wp-instance
インスタンス内で WordPress を構築
## root ユーザーに変更
sudo su -
## タイムゾーン設定
timedatectl set-timezone Asia/Tokyo
## Debian のパッケージをアップデート
apt update && apt upgrade -y
## Nginx をインストール
apt install nginx -y
## Nginx の状態を確認
systemctl status nginx
## MariaDB をインストール
apt install mariadb-server -y
## MariaDB の状態を確認
systemctl status mariadb
## MariaDB にログイン
mysql -u root
## DB を作成
create database wp;
## DB に権限付与
grant all on wp.* to root@localhost identified by 'SuperStr0ngP@ssw0rd';
## 作成した DB を確認
show databases;
## MariaDB からログアウト
exit;
## PHP と PHP のライブラリをインストール
apt install php php-{fpm,pear,cgi,common,zip,mbstring,net-socket,gd,xml-util,mysql,bcmath,curl,imagick,intl} -y
## WordPress をダウンロード
curl -O https://wordpress.org/latest.tar.gz
## WordPress を解凍
tar xfz latest.tar.gz
## WordPress を Nginx ディレクトリに移動
mv wordpress/* /var/www/html/
## WordPress がファイル生成できるようディレクトリに権限付与
chown -R www-data:www-data /var/www/html
## WordPress 用の Nginx 設定ファイルを作成
vi /etc/nginx/sites-available/wordpress.conf
WordPress 用の Nginx 設定ファイルを作成
server {
listen 80;
root /var/www/html;
server_name wp.mnrst.com;
access_log /var/log/nginx/wp_access.log;
error_log /var/log/nginx/wp_error.log;
location / {
index index.php index.html;
try_files $uri $uri/ /index.php?$args;
}
charset utf-8;
gzip off;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~* ^.+.(xml|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_read_timeout 3600s;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 128k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php-fpm.sock;
fastcgi_index index.php;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
}
Nginx の設定ファイルを変更
## デフォルトの Nginx 設定ファイルを削除
rm /etc/nginx/sites-enabled/default
## WordPress 用の Nginx 設定ファイルを有効化
ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
## Nginx 設定ファイルの構文エラーチェック
nginx -t
## Nginx 再起動
systemctl restart nginx
SSL/TLS サーバー証明書をインストール
## Debian パッケージをインストール
apt install certbot python3-certbot-nginx -y
## Nginx に SSL/TLS サーバー証明書を導入
certbot --nginx
## サーバー証明書を更新する場合は下記のコマンドを実行
certbot renew
参考サイト
Install WordPress on Debian 11 with Nginx and Let’s Encrypt
タグ: Google Cloud, Google Compute Engine, WordPress