您的位置:首页 > 运维架构 > Nginx

在CentOS/RHEL6.2/5.8,Fedora17/16上安装Nginx/PHP-FPM环境

2012-07-11 15:48 597 查看
以前我写过一篇文章《在CentOS/RHEL5.5,Fedora13上安装Nginx/PHP-FPM环境》,这篇文章是个升级版本。本文将介绍如何在Fedora17/16/15/14,CentOS6.2/6.1/6/5.8及Red Hat(RHEL)6.2/6.1/6/5.8上安装配置Nginx/PHP-FPM。

什么是PHP-FPM

  PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.
  PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

  现在我们可以在最新的PHP 5.3.2的源码树里下载得到直接整合了PHP-FPM的分支,据说下个版本会融合进PHP的主分支去。相对Spawn-FCGI,PHP-FPM在CPU和内存方面的控制都更胜一筹,而且前者很容易崩溃,必须用crontab进行监控,而PHP-FPM则没有这种烦恼。

  PHP5.3.3已经集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多有点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM。

  以上摘自:什么是CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI?

什么是Nginx

  Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

在Fedora17/16/15/14,CentOS6.2/6.1/6/5.8及Red Hat(RHEL)6.2/6.1/6/5.8上安装配置Nginx/PHP-FPM

让我们开始行动起来吧

STEP1.切换到root用户

su -
## OR ##
sudo -i


STEP2.安装必要的软件源

2-1. Fedora 17/16/15/14 下安装Remi源

## Remi Dependency on Fedora 17, 16, 15
rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm 
rpm -Uvh http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm  
## Fedora 17 ##
rpm -Uvh http://rpms.famillecollet.com/remi-release-17.rpm  
## Fedora 16 ##
rpm -Uvh http://rpms.famillecollet.com/remi-release-16.rpm  
## Fedora 15 ##
rpm -Uvh http://rpms.famillecollet.com/remi-release-15.rpm  
## Fedora 14 ##
rpm -Uvh http://rpms.famillecollet.com/remi-release-14.rpm[/code] 
2-1. CentOS 6.2/6.1/6/5.8 及 Red Hat (RHEL) 6.2/6.1//6/5.8下安装Remi源

## Remi Dependency on CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-7.noarch.rpm  
## CentOS 6 and Red Hat (RHEL) 6 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm  
 
## Remi Dependency on CentOS 5 and Red Hat (RHEL) 5 ##
rpm -Uvh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm  
## CentOS 5 and Red Hat (RHEL) 5 ## 
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm[/code] 
2-2. CentOS 6.2/6.1/6/5.8 及 Red Hat (RHEL) 6.2/6.1/6/5.8 下还要配置Nginx 源

创建/etc/yum.repos.d/nginx.repo文件并写入以下内容

CentOS



[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1


RedHat(RHEL)

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
gpgcheck=0
enabled=1


STEP3.安装Nginx,PHP5.4.4&PHP-FPM

3-1. Fedora 17/16/15/14下

yum --enablerepo=remi install nginx php php-fpm php-common


3-1. CentOS 6.2/5.8 及 Red Hat (RHEL) 6.2/5.8下

yum --enablerepo=remi,remi-test install nginx php php-fpm php-common


STEP4.安装PHP5.4.4模块扩展

APC (php-pecl-apc) – APC缓存优化中间件
CLI (php-cli) – PHP命令行插件
PEAR (php-pear) – PHP官方扩展
PDO (php-pdo) – 数据库PDO扩展
MySQL (php-mysql) –MYSQL驱动
PostgreSQL (php-pgsql) – PostgreSQL驱动
MongoDB (php-pecl-mongo) – PHP MongoDB驱动
SQLite (php-sqlite) – SQLite V2引擎及驱动
Memcache (php-pecl-memcache) – Memcache 驱动
Memcached (php-pecl-memcached) – Memcached 驱动
GD (php-gd) – GD图片类库扩展
XML (php-xml) –XML扩展
MBString (php-mbstring) –多字节字符串处理扩展
MCrypt (php-mcrypt) – MCrypt类库扩展

4-1. Fedora 17/16/15/14下使用命令:

yum --enablerepo=remi install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml


4-1. CentOS 6.2/5.8 and Red Hat (RHEL) 6.2/5.8下使用命令:

yum --enablerepo=remi,remi-test install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml


STEP5.停止httpd(Apache)服务器,启动Nginx HTTP服务及PHP-FPM

5-1. 停止httpd

/etc/init.d/httpd stop
## OR ##
service httpd stop


[b]5-2. 启动Nginx[/b]

/etc/init.d/nginx start
## OR ##
service nginx start


[b]5-3. 启动PHP_FPM[/b]

/etc/init.d/php-fpm start
## OR ##
service php-fpm start


【译者注】

到这一步,再按照步骤9配置一下防火墙开放80端口,打开浏览器输入http://localhost就可以看到nginx默认页面。

STEP6.设置Nginx&PHP-FPM开机自启动(同时禁止Httpd自启动)

[b]6-1. 禁止Httpd自启动[/b]

chkconfig httpd off


[b]5-2 设置Nginx&PHP-FPM开机自启动[/b]

chkconfig --add nginx
chkconfig --levels 235 nginx on
chkconfig --add php-fpm
chkconfig --levels 235 php-fpm on


STEP7. 配置Nginx&PHP-FPM

7-1. 创建网站目录

在这里我使用testsite.local作为站点目录,在实际应用用我们常常使用对应域名作为站点目录,如www.csdn.com

##创建public_html目录及logs日志目录
mkdir -p /srv/www/testsite.local/public_html
mkdir /srv/www/testsite.local/logs
##将以上目录的所有者修改为nginx
chown -R nginx:nginx /srv/www/testsite.local


配置一下日志目录

mkdir -p /srv/www/testsite.local/public_html
mkdir -p /var/log/nginx/testsite.local
chown -R nginx:nginx /srv/www/testsite.local
chown -R nginx:nginx /var/log/nginx


7-2. 创建并配置nginx虚拟主机目录

mkdir /etc/nginx/sites-available
mkdir /etc/nginx/sites-enabled


打开 /etc/nginx/nginx.conf 文件,在include /etc/nginx/conf.d/*.conf”行后(在http block内)加入以下代码

include /etc/nginx/sites-enabled/*;


7-3. 为站点testsite.local配置nginx虚拟主机

在/etc/nginx/sites-available/目录下添加testsite.local文件,其内容如下。

server {
    server_name testsite.local;
    access_log /srv/www/testsite.local/logs/access.log;
    error_log /srv/www/testsite.local/logs/error.log;
    root /srv/www/testsite.local/public_html;
 
    location / {
        index index.html index.htm index.php;
    }
 
    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/testsite.local/public_html$fastcgi_script_name;
    }
}


将testsite.local链接到/etc/nginx/sites-enabled下

cd /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/testsite.local
service nginx restart

把testsite.local这个"域名"加到/etc/hosts中

修改/etc/hosts文件

cd /etc/nginx/sites-enabled/
127.0.0.1               localhost.localdomain localhost testsite.local


STEP8. 测试

在/srv/www/testsite.local/public_html/下创建index.php文件,其内容如下

cd /etc/nginx/sites-enabled/
<?php 
    phpinfo();
?>


打开你的浏览器,访问http://testsite.local/



STEP9. 防火墙iptables配置

为Nginx Web Server开放80端口,修改/etc/sysconfig/iptables文件,加入如下内容

cd /etc/nginx/sites-enabled/
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

重启iptables防火墙

cd /etc/nginx/sites-enabled/
service iptables restart
## OR ##
/etc/init.d/iptables restart


********************************************

* 作者:叶文涛

* 链接:在CentOS/RHEL6.2/5.8,Fedora17/16上安装Nginx/PHP-FPM环境

* 源文:Install Nginx/PHP-FPM on Fedora 17/16, CentOS/RHEL 6.2/5.8

******************转载请注明来源 ***************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: