LoginSignup
27
28

More than 5 years have passed since last update.

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

Last updated at Posted at 2015-03-16

各種リポジトリのインストール

各種リポジトリインストール(CetnOS 6)

PHP-FPM のインストール

# yum -y --enablerepo=remi-php56,remi,epel install php-fpm php-mbstring php-xml php-mysql php-pdo php-mcrypt php-pecl-memcached php-pecl-msgpack

nginx のインストール

# yum -y --enablerepo=nginx install nginx

PHP の設定

# cp -p /etc/php.ini /etc/php.ini.org
# sed -i -e 's/;date.timezone =/date.timezone = "Asia\/Tokyo"/' /etc/php.ini | grep date.timezone

PHP-FPM の設定

# cp -p /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.org
# sed -i -e 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf
# sed -i -e 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf

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

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

# cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
# 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
# mkdir /etc/nginx/sites-available
# mkdir /etc/nginx/sites-enabled
# mkdir /usr/share/nginx/virtualhost
# cp /etc/nginx/conf.d/default.conf /etc/nginx/sites-available/virtualhost.conf
# sed -i -e 's/server_name  localhost/server_name  virtualhost.tatsunet.net/' /etc/nginx/sites-available/virtualhost.conf
# sed -i -e '9s/root   \/usr\/share\/nginx\/html/root   \/usr\/share\/nginx\/virtualhost/' /etc/nginx/sites-available/virtualhost.conf
# sed -i -e 's/index  index.html index.htm;/index  index.php index.html index.htm;/' /etc/nginx/sites-available/virtualhost.conf
# sed -i -e '30,36s/#//' /etc/nginx/sites-available/virtualhost.conf
# sed -i -e 's/root           html;/root   \/usr\/share\/nginx\/virtualhost;/' /etc/nginx/sites-available/virtualhost.conf
# sed -i -e 's/fastcgi_param  SCRIPT_FILENAME  \/scripts$fastcgi_script_name;/fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;/' /etc/nginx/sites-available/virtualhost.conf
# ln -s /etc/nginx/sites-available/virtualhost.conf /etc/nginx/sites-enabled/
# cat /etc/nginx/sites-enabled/virtualhost.conf
server {
    listen       80;
    server_name  virtualhost.tatsunet.net;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root   /usr/share/nginx/virtualhost;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


iptables の設定

iptablesを使用している場合は80番ポートへのアクセスを許可する。

# cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.org
# sed -i -e 's/-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT/-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT/' /etc/sysconfig/iptables
# /etc/rc.d/init.d/iptables restart

PHP-FPM の起動

# /etc/rc.d/init.d/php-fpm start
# chkconfig php-fpm on

nginx の起動

# /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
# /etc/rc.d/init.d/nginx start
# chkconfig nginx on

動作確認

# echo '<?php phpinfo();' > /usr/share/nginx/virtualhost/index.php

ブラウザで以下のURLにアクセスして、PHP情報が表示されることを確認する。
http://virtualhost.tatsunet.net/

スクリプト

install_php-fpm.sh
#!/bin/bash

# 開発ツールのインストール
sudo yum -y groupinstall development

# 各種リポジトリのインストール
sudo rpm --import http://ftp.riken.jp/Linux/fedora/epel/RPM-GPG-KEY-EPEL-6
sudo rpm -ivh http://ftp.riken.jp/Linux/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
sudo yum -y update epel-release
sudo cp -p /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.org
sudo sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/epel.repo
sudo rpm --import http://rpms.famillecollet.com/RPM-GPG-KEY-remi
sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
sudo yum -y update remi-release
sudo rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
sudo yum -y update nginx-release-centos
sudo cp -p /etc/yum.repos.d/nginx.repo /etc/yum.repos.d/nginx.repo.org
sudo sed -i -e "s/enabled=1/enabled=0/g" /etc/yum.repos.d/nginx.repo

# PHP-FPM のインストール
sudo yum -y --enablerepo=remi-php56,remi,epel install php-fpm php-mbstring php-xml php-mysql php-pdo php-mcrypt php-pecl-memcached php-pecl-msgpack

# nginx のインストール
sudo yum -y --enablerepo=nginx install nginx

# PHP の設定
sudo cp -p /etc/php.ini /etc/php.ini.org
sudo sed -i -e 's/;date.timezone =/date.timezone = "Asia\/Tokyo"/' /etc/php.ini | grep date.timezone

# PHP-FPM の設定
sudo cp -p /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.org
sudo sed -i -e 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i -e 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf

# nginx の設定(バーチャルホストの作成)
sudo mkdir /etc/nginx/sites-available
sudo mkdir /etc/nginx/sites-enabled
sudo mkdir /usr/share/nginx/virtualhost
sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.org
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
cat << EOF | sudo tee /etc/nginx/sites-available/virtualhost.conf
server {
    listen       80;
    server_name  virtualhost.tatsunet.net;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

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

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root   /usr/share/nginx/virtualhost;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  \$document_root\$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
EOF
sudo sudo ln -s /etc/nginx/sites-available/virtualhost.conf /etc/nginx/sites-enabled/

# iptables の設定
sudo cp -p /etc/sysconfig/iptables /etc/sysconfig/iptables.org
sudo sed -i -e 's/-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT/-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT\n-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT/' /etc/sysconfig/iptables
sudo /etc/rc.d/init.d/iptables restart

# PHP-FPM の起動
sudo /etc/rc.d/init.d/php-fpm start
sudo chkconfig php-fpm on

# nginx の起動
sudo /etc/rc.d/init.d/nginx start
sudo chkconfig nginx on
27
28
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
27
28