你們以前聽說過LAMP嗎? 我會在這裏簡單的描述一下其工作原理,以及實現的過程。LAMP是一種平臺,也是一種架構。我們瞭解,在web服務器中,有一個httpd進程,但這個進程僅能解釋靜態內容(網頁文件,圖片,各種CSS樣式文件等)。若有一個動態文件,例:index.php ,則httpd無法執行,因此就需要使用php解釋器實現這項功能。讓httpd與php交互,將動態內容調用php解釋器解析爲靜態內容交給httpd進程。而我們大多數請求的內容會有許多數據,因此,還需要一個數據庫服務器來實現數據的管理。簡單說,LAMP平臺即爲: httpd+php+mysql,三臺服務器組成的平臺。
我們這裏演示完全手動編譯配置LAMP,編譯的版本爲httpd 2.4.4 + mysql-5.5.28 + php-5.4.13,可以在官方網站下載其各個軟件包(使用通用二進制安裝mysql)。
一、準備軟件源碼包
1、在編譯安裝http之前需要先將apr和apr-util升級,升級的兩種方式:rpm包直接升級或編譯安裝新版本的apr,apr-util。我在這裏下載編譯安裝新版本的源碼apr-1.4.6.tar.bz2, apr-util-1.5.2.tar.bz2。
2、下載http的源碼包,httpd-2.4.4.tar.bz2
3、下載mysql的源碼包,mysql-5.5.28-linux2.6-i686.tar.gz
4、下載php的源碼包,php-5.4.13.tar.bz2 (若需要php支持mcrypt擴展,則還需要下載安裝兩個rpm包:libmcrypt-2.5.7-5.el5.i386.rpm,libmcrypt-devel-2.5.7-5.el5.i386.rpm。我這裏直接下載安裝)
5、下載php加速器:xche的源碼包,xcache-3.0.1.tar.gz
二、前提環境
1、基於Redhat5的虛擬機
2、配置好yum倉庫
3、關閉selinux
# setenforce 0
4、將系統時間與硬件時間同步
# hwclock -s
5、準備好安裝環境(Development Libraries)
# yum groupinstall "Development Libraries"
三、詳細過程
1、編譯安裝apr
【先解壓縮,在解壓後的源碼目錄中安裝,編譯安裝三步驟:./configure,make,make install】
2、編譯安裝apr-util
【解壓縮以及編譯安裝三步驟】
3、編譯安裝httpd
【由於httpd編譯過程中依賴 pcre-devel軟件包,因此需先將其安裝】
【解壓縮,手動編譯】
(# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util)
【我們先可以根據二進制啓動一下服務,並打開瀏覽器訪問。此時已能工作】
【將http進程的pid文件放到/var/run下(默認編譯後在apache/logs下)】
(編輯主配置文件後,啓動服務驗證即可)
# vim /etc/httpd/httpd.conf
【爲httpd提供服務腳本】
我這裏已經提供好一個腳本,我們只需要創建腳本文件將其複製過去就行。
- # vim /etc/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
# chmod +x /etc/init.d/httpd --> 加權限
# chkconfig --add httpd --> 加入服務列表
4、編譯安裝mysql
由於使用通用二進制安裝,則可直接將壓縮包解壓至/usr/local使用。
【準備好一個邏輯卷,並創建一個單獨的目錄/mydata/data,將邏輯卷掛載至/mydata即可】
【初始化mysql要使用mysql用戶和mysql組,因此,我們需自己創建】
初始化mysql
【提供主配置文件以及sysv服務的腳本】
【添加環境變量,指定二進制文件的所在路徑】
# vim /etc/profile.d/mysql.sh
【啓動服務測試使用】
5、編譯安裝php-5.4.13
【安裝兩個rpm包】
# rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm libmcrypt-devel-2.5.7-5.el5.i386.rpm
【解壓縮,編譯安裝】
# 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
# make
# make install
【提供配置文件】
【重啓httpd,測試php是否成功】
6、編譯安裝xcache,爲php加速
【解壓縮,編譯】
【配置文件】
【重啓httpd,打開web頁面驗證】
# service httpd restart
這樣就實現了httpd,php,mysql的結合 --> LAMP平臺。必須注意,要先安裝mysql載安裝php,因爲php的編譯需要mysql的安裝路徑。