轉自:http://www.codingthink.com/c/20120203/201202030006062.html
自己在 ubuntu下編譯安裝php 發現很麻煩,所以把他人的東西搬來。
摘要:
在64位Ubuntu下源碼安裝PHP5.3.8,並且配置Nginx為Web伺服器。網上對於源碼編譯安裝PHP的文章已經很多了,google下一大摞,但是為啥我還這麼寫上呢, 一來是因為工作需要,重裝了系統比較重要的就是配置環境,以前是一直用sudo apt-get的調過過程的安裝方式,所以趁著這個機會能自己嘗試下源碼方式安裝,體驗下過程。二來是因為這個是第一次源碼安裝,特此紀念下。
一、 準備
環境:64位 Ubuntu 11.10
伺服器: Nginx 1.1.13
下載PHP源碼:本文使用5.3.8版本
apt-get install build-essential
需要用到的其他軟體包
apt-get install libxml2-dev libcurl4-openssl-dev libbz2-dev libpng-dev libfreetype6-dev libmcrypt-dev libmhash-dev libmysqlclient-dev libxslt1-dev libjpeg62-dev libltdl-dev
二、 安裝
# 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