nginx php-fpm安裝配置

www.nginx.cn/231.html


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下執行

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

舊版手動打補丁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運行用戶進行設置

修改
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-fpm和nginx

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

六、瀏覽器訪問

訪問http://你的服務器ip/index.php,皆可以見到php信息了。

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

1. php configure時出錯

configure: error: XML configuration could not be found

2. Please reinstall the BZip2 distribution

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

發佈了21 篇原創文章 · 獲贊 24 · 訪問量 43萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章