在CentOS 6上安装PHP-FPM + nginx

安裝不同的倉庫

各种软件源安装(CentOS 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

请使用浏览器访问以下网址,确认PHP信息能够显示:
http://virtualhost.tatsunet.net/

脚本

#!/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
广告
将在 10 秒后关闭
bannerAds