天天看點

Linux自學筆記——手動編譯安裝LAMP

本文主要示範編譯安裝LAMP:

第一部分:httpd 2.4.9 + mariadb-5.5.46 + php-5.4.26編譯安裝過程:

一、   編譯安裝apache

1.      解決依賴關系

httpd-2.4.9需要教新版本的apr和apr-util,是以需要事先對其進行更新。更新方式有兩種,一種是通過源代碼編譯安裝,一種是直接更新rpm包。這裡選擇使用編譯源代碼的方式運作。

首先下載下傳這三個包httpd-2.4.9,apr-1.5.0.tar.bz2,apr-util-1.5.3.tar.bz2

Linux自學筆記——手動編譯安裝LAMP

準備開發環境,安裝Development Tools ,Server Platform Development;使用指令yum groupinstall,這裡不多闡述;

            1)      編譯安裝apr-1.5.0.tar.bz2

a.       解壓縮apr-1.5.0.tar.bz2:

Linux自學筆記——手動編譯安裝LAMP

b.      進入apr-1.5.0目錄;

Linux自學筆記——手動編譯安裝LAMP

c.       依次執行以下指令;

# ./configure --prefix=/usr/local/apr            設定安裝目錄;

# make && make install                           編譯及安裝;

            2)      編譯安裝apr-util

a.       解壓apr-util-1.5.3.tar.bz2,并進入解壓後的apr-util-1.5.3目錄;

Linux自學筆記——手動編譯安裝LAMP

b.      依次執行以下指令;

# ./configure --prefix=/usr/local/apr-util  --with-apr=/usr/local/apr  

# make && make install

       附:apache官方對APR的介紹:

The mission of the Apache Portable Runtime (APR) project is to create and maintain software libraries that provide a predictable and consistent interface to underlying platform-specific implementations. The primary goal is to provide an API to which software developers may code and be assured of predictable if not identical behaviour regardless of the platform on which their software is built, relieving them of the need to code special-case conditions to work around or take advantage of platform-specific deficiencies or features.

        3)      Httpd-2.4.9編譯過程也要依賴于pcre-devel軟體包,需要事先安裝。此軟體為系統CD光牒自帶,找到并安裝即可;

Linux自學筆記——手動編譯安裝LAMP

2.      編譯安裝httpd-2.4.9

首先下載下傳httpd2.4.9到本地,然後執行如下的安裝過程;

        1)      解壓縮httpd-2.4.9.tar.bz2包并進入解壓後的目錄;

Linux自學筆記——手動編譯安裝LAMP

        2)      依次執行以下指令;

            設定安裝路徑等選項:# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --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-modules=most --enable-mpms-shared=all --            with-mpm=event

Linux自學筆記——手動編譯安裝LAMP

編譯安裝:#make && make install

補充:

a.       建構MPM為靜态子產品

        在全部平台中,MPM都可以建構為靜态子產品。在建構時選擇一種MPM,連結到伺服器中。如果要改變MPM,必須重新建構。為了使用指定的MPM,請在執行configure腳本時,使用參數--with-mpm=NAME。NAME時指定的MPM名稱。編譯完成後,,可以使用./httpd –l 來選擇MPM。此指令會列出編譯到伺服器程式中的所有子產品,包括MPM。

    b.      建構MPM為動态子產品

        UNIX或類似平台中,MPM可以建構為動态子產品,與其它動态子產品一樣在運作時加載。建構MPM為動态子產品允許通過修改LoadModule指令來改變MPM,而不用重新建構伺服器程式。在執行configure腳本時,使用--enable-mpms-shared選項即可啟用此特性。當給出的參數為all時,所有此平台支援的MPM子產品都會被安裝。還可以在參數中給出子產品清單。預設MPM,可以自動選擇或者在執行configure腳本時通過--with-mpm選項來之指定,然後出現在生成的伺服器配置檔案中。編輯Loadmodule指令内容可以選擇不同的MPM。

        3)      修改httpd的主配置檔案,設定其pid路徑;

            編輯/etc/httpd24/httpd.conf,添加如下行即可;

            PidFile “/var/run/httpd/httpd.pid”

Linux自學筆記——手動編譯安裝LAMP

       4)      提供腳本配置檔案/etc/rc.d/init.d/httpd24;

            修改相應為如下内容;

Linux自學筆記——手動編譯安裝LAMP

            配置檔案所有内容如下:

            #!/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

                    echo $"not reloading due to configuration syntax error"

                    failure $"not reloading $httpd due to configuration syntax error"

                else

                    killproc -p ${pidfile} $httpd -HUP

                fi

                echo

            # See how we were called.

            case "$1" in

              start)

              start

              ;;

              stop)

              stop

              status)

                    status -p ${pidfile} $httpd

              restart)

              condrestart)

              if [ -f ${pidfile} ] ; then

                stop

                start

              fi

              reload)

                    reload

              graceful|help|configtest|fullstatus)

              $apachectl $@

              *)

              echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"

              exit 1

            esac

            exit $RETVAL

       5)      為此腳本賦予權限;

Linux自學筆記——手動編譯安裝LAMP

        6)      加入服務清單;

Linux自學筆記——手動編譯安裝LAMP

       7)      測試;

Linux自學筆記——手動編譯安裝LAMP

二、   安裝mariadb-5.5.46

1.      準備資料存放的檔案系統;

建立一個邏輯卷,并将其挂在至特定目錄即可。這裡建邏輯卷的過程不再闡述;

這裡假設其邏輯卷挂載目錄為/mydata,而後需要建立/mydata/data目錄作為mysql資料的存放目錄。

Linux自學筆記——手動編譯安裝LAMP

2.      建立使用者以安全方式運作程序:

Linux自學筆記——手動編譯安裝LAMP

3.      安裝并初始化mariadb-5.5.46

        1)      首先下載下傳二進制mariadb安裝包至本地,

Linux自學筆記——手動編譯安裝LAMP

        2)      解壓縮檔案至/usr/local目錄;

Linux自學筆記——手動編譯安裝LAMP

        3)      建立軟連接配接;

Linux自學筆記——手動編譯安裝LAMP

        4)      進入mysql目錄,修改目錄内檔案屬主屬組;

Linux自學筆記——手動編譯安裝LAMP

        5)      運作腳本,生成中繼資料庫;指明使用者和資料庫存放位置;

Linux自學筆記——手動編譯安裝LAMP

4.      為mysql提供主配置檔案;

        1)      複制檔案;

Linux自學筆記——手動編譯安裝LAMP

        2)      編輯配置檔案/etc/my.cnf;

Linux自學筆記——手動編譯安裝LAMP

5.      為mysql提供sysv服務腳本;

        1)      複制腳本檔案;

Linux自學筆記——手動編譯安裝LAMP

        2)      添加腳本檔案執行權限;

Linux自學筆記——手動編譯安裝LAMP

        3)      添加服務清單;

Linux自學筆記——手動編譯安裝LAMP

        4)      測試使用mysql;

Linux自學筆記——手動編譯安裝LAMP

    為了使用mysql的安裝符合系統使用規範,并将其開發元件導出給系統使用,這裡還需要執行如下步驟;

6.      輸出mysql的man手冊至man指令的查找路徑;

編輯/etc/man.config,添加如下行;

Linux自學筆記——手動編譯安裝LAMP

7.      輸出mysql頭檔案至系統頭檔案路徑下/usr/include;可以通過簡單的建立連結實作;

Linux自學筆記——手動編譯安裝LAMP

8.      輸出mysql的庫檔案給系統查找路徑;

Linux自學筆記——手動編譯安裝LAMP

9.      修改PATH環境變量,讓系統可以直接使用mysql相關指令。

Linux自學筆記——手動編譯安裝LAMP

至此,mariadb全部安裝完成;

三、   編譯安裝php-5.4.26

1.      解決依賴關系:

首先配置号yum源(系統安裝及epel源)後執行如下指令

# yum -y groupinstall "Desktop Platform Development"

# yum -y install bzip2-devel libmcrypt-devel libxml2-devel

2.      編譯安裝php-5.4.26

        1)      下載下傳源碼至本地目錄;

Linux自學筆記——手動編譯安裝LAMP

        2)      解壓縮檔案并進入php-5.4.26目錄;

Linux自學筆記——手動編譯安裝LAMP

        3)      編譯安裝php,執行如下指令;

        # ./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

Linux自學筆記——手動編譯安裝LAMP

    各參數解釋:

       --prefix=/usr/local/php 預設安裝路徑

--with-mysql=/usr/local/mysql 指明mysql安裝路徑,如果有特定路徑就使用等号"="後面跟上路徑,沒有則省略等号"="

--with-openssl 使用OpenSSL

--with-mysqli=/usr/local/mysql/bin/mysql_config 定義mysqli接口

--enable-mbstring 支援多位元組字元串支援

--with-freetype-dir 支援各種字型

--with-jpeg-dir 支援處理jpeg格式圖檔

--with-png-dir 支援處理png格式圖檔

--with-zlib 支援壓縮庫

--with-libxml-dir=/usr 支援處理xml文檔

--enable-xml 支援xml

--enable-sockets 使php支援以sockets方式通信

--with-apxs2=/usr/local/apache/bin/apxs (關鍵)表示把php編譯成Apache的子產品

--with-mcrypt 支援加密解密庫

--with-config-file-path=/etc 定義php配置檔案(php.ini)放置路徑

--with-config-file-scan-dir=/etc/php.d 其他配置檔案查找路徑

--with-bz2 支援bz2格式加密

--enable-maintainer-zts 僅針對mpm為event和worker的情況,編譯成zts子產品,如果是prefork則不需要

    說明:

        a.       這裡為了支援apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。

        b.      --with-config-file-path=/etc:指定配置檔案php.ini位址;

        c.       –with-config-file-scan-dir=/etc/php.d:指定額外的ini檔案目錄;

        d.      如果使用php5.3以上版本,為了連結MYSQL資料庫,可以指定mysqld,這樣在本機就不需要先安裝mysql或mysql開發包了。Mysqlnd從php5.3開始可用,可以編譯時綁定到它(而不用具體的mysql用戶端庫綁定形成依賴),但從php5.4開始它就是預設設定了。

        #./configure --with-msyql=mysqlnd --with-pdo-mysql=mysqlnd --with-msyqli=msyqlnd

         4)      編譯;

            #make && make install

         5)      為php提供配置檔案;

            #cp php.ini-production /etc/php.ini

Linux自學筆記——手動編譯安裝LAMP

            6)      編輯apache配置檔案httpd.conf,以apache支援php

                #vim /etc/httpd24/httpd.conf

a.       添加以下兩行;

    AddType application/x-httpd-php .php

    AddType application/x-httpd-php-source .phps

Linux自學筆記——手動編譯安裝LAMP

b.      定位至DirectoryIndex index.html

    修改為

        DirectoryIndex       index.php  index.html

Linux自學筆記——手動編譯安裝LAMP

c.       編輯index.php

Linux自學筆記——手動編譯安裝LAMP

            7)      測試;

Linux自學筆記——手動編譯安裝LAMP

四、     安裝xcache,為php加速:

1.      壓力測試,首先我們用ab指令通路在這台機器上部署的wordpress,進行壓力測試,然後與安裝xcache之後的壓力測試做對比;

Linux自學筆記——手動編譯安裝LAMP

2.      按如下步驟安裝;

        1)      解壓源碼包等操作;

Linux自學筆記——手動編譯安裝LAMP

        2)      編譯安裝;

        # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config

        # make && make install

               安裝結束時,會出現類似如下行:

               Build complete.

        Don't forget to run 'make test'.

        Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

3.      編輯php.ini,整合php和xcache:

        1)      首先将xcache提供的樣例配置檔案導入php.ini

Linux自學筆記——手動編譯安裝LAMP

          說明:xcache.ini檔案在xcache目錄中;

        2)      接下來編輯/etc/php.d/xcache.ini,找到extension開頭的行,修改為如下行:

            extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

Linux自學筆記——手動編譯安裝LAMP

            Note:如果xcache.ini中有多條extension指令行,要確定此新增的行排在第一位;

        3)      再次通路壓力測試;

Linux自學筆記——手動編譯安裝LAMP

    可以發現,傳輸速率得到了很大的提升;

第二部分:配置apache-2.4.9以fpm方式的php-5.4.26

一、     apache、mysql的安裝與前一部分相同;根據以上安裝;

二、     編譯安裝php-5.4.26

Linux自學筆記——手動編譯安裝LAMP
Linux自學筆記——手動編譯安裝LAMP

        # ./configure --prefix=/usr/local/php5-fpm--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 --enable-fpm --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2

        4)      編譯及安裝;

        make && make install

3.      為php提供配置檔案;

    #cp php.ini-production  /etc/php.ini

Linux自學筆記——手動編譯安裝LAMP

4.      配置php-fpm;

        1)      為php-fpm提供SysV init腳本,将其添加至服務清單;

Linux自學筆記——手動編譯安裝LAMP

        2)      為php-fpm提供配置檔案;

Linux自學筆記——手動編譯安裝LAMP

        3)      編輯php-fpm配置檔案;

            配置fpm的相關選項為你所需要的值,并啟用pid檔案;

Linux自學筆記——手動編譯安裝LAMP

            啟用pid檔案:

Linux自學筆記——手動編譯安裝LAMP

        4)      啟動服務并測試;

Linux自學筆記——手動編譯安裝LAMP

5.      配置httpd,使apache支援php-fpm;

        1)      啟動以下兩個子產品;

Linux自學筆記——手動編譯安裝LAMP

        2)      添加檔案類型;

Linux自學筆記——手動編譯安裝LAMP

        3)      定位到DirectoryIndex  index.html,修改為以下;

Linux自學筆記——手動編譯安裝LAMP

        4)      配置虛拟主機;

Linux自學筆記——手動編譯安裝LAMP

    Note:

    ProxyRequests Off:關閉正向代理

    ProxyPassMatch:把以.php結尾的檔案請求發送到php-fpm程序,php-fpm至少需要知道運作的目錄和URI,是以這裡直接在fcgi://127.0.0.1:9000後指明了這兩個參數,其它的參數的傳遞已經被mod_proxy_fcgi.so進行了封裝,不需要手動指定。

6.      測試;

        1)      編輯index.php測試;

Linux自學筆記——手動編譯安裝LAMP

        2)      網頁輸入位址;

Linux自學筆記——手動編譯安裝LAMP

繼續閱讀