LoginSignup
1
1

More than 3 years have passed since last update.

Ubuntu 20.04 + Nginx 1.18 + PHP(WordPress) + MariaDBで環境構築メモ

Posted at

nginxの学習のためローカルで環境を構築したのでメモ

環境

Ubuntu 20.04.1 LTS (Focal Fossa)
nginx nginx/1.18.0 (Ubuntu)

Ubuntu + nginx + PHP(WordPress)

ApacheにはPHPの拡張モジュール(mod_php)があるが、nginxにはモジュールがない。
そのため、FastCGIインターフェースを使ってPHPを動作させる PHP-FPM(FastCGI Process Manager)を使ってnginxと接続させる。
FastCGIは、Webアプリケーションのプロセスがデーモンとしてappサーバ上に常駐することでCGI(Common Gateway Interface)の欠点を補ったプロトコル。
PHP-FPMはPHPの開発元が配布しており、FastCGIのインターフェイスを使ってPHPプログラムを動作させる。

PHP-FPM

インストール

まず、aptでPHP-FPMと、WordPressで使用するためのMySQLのライブラリをインストール

$ sudo apt install php-fpm php-mysql -y
Reading package lists... Done
Building dependency tree       
Reading state information... Done
...
# 省略
...
# インストール完了

$ php -v
PHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

$ php-fpm7.4 -v
Sun 16 Aug 2020 08:06:23 AM UTC
PHP 7.4.3 (fpm-fcgi) (built: May 26 2020 12:24:22)
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
    with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies

PHP-FPMの設定ファイル

PHP-FPMは/etc/php/7.4/fpm/以下にある。
fpm/php-fpm.confにPHP-FPMの設定が、fpm/php.iniにPHPの設定が記述されている。

また、各プール(PHP-FPM内部でプロセスやソケットを共有するグループ)ごとの設定はfpm/pool.d/以下に*.confとしてファイルを作成。
デフォルトでは/fpm/pool.d/www.confが作成されていて、fpm/php-fpm.conf内で`include=/etc/php/7.4/fpm/pool.d/*.conf'で読み込まれている。

項目listenを編集し、TCPソケット通信ではなくUnixドメインソケットを利用する設定。(今回はデフォルトのまま使用)
また、php-fpm.sockがFPMの再起動によって削除されることに対応するため、listen.owner, listen.groupを編集
(ここではnginxする)
参考:Nginxとphp-fpmをsock接続するときphp-fpm.sockが消える件
PHPプログラムとnginxの権限の違いによってファイルアクセスができなくなるなどトラブルを防止するため、user, groupnginxで設定。
参考:Ubuntu 18.04にNginx、PHP 7.4、MySQL 5.7をインストールする

$ sudo vim /etc/php/7.4/fpm/pool.d/www.conf
 ...
listen = /run/php/php7.4-fpm.sock
 ...
user = nginx
group = nginx
listen.owner = username 
listen.group = username

WordPressをインストール

$ sudo mkdir -p /var/www/php-fpm.example.com
$ cd /var/www/php-fpm.example.com/
$ sudo tar xfz wordpress-5.5-ja.tar.gz  # 公式サイトからダウンロードする
$ sudo chown -R nginx: wordpress

nginxの設定

以下の設定を/etc/nginx/conf.d/wordpress.confとして保存

server {
    listen 80;
    server_name 127.0.0.1;
    root /var/www/php-fpm.example.com/wordpress;    # wordpressの格納先をルートにする
    index index.php;
    location ~ \.php$ {         # /*.phpにマッチするリクエスト
        try_files $uri =404;    # ファイルが存在しない場合は404エラー
        include fastcgi_params; # FastCGIに使用される変数を読み込む
        fastcgi_index index_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;   # fastcgi_paramにない変数を定義
        fastcgi_pass unix:/run/php/php7.4-fpm.sock; # FastCGIサーバの待受アドレス。Unixドメインソケットで通信する場合は戦闘に`unix:`が必要
    }
    location / {
        try_files $uri/ /index.php?$args;   # *.php以外のファイルへの対応
    }
}

MariaDBのインストールと設定

wordpressテーブルの作成、ユーザの作成を行う

$ sudo apt install mariadb-server
$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb
$ sudo mysqladmin -uroot create wordpress
$ sudo mysql -uroot
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 50
Server version: 10.3.22-MariaDB-1ubuntu1 Ubuntu 20.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)]> GRANT ALL PRIVILEGES ON wordpress.* TO [ユーザ名]@localhost IDENTIFIED BY '[パスワード]';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> exit
Bye

nginxを再起動

systemctl restart nginx

ブラウザで120.0.0.1にログインすればWordPressの管理者画面に遷移
DB、管理者情報登録画面ではCSSが読み込まれていないような白背景の画面だったが、登録後にログインすれば通常のダッシュボードに入れる

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