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

以前我寫過一篇文章《在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



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



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

5-2.  啓動Nginx

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

5-3.  啓動PHP_FPM

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

【譯者注

到這一步,再按照步驟9配置一下防火牆開放80端口,打開瀏覽器輸入http://localhost就可以看到nginx默認頁面。


STEP6.設置Nginx&PHP-FPM開機自啓動(同時禁止Httpd自啓動

6-1.  禁止Httpd自啓動

chkconfig httpd off

5-2  設置Nginx&PHP-FPM開機自啓動

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

******************轉載請註明來源 ***************


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章