天天看點

LAMP編譯安裝

一、安裝順序httpd-->Mysql-->PHP-->XCache。首先Apache HTTPD編譯安裝。

    1、所需源碼包。

    apr-1.5.1.tar.gz     #Apache Portable Runtime不同作業系統相容程式

    apr-util-1.5.3.tar.gz

    httpd-2.4.9.tar.gz

    2、開始編譯安裝。

[root@localhost ~]# yum -y groupinstall "Development Tools" #安裝開發工具
[root@localhost ~]# yum -y groupinstall "Additional Development"
[root@localhost ~]# tar -zxvf apr-1.5.1.tar.gz
[root@localhost ~]# cd apr-1.5.1
[root@localhost apr-1.5.1]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.5.1]# make
[root@localhost apr-1.5.1]# make install
[root@localhost ~]# tar -zxvf apr-util-1.5.3.tar.gz
[root@localhost ~]# cd apr-util-1.5.3
[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util \
--with-apr=/usr/local/apr
[root@localhost apr-util-1.5.3]# make
[root@localhost apr-util-1.5.3]# make install
[root@localhost ~]# tar -zxvf httpd-2.4.9.tar.gz
[root@localhost ~]# cd httpd-2.4.9
[root@localhost httpd-2.4.9]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd --enable-so --enable-rewrite --enable-ssl --enable-cgi \
--enable-cgid --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr\
--with-apr-util=/usr/local/apr-util
configure: error: pcre-config for libpcre not found. PCRE is required and available from 
[root@localhost httpd-2.4.9]# yum -y install pcre-devel #安裝缺失依賴包
[root@localhost httpd-2.4.9]# make
[root@localhost httpd-2.4.9]# make install
[root@localhost ~]# vim /etc/httpd/httpd.conf  
PidFile "/var/run/httpd.pid"  #修改PidFile路徑
ServerName 127.0.0.1   #若無DNS即填IP
[root@localhost ~]# vim /etc/init.d/httpd  #建立服務啟動檔案,可從rpm包複制該檔案。
#!/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
[root@localhost ~]# chmod +x /etc/init.d/httpd 
[root@localhost ~]# chkconfig --add httpd
[root@localhost ~]# chkconfig --level 35 httpd on
[root@localhost ~]# vim /etc/profile.d/httpd.sh   #添加PATH,必須以sh結尾
export PATH=$PATH:/usr/local/apache/bin
[root@localhost ~]# echo $PATH  #重新登入即可生效
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/apache/bin:/root/bin
[root@localhost ~]# setenforce 0
[root@localhost ~]# service iptables stop
[root@localhost ~]# service httpd start      

二、編譯安裝Mysql。

    1、源碼包。

    mysql-5.5.30-linux2.6-x86_64.tar.gz

[root@localhost ~]# tar -zxvf mysql-5.5.30-linux2.6-x86_64.tar.gz -C /usr/local  #注意:需要解壓到/usr/local目錄下且命名為mysql
[root@localhost ~]# ln -sv /usr/local/mysql-5.5.30-linux2.6-x86_64/ /usr/local/mysql
[root@localhost ~]# groupadd -r -g 306 mysql
[root@localhost ~]# useradd -g 306 -r -u 306 mysql  #建立不允許登入的使用者mysql
[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/*
[root@localhost ~]# mkdir -p /mydata/data   #建立mysql資料存放目錄,建議獨立邏輯卷挂載
[root@localhost ~]# chown -R mysql.mysql /mydata/data
[root@localhost ~]# chmod o-rx /mydata/data
[root@localhost mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data/  #運作安裝腳本
[root@localhost mysql]# chown -R root /usr/local/mysql/*   #安全考慮屬主該為root,但資料目錄必須為mysql
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld #添加啟動腳本
[root@localhost mysql]# chmod +x /etc/init.d/mysqld 
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --level 35 mysqld on
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf  #添加配置檔案
cp: overwrite `/etc/my.cnf'? y
[root@localhost mysql]# vim /etc/my.cnf
[mysqld]
thread_concurrency = 4   #線程并發量
datadir = /mydata/data   #資料目錄
[root@localhost mysql]# vim /etc/man.config  #Manual檔案連結
MANPATH /usr/local/mysql/man
[root@localhost mysql]# vim /etc/profile.d/mysql.sh #Mysql執行程式連結
export PATH=$PATH:/usr/local/mysql/bin
[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf  #庫檔案連結
/usr/local/mysql/lib
[root@localhost ~]# ldconfig -v  #重新讀取庫檔案
[root@localhost ~]# ln -sv /usr/local/mysql/include /usr/include/mysql #頭檔案連結
[root@localhost ~]# service mysqld start      

三、編譯安裝PHP。

    php-5.4.31.tar.gz

    libmcrypt-2.5.7-5.el5.i386.rpm   #這四個包為--with-mcrypt選項的依賴包

    libmcrypt-devel-2.5.7-5.el5.i386.rpm

    mhash-0.9.9-1.el5.centos.i386.rpm

    mhash-devel-0.9.9-1.el5.centos.i386.rpm

    2、編譯安裝以子產品化運作的php以及xcache擴充。

[root@localhost ~]# tar xf php-5.4.31.tar.gz
[root@localhost ~]# cd php-5.4.31
[root@localhost php-5.4.31]#./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
[root@localhost php-5.4.31]# make
[root@localhost php-5.4.31]# make install
[root@localhost php-5.4.31]# cp php.ini-production /etc/php.ini  #定義配置檔案
[root@localhost ~]# vim /etc/httpd/httpd.conf  #配置Apache識别php程式
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php
[root@localhost ~]# cd /usr/local/apache/htdocs/
[root@localhost htdocs]# mv index.html index.php
[root@localhost htdocs]# vim index.php  
<html><body><h1>It works!</h1></body></html>
<?php
        phpinfo();    #驗證php子產品是否正常工作
        $conn=mysql_connect('localhost','root','');   #驗證php與mysql連接配接
        if ($conn)
                echo "Success...";
        else
                echo "Failure...";
?>
[root@localhost ~]# tar -zxvf xcache-2.0.1.tar.gz
[root@localhost ~]# cd xcache-2.0.1
[root@localhost xcache-2.0.1]# /usr/local/php/bin/phpize  #php擴充編譯準備
[root@localhost xcache-2.0.1]# ./configure --enable-xcache \
--with-php-config=/usr/local/php/bin/php-config 
[root@localhost xcache-2.0.1]# make install
Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/
[root@localhost xcache-2.0.1]# mkdir /etc/php.d
[root@localhost xcache-2.0.1]# cp xcache.ini /etc/php.d/
[root@localhost xcache-2.0.1]# vim /etc/php.d/xcache.ini
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
;zend_extension_ts = c:/php/extensions/php_xcache.dll
[root@localhost xcache-2.0.1]# service httpd restart #重新開機httpd,首頁有xcache子產品即成功      
[root@localhost ~]# tar xf php-5.4.31.tar.gz
[root@localhost ~]# cd php-5.4.31
[root@localhost ~]# ./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 --enable-fpm --with-mcrypt  --with-config-file-path=/etc \
--with-config-file-scan-dir=/etc/php.d --with-bz2
[root@localhost php-5.4.31]# make
[root@localhost php-5.4.31]# make install
為php提供配置檔案:
# cp php.ini-production /etc/php.ini

3、配置php-fpm
 
為php-fpm提供Sysv init腳本,并将其添加至服務清單:
# cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
# chmod +x /etc/rc.d/init.d/php-fpm
# chkconfig --add php-fpm
# chkconfig php-fpm on

為php-fpm提供配置檔案:
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 

編輯php-fpm的配置檔案:
# vim /usr/local/php/etc/php-fpm.conf
配置fpm的相關選項為你所需要的值,并啟用pid檔案(如下最後一行):
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php/var/run/php-fpm.pid 

接下來就可以啟動php-fpm了:
# service php-fpm start

使用如下指令來驗正(如果此指令輸出有中幾個php-fpm程序就說明啟動成功了):
# ps aux | grep php-fpm

預設情況下,fpm監聽在127.0.0.1的9000端口,也可以使用如下指令驗正其是否已經監聽在相應的套接字。
# netstat -tnlp | grep php-fpm
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      689/php-fpm

啟用httpd的相關子產品

在Apache httpd 2.4以後已經專門有一個子產品針對FastCGI的實作,此子產品為mod_proxy_fcgi.so,它其實是作為mod_proxy.so子產品的擴充,是以,這兩個子產品都要加載
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so


2、配置虛拟主機支援使用fcgi

在相應的虛拟主機中添加類似如下兩行。
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

    
例如:
<VirtualHost *:80>
    DocumentRoot "/www/magedu.com"
    ServerName magedu.com
    ServerAlias www.magedu.com

    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/magedu.com/$1

    <Directory "/www/magedu.com">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

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

3、編輯apache配置檔案httpd.conf,讓apache能識别php格式的頁面,并支援php格式的首頁
 
 # vim /etc/httpd/httpd.conf
 1、添加如下二行
   AddType application/x-httpd-php  .php
   AddType application/x-httpd-php-source  .phps

 2、定位至DirectoryIndex index.html 
   修改為:
    DirectoryIndex  index.php  index.html

補充:Apache httpd 2.4以前的版本中,要麼把PHP作為Apache的子產品運作,要麼添加一個第三方子產品支援PHP-FPM實作。      
[root@localhost ~]# yum -y install bind
[root@localhost ~]# vim /etc/named.conf 
options {
        listen-on port 53 { any; };
        allow-query     { any; };
        forwarders      { 114.114.114.114; };
        dnssec-validation no;
zone "a.net" IN {
        type master;
        file "/var/named/a.net";
};
zone "1.16.172.in-addr.arpa" IN {
        type master;
        file "named-1.16.172";
};
[root@localhost ~]# vim /var/named/a.net 
$TTL    86400
@       IN      SOA     a.net. root.a.net. ( 2014071011 8H 15M 1W 1D );
@       IN      NS      a.net.;
@       IN      A       172.16.1.1;
www     IN      A       172.16.1.1;
[root@localhost ~]# vim /var/named/named-1.16.172 
$TTL    86400
@       IN      SOA     a.net. root.a.net. ( 2014071011 8H 15M 1W 1D );
@       IN      NS      a.net.;
1       IN      PTR     a.net.; 
1       IN      PTR     www.a.net.;
[root@localhost ~]# vim /etc/httpd/httpd.conf
#DocumentRoot "/usr/local/apache/htdocs"    #注釋該行
Include /etc/httpd/extra/httpd-vhosts.conf  #此行取消注釋
[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80>
    DocumentRoot "/web/a.net"
    ServerName www.a.net
    <Directory "/web/a.net">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
    ErrorLog "/var/log/httpd/a.net-error_log"
    CustomLog "/var/log/httpd/a.net-access_log" combined
</VirtualHost>      
上一篇: 編譯安裝LAMP
下一篇: 編譯安裝LAMP