LoginSignup
1
2

More than 3 years have passed since last update.

VPSにWordPress(Ubutnu18.04 + nginx + PHP7.3-fpm)をインストールするメモ

Last updated at Posted at 2019-12-22

はじめに

久しぶりに安いVPSを探していたら、WebARENA IndigoというVPSサービス(メモリ1GBのプランだと月349円)を見つけたので、WordPressをインストールしてみました。その時のインストール手順のメモです。

出来上がるもの

  • Ubutnu18.04 + nginx1.14.0 (HTTP/2) + PHP7.3-fpm で動くWordPressサイト

各種アップデート

VPSを起動したらまずはupdateを実施します。

sudo apt update
sudo apt upgrade

マシンの設定

作業用ユーザの追加

デフォルトではubuntuというroot権限の付与されたユーザのみが存在します。
今回はそれとは別に作業用ユーザを追加します。

adduser user
sudo gpasswd -a user sudo

ubuntuユーザの公開鍵を作業用ユーザの.ssh配下へ移設し、ubuntuユーザは削除しました。

rootパスワードの変更

rootのパスワードが設定されていなかったため変更します。

sudo su
passwd

host名の変更

デフォルトのhost名はわかりづらいため変更します。

user@i-10100000002311:~$ sudo vim /etc/hostname 

タイムゾーンを日本時間へ変更

お好みですが、VPSのタイムゾーンを日本時間(JST)に変更しました。
参考: https://qiita.com/zaki-lknr/items/cd9844fa6a1efa58cab1

timedatectl set-timezone Asia/Tokyo

変更前

user@hostname:~$ timedatectl
                      Local time: Sun 2019-12-08 12:52:33 UTC
                  Universal time: Sun 2019-12-08 12:52:33 UTC
                        RTC time: Sun 2019-12-08 12:52:34
                       Time zone: Etc/UTC (UTC, +0000)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

変更後

user@hostname:~$ timedatectl
                      Local time: Sun 2019-12-08 21:53:38 JST
                  Universal time: Sun 2019-12-08 12:53:38 UTC
                        RTC time: Sun 2019-12-08 12:53:39
                       Time zone: Asia/Tokyo (JST, +0900)
       System clock synchronized: yes
systemd-timesyncd.service active: yes
                 RTC in local TZ: no

ミドルウェアのインストール

WordPressの動作に必要になるミドルウェア等をインストールしていきます。
今回は、nginx、PHP7.3、MySQL、ufw(ファイアウォール)、certbot(HTTPS対応のため)もインストールします。

nginxのインストール

sudo apt install -y nginx

nginx 1.14.0が入りました。

user@hostname:~$ nginx -v
nginx version: nginx/1.14.0 (Ubuntu)

PHP7.3のインストール

デフォルトだとPHP7.2がインストールされ、WordPressのサイトヘルス機能で警告が出るためPHP7.3をインストールします。

sudo apt install software-properties-common
sudo apt-add-repository ppa:ondrej/php
sudo apt install -y php7.3-fpm php-curl php7.3-bcmath php7.3-gd php7.3-mbstring php7.3-mysql php7.3-xml php7.3-zip

MySQLのインストール

sudo apt install -y mysql-server mysql-client

ufwのインストール

念のためファイアウォールを入れておきます。

sudo apt install -y ufw

certbotのインストール

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get install certbot python-certbot-nginx

ミドルウェアの設定

続いてミドルウェアの設定をしていきます。
設定の変更が済んだらプロセスの再起動を忘れずに実施します。

nginx

nginxの設定ファイル /etc/nginx/sites-available/default を設定します。

server {
        listen 80;
        listen [::]:80;

        root /var/www/html/example;

        index index.html index.htm;
        client_max_body_size 20M;
        server_name example.org;

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

        # pass PHP scripts to FastCGI server
        location ~ [^/]\.php(/|$) {
                fastcgi_index index.php;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ /\.ht {
                deny all;
        }
}

PHP

/etc/php/7.3/fpm/php.ini を開き upload_max_filesize を変更します。

upload_max_filesize = 20M

MySQL

(お好みで)文字コードをutf8mb4を使用するように変更

2019/12/27 追記: utf8だと寿司ビール問題が起きるため、utf8mb4を用いたほうが良いとコメントを頂いたため修正しました。

[mysqld]
character-set-server = utf8mb4
[mysql]
default-character-set=utf8mb4

WordPress用のデータベース作成

CREATE DATABASE wordpress;
CREATE USER 'user'@'localhost' IDENTIFIED BY 'hogehoge'
grant all on wordpress.* to 'user'@'localhost';

ufwの有効化

ファイアウォールを有効化し、SSHとHTTP以外の通信はブロックするようにします。
参考: https://qiita.com/shimakaze_soft/items/c3cce2bfb7d584e1fbce

設定内容

sudo ufw enable
sudo ufw default DENY
sudo ufw allow 80
sudo ufw allow 443
sudo ufw limit 22

設定内容を確認

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
80                         ALLOW       Anywhere                  
443                        ALLOW       Anywhere                  
22                         LIMIT       Anywhere                  
22 (v6)                    LIMIT       Anywhere (v6)             
80 (v6)                    ALLOW       Anywhere (v6)             
443 (v6)                   ALLOW       Anywhere (v6)  

certbotの設定

certbotを実行し、SSL/TLS証明書をnginxへ適用します。

sudo certbot --nginx

詳しくは公式ドキュメントに書かれています。(以下はUbuntu 18.04かつnginx向け)
https://certbot.eff.org/lets-encrypt/ubuntubionic-nginx

実行を終えたら、nginxの設定ファイル( /etc/nginx/sites-available/default )に公開鍵のパス情報が記載されていることを確認します。(便利で感動しました。)
ついでにHTTPのアクセスはHTTPSへリダイレクトするように変更します。

server {
        listen 80;
        listen [::]:80;
        server_name example.org;
        return 301 https://$host$request_uri;
}
server {
        root /var/www/html/example;

        index index.html index.htm;
        client_max_body_size 20M;
        server_name example.org;

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

        # pass PHP scripts to FastCGI server
        location ~ [^/]\.php(/|$) {
                fastcgi_index index.php;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        }

        location ~ /\.ht {
                deny all;
        }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.org/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.org/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

WordPressのインストール

WordPressの最新版をダウンロードし、nginxのrootディレクトリへ配置します。

wget https://ja.wordpress.org/latest-ja.tar.gz
tar -xzvf latest-ja.tar.gz 

あとは、WordPressのURLへアクセスしセットアップを行えば完了です。

あとがき

慣れると2時間程度でできるとはいえ、毎回やるのはしんどいので自動化したいですね。
または最近流行のDockerでコンテナを立てればもっと楽にできそう。次回は別の方法で挑戦したいなと思いました。

WebARENA Indigoは ConoHa VPSと比べるとCPU性能は6~7割程度だったり、CPUが Intel Xeon E312xx (Sandy Bridge) だったりと若干不安ではありました。ただ、WordPressを動かす程度でしたら特に問題なく、私は満足しています。

1
2
1

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
1
2