WordPressをUbuntu18.04(LTS)にインストール (LEMP Stack: Nginx, MariaDB, PHP )


はじめに

最新版のWordPressをUbuntu18.04にインストール方法を記載。
DatabaseはMariaDBにします。(調べていくとMySQLよりも将来性が高いため)

ワードプレスの設定用のユーザー名はwordpressuser、パスワードはgorillagorilla
とする。
データベース名はwordpressとする。

ドメインはwww.example.com
サーバーのローカルIPアドレスは 192.168.0.2
と仮定して、以下設定方法を記載する。

Step 1: Nginx HTTP Serverのインストール

sudo apt install nginx

Step 2: MariaDB Database Serverのインストール

sudo apt-get install mariadb-server mariadb-client

インストール後、次のコマンドを叩いてmysqlの初期設定を実施

sudo mysql_secure_installation
  • Enter current password for root (enter for none): そのままエンターを押す
  • Set root password? [Y/n]: Y
  • New password: パスワードの入力
  • Re-enter new password: パスワードの入力
  • Remove anonymous users? [Y/n]: Y
  • Disallow root login remotely? [Y/n]: Y
  • Remove test database and access to it? [Y/n]: Y
  • Reload privilege tables now? [Y/n]: Y

上記のパスワードはWordPressのもの(gorillagorilla)とは関係ない。

Step 3: PHP 7.2とそれ関連のモジュールインストール

sudo apt install php7.2-fpm php7.2-common php7.2-mbstring php7.2-xmlrpc php7.2-soap php7.2-gd php7.2-xml php7.2-intl php7.2-mysql php7.2-cli php7.2-zip php7.2-curl

おすすめ設定として、インストールが終わったら次の通り設定を編集。
なお、やらなくても特に問題はない。

sudo emacs /etc/php/7.2/fpm/php.ini

変更するところは以下の通り

file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
cgi.fix_pathinfo=0
max_execution_time = 360
date.timezone = Asia/Tokyo

Step 4: WordPress用のDatabaseを作成

パスワードはgorillagorilla, ユーザー名はwordpressuserとする。

sudo mysql -u root -p

DBに入ったら次のコマンドを打つ
(ここでは、データベース名はwordpressとしている)

CREATE DATABASE wordpress;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'gorillagorilla';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost' IDENTIFIED BY 'gorillagorilla' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;

※パスワードはgorillagorilla, ユーザー名はwordpressuserとしている

Step 5: 最新版のWordPressをインストール

cd /tmp && wget https://wordpress.org/latest.tar.gz
tar -zxvf latest.tar.gz
sudo mv wordpress /var/www/html/wordpress

パーミッション関連の変更

sudo chown -R www-data:www-data /var/www/html/wordpress/
sudo chmod -R 755 /var/www/html/wordpress/

Step 6: Nginx HTTP Serverの設定更新

以下のファイルを作成

sudo emacs /etc/nginx/sites-available/wordpress

内容は次の通り。servernameはwww.example.comとしているが、ここは適時変更

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/wordpress;
    index  index.php index.html index.htm;
    server_name  192.168.0.2 www.example.com;

     client_max_body_size 100M;

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

    location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

Step 7: NginxでWordPressのサイトを有効化

sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/

Nginxを再起動

sudo /etc/init.d/nginx restart

STEP 8: WordPressの設定ファイルを編集

sudo cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php
sudo emacs /var/www/html/wordpress/wp-config.php

変更内容は以下のとおりとする。

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

/** MySQL database password */
define('DB_PASSWORD', 'gorillagorilla');

/** MySQL hostname */
define('DB_HOST', 'localhost');

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

※パスワードはgorillagorilla, ユーザー名はwordpressuserとする。

STEP 9:アクセス

サーバーにアクセスする。(192.168.0.2をブラウザで叩く)

以上で正常にみえるはず。

補足:ハマった所

最初間違ってMysqlをインストールしてしまって、慌てて消してMariaDBをインストールしたが、なんかエラーが出まくった。
すでにMysqlをインストールしていたら、MariaDBをインストールしないで素直にMySqlでやったほうがいいかも。