php-fpm的作用

來源:互聯網
上載者:User

nginx本身不能處理PHP,它只是個web伺服器,當接收到請求後,如果是php請求,則發給php解譯器處理,並把結果返回給用戶端。

nginx一般是把請求發fastcgi管理進程處理,fascgi管理進程選擇cgi子進程處理結果並返回被nginx

本文以php-fpm為例介紹如何使nginx支援PHP

一、編譯安裝php-fpm

什麼是PHP-FPM

PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在 http://php-fpm.org/download下載得到.

PHP-FPM其實是PHP原始碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP原始碼中,在編譯安裝PHP後才可以使用。

新版PHP已經整合php-fpm了,不再是第三方的包了,推薦使用。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制記憶體和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多優點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM,其它參數都是配置php的,具體選項含義可以查看這裡。

安裝前準備
centos下執行

yum -y install gcc automake autoconf libtool makeyum -y install gcc gcc-c++ glibcyum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel

新版php-fpm安裝(推薦安裝方式)

wget http://cn2.php.net/distributions/php-5.4.7.tar.gztar zvxf php-5.4.7.tar.gzcd php-5.4.7./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-mysqli --with-gd --with-jpeg-dirmake all install

舊版手動打補丁php-fpm安裝(舊版程式已經沒有了,大家新版的吧,這裡做個展示)
wget http://cn2.php .NET/get/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
tar zvxf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17
./configure --prefix=/usr/local/php -with-config-file-path=/usr/local/php/etc
-with- MySQL=/usr/local/mysql
-with-mysqli=/usr/local/mysql/bin/mysql_config -with-openssl -enable-fpm -enable-mbstring
-with-freetype-dir -with-jpeg-dir -with-png-dir -with-zlib-dir -with-libxml-dir=/usr -enable-xml
-with-mhash -with-mcrypt -enable-pcntl -enable-sockets  -with-bz2 -with-curl -with-curlwrappers
-enable-mbregex -with-gd -enable-gd-native-ttf -enable-zip -enable-soap -with-iconv -enable-bcmath
-enable-shmop -enable-sysvsem -enable-inline-optimization -with-ldap -with-ldap-sasl -enable-pdo
-with-pdo-mysql
make all install

以上兩種方式都可以安裝php-fpm,安裝後內容放在/usr/local/php目錄下

以上就完成了php-fpm的安裝。

下面是對php-fpm運行使用者進行設定

cd /usr/local/phpcp etc/php-fpm.conf.default etc/php-fpm.confvi etc/php-fpm.conf

修改
user = www-data
group = www-data

如果www-data使用者不存在,那麼先添加www-data使用者
groupadd www-data
useradd -g www-data www-data

二、編譯安裝nginx

然後按照http://www.nginx.cn/install 安裝nginx

三、修改nginx設定檔以支援php-fpm

nginx安裝完成後,修改nginx設定檔為,nginx.conf

其中server段增加如下配置,注意標紅內容配置,否則會出現No input file specified.錯誤

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

四、建立測試php檔案

建立php檔案

在/usr/local/nginx/html下建立index.php檔案,輸入如下內容

<?php    echo phpinfo();?>

五、啟動服務

啟動php-fpm和nginx

/usr/local/php/sbin/php-fpm #手動打補丁的啟動方式/usr/local/php/sbin/php-fpm startsudo /usr/local/nginx/nginx

php-fpm關閉重啟見文章結尾

六、瀏覽器訪問

訪問http://你的伺服器ip/index.php,皆可以見到php資訊了。

安裝php-fpm時可能遇到的錯誤:

1. php configure時出錯

configure: error: XML configuration could not be found

apt-get install libxml2 libxml2-dev  (ubuntu下)yum -y install libxml2 libxml2-devel(centos下)

2. Please reinstall the BZip2 distribution

wget http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gztar -zxvf bzip2-1.0.5.tar.gzcd bzip2-1.0.5makemake install

3. php的設定檔中有一行--with-mysql=/usr
安裝的時候提示:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore.

這是由於安裝mysql時沒有安裝mysql標頭檔,或者是路徑指定不正確,php找不到mysql的標頭檔引起的錯誤提示。
解決方案。
(1.) 查看你的系統有沒有安裝mysql header
find / -name mysql.h
如果有。請指定--with-mysql=/跟你的正常路徑。
如果沒有。請看下一步。
(2.)redhat安裝
rpm -ivh MySQL-devel-4.1.12-1.i386.rpm
(3.)ubuntu安裝
apt-get install libmysqlclient15-dev
(4.)最後一步php的配置選項添加--with-mysql=/usr即可。

4.No input file specified.

location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

5. 如果php configure時缺庫,可以先安裝庫(ubuntu下)

sudo apt-get install make bison flex gcc patch autoconf subversion locate
sudo apt-get install libxml2-dev libbz2-dev libpcre3-dev libssl-dev zlib1g-dev libmcrypt-dev libmhash-dev libmhash2 libcurl4-openssl-dev libpq-dev libpq5 libsyck0-dev

6. mcrypt.h not found. Please reinstall libmcrypt

apt-get install libmcrypt-dev

或者

cd /usr/local/src
wget http://softlayer.dl.sourceforge.Net/sourceforge/mcrypt/libmcrypt-2.5.8.tar.gz
tar -zxvf libmcrypt-2.5.8.tar.gz
cd /usr/local/src/libmcrypt-2.5.8
./configure --prefix=/usr/local
make
make install

7. php-fpm 5.4.7 如何關閉 重啟。

php 5.4.7 下的php-fpm 不再支援 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用訊號控制:

master進程可以理解以下訊號

INT, TERM 立刻終止 QUIT 平滑終止 USR1 重新開啟記錄檔 USR2 平滑重載所有worker進程並重新載入配置和二進位模組

樣本:

php-fpm 關閉:

kill -INT `cat /usr/local/php/var/run/php-fpm.pid`

php-fpm 重啟:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

查看php-fpm進程數:

ps aux | grep -c php-fpm

8.命令列下執行php,提示找不到命令

-bash: /usr/bin/php: No such file or directory

vi /etc/profile

在檔案底部增加一行配置
export PATH=/usr/local/php/bin:$PATH

儲存退出

source /etc/profile


原文:http://blog.csdn.net/lzz360/article/details/49100109

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.