爛泥:php5.6源碼安裝及php-fpm配置

來源:互聯網
上載者:User

標籤:microsoft

本文首發於爛泥行天下。

LNMP環境的搭建中,現在只有php沒有源碼安裝過。這篇文章就把這個介紹下。

注意本篇文章使用的centos 6.5 64bit。

登陸centos下載php5.6的安裝包。php的軟體包可以去國內的souhu鏡像網站去下載。

wget http://mirrors.sohu.com/php/php-5.6.2.tar.gz

650) this.width=650;" title="clip_image001[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image001[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931455TB1n.png" width="549" height="220" />

在安裝之前,我們需要安裝php5.6編譯時間所依賴的軟體包。如下:

yum -y install gcc gcc-c++ libxml2 libxml2-devel

650) this.width=650;" title="clip_image002[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image002[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931456yzCh.png" width="548" height="151" />

650) this.width=650;" title="clip_image003[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image003[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314569zbM.png" width="344" height="238" />

以上軟體包安裝完畢後,我們現在來解壓剛剛下載的php5.6源碼包。使用如下命令:

tar -xf php-5.6.2.tar.gz

650) this.width=650;" title="clip_image004[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image004[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931457Hiqf.png" width="522" height="283" />

進入php5.6的解壓目錄,現在開始配置php5.6,使用如下命令:

cd php-5.6.2

./configure --enable-fpm --enable-mbstring --with-mysql=/usr/local/mysql

650) this.width=650;" title="clip_image005[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image005[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931457yDCn.png" width="702" height="190" />

如果沒有錯誤的情況下,配置完畢後。系統會出現如下的提示資訊:

650) this.width=650;" title="clip_image006[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image006[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931457fskc.png" width="414" height="347" />

注意上述命令中--enable-fpm的作用是開啟php的fastcgi功能即開啟php-fpm功能,--with-mysql=/usr/local/mysql是啟用php支援mysql的功能,/usr/local/mysql是mysql資料庫的安裝路徑。

--enable-mbstring表示啟用mbstring模組mbstring模組的主要作用在於檢測和轉換編碼,提供對應的多位元組操作的字串函數。目前php內部的編碼只支援ISO-8859-*、EUC-JP、UTF-8,其他的編碼的語言是沒辦法在php程式上正確顯示的,所以我們要啟用mbstring模組。

同時我們也只是簡單的開啟和擴充php的一部分功能,其他需要的功能,請自行添加。

配置完畢後,我們現在來編譯php,使用make命令,如下:

make

650) this.width=650;" title="clip_image007[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image007[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931458LSfp.png" width="571" height="201" />

php編譯的時間比較長,根據機器的效能不同需要等待10-20分鐘左右。編譯完畢後的,如下:

650) this.width=650;" title="clip_image008[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image008[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314583ALA.png" width="583" height="274" />

編譯完畢後,我們現在開始安裝php,使用如下命令:

make install

650) this.width=650;" title="clip_image009[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image009[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931458sgB6.png" width="600" height="346" />

安裝完畢後,我們可以通過php –v命令查看是否安裝成功。如下:

php –v

650) this.width=650;" title="clip_image010[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image010[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931459HmID.png" width="501" height="116" />

通過這樣安裝完畢後,你會發現在/usr/local/lib目錄下沒有php.ini檔案。在這我們就先複製php安裝檔案提供的模版,如下:

cp php.ini-production /usr/local/lib/php.ini

650) this.width=650;" title="clip_image011[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image011[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931459XtXN.png" width="579" height="118" />

注意php.ini檔案一般在/usr/local/lib/和/etc目錄下。有關php安裝完畢後,沒有php.ini檔案的,我們再另外一篇文章再介紹。

不要以為到這php的安裝就完畢了,LNMP環境中的nginx是不支援php的,需要通過fastcgi來處理有關php的請求。而php需要php-fpm這個組件來支援。

在php5.3.3以前的版本php-fpm是以一個補丁包的形式存在的,而php5.3.3以後的php-fpm只需要在安裝php-fpm開啟這個功能即可。這個也就是前邊,我們再配置php使用到的那個命令--enable-fpm。

php-fpm功能開啟後,我們還需要配置php-fpm。其實php-fpm的設定檔在安裝php時,已經為我們提供了一個設定檔的模版。該模版為/usr/local/etc/php-fpm.conf.default,如下:

more /usr/local/etc/php-fpm.conf.default

650) this.width=650;" title="clip_image012[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image012[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314594jSX.png" width="583" height="280" />

我們現在只需要複製一份該檔案,並重新命名為php-fpm.conf,如下:

cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf

650) this.width=650;" title="clip_image013[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image013[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314602ggh.png" width="696" height="59" />

為了讓php-fpm已服務的形式啟動。我們需要複製php安裝目錄下/sapi/fpm/init.d.php-fpm檔案。如下:

cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

650) this.width=650;" title="clip_image014[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image014[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931460WBjF.png" width="550" height="100" />

通過,我們也可以很清楚的看到php-fpm檔案目前沒有執行許可權。賦予php-fpm執行許可權,並啟動php-fpm,如下:

chmod a+x /etc/init.d/php-fpm

/etc/init.d/php-fpm start

netstat -tunlp |grep 9000

650) this.width=650;" title="clip_image015[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image015[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931460gW5C.png" width="735" height="144" />

通過,我們可以很明顯的看到php-fpm已經正常啟動。

注意php-fpm預設監聽的是9000連接埠。

現在再來配置nginx,使其支援php,如下:

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;

}

650) this.width=650;" title="clip_image016[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image016[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931461nNBL.png" width="571" height="498" />

然後在nginx的網站根目錄下建立index.php檔案內容如下:

vi /usr/local/nginx/html/index.php

<?php phpinfo(); ?>

650) this.width=650;" title="clip_image017[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image017[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314619z7i.png" width="473" height="91" />

現在來啟動nginx,如下:

/usr/local/nginx/sbin/nginx

netstat -tunlp |grep 80

650) this.width=650;" title="clip_image018[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image018[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314618sKz.png" width="713" height="110" />

nginx啟動完畢後,現在來訪問網站,如下:

650) this.width=650;" title="clip_image019[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image019[4]" src="http://img1.51cto.com/attachment/201411/14/526870_14159314629tYZ.png" width="1249" height="405" />

650) this.width=650;" title="clip_image020[4]" style="border-left- 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px" border="0" alt="clip_image020[4]" src="http://img1.51cto.com/attachment/201411/14/526870_1415931462cJUr.png" width="1017" height="283" />

通過,我們可以很明顯的看到。php目前已經安裝成功,並且也成功的支援mysql資料庫。同時也與nginx整合完畢。

至此。我們的php5.6的安裝到此結束。

爛泥:php5.6源碼安裝及php-fpm配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.