LoginSignup
17
25

More than 5 years have passed since last update.

Ubuntu 16.04 インストール後、nginx + PHP7.2 環境を構築するまでの流れ

Last updated at Posted at 2018-03-13

目的

共用レンタルサーバ上の WordPress を VPS へ移行するための下準備。
将来的に WordPress から Ruby on Rails へ移行するため、Webサーバは nginx を使用することにした。

サーバ OS

/etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"

サーバ OS は CentOS の方がよく使われるそうだが、Ubuntu の方が使い慣れているため、Ubuntu Server を採用した。

作業内容

0. SSH 接続のセキュリティ対策

VPS の SSH 接続のセキュリティが初期状態だと脆すぎるため、セキュリティ対策を真っ先に行なった。

※本記事の主旨とは少し外れるので、別記事にしました。

1. nginx のインストール

Ubuntu の初期リポジトリからもインストールできるが、古いバージョンなので、公式 nginx リポジトリからインストールする。

$ curl http://nginx.org/keys/nginx_signing.key | sudo apt-key add -
$ VCNAME=`cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2` && sudo -E sh -c "echo \"deb http://nginx.org/packages/ubuntu/ $VCNAME nginx\" >> /etc/apt/sources.list"
$ VCNAME=`cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2` && sudo -E sh -c "echo \"deb-src http://nginx.org/packages/ubuntu/ $VCNAME nginx\" >> /etc/apt/sources.list"
$ sudo apt-get update
$ sudo apt-get install nginx

2. MySQL のインストール

PHP 環境を作るだけなら不要だが、後で WordPress を入れるので、このタイミングでインストール。

$ sudo apt-get install mysql-server

初期状態だとサーバ内で扱う文字コードが latin1 (iso8859-1) のため、日本語を含んだデータを投入したりすると文字化けする。

$ sudo mysql -u root
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper

Connection id: 4
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 3 hours 4 min 41 sec

Threads: 1 Questions: 9 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 26 Queries per second avg: 0.000
--------------

MySQL の設定ファイルを変更し、キャラクタセットを UTF-8 にする。

/etc/mysql/my.cnf
[mysqld]
character-set-server=utf8
skip-character-set-client-handshake
default-storage-engine=INNODB

[mysqldump]
default-character-set=utf8

[mysql]
default-character-set=utf8

MySQL サーバを再起動する。

$ sudo /etc/init.d/mysql restart

文字コードが UTF-8 に変更されていることを確認。

$ sudo mysql -u root
mysql> status
--------------
mysql Ver 14.14 Distrib 5.7.21, for Linux (x86_64) using EditLine wrapper

Connection id: 3
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.21-0ubuntu0.16.04.1 (Ubuntu)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/run/mysqld/mysqld.sock
Uptime: 11 sec

Threads: 1 Questions: 7 Slow queries: 0 Opens: 107 Flush tables: 1 Open tables: 26 Queries per second avg: 0.636
--------------

3. PHP 7.2 のインストール

初期リポジトリの最新バージョンは 7.0 だったので、リポジトリを追加してインストール。
併せて PHP-FPM もインストール。

# add-apt-repository ppa:ondrej/php
# apt-get update
# apt-get install php7.2 php7.2-fpm php7.2-mysql php7.2-mbstring php7.2-zip

/etc/php/7.2/cli/php.ini、/etc/php/7.2/fpm/php.ini の両ファイルに以下の設定を追加する。

cgi.fix_pathinfo=0
date.timezone = "Asia/Tokyo"

nginx がらみの PHP-FPM の設定が残っているため、このタイミングではサービスの再起動はしない。

4. nginx および PHP-FPM の設定

nginx 〜 PHP-FPM(PHP の FastCGI 実装の一つ)間の連携設定をする。

設定ファイルを必要に応じてバックアップ。

# cp -p /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.org
# cp -p /etc/php/7.2/fpm/pool.d/www.conf /etc/php/7.2/fpm/pool.d/www.conf.org

nginx の設定

/etc/nginx/conf.d/default.conf
server {
  listen 80;
  server_name example.com;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm index.php;
  }

  location ~ \.php$ {
    root /usr/share/nginx/html;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

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

WordPress インストール時に修正すると思うが、まずは一旦動かせる状態にしたいので、上記の通りに設定した。

fastcgi_pass は 127.0.0.1:9000 でも問題ないのだが、TCP ソケットより UNIX ドメインソケットの方が高速らしいので、unix:/run/php/php7.2-fpm.sock に設定している。

PHP-FPM の設定

user、group が www-data となっている箇所を nginx に変更する。

/etc/php/7.2/fpm/pool.d/www.conf
user = nginx
group = nginx

listen.owner = nginx
listen.group = nginx

サービス再起動

nginx と PHP-FPM のサービスを再起動する。

# systemctl restart nginx.service
# systemctl restart php7.2-fpm.service

サービスの自動起動設定

OS 起動時に nginx と PHP-FPM が自動起動するよう設定する。

# systemctl enable nginx.service
Synchronizing state of nginx.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable nginx
# systemctl enable php7.2-fpm.service
Synchronizing state of php7.2-fpm.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable php7.2-fpm

5. ドキュメントルートのパーミッション設定

nginx とログインユーザ(通常使うユーザ)がドキュメントルート以下にアクセスできるよう、パーミッションの設定を変更する。

ログインユーザを nginx グループに追加

# usermod -aG nginx foo
# id foo
uid=1000(foo) gid=1000(foo) groups=1000(foo),27(sudo),121(nginx)

ドキュメントルートの所有者とグループ、アクセス権限の変更

nginx のドキュメントルート (/usr/share/nginx/html) の所有者/グループは root のままで OK。

ドキュメントルート内のファイルを nginx とログインユーザがアクセスできるよう、所有者/グループを nginx に変更し、アクセス権限を変更する。

# chown -R nginx:nginx /usr/share/nginx/html/*
# find /usr/share/nginx/html -type f -exec chmod 664 {} \;

パーミッションが正しく変更されていることを確認。

# ls -l /usr/share/nginx/html
total 8
-rw-rw-r-- 1 nginx nginx 537 Oct 17 22:16 50x.html
-rw-rw-r-- 1 nginx nginx 612 Oct 17 22:16 index.html

6. その他の設定(任意)

ドキュメントルートのシンボリックリンクの作成

ログインユーザのホームディレクトリに DocumentRoot のシンボリックリンクを作成。

$ ln -s /usr/share/nginx/html ~/www

稼動確認

/usr/share/nginx/html 直下に index.php を作成。

/usr/share/nginx/html/index.php
<?php phpinfo(); ?>

Web ブラウザから http://[IPアドレス]/index.php にアクセスすると、PHP の設定情報が問題なく出力された。

※ URL に IP アドレスを指定しているのは、ネームサーバの変更が完了していないため。

参考サイト

Ubuntuに最新のnginxをインストールする
Ubuntu で MySQL
Install PHP 7.1 with Nginx on Ubuntu 16.04
UbuntuにNginxをインストールするメモ(GitHubより移転)
Nginxの公開ディレクトリのパーミッションについて
パーミッションやオーナーを一括で変更する

17
25
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
17
25