本文以 CentOS 6.1 x86 為作業環境,示範如何純源碼方式打造一套 LAMP 平台,並在 MySQL 側啟用 NDB Cluster 叢集引擎。
apr-1.4.6.tar.gz libevent-2.0.18-stable.tar.gz apr-iconv-1.2.1.tar.gz libiconv-1.13.tar.gz apr-util-1.4.1.tar.gz httpd-2.2.22.tar.gz pcre-7.9.tar.gz memcached-1.4.13.tar.gz mysql-cluster-gpl-7.0.5.tar.gz mysql-cluster-gpl-7.2.5.tar.gz jdk-6u18-ea-bin-b02-linux-i586-09_sep_2009.bin
yum remove java -y ./jdk-6u18-ea-bin-b02-linux-i586-09_sep_2009.bin mv jdk1.6.0_18/ /usr/java vim /etc/profile
在 /etc/profile 末尾追加:
JAVA_HOME=/usr/java CLASS_PATH=/usr/java/jre:/usr/java/lib PATH=$PATH:/usr/java/bin/
source /etc/profile
tar zxf libevent-2.0.18-stable.tar.gz cd libevent-2.0.18-stable ./configure make && make install echo /usr/local/lib >/etc/ld.so.conf.d/lamp.conf ldconfig tar zxf memcached-1.4.13.tar.gz cd memcached-1.4.13 ./configure --enable-sasl --enable-sasl-pwdb --with-libevent=/usr/local make && make install
先確認系統存在 uid=27、gid=27 的 mysql 使用者。若機器上裝過舊版 MySQL,需先停庫並清掉安裝目錄:
stop mysql rm -rf /usr/local/mysql
接著用 CMake 指定編譯選項,關鍵是打開 WITH_NDBCLUSTER_STORAGE_ENGINE:
cmake -DCMAKE_INSTALL_PREFIX:PATH=/usr/local/mysql \ -DMYSQL_DATADIR:PATH=/database \ -DMEMCACHED_HOME:PATH=/usr/local \ -DWITH_CLASSPATH:STRING=/usr/java \ -DWITH_EXTRA_CHARSETS:STRING=all \ -DWITH_INNOBASE_STORAGE_ENGINE:BOOL=ON \ -DWITH_NDBCLUSTER_STORAGE_ENGINE:BOOL=ON \ -DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON \ -DWITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON make && make install
安裝完成後佈置啟動腳本與設定檔:
cp /usr/src/lamp/mysql-cluster-gpl-7.2.5/support-files/mysql.server /etc/rc.d/init.d/mysql5 mkdir /usr/local/mysql/etc vim /usr/local/mysql/etc/my.cnf
[mysqld] user=mysql basedir=/usr/local/mysql datadir=/database socket=/var/run/mysqld/mysql5.socket pid-file=/var/run/mysqld/mysql5.pid general-log general-log-file=/var/log/mysqld/mysql5-access.log log-error=/var/log/mysqld/mysql5-error.log port=3306
先清掉舊的 Apache 目錄,並用 yum 補齊編譯所需的開發套件:
rm -rf /usr/local/apache2 yum install -y openssl* nss-devel bzip2-devel libzip-* net-snmp-*
依序編譯 apr、libiconv、apr-iconv、apr-util:
cd apr-1.4.6 && ./configure --disable-ipv6 --enable-threads && make && make install echo /usr/local/apr/lib >>/etc/ld.so.conf.d/lamp.conf ldconfig cd libiconv-1.13 && ./configure --enable-static && make && make install cd apr-iconv-1.2.1 && ./configure --with-apr=/usr/local/apr/bin/apr-1-config && make && make install ln -s /usr/src/lamp/apr-iconv-1.2.1 /usr/src/lamp/apr-util-1.4.1/apr-iconv cd apr-util-1.4.1 ./configure --with-apr=/usr/local/apr/bin/apr-1-config \ --with-apr-iconv=./apr-iconv --with-openssl=/usr \ --with-nss=/usr --with-mysql=/usr/local/mysql --with-iconv=/usr/local make && make install
PCRE 與 httpd 本體:
cd pcre-7.9 ./configure --enable-rebuild-chartables --enable-utf8 --enable-newline-is-any \ --enable-pcregrep-libz --enable-pcregrep-libbz2 make && make install cd httpd-2.2.22 ./configure --enable-modules=all --enable-mods-shared=all --enable-file-cache \ --enable-cache --enable-disk-cache --enable-mem-cache --enable-dumpio \ --enable-logio --enable-mime-magic --enable-expires --enable-headers \ --enable-unique-id --enable-proxy --enable-proxy-balancer --enable-proxy-http \ --enable-proxy-ajp --enable-proxy-connect --enable-ssl --enable-static-ab \ --enable-http --enable-info --enable-cgi --enable-vhost-alias --enable-so \ --enable-rewrite --with-apr=/usr/local/apr/bin/apr-1-config \ --with-apr-util=/usr/local/apr/bin/apu-1-config --with-pcre=/usr/local \ --with-ssl=/usr --with-mpm=prefork make && make install
conf/extra/httpd-languages.conf 設 DefaultLanguage zh-CN,並把 LanguagePriority 的 zh-CN 提前。Include conf/extra/httpd-vhosts.conf 與 NameVirtualHost *:80,於 vhosts 檔內定義 DocumentRoot、ServerName、CustomLog、ErrorLog。ErrorDocument 404 /404.html。Order deny,allow 搭配 Deny from all 做白名單式放行。DirectoryIndex index.html index.php。httpd-info.conf,在 <Location /server-status> 中限制來源網段,並開啟 ExtendedStatus On。freetype-2.4.9: ./configure && make && make install libpng-1.5.9: ./configure --with-zlib-prefix=/usr --with-pkgconfigdir=/usr/lib/pkgconfig/ jpegsrc.v8: ./configure && make && make install yum install -y imake xpm-3.4k: xmkmf -a && make && make install.man gd-2.0.36RC1: ./configure --with-libiconv-prefix=/usr/local --with-png=/usr/local \ --with-freetype=/usr/local --with-fontconfig=/usr --with-jpeg=/usr/local --with-xpm=/usr/local libmcrypt-2.5.8: ./configure --enable-static && make && make install mhash-0.9.9.9: ./configure && make && make install mcrypt-2.6.6: ./configure --with-libiconv-prefix=/usr/local --with-libmcrypt-prefix=/usr/local yum install -y libxml2 libcurl-devel
./configure --with-apxs2=/usr/local/apache2/bin/apxs --disable-ipv6 \ --with-libxml-dir=/usr --with-openssl=/usr --with-pcre-regex=/usr/local \ --with-zlib=/usr --with-bz2=/usr --enable-calendar --with-curl=/usr \ --enable-ftp --with-openssl-dir=/usr --with-gd=/usr/local \ --with-jpeg-dir=/usr/local --with-png-dir=/usr/local --with-zlib-dir=/usr \ --with-xpm-dir=/usr --with-freetype-dir=/usr/local \ --enable-gd-native-ttf --enable-gd-jis-conv --with-mhash=/usr/local \ --enable-mbstring --with-mcrypt=/usr/local --with-mysql=/usr/local/mysql \ --with-mysql-sock=/var/run/mysqld/mysql5.socket \ --with-mysqli=/usr/local/mysql/bin/mysql_config --with-snmp=/usr \ --enable-sockets --enable-zip make && make install
實務經驗:PHP 一旦編譯失敗,最乾淨的做法是刪掉源碼目錄重新解壓再編,反覆在同一棵樹上重試往往越弄越亂。
cp /usr/src/lamp/php-5.3.10/php.ini-production /usr/local/lib/php.ini
session.save_path = "/tmp/session",目錄需先建立並把屬主改為 Apache 執行用戶。mysql.default_socket 與 mysqli.default_socket 指向 /var/run/mysqld/mysql5.socket。upload_max_filesize = 2M。httpd.conf 中 LoadModule php5_module modules/libphp5.so 安裝時已自動寫入,但以下兩行需手動補上:
AddType application/x-httpd-php .php .phtml AddType application/x-httpd-php-source .phps
重啟 Apache 生效。
以 bcmath 為例,PHP 自帶擴充位於源碼 ext/ 目錄下:
cd ext/bcmath phpize ./configure make install vi /usr/local/lib/php.ini
extension_dir ="/usr/local/lib/php/extensions/no-debug-non-zts-20090626/" extension="bcmath.so"