CentOS7.1 + Nginx1.6 + PHP-FPM + MariaDB 環境構築

# OS状況確認

CentOS 7 (x86_64) with Updates HVM
https://aws.amazon.com/marketplace/pp/B00O7WM7QW
※user:'centos'

# OS初期セットアップ

```shell-session
$ yum -y update
$ yum -y install wget ntp mlocate
```

## 時刻サーバ設定
```shell-session
$ sed -i "s/server 0.centos.pool.ntp.org iburst/server -4 ntp.nict.jp iburst/g" /etc/ntp.conf
$ sed -i "s/server 1.centos.pool.ntp.org iburst/server -4 ntp1.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
$ sed -i "s/server 2.centos.pool.ntp.org iburst/server -4 ntp2.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
$ sed -i "s/server 3.centos.pool.ntp.org iburst/server -4 ntp3.jst.mfeed.ad.jp iburst/g" /etc/ntp.conf
```

## NTPサーバ起動

```shell-session
$ sudo systemctl enable ntpd
$ sudo systemctl start ntpd
$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp-a3.nict.go. .NICT.           1 u   29   64  177    2.445  -12.927   7.649
+ntp1.jst.mfeed. 172.29.2.50      2 u   21   64  177    2.361   -9.481   4.574
+ntp2.jst.mfeed. 172.16.176.60    2 u   20   64  177    2.330  -10.254   4.722
-ntp3.jst.mfeed. 133.243.236.17   2 u   22   64  177    2.913   -4.273   5.107
```

#タイムゾーンを日本に書き換え

```shell-session
$ sudo timedatectl set-timezone Asia/Tokyo
$ timedatectl status
$ timedatectl status
      Local time: Wed 2015-04-15 16:39:04 JST
  Universal time: Wed 2015-04-15 07:39:04 UTC
        Timezone: Asia/Tokyo (JST, +0900)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
```

# PHP5.4及びPHP-FPM関連プログラムインストール

##PHP関連プログラムインストール

```shell-session
$ sudo yum install -y epel-release
$ sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
$ sudo yum install -y yum install -y php php-fpm php-intl php-mbstring php-mysql php-pdo php-pear php-pecl-apc php-process php-xml php-xdebug php-xhprof php-opcache
$ sudo cp -a /usr/share/doc/php-common-5.4.16/php.ini-production /etc/php.ini
$ sudo sed -i "s|;date.timezone =|date.timezone = Asia/Tokyo|g" /etc/php.ini
$ sudo echo 'xdebug.max_nesting_level = 250' >> /etc/php.d/xdebug.ini
```
※Symfony2の開発環境セットアップ用なので適時PHPのエクステンションは書き換える事。

## PHP-FPM設定

```shell-session
$ sudo sed -i "s|user = apache|user = nginx|g" /etc/php-fpm.d/www.conf
$ sudo sed -i "s|group= apache|group= nginx|g" /etc/php-fpm.d/www.conf
```

## PHP-FPM起動設定

```shell-session
$ sudo systemctl enable php-fpm
$ sudo systemctl start php-fpm
$ sudo systemctl status php-fpm
php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled)
   Active: active (running) since Tue 2015-04-21 05:28:21 JST; 3h 40min ago
 Main PID: 1498 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 16, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─1498 php-fpm: master process (/etc/php-fpm.conf)
           ├─1500 php-fpm: pool www
           ├─1501 php-fpm: pool www
           ├─1502 php-fpm: pool www
           ├─1503 php-fpm: pool www
           └─1504 php-fpm: pool www

Apr 21 05:28:21 ip-172-31-19-117 systemd[1]: Starting The PHP FastCGI Process Manager...
Apr 21 05:28:21 ip-172-31-19-117 systemd[1]: Started The PHP FastCGI Process Manager.
```

# MariaDBインストール
## MariaDBインストール

```shell-session
$ sudo yum install -y mariadb mariadb-server
```

## MariaDB設定

```shell-session
$ cp -a /usr/share/mysql/my-large.cnf /etc/my.cnf
$ sudo sed -i "s|#innodb_data_home_dir = /var/lib/mysql|innodb_data_home_dir = /var/lib/mysql|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_data_file_path = ibdata1:10M:autoextend|innodb_data_file_path = ibdata1:100M:autoextend|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_log_group_home_dir = /var/lib/mysql|innodb_log_group_home_dir = /var/lib/mysql|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_buffer_pool_size = 256M|innodb_buffer_pool_size = 256M|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_additional_mem_pool_size = 20M|innodb_additional_mem_pool_size = 20M|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_log_file_size = 64M|innodb_log_file_size = 64M|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_log_buffer_size = 8M|innodb_log_buffer_size = 8M|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_flush_log_at_trx_commit = 1|innodb_flush_log_at_trx_commit = 1|g" /etc/my.cnf
$ sudo sed -i "s|#innodb_lock_wait_timeout = 50|innodb_lock_wait_timeout = 50|g" /etc/my.cnf
$ sudo sed -i -e "/^innodb_lock_wait_timeout = 50$/a innodb_file_format = Barracuda" /etc/my.cnf
$ sudo sed -i -e "/^innodb_lock_wait_timeout = 50$/a innodb_file_per_table = 1" /etc/my.cnf
$ sudo sed -i -e "/^innodb_lock_wait_timeout = 50$/a innodb_flush_method=O_DIRECT" /etc/my.cnf
$ sudo sed -i -e "/^innodb_lock_wait_timeout = 50$/a default-storage-engine = InnoDB" /etc/my.cnf
$ sudo sed -i -e "/^innodb_lock_wait_timeout = 50$/a character-set-server = utf8" /etc/my.cnf
```

以下を参考に設定する

>サンプルファイル	搭載メモリ	用途
>my-small.cnf	〜64MB	小規模なDB
>my-medium.cnf	〜128MB	共用サーバでの小規模なDB
>my-large.cnf	〜512MB	MySQLを主とするサーバ
>my-huge.cnf	1G〜2G	MySQL専用サーバ
>my-innodb-heavy-4G.cnf	4G	InnoDBメインとするサーバ

## MariaDB起動設定

```shell-session
$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb
mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled)
   Active: active (running) since Tue 2015-04-21 05:17:04 JST; 3h 52min ago
 Main PID: 802 (mysqld_safe)
   CGroup: /system.slice/mariadb.service
           ├─ 802 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
           └─1374 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/lib/mysql/ip-172-31-...

Apr 21 05:16:58 ip-172-31-19-117 systemd[1]: Starting MariaDB database server...
Apr 21 05:16:58 ip-172-31-19-117 mysqld_safe[802]: 150421 05:16:58 mysqld_safe Logging to '/var/lib/mysql/ip-172-31-19-117.err'.
Apr 21 05:16:59 ip-172-31-19-117 mysqld_safe[802]: 150421 05:16:58 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Apr 21 05:17:04 ip-172-31-19-117 systemd[1]: Started MariaDB database server.
```

# Nginxインストール
## Nginxインストール

```shell-session
$ sudo yum install -y nginx 
```

## Nginx サーバ初期設定

```shell-session
$ sudo cat <<_EOF_ > /etc/nginx/nginx.conf
user  nginx;
worker_processes  1; #　CPUの数に合わせる

#error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  warn;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;
pid        /var/run/nginx.pid;

events {
    worker_connections  2048;
}

http {
    #server_tokens off; #本番環境の場合、コメント外す

    # default log format
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    # don't rely on the kernel's sendfile() this is more efficient.
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    # allow user agents to keep a connection open with us.
    keepalive_timeout         10;
    client_header_timeout     10;
    client_body_timeout       10;
    reset_timedout_connection on;
    send_timeout              10;

    limit_conn_zone $binary_remote_addr zone=addr:10m;
    limit_conn addr 100;

    include       /etc/nginx/mime.types;
    default_type  text/html;

    # deliver content faster with gzip.
    gzip              on;
    gzip_http_version 1.0;
    gzip_disable      "msie6";
    gzip_vary         on; # 本番環境の場合、off
    #gzip_proxied      any; # Proxy利用の場合、有効化
    gzip_min_length 1024;
    gzip_comp_level 6;
    gzip_types      text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    # cache the open file descriptors
    open_file_cache max=10000 inactive=20s;
    open_file_cache_valid    30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors   on;

    # allow 20Mb as max upload size.
    client_body_buffer_size 20M;

    include /etc/nginx/conf.d/*.conf;
}
_EOF_
$ sudo sed -i "s|#location ~ \.php$ {|location ~ \.php$ {|g" /etc/nginx/conf.d/default.conf
$ sudo sed -i -e "/server_name  localhost;/a root   /usr/share/nginx/html;" /etc/my.cnf
$ sudo sed -i "s|#fastcgi_pass   127.0.0.1:9000;|fastcgi_pass   127.0.0.1:9000;|g" /etc/nginx/conf.d/default.conf
$ sudo sed -i "s|#fastcgi_index  index.php;|fastcgi_index  index.php;|g" /etc/nginx/conf.d/default.conf
$ sudo sed -i "s|#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;|fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;|g" /etc/nginx/conf.d/default.conf
$ sudo sed -i "s|#include        fastcgi_params;|include        fastcgi_params;|g" /etc/nginx/conf.d/default.conf
$ sudo sed -i -e "/include        fastcgi_params;$/a }" /etc/default.conf
```

## Nginx自動起動設定

```shell-session
$ sudo systemctl enable nginx
$ sudo systemctl start nginx
$ sudo systemctl status nginx
nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled)
   Active: active (running) since Tue 2015-04-21 08:46:26 JST; 23min ago
     Docs: http://nginx.org/en/docs/
  Process: 1967 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1972 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 1971 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 1974 (nginx)
   CGroup: /system.slice/nginx.service
           ├─1974 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─1975 nginx: worker process

Apr 21 08:46:26 ip-172-31-19-117 systemd[1]: Starting nginx - high performance web server...
Apr 21 08:46:26 ip-172-31-19-117 nginx[1971]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Apr 21 08:46:26 ip-172-31-19-117 nginx[1971]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Apr 21 08:46:26 ip-172-31-19-117 systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Apr 21 08:46:26 ip-172-31-19-117 systemd[1]: Started nginx - high performance web server.
```

# 表示確認

```shell-session
$ sudo cat <<_EOF_ > /usr/share/nginx/html/phpinfo.php
<?php
   phpinfo();
_EOF_
```

ブラウザでサーバIPにアクセスするとphpinfoが表示されました。

![phpinfoが表示されました。](https://qiita-image-store.s3.amazonaws.com/0/19439/8a2195a4-c57e-401b-d5ba-0c81d6f762d2.png)


# 参考URL
CentOSにnginx, php-fpm, mysqlの環境を構築する１
http://qiita.com/puttyo_bubu/items/5bf554ef2790f6435c32