一、需要的軟件包
httpd-2.4.4.tar.bz2
mysql-5.5.28-linux2.6-i686.tar.gz
php-5.4.13.tar.bz2
apr-1.4.6.tar.bz2
apr-util-1.5.2.tar.bz2
xcache-3.0.1.tar.bz2
確保開發環境已經安裝上 Development Libraries和Development Tools 軟件包組,如果沒有安裝要使用 # yum groupinstall “Development Libraries”“Development Tools”命令安裝開發環境。
二、手動編譯安裝httpd軟件包
因爲httpd-2.4.4需要新版本的apr和apr-util,我們要先編譯安裝apr和apr-util。
1、編譯安裝apr-1.4.6
# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install
2、編譯安裝apr-util-1.5.2
# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3、編譯安裝httpd-2.4.4
編譯過程需要依賴pcre-devel包,pcre-devel系統磁盤自帶的有,我們可以用yum命令安裝。
# yum install pcre-devel
安裝好後解壓httpd源碼包,並編譯安裝
# tar xf httpd-2.4.4.tar.bz2
# cd httpd-2.4.4
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-module=most --enable-mpm-shared=most --with-mpm=event
# make && make install
./configure選項的意義:
--prefix=/...:安裝路徑
--with-apr=/...:依賴包的路徑
--sysconfdir=/...:配置文件安裝路徑
--enable-modules=most:啓用大多數模塊
--enable-mods-shared=MODULE-LIST:是否啓用共享模塊(all,most,few,reallyall)
--enable-so:支持動態共享模塊(默認支持,如果不支持httpd無法和php進行交互)
--enable-ssl:啓用ssl功能(不然無法啓用https功能)
--enable-cgi:以cgi的方式進行交互
--enable-cgid:在(worker,event)啓用
--enable-mpms-shared:構建 MPM 爲動態模塊允許通過修改LoadModule指令內容來改變MPM
--with-mpm:默認爲MPM
4、修改httpd的主配置文件,設置其pid文件路徑
# vim /etc/httpd/httpd.conf
添加如下內容:
PidFile "/var/run/httpd.pid"
5、爲httpd提供SysV服務腳本/etc/rc.d/init.d/httpd
# vim /etc/c.d/init.d/httpd
添加內容如下:
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
給予此腳本執行權限:
# vim chmod +x /etc/rc.d/init.d/httpd
將httpd加入服務列表
# chkconfig --add httpd
# chkconfig httpd on
修改PATH環境變量
# vim /etc/profile.d/httpd.sh
添加內容如下:
export PATH=$PATH:/usr/local/apache/bin
Apache服務到這裏就編譯安裝完成了,可以啓動服務測試。
三、編譯安裝mysql-5.5.28
1、提供一個存放數據的文件系統,也就是新建一個邏輯卷,將其掛載至某一目錄下,作爲數據存放目錄。創建一個新的分區(這裏新分區大小爲5G),將其文件系統屬性調整爲8e。保存並用partprobe命令通知內核重讀分區。然後就可以開始創建邏輯捲了。
# pvcreate /dev/sda5 創建物理卷
# vgcreate mysql /dev/sda5 創建卷組,名爲mysql
# lvcreate -n lv_mysql -L 3G mysql 創建邏輯卷,大小爲3G(注意:邏輯卷大小一定要小於分區大小),邏輯卷名爲lv_mysql
2、新建mysql用戶,以安全方式運行進程
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data
3、編譯安裝並初始化mysql
# tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.28-linux2.6-i686 mysql
# cd mysql
# chown -R mysql:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root . #將屬主改爲root用戶是防止mysql用戶密碼泄漏後,數據庫中文件被修改
4、爲mysql提供主配置文件:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
並修改此文件中thread_concurrency的值爲你的CPU個數乘以2:
thread_concurrency = 2
另外還需要添加如下行指定mysql數據文件的存放位置:
datadir = /mydata/data
5、爲mysql提供sysv服務腳本:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
添加至服務列表:
# chkconfig --add mysqld
# chkconfig mysqld on
爲了方便以後使用man命令,我們在這裏要將mysql的開發組件導出給系統。
6、輸出mysql的man手冊至man命令的查找路徑:
編輯/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
7、輸出mysql的頭文件至系統頭文件路徑/usr/include:
這可以通過簡單的創建鏈接實現:
# ln -sv /usr/local/mysql/include /usr/include/mysql
8、輸出mysql的庫文件給系統庫查找路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而後讓系統重新載入系統庫:
# ldconfig
9、修改PATH環境變量,讓系統可以直接使用mysql的相關命令。
# vim /etc/profile.d/mysql.sh
添加內容如下:
export PATH=$PATH:/usr/local/mysql/bin
這樣mysql服務也編譯安裝完成了,下面我們就來編譯安裝php。編譯安裝php的時候需要等待很長的時間:
四、編譯安裝php
應爲./configre 選項中啓用了加密模塊,我們要安裝四個rpm包
libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm
mhash-0.9.2-6.el5.i386.rpm
mhash-devel-0.9.2-6.el5.i386.rpm
使用rpm -ivh *rpm就可以安裝。
1、編譯安裝php-5.4.13
# tar xf php-5.4.13.tar.bz2
# cd php-5.4.13
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
./configure選項作用:
--enable-maintainer-zts:支持apache的worker或event這兩個MPM
其它選項功能和mysql編譯安裝時一樣
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make
# make test #用來測試使用,這一步驟可以省略。
# make intall
爲php提供配置文件:
# cp php.ini-production /etc/php.ini
2、編輯apache配置文件httpd.conf,以apache支持php
# vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html
修改爲:
DirectoryIndex index.php index.html
3、提供php文件
# cd /usr/local/apache/htdocs
# mv index.html index.php
# vim index.php
<html><body><h1>It works!</h1></body></html>
<?php
$conn=mysql_connect('localhost','root','');
if ($conn)
echo "Success...";
else
echo "Failure...";
phpinfo();
?>
五、安裝xcache,爲php加速:
1、安裝
# tar xf xcache-3.0.1.tar.gz
# cd xcache-3.0.1
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
說明:xcache.ini文件在xcache的源碼目錄中。
編輯/etc/php.d/xcache.ini,找到zend_extension開頭的行,修改爲如下行:
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
要確保新增的這一行在最前邊。
到這裏,手動編譯安裝LAMP就結束了。如果訪問php網頁的時候出錯原因有:
1、SELinux沒有調整到permissive,編輯/etc/selinux/config文件,將SELINUX後改爲permissive即可;
2、php模塊沒有裝載上,使用httpd -M 命令就可以查看裝載的模塊,看其中是否有php模塊,模塊總數爲27;沒有的話重新編譯安裝一次就可以了。