Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux管理 >> Linux 設定

LAMP 源碼實戰:MySQL Cluster 集群編譯與部署全流程

本文以 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

一、準備 Java 執行環境

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

二、libevent 與 memcached

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

三、編譯安裝 MySQL Cluster

先確認系統存在 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 依賴庫編譯順序

先清掉舊的 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

五、httpd.conf 常用調整

  1. 中文優先:在 conf/extra/httpd-languages.conf 設 DefaultLanguage zh-CN,並把 LanguagePriority 的 zh-CN 提前。
  2. 虛擬主機:httpd.conf 中開啟 Include conf/extra/httpd-vhosts.conf 與 NameVirtualHost *:80,於 vhosts 檔內定義 DocumentRoot、ServerName、CustomLog、ErrorLog。
  3. 自訂 404:ErrorDocument 404 /404.html。
  4. 存取控制:Order deny,allow 搭配 Deny from all 做白名單式放行。
  5. 預設首頁:DirectoryIndex index.html index.php。
  6. 狀態監控:Include 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

七、編譯 PHP 5.3.10

./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 一旦編譯失敗,最乾淨的做法是刪掉源碼目錄重新解壓再編,反覆在同一棵樹上重試往往越弄越亂。

八、PHP 設定要點

cp /usr/src/lamp/php-5.3.10/php.ini-production /usr/local/lib/php.ini
  • session 存放:session.save_path = "/tmp/session",目錄需先建立並把屬主改為 Apache 執行用戶。
  • MySQL socket: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"
Copyright © Linux教程網 All Rights Reserved