摘要:
在64位Ubuntu下源碼安裝PHP5.3.8,並且配置Nginx為Web服務器。網上對於源碼編譯安裝PHP的文章已經很多了,google下一大摞,但是為啥我還這么寫上呢, 一來是因為工作需要,重裝了系統比較重要的就是配置環境,以前是一直用sudo apt-get的調過過程的安裝方式,所以趁着這個機會能自己嘗試下源碼方式安裝,體驗下過程。二來是因為這個是第一次源碼安裝,特此紀念下。
一、 准備
環境:64位 Ubuntu 11.10
服務器: Nginx 1.1.13
下載PHP源碼:本文使用5.3.8版本
二、 安裝
# tar zxvf php5.3.8.tar.gz
# cd php5.3.8
# ./configure
--prefix=/usr/local/php5
--with-config-file-path=/usr/local/php5/etc
--with-curl
--with-pear
--with-gd
--with-jpeg-dir
--with-png-dir
--with-zlib
--with-xpm-dir
--with-freetype-dir
--with-t1lib
--with-mcrypt
--with-mhash
--with-mysql
--with-mysqli
--with-pdo-mysql
--with-openssl
--with-xmlrpc
--with-xsl
--with-bz2
--with-gettext
--with-fpm-user=xiaoxiao
--with-fpm-group
--enable-fpm
--enable-exif
--enable-wddx
--enable-zip
--enable-bcmath
--enable-calendar
--enable-ftp
--enable-mbstring
--enable-soap
--enable-sockets
--enable-sqlite-utf8
--enable-shmop
--enable-dba
--enable-sysvmsg
--enable-sysvsem
--enable-sysvshm
出現得錯誤如下:
錯誤一:
configure: error: xml2-config not found. Please check your libxml2 installation.
而我已經安裝過了libxml2,但是還是有這個提示:
解決辦法:
# sudo apt-get install libxml2-dev
錯誤二:
configure: error: Please reinstall the BZip2 distribution
而我也已經安裝了bzip2,網上找到得解決方案都是需要安裝bzip2-dev,可是11.10里面沒有這個庫。
解決辦法:在網上找到bzip2-1.0.5.tar.gz,解壓,直接make ,sudo make install.(我使用的該源來自於http://ishare.iask.sina.com.cn/f/9769001.html)
錯誤三:
configure: error: Please reinstall the libcurl distribution -easy.h should be in <curl-dir>/include/curl/
解決辦法:
# sudo apt-get install libcurl4-gnutls-dev
錯誤四:
configure: error: jpeglib.h not found.
解決辦法:
# sudo apt-get install libjpeg-dev
錯誤五:
configure: error: png.h not found.
解決辦法:
# sudo apt-get install libpng-dev
錯誤六:
configure: error: libXpm.(a|so) not found.
解決辦法:
# sudo apt-get install libxpm-dev
錯誤七:
configure: error: freetype.h not found.
解決辦法:
# sudo apt-get install libfreetype6-dev
錯誤八:
configure: error: Your t1lib distribution is not installed correctly. Please reinstall it.
解決辦法:
# sudo apt-get install libt1-dev
錯誤九:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解決辦法:
# sudo apt-get install libmcrypt-dev
錯誤十:
configure: error: Cannot find MySQL header files under yes.
Note that the MySQL client library is not bundled anymore!
解決辦法:
# sudo apt-get install libmysql++-dev
錯誤十一:
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
解決辦法:
# sudo apt-get install libxslt1-dev
可見PHP源碼安裝之前需要先安裝這些依賴,詳細可見http://forum.ubuntu.org.cn/viewtopic.php?f=88&t=231159
如上錯誤都解決之后,再次./config....沒有錯誤之后,
# make
# sudo make install
三、 配置
php-fpm是一個php fastCGI得管理器,在PHP5.3.X已經集成了這個東西,所以我們可以省去自己裝這個得麻煩了,只要配置一下就可以。
先將相關得配置文件都拷貝到php5/etc目錄下(該目錄是我得配置文件存放目錄),主要涉及有:
1、php-fpm配置文件:php源目錄/sapi/fpm/php-fpm.conf.in
2、php-fpm啟動文件:php源目錄/sapi/fpm/init.d.php-fpm.in
3、php本身配置文件:php源目錄/php.ini-development php源目錄/php.ini-production
如果需要保存一份原始得文件,那么就將以上文件另外cp一份出來,如果是不保存的,那么以上文件cp到etc下就直接更名為自己需要的。我是保留了一份在etc目錄下。
現在php5/etc目錄下有如下格式配置文件:
php-fpm.conf init.d.php-fpm php.ini pear.conf(安裝完成之后就有該文件)
首先:配置php-fpm.conf文件:
1、pid配置
;pid=run/php-fpm.pid
將前面得;號去掉,可以后面得路徑可以根據自己得需求配置一個路徑。
2、log配置
;error-log=log/php-fpm.log
log的配置可以根據自己的需求是否開啟及存放位置
3、listen 配置
listen=127.0.0.1:9000
這個配置主要注意端口不要沖突,我在配置這個的時候采用的是網上說的socket方式,配置如下:
listen=/usr/local/php5/var/run/php-fpm.socket
然后在/usr/local/php5/var/run/該目錄下建立該文件,注意權限需要
其他項的配置可以根據自己的需求更改,配置項的說明在這可以看到:http://www.php.net/manual/en/install.fpm.configuration.php。同時在github上有一份比較完整可用的配置:https://github.com/vladgh/VladGh.com/blob/master/php-fpm.conf 可以作為參考。
其次:配置init.d.php-fpm
改文件作為啟動文件,需要配置的就是在開頭位置的幾個變量值,包括:
1、php_fpm_BIN:指向安裝好的php目錄/sbin/php-fpm
2、php_fpm_CONF:指向安裝好的php目錄/etc/php-fpm.conf {如果該配置文件你不是叫這個名字,跟這里應該為你真是得配置名}
3、php_fpm_PID:這里指向php-fpm的pid存放路徑。
完成之后,將該文件拷貝到/etc/init.d目錄下,本文是采用建立軟鏈接的方式:
# sudo ln -s /usr/local/php5/etc/init.d.php-fpm /etc/init.d/php-fpm
# sudo update-rc.d -f php-fpm defaults
然后:配置php
我是拿的php.ini- development,幾乎不用配置
最后:配置nginx.conf
完成上面之后,需要配置nginx可運行php,如下配置:
Server{}下:
1、root html: 該項配置指明腳本程序的目錄,本文是設置為root /home/www
2、識別.php文件:
location / {
index index.html index.htm index.php;
}
3、解析php腳本:這塊只要將注釋”#”去掉就好。
location ~ \.php$ {
fastcgi_pass unix:/usr/local/php5/var/run/php-fpm.socket; #127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
注意:fastcgi_pass 這里,后面跟php-fpm.conf中配置得listen一致,在listen中我是配置了socket方式,所以這里也這樣配。
啟動服務:
# sudo /etc/init.d/nginx restart
# sudo /etc/init.d/php-fpm start
在網頁中輸入localhost,就可以看到啦:
如果有需要,可以添加為系統路徑中:
# sudo vim /etc/bash.bashrc
//在最后添加如下
if [ -d "/usr/local/php5/bin" ] && [ -d "/usr/local/php5/sbin" ]; then
PATH="$PATH:/usr/local/php5/bin:/usr/local/php5/sbin"
fi
四、虛擬機配置
nginx的虛擬機配置其實挺簡單的,打開nginx.conf 在http{}中加入 include /usr/local/nginx/conf/virtual_sites.conf;(也可以是文件夾下的文件如sites/*.conf)
然后在virtual_sites.conf中配置需要得虛擬機:
server {
listen 80;
server_name p9.xxx;
root /home/www/p9/www;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/usr/local/php5/var/run/php-fpm.socket;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
}
然后綁定p9.xxx到hosts中即可。一份可用的虛擬機配置也許是這樣的http://wiki.nginx.org/NginxVirtualHostExample
如果你覺得好,可以點此下載
-------- 同樣感謝萬能的互聯網,讓我時刻可以站在“巨人”的肩膀上,讓我省去很多荊棘坎坷。
-------- 特此紀念,2012年1月31日 by 肖肖