PHP-FPM(PHP7) + nginx インストール(CentOS 6)


### PHP-FPM のインストール

```console:
[vagrant@localhost ~]$ sudo rpm --import http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
[vagrant@localhost ~]$ sudo rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
[vagrant@localhost ~]$ sudo yum -y update epel-release
[vagrant@localhost ~]$ sudo cp -p /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.org
[vagrant@localhost ~]$ sudo sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo
[vagrant@localhost ~]$ sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
[vagrant@localhost ~]$ sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
[vagrant@localhost ~]$ sudo yum -y update remi-release
[vagrant@localhost ~]$ sudo yum -y --enablerepo=remi-php70,epel install php-fpm php-gd php-gmp php-mbstring php-mcrypt php-opcache php-pdo php-pear-MDB2-Driver-mysqli php-pecl-memcached php-pecl-msgpack php-xml
[vagrant@localhost ~]$ php -v
PHP 7.0.0RC4 (cli) (built: Sep 30 2015 10:46:32) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
```

### nginx のインストール

```console:
[vagrant@localhost ~]$ sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
[vagrant@localhost ~]$ sudo yum -y update nginx-release-centos
[vagrant@localhost ~]$ sudo cp -p /etc/yum.repos.d/nginx.repo /etc/yum.repos.d/nginx.repo.org
[vagrant@localhost ~]$ sudo sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/nginx.repo
[vagrant@localhost ~]$ sudo yum -y --enablerepo=nginx install nginx
```

### PHP の設定

```console:
[vagrant@localhost ~]$ sudo cp -p /etc/php.ini /etc/php.ini.org
[vagrant@localhost ~]$ sudo sed -i -e 's/;date.timezone =/date.timezone = "Asia\/Tokyo"/' /etc/php.ini
[vagrant@localhost ~]$ php -i | grep date.timezone
date.timezone => Asia/Tokyo => Asia/Tokyo
```

### PHP-FPM の設定

```console:
[vagrant@localhost ~]$ sudo cp -p /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.org
[vagrant@localhost ~]$ sudo sed -i -e 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/listen = 127.0.0.1:9000/listen = \/var\/run\/php-fpm\/php-fpm.sock/' /etc/php-fpm.d/www.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/;listen.owner = nobody/listen.owner = nginx/' /etc/php-fpm.d/www.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/;listen.group = nobody/listen.group = nginx/' /etc/php-fpm.d/www.conf
```

### nginx の設定(バーチャルホストの作成)

※ここでは「virtualhost.tatsunet.net」という名前のバーチャルホストを作成する。

```console:
[vagrant@localhost ~]$ sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
[vagrant@localhost ~]$ sudo sed -i -e "s/worker_processes  1;/worker_processes  auto;/" /etc/nginx/nginx.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/http_x_forwarded_for"'\'';/http_x_forwarded_for"'\'';\n    log_format  ltsv  '\''time:$time_local\\t'\''\n                      '\''host:$remote_addr\\t'\''\n                      '\''user:$remote_user\\t'\''\n                      '\''req:$request\\t'\''\n                      '\''status:$status\\t'\''\n                      '\''size:$body_bytes_sent\\t'\''\n                      '\''referer:$http_referer\\t'\''\n                      '\''ua:$http_user_agent\\t'\''\n                      '\''forwardedfor:$http_x_forwarded_for'\'';/' /etc/nginx/nginx.conf
[vagrant@localhost ~]$ sudo sed -i -e 's/include \/etc\/nginx\/conf.d\/\*.conf;/include \/etc\/nginx\/conf.d\/\*.conf;\n    include \/etc\/nginx\/sites-enabled\/\*.conf;/' /etc/nginx/nginx.conf
[vagrant@localhost ~]$ cat /etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  ltsv  'time:$time_local\t'
                      'host:$remote_addr\t'
                      'user:$remote_user\t'
                      'req:$request\t'
                      'status:$status\t'
                      'size:$body_bytes_sent\t'
                      'referer:$http_referer\t'
                      'ua:$http_user_agent\t'
                      'forwardedfor:$http_x_forwarded_for';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;
}
[vagrant@localhost ~]$ sudo mkdir /etc/nginx/sites-available
[vagrant@localhost ~]$ sudo mkdir /etc/nginx/sites-enabled
[vagrant@localhost ~]$ sudo mkdir /usr/share/nginx/virtualhost
[vagrant@localhost ~]$ cat << EOF | sudo tee /etc/nginx/sites-available/virtualhost.conf
server {
    listen       80;
    server_name  virtualhost.tatsunet.net;

    access_log  /var/log/nginx/virtualhost.access.log  ltsv;
    error_log   /var/log/nginx/virtualhost.error.log   warn;

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

    location ~ \.php\$ {
        root           /usr/share/nginx/virtualhost;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
        include        fastcgi_params;
    }
}
EOF
[vagrant@localhost ~]$ cat /etc/nginx/sites-available/virtualhost.conf
server {
    listen       80;
    server_name  virtualhost.tatsunet.net;

    access_log  /var/log/nginx/virtualhost.access.log  ltsv;
    error_log   /var/log/nginx/virtualhost.error.log   warn;

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

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /usr/share/nginx/virtualhost;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
[vagrant@localhost ~]$ sudo ln -s /etc/nginx/sites-available/virtualhost.conf /etc/nginx/sites-enabled/
```

### PHP-FPM の起動

```console:
[vagrant@localhost ~]$ sudo /etc/rc.d/init.d/php-fpm start
php-fpm を起動中:                                          [  OK  ]
[vagrant@localhost ~]$ sudo chkconfig php-fpm on
```

### nginx の起動

```console:
[vagrant@localhost ~]$ sudo /etc/rc.d/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[vagrant@localhost ~]$ sudo /etc/rc.d/init.d/nginx start
nginx を起動中:                                            [  OK  ]
[vagrant@localhost ~]$ sudo chkconfig nginx on
```

### 動作確認

```console:
[vagrant@localhost ~]$ echo '<?php phpinfo();' | sudo tee /usr/share/nginx/virtualhost/index.php
<?php phpinfo();
[vagrant@localhost ~]$ sudo cp -p /etc/hosts /etc/hosts.org
[vagrant@localhost ~]$ sudo sed -i -e "s/`grep 127.0.0.1 /etc/hosts`/& virtualhost.tatsunet.net/" /etc/hosts
[vagrant@localhost ~]$ curl -s http://virtualhost.tatsunet.net/ | grep "PHP Version" | grep -v h1
<tr><td class="e">PHP Version </td><td class="v">7.0.0RC4 </td></tr>
```