2012年9月11日 星期二

從源碼編譯安裝 PHP 5 (Debian/Ubuntu 為例)


source:  WangYan Blog http://wangyan.org/blog/install-php-from-source.html
PHP 是一種在服務器端執行的腳本語言,主要用來製作動態網頁以提供瀏覽器讀取,但也可以用來開發命令行腳本程序和用戶端的GUI應用程序。PHP 可以在許多的不同種的服務器、操作系統、平台上運行,也可以和許多數據庫系統結合。

一、準備工作

系統版本:Ubuntu-10.10-Server-I386
OpenSSL 版本:openssl-1.0.0c (安裝方法
OpenSSH 版本:openssh-5.6p1 (安裝方法
Apache 版本:httpd-2.2.17 (安裝方法
MySQL 5 版本:mysql-5.1.53-linux-i686-glibc23 (安裝方法

二、編譯安裝 GD

GD不是必須的,如果要安裝GD,那麼 jpeg 庫,png 庫,zlib 庫則是必須的,其餘的選擇安裝。

2.1、編譯安裝 Zlib

1
2
3
4
5
wget http://zlib.net/zlib-1.2.5.tar.gz
tar -zxf zlib-1.2.5.tar.gz
cd zlib-1.2.5/
./configure --prefix=/usr/local
make && make install

2.2、編譯安裝 PNG

1
2
3
4
5
wget ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng-1.4.4.tar.gz
tar -zxf libpng-1.4.4.tar.gz
cd libpng-1.4.4/
./configure --prefix=/usr/local
make && make install

2.3、編譯安裝 JPEG

1
2
3
4
5
wget http://www.ijg.org/files/jpegsrc.v8b.tar.gz
tar -zxf jpegsrc.v8b.tar.gz
cd jpeg-8b/
./configure --prefix=/usr/local
make && make install

2.4、編譯安裝 Libiconv

1
2
3
4
5
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz
tar -zxf libiconv-1.13.1.tar.gz 
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make && make install

2.5、編譯安裝 FreeType

1
2
3
4
5
wget http://nchc.dl.sourceforge.net/project/freetype/freetype2/2.4.3/freetype-2.4.3.tar.bz2
tar -jxf freetype-2.4.3.tar.bz2 
cd freetype-2.4.3/
./configure --prefix=/usr/local
make && make install

2.6、編譯安裝 GD

1
2
3
4
5
wget http://www.libgd.org/releases/gd-2.0.36RC1.tar.gz
tar -zxf gd-2.0.36RC1.tar.gz
cd gd-2.0.36RC1/
./configure --prefix=/usr/local --with-libiconv-prefix=/usr/local --with-png=/usr/local --with-freetype=/usr/local --with-jpeg=/usr/local
make && make install

三、編譯安裝 PHP 5

3.1、編譯安裝 LibXML

1
2
3
4
5
wget http://xmlsoft.org/sources/libxml2-2.7.8.tar.gz
tar -zxf libxml2-2.7.8.tar.gz
cd libxml2-2.7.8
./configure --prefix=/usr/local
make && make install

3.2、編譯安裝 Mhash

1
2
3
4
5
wget http://nchc.dl.sourceforge.net/project/mhash/mhash/0.9.9.9/mhash-0.9.9.9.tar.bz2
tar -jxf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9/
./configure --prefix=/usr/local
make && make install

3.3、編譯安裝 LibMcrypt & Mcrypt

1
2
3
4
5
wget http://nchc.dl.sourceforge.net/project/mcrypt/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz
tar -zxf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure --prefix=/usr/local
make && make install
如果要編譯安裝 Mcrypt,那麼必須先安裝 Mhash 和 LibMcrypt,否則會報錯。
1
2
3
4
5
6
wget http://nchc.dl.sourceforge.net/project/mcrypt/MCrypt/2.6.8/mcrypt-2.6.8.tar.gz
tar -zxf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
ln -s   /usr/local/bin/libmcrypt-config   /usr/local/bin
LD_LIBRARY_PATH=/usr/local/lib ./configure --prefix=/usr/local
make && make install
編譯安裝 Mcrypt,配置時切勿遺漏了在環境變量 LD_LIBRARY_PATH 中指明庫的搜索路徑 ,或者按一下方法在 /etc/ld.so.conf 動態鏈接庫文件中添加庫的搜索路徑。(資料1資料2)。
1
2
3
4
#編輯,添加共享庫路徑。
sudo vim /etc/ld.so.conf 
添加一行:/usr/local/lib
sudo ldconfig //更新ld.so.cache

3.4、安裝編譯 Curl

1
2
3
4
5
wget http://www.libssh2.org/download/libssh2-1.2.7.tar.gz
tar -zxf libssh2-1.2.7.tar.gz
cd libssh2-1.2.7/
./configure --prefix=/usr/local
make && make install
1
2
3
4
5
wget http://curl.haxx.se/download/curl-7.21.2.tar.gz
tar -zxf curl-7.21.2.tar.gz
cd curl-7.21.2/
./configure --prefix=/usr/local --with-ssl=/usr/local/ssl --with-libssh2=/usr/local
make && make install

3.5、編譯安裝 php5.3.3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
wget http://cn.php.net/distributions/php-5.3.3.tar.gz
tar -zxf php-5.3.3.tar.gz
cd php-5.3.3/
./configure --prefix=/usr/local/php \
--with-apxs2=/usr/local/apache/bin/apxs \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-jpeg-dir=/usr/local \
--with-png-dir=/usr/local \
--with-zlib-dir=/usr/local \
--with-freetype-dir=/usr/local \
--with-iconv-dir=/usr/local \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gd=/usr/local \
--with-libxml-dir=/usr/local \
--with-mhash=/usr/local \
--with-mcrypt=/usr/local \
--with-openssl=/usr/local \
--with-curl=/usr/local \
--with-curlwrappers \
--enable-bcmath \
--enable-wddx \
--enable-calendar \
--enable-mbstring \
--enable-ftp \
--enable-zip \
--enable-sockets
make && make install

四、將Apache與PHP5相結合

1
2
cp php.ini-production /usr/local/php/lib/php.ini
vim /usr/local/apache/conf/httpd.conf
4.1、設置php文件後綴
查找"AddType application/x-gzip .gz .tgz",在下面添加:
1
AddType application/x-httpd-php .php
4.2、設置apache的默認文件名次序
查找"index.html",後面添加「index.php」
1
index.htm index.html  index.php
4.3、開啟Apache Rewrite 模塊
查找"Options FileInfo" 將"AllowOverride none"
改為 "AllowOverride all" //

4.4、關閉非必須的Apache模塊

因為前期我將所有模塊都編譯了,所以現在要關閉暫不需要的模塊以節省資源。必須開啟的模塊如下:
1
2
3
4
5
6
7
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule mime_module modules/mod_mime.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule php5_module        modules/libphp5.so

4.5、編譯安裝 ZendOptimizer

官方說明最新版的ZendOptimizer-3.3.9只適用於php5.2,不過你用在php5.3倒也不至於出錯。
1
2
3
4
wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
tar -zxf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
cd ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp
cp ZendOptimizer.so /usr/local/lib
1
2
3
4
vim /usr/local/php/lib/php.ini
[ZendOptimizer]
zend_optimizer.optimization_level=15
zend_extension=/usr/local/lib/ZendOptimizer.so

4.6、重啟Apache服務器

1
httpd -k restart

五、添加 php5 擴展

服務器運行一段時間後,可能需求額外添加某個擴展,這就需要在不重新編譯PHP的情況下獨立添加擴展。下面以Memcache為例說明。

4.1、編譯安裝Memcache服務器端 (可選)

因為Memcache用到了libevent這個庫用於Socket的處理,所以先安裝libevent。(Memcache是什麼?Memcache如何安裝?
1
2
3
4
5
wget http://www.monkey.org/~provos/libevent-1.4.14b-stable.tar.gz
tar -zxf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable/
./configure --prefix=/usr/local
make && sudo make install
1
2
3
4
5
wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
tar -zxf memcached-1.4.5.tar.gz
cd memcached-1.4.5/
./configure --prefix=/usr/local --with-libevent=/usr/local
make && sudo make install

4.2、編譯安裝Memcache的PHP擴展

添加擴展一般我們只需到php源碼ext目錄下找即可,但因為php沒有自帶Memcache擴展,所以你需要另外下載。
1
2
3
4
5
6
wget http://pecl.php.net/get/memcache-3.0.5.tgz
tar -zxf memcache-3.0.5.tgz
cd memcache-3.0.5/
sudo /usr/local/php5/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php5/bin/php-config
make && sudo make install
安裝完後會看倒下面提示:Installing shared extensions:/usr/local/php5/lib/php/extensions/no-debug-non-zts-20090626/
運行memcached: "/usr/local/bin/memcached",新建php文件檢測是否成功:
1
2
3
4
5
6
7
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;
?>

4.3、修改php.ini文件

1
sudo gvim /usr/local/php/lib/php.ini
將 extension_dir = "./" 改為
1
2
extension_dir ="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension=memcache.so
保存退出後,用以下命令檢查php配置文件是否正確,然後重啟Apache服務器
1
2
/usr/local/php5/bin/php -v
sudo httpd -k restart

4.3、編譯安裝 eaccelerator

eaccelerator安裝方法同上面基本一致(詳細資料
1
2
3
4
5
6
7
wget http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2
tar jxf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
sudo /usr/local/php5/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make && sudo make install
sudo mkdir /usr/local/cache
修改php.ini文件,在[ZendOptimizer]前面加入下面代碼,然後重啟apache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[eaccelerator]
extension='/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so'
eaccelerator.shm_size='32'
eaccelerator.cache_dir='/usr/local/cache'
eaccelerator.enable='1'
eaccelerator.optimizer='1'
eaccelerator.check_mtime='1'
eaccelerator.debug='0'
eaccelerator.filter=''
eaccelerator.shm_max='0'
eaccelerator.shm_ttl='3600'
eaccelerator.shm_prune_period='3600'
eaccelerator.shm_only='0'
eaccelerator.compress='1'
eaccelerator.compress_level='9'

五、安裝 phpmyadmin

5.1、安裝 phpMyAdmin-3.3.8

1
2
3
4
5
wget http://nchc.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/3.3.8.1/phpMyAdmin-3.3.8.1-all-languages.tar.gz
tar -zxf phpMyAdmin-3.3.8.1-all-languages.tar.gz -C /usr/local/apache/htdocs/
mv /usr/local/apache/htdocs/phpMyAdmin-3.3.8.1-all-languages/ /usr/local/apache/htdocs/phpMyAdmin
cd /usr/local/apache/htdocs/phpMyAdmin
cp libraries/config.default.php ./config.inc.php

5.2、激活鏈接表的附加功能

不想出現「鏈接表的附加功能尚未激活。要查出原因,請點擊此處「這樣提示就照做吧。
5.2.1.先建立一個名為phpmyadmin的數據庫
1
2
/usr/local/mysql/bin/mysql -u root -p 
CREATE DATABASE `phpmyadmin`;
5.2.2.然後把目錄下scripts/create_tables.sql導入到這個庫
1
2
3
/usr/local/mysql/bin/mysql -u root -p 
use phpmyadmin
source /usr/local/apache/htdocs/phpMyAdmin/scripts/create_tables.sql
3.編輯config.inc.php配置文件.
1
vim /usr/local/apache/htdocs/phpMyAdmin/config.inc.php
1
2
3
4
5
6
7
8
9
$cfg['Servers'][$i]['pmadb']           = ''; 
$cfg['Servers'][$i]['bookmarktable']   = ''; 
$cfg['Servers'][$i]['relation']        = ''; 
$cfg['Servers'][$i]['table_info']      = ''; 
$cfg['Servers'][$i]['table_coords']    = '';  
$cfg['Servers'][$i]['pdf_pages']       = ''; 
$cfg['Servers'][$i]['column_info']     = '';  
$cfg['Servers'][$i]['history']         = ''; 
$cfg['Servers'][$i]['tracking']        = '';
將上面語句改成下面的
1
2
3
4
5
6
7
8
9
10
$cfg['Servers'][$i]['pmadb']           = 'phpmyadmin'; 
$cfg['Servers'][$i]['bookmarktable']   = 'pma_bookmark'; 
$cfg['Servers'][$i]['relation']        = 'pma_relation';
$cfg['Servers'][$i]['table_info']      = 'pma_table_info';
$cfg['Servers'][$i]['table_coords']    = 'pma_table_coords'; 
$cfg['Servers'][$i]['pdf_pages']       = 'pma_pdf_pages'; 
$cfg['Servers'][$i]['column_info']     = 'pma_column_info';
$cfg['Servers'][$i]['history']         = 'pma_history'; 
$cfg['Servers'][$i]['designer_coords'] = 'pma_designer_coords';
$cfg['Servers'][$i]['tracking']        = 'pma_tracking';

5.3、配置文件設置短語密碼

同理,不想出現「配置文件現在需要一個短語密碼。「這樣提示,就照做吧。
編輯config.inc.php配置文件. 修改$cfg['blowfish_secret']的值,其中'abc'為任意值
1
$cfg['blowfish_secret'] = 'abc';

五、結語


至此,基本的LAMP環境搭建結束了,上述方法同樣適用於ubuntu server 和 Debian 。需要說的是目前只是進行了初步的配置,沒做任何安全配置,實際部署還請參考其他相關文章。




沒有留言: