EC2でphp環境構築 ① Nginx + php56 + php56-fpm + virtualhost

# 概要
サーバー構築の経験があんまりにもないので、今回やってみた流れをまとめてみた。


# 目次
- nginxインストール
- php及びphp-fpmインストール
- バーチャル環境構築

####### EC2 IP Adress(仮)
```
52.111.111.111
```
# nginxインストール
## EC2へログイン
```bash
[hoge@local ~]$ ssh -i "XXX.pem" ec2-user@52.111.111.111
```
## 1. nginx情報確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ yum info nginx
```
## 2. nginxインストール
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install nginx -y
```

## 3. インストール確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ which nginx
/usr/sbin/nginx
```

## 4. nginx稼働
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx start
Starting nginx:                                            [  OK  ]

[ec2-user@ip-172-31-4-XXX ~]$ ps -ef | grep nginx
root      1563     1  0 09:06 ?        00:00:00 nginx: master process nginx
nginx     1564  1563  0 09:06 ?        00:00:00 nginx: worker process
ec2-user   1566  1385  0 09:06 pts/0    00:00:00 grep nginx
```

## 5. ブラウザへのアクセスを確認する
ブラウザで http://52.111.111.111 へアクセス。
以下のような画面が表示されれば OK 。
![20130818181233.png](https://qiita-image-store.s3.amazonaws.com/0/71860/8c9de119-a673-1daa-6b36-7284851c251a.png)

## 6. サービスの自動稼働に設定しておく
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo chkconfig nginx on
```
## 7. nginxのログディレクトリへのアクセス確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ tail -f /var/log/nginx/access.log
tail: `/var/log/nginx/access.log' を 読み込み用に開くことが出来ません: 許可がありません
[ec2-user@ip-172-31-4-XXX ~]$ ls -ald /var/log/nginx
drwx------ 2 nginx nginx 4096  9月 20 08:26 /var/log/nginx
```

## 8. ログディレクトリへのアクセス権限を変更
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo chmod 755 /var/log/nginx/
[ec2-user@ip-172-31-4-XXX ~]$ ls -ald /var/log/nginx
drwxr-xr-x 2 nginx nginx 4096  9月 20 08:26 /var/log/nginx
[ec2-user@ip-172-31-4-XXX ~]$ tail -f /var/log/nginx/access.log
XXX.XXX.XX.XXX - - [20/Sep/2015:08:29:09 +0000] ...
XXX.XXX.XX.XXX - - [20/Sep/2015:08:30:01 +0000] ...
```

# php-fpmインストール
## 1. php5.6及びphp-fpmインストール
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo yum install php56 php56-fpm -y
```

## 2. 確認用のphpファイル作成
```php:/usr/share/nginx/html/phpinfo.php
<?php
    phpinfo();
```

## 3. php-fpmの設定変更
###### userとgroupをapacheからnginxに変更
```/etc/php-fpm-5.6.d/www.conf
- user = apache
+ user = nginx

- group = apache
+ group = nginx
```
## 4. nginxの設定変更
```nginx:/etc/nginx/nginx.conf
  http {
-   index index.html index.htm;
+   index index.php index.html index.htm;
      server {
       root /usr/share/nginx/html;

-       #location ~ \.php$ {
-       #    root           html;
-       #    fastcgi_pass   127.0.0.1:9000;
-       #    fastcgi_index  index.php;
-       #    fastcgi_param  SCRIPT_FILENAME/scripts$fastcgi_script_name;
-       #    include        fastcgi_params;
-       #}
+       location ~ \.php$ {
+       root           html;
+       fastcgi_pass   127.0.0.1:9000;
+       fastcgi_index  index.php;
+       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
+       include        fastcgi_params;
+     }
```

## 5. php-fpm稼働及び自動稼働設定
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm start
[ec2-user@ip-172-31-4-XXX ~]$ sudo chkconfig php-fpm on
```

## 6. nginx再起動
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
```
## 7. 設定変更の確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.8 </td></tr>
```

## 8. nginxとphp-fpmの接続をsocketにする
### php-fpmの設定変更
```/etc/php-fpm-5.6.d/www.conf
-   listen = 127.0.0.1:9000
+   listen =  /var/run/php-fpm/php-fpm.sock

-   ;listen.owner = nobody
-   ;listen.group = nobody
+   listen.owner = nginx  
+   listen.group = nginx
```

### nginxの設定変更
```nginx:/etc/nginx/nginx.conf
-   fastcgi_pass   127.0.0.1:9000;
+   fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
```

### サービス再起動
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart 
```

### php実行に問題ないか確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ curl -s http://localhost/phpinfo.php |grep "PHP Version"
```

# バーチャル環境作成(feature1-develop.com)
## 1. php-fpmを設定
```/etc/php-fpm-5.6.d/php-fpm-feature1-develop.conf
[feature1-develop]
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm-feature1-develop.sock
listen.owner = nginx
listen.group = nginx
php_admin_value[disable_functions] = exec,passthru,shell_exec,system
php_admin_flag[allow_url_fopen] = off
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
chdir = /
```

## 2. nginxのvirtual.conf設定
```nginx:/etc/nginx/conf.d/virtual.conf
server {
    listen        80;
    server_name   feature1-develop.com;
    access_log    /var/log/nginx/feature1-develop.access.log main;
    error_log    /var/log/nginx/feature1-develop.error.log;
    root /var/www/feature1;
    index index.php;
    location ~ \.php$ {
        root           /var/www/feature1;
        fastcgi_pass   unix:/var/run/php-fpm/php-fpm-feature1-develop.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
```

## 3. ドキュメントルートディレクトリ作成
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo mkdir -p /var/www/feature1
```
## 4. index.phpファイル作成
```php:/var/www/feature1/index.php
<?php
    phpinfo();
```

## 5. php-fpmとnginx再起動
```bash
[ec2-user@ip-172-31-4-XXX ~]$ sudo service php-fpm restart
[ec2-user@ip-172-31-4-XXX ~]$ sudo service nginx restart
```

## 6. sockファイル追加確認
```bash
[ec2-user@ip-172-31-4-XXX ~]$ ls -al /var/run/php-fpm/php-fpm-feature1-develop.sock
srw-rw---- 1 nginx nginx 0  9月 20 08:54 /var/run/php-fpm/php-fpm-feature1-develop.sock
```

## 7. 自分のパソコンにバーチャルホストへのアクセス設定
```bash
[hoge@local ~]$ sudo -- sh -c "echo 52.111.111.111 feature1-develop.com >> /etc/hosts"
```

## 8. アクセス確認
#### shell上で確認
```bash
# アクセス確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "PHP Version"
<tr><td class="e">PHP Version </td><td class="v">5.6.12 </td></tr>

# 「DOCUMENT_ROOT」確認
[hoge@local ~]$ curl -s http://feature1-develop.com/phpinfo.php | grep "DOCUMENT_ROOT"
<tr><td class="e">_SERVER["DOCUMENT_ROOT"]</td><td class="v">/var/www/feature1</td></tr>
```
#### ブラウザ上で確認
http://feature1-develop.com/phpinfo.php
「DOCUMENT_ROOT」が「**/var/www/feature1**」になっているか確認
![FireShot Capture 1 - phpinfo() - http___feature1-develop.com_.png](https://qiita-image-store.s3.amazonaws.com/0/71860/91ba9813-094c-8360-95ad-d6b43a46ba29.png)

# 次にやったこと
> [EC2でphp環境構築 ② gitサーバー構築 + altax導入 + デプロイコマンド作成](http://qiita.com/jjongs08/items/ba8b813182ef0d42e568)