Linux編譯安裝LAMP+Xcache+FastCGI環(huán)境

2016年11月17日20:47:17 發(fā)表評(píng)論 4,010 ℃

本次編譯安裝LAMP環(huán)境版本(Apache2.4.23+MySQL5.6.33+php5.6.9+Xcache3.2.0):

Linux:Centos6x-el6.x86_64

Apache:2.4.23

Mysql:5.6.33 (rpm,通用二進(jìn)制,源碼)

PHP:5.6.9+FastCGI

Xcache:3.2.0

安裝順序:Aapche-->Mysql-->PHP(fastcgi)-->Xcache

首先關(guān)閉selinux 

#vim /etc/sysconfig/selinux

把里邊的一行改為

SELINUX=disabled  //重啟生效

#setenforce 0 臨時(shí)生效

一、編譯安裝httpd2.4.23

1、安裝 Development tools和 Server Platform Development開發(fā)環(huán)境。

#yum groupinstall -y Development Tools Server Platform Development

2、安裝apr 然后安裝apr-util,默認(rèn)已經(jīng)安裝,可以不用安裝

#wget http://mirrors.cnnic.cn/apache/apr/apr-1.5.2.tar.bz2

#tar -jxf apr-1.5.2.tar.bz2

#cd apr-1.5.2

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

#make

#make install

#wget http://mirrors.cnnic.cn/apache/apr/apr-util-1.5.4.tar.bz2

#tar -jxf apr-util-1.5.4.tar.bz2

#cd apr-util-1.5.4

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

#make

#make install

3、編譯安裝httpd2.4.23

#yum install -y pcre-devel

#yum install openssl-devel

#wget http://mirrors.cnnic.cn/apache/httpd/httpd-2.4.23.tar.bz2

#./configure --prefix=/usr/local/httpd -sysc --enable-so --enable-rewrite --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/ --with-ssl=/etc/pki/tls

#make

#make install

補(bǔ)充:

(1)構(gòu)建MPM為靜態(tài)模塊

在全部平臺(tái)中,MPM都可以構(gòu)建為靜態(tài)模塊。在構(gòu)建時(shí)選擇一種MPM,鏈接到服務(wù)器中。如果要改變MPM,必須重新構(gòu)建。為了使用指定的MPM,請(qǐng)?jiān)趫?zhí)行configure腳本 時(shí),使用參數(shù) --with-mpm=NAME。NAME是指定的MPM名稱。編譯完成后,可以使用 ./httpd -l 來確定選擇的MPM。 此命令會(huì)列出編譯到服務(wù)器程序中的所有模塊,包括 MPM。

(2)構(gòu)建 MPM 為動(dòng)態(tài)模塊

在Unix或類似平臺(tái)中,MPM可以構(gòu)建為動(dòng)態(tài)模塊,與其它動(dòng)態(tài)模塊一樣在運(yùn)行時(shí)加載。 構(gòu)建 MPM 為動(dòng)態(tài)模塊允許通過修改LoadModule指令內(nèi)容來改變MPM,而不用重新構(gòu)建服務(wù)器程序。在執(zhí)行configure腳本時(shí),使用--enable-mpms-shared選項(xiàng)即可啟用此特性。當(dāng)給出的參數(shù)為all時(shí),所有此平臺(tái)支持的MPM模塊都會(huì)被安裝。還可以在參數(shù)中給出模塊列表。默認(rèn)MPM,可以自動(dòng)選擇或者在執(zhí)行configure腳本時(shí)通過--with-mpm選項(xiàng)來指定,然后出現(xiàn)在生成的服務(wù)器配置文件中。編輯LoadModule指令內(nèi)容可以選擇不同的MPM。

4、配置httpd2.4.23

#vim /etc/httpd/httpd.conf

在ServerRoot "/usr/local/httpd"后面添加一行PidFile "/var/run/httpd.pid"

#vim /etc/rc.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/httpd/bin/apachectl

httpd=${HTTPD-/usr/local/httpd/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 為此腳本賦予執(zhí)行權(quán)限:

#chkconfig --add httpd   //添加服務(wù)到開機(jī)啟動(dòng)

#chkconfig --level 35 httpd on //設(shè)定啟動(dòng)級(jí)別

#vim /etc/profile.d/httpd.sh  //添加httpd環(huán)境變量

添加export PATH=$PATH:/usr/local/httpd/bin

#vim /etc/sysconfig/iptables

添加 -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

#/etc/init.d/iptables restart

5、啟用虛擬主機(jī)

#vim /etc/httpd/httpd.conf

找到DocumentRoot "/usr/local/httpd/htdocs"

改成#DocumentRoot "/usr/local/httpd/htdocs"

找到#Include /etc/httpd/extra/httpd-vhosts.conf

改成Include /etc/httpd/extra/httpd-vhosts.conf

#/etc/init.d/httpd restart    

添加站點(diǎn),編輯 /etc/httpd/extra/httpd-vhosts.conf文件即可,新建站點(diǎn)格式如下:

<VirtualHost *:80>

    ServerAdmin admin@amd5.cn

    DocumentRoot "/www/wwwroot/test"

    ServerName amd5.cn

    ServerAlias www.zhongjima.net

    ErrorLog "/var/log/httpd/amd5.cn-error_log"

    CustomLog "/var/log/httpd/amd5.cn-access_log" combined

</VirtualHost>

說明:重啟httpd如果提示如下錯(cuò)誤:

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using xxxx. Set the 'ServerName' directive globally to suppress this message

可以vim /etc/httpd/httpd.conf 找到

#ServerName www.example.com:80

更改為:

ServerName localhost:80

6、啟用服務(wù)器狀態(tài)(可省略)

mod_status模塊可以讓管理員查看服務(wù)器的執(zhí)行狀態(tài),它通過一個(gè)HTML頁面展示了當(dāng)前服務(wù)器的統(tǒng)計(jì)數(shù)據(jù)。這些數(shù)據(jù)通常包括但不限于:

(1) 處于工作狀態(tài)的worker進(jìn)程數(shù);

(2) 空閑狀態(tài)的worker進(jìn)程數(shù);

(3) 每個(gè)worker的狀態(tài),包括此worker已經(jīng)響應(yīng)的請(qǐng)求數(shù),及由此worker發(fā)送的內(nèi)容的字節(jié)數(shù);

(4) 當(dāng)前服務(wù)器總共發(fā)送的字節(jié)數(shù);

(5) 服務(wù)器自上次啟動(dòng)或重啟以來至當(dāng)前的時(shí)長;

(6) 平均每秒鐘響應(yīng)的請(qǐng)求數(shù)、平均每秒鐘發(fā)送的字節(jié)數(shù)、平均每個(gè)請(qǐng)求所請(qǐng)求內(nèi)容的字節(jié)數(shù);

啟用狀態(tài)頁面的方法很簡(jiǎn)單,只需要在主配置文件中添加如下內(nèi)容即可:

<Location /server-status>

    SetHandler server-status

    Require all granted

</Location>

需要提醒的是,這里的狀態(tài)信息不應(yīng)該被所有人隨意訪問,因此,應(yīng)該限制僅允許某些特定地址的客戶端查看。比如使用Require ip 172.16.0.0/16來限制僅允許指定網(wǎng)段的主機(jī)查看此頁面。

7、啟用openssl

#vim /etc/init.d/httpd restart

找到#LoadModule ssl_module modules/mod_ssl.so

改成LoadModule ssl_module modules/mod_ssl.so

找到#Include /etc/httpd/extra/httpd-ssl.conf

改為Include /etc/httpd/extra/httpd-ssl.conf

#/etc/init.d/httpd restart    

編輯/etc/httpd/extra/httpd-ssl.conf可以對(duì)openssl進(jìn)行配置。

  openssl詳細(xì)配置可以參考阿湯博客教程 Centos Apache基于openssl的https服務(wù)配置

8、profork、worker、event參數(shù)設(shè)置

#vim /etc/httpd/httpd.conf

找到#Include /etc/httpd/extra/httpd-mpm.conf

改為Include /etc/httpd/extra/httpd-mpm.conf

編輯/etc/httpd/extra/httpd-mpm.conf可對(duì)相應(yīng)參數(shù)做調(diào)整

#/etc/init.d/httpd restart  

  profork、worker、event三種工作模式比較可以參考阿湯博客文章 Apache的mpm prefork、worker、event三種工作模式比較

二、mysql5.6.33通用二進(jìn)制安裝配置:

1、下載解壓,并添加mysql系統(tǒng)用戶和mysql系統(tǒng)組

#wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz

#tar -zxf mysql-5.6.33-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

#cd /usr/local/

#ln -sv mysql-5.6.33-linux-glibc2.5-x86_64 mysql

#groupadd -r -g 306 mysql

#useradd -g 306 -r -u 306 mysql

#chown -R mysql:mysql /usr/local/mysql/*

2、創(chuàng)建一個(gè)邏輯卷存放數(shù)據(jù)庫數(shù)據(jù),方便以后擴(kuò)展,也可以mkdir一個(gè)目錄,存放。(數(shù)據(jù)量不大,可以省略此步驟,直接在現(xiàn)有磁盤新建目錄存放)

#fdisk /dev/vdb      //創(chuàng)建一個(gè)20G的分區(qū) 分區(qū)id 為8e

#partprobe /dev/vdb  //讓內(nèi)核識(shí)別分區(qū) cat /proc/partitions 查看,如果命令無法識(shí)別,重啟服務(wù)器

#pvcreate /dev/vdb2  //在創(chuàng)建的分區(qū)上面創(chuàng)建pv

#vgcreate mysqlvg/dev/vdb2   //創(chuàng)建vg

#lvcreate -n mysqllv -L 20G mysqlvg  //創(chuàng)建lv

#mkfs.ext4 /dev/mysqlvg/mysqllv   

#mkdir   /mysql_data/

#mount /dev/mysqlvg/mysqllv /mysql_data/

#vim /etc/fstab 

添加/dev/mapper/mysqlvg-mysqllv  /mysql_data         ext4    defaults 0 0

3、配置初始化安裝mysql

#mkdir /mysql_data/data

#chmod o-rx /mysql_data/data/    //取消其他用戶組權(quán)限

#chown -R mysql.mysql /mysql_data/data/  //設(shè)置數(shù)據(jù)存放路徑 用戶和組

#/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mysql_data/data/ --keep-my-cnf

#chown -R root /usr/local/mysql/*

#cp support-files/mysql.server /etc/init.d/mysqld  //為mysql提供sysv服務(wù)腳本

#chmod +x /etc/rc.d/init.d/mysqld

#chkconfig --add mysqld   //設(shè)置開機(jī)啟動(dòng),默認(rèn)2345級(jí)別啟動(dòng)

#cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

#vim /etc/my.cnf

添加下列兩行指定安裝路徑和數(shù)據(jù)庫存放路徑

basedir = /usr/local/mysql

datadir = /mysql_data/data

4、環(huán)境變量、頭文件、庫文件、幫助手冊(cè)設(shè)置(可省略)

#vim /etc/profile.d/mysql.sh   //設(shè)置環(huán)境變量

添加export PATH=$PATH:/usr/local/mysql/bin

#vim /etc/man.config   //設(shè)置幫助手冊(cè)

添加MANPATH /usr/local/mysql/man

#vim /etc/ld.so.conf.d/mysql.conf  //導(dǎo)出庫文件

添加/usr/local/mysql/lib

#ldconfig -v   //重新建立庫文件緩存

#ln -sv /usr/local/mysql/include/ /usr/include/mysql  //導(dǎo)出頭文件

5、設(shè)置mysql密碼

#mysql

>set password for 'root'@'localhost' =password('12344321');  //默認(rèn)密碼為空,這里更改為12344321

>quit

Linux編譯安裝LAMP+Xcache+FastCGI環(huán)境

三、編譯安裝php-5.6.9

1、安裝依賴庫文件,解決依賴關(guān)系

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

如果想讓編譯的php支持mcrypt擴(kuò)展,此處還需要下載如下兩個(gè)rpm包并安裝(如果不需要此擴(kuò)展,省略 --with-mcrypt即可 ):

#wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm

#wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm

#rpm -ivh libmcrypt-2.5.7-1.2.el6.rf.x86_64.rpm  libmcrypt-devel-2.5.7-1.2.el6.rf.x86_64.rpm 

2、編譯安裝php5.6.9

#wget http://mirrors.sohu.com/php/php-5.6.9.tar.bz2

#tar jxf php-5.6.9.tar.bz2

#cd php-5.6.9

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

說明:

1、如果使用PHP5.3以上版本,為了鏈接MySQL數(shù)據(jù)庫,可以指定mysqlnd,這樣在本機(jī)就不需要先安裝MySQL或MySQL開發(fā)包了。mysqlnd從php 5.3開始可用,可以編譯時(shí)綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認(rèn)設(shè)置了。

# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

# make

# make intall

3、配置php-fpm

為php-fpm提供Sysv init腳本,并將其添加至服務(wù)列表:

# 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的相關(guān)選項(xiàng)為你所需要的值,并啟用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 

接下來就可以啟動(dòng)php-fpm了:

# service php-fpm start

使用如下命令來驗(yàn)正(如果此命令輸出有中幾個(gè)php-fpm進(jìn)程就說明啟動(dòng)成功了):

# ps aux | grep php-fpm

默認(rèn)情況下,fpm監(jiān)聽在127.0.0.1的9000端口,也可以使用如下命令驗(yàn)正其是否已經(jīng)監(jiān)聽在相應(yīng)的套接字。

# netstat -tnlp | grep php-fpm

tcp        0      0 127.0.0.1:9000       0.0.0.0:*       LISTEN      689/php-fpm 

四、配置httpd支持php+fastcgi

1、啟用httpd的相關(guān)模塊

在Apache httpd 2.4以后已經(jīng)專門有一個(gè)模塊針對(duì)FastCGI的實(shí)現(xiàn),此模塊為mod_proxy_fcgi.so,它其實(shí)是作為mod_proxy.so模塊的擴(kuò)充,因此,這兩個(gè)模塊都要加載

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

2、配置虛擬主機(jī)支持使用fcgi

在相應(yīng)的虛擬主機(jī)中添加類似如下兩行。

ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1

例如:

<VirtualHost *:80>

    DocumentRoot "/www/wwwroot/test"

    ServerName amd5.cn

    ServerAlias www.zhongjima.net

ProxyRequests Off

ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/wwwroot/test/$1

    <Directory "/www/wwwroot/test">

        Options none

        AllowOverride none

        Require all granted

    </Directory>

</VirtualHost>

ProxyRequests Off:關(guān)閉正向代理

ProxyPassMatch:把以.php結(jié)尾的文件請(qǐng)求發(fā)送到php-fpm進(jìn)程,php-fpm至少需要知道運(yùn)行的目錄和URI,所以這里直接在fcgi://127.0.0.1:9000后指明了這兩個(gè)參數(shù),其它的參數(shù)的傳遞已經(jīng)被mod_proxy_fcgi.so進(jìn)行了封裝,不需要手動(dòng)指定。

3、編輯apache配置文件httpd.conf,讓apache能識(shí)別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

補(bǔ)充:Apache httpd 2.4以前的版本中,要么把PHP作為Apache的模塊運(yùn)行,要么添加一個(gè)第三方模塊支持PHP-FPM實(shí)現(xiàn)。

五、編譯安裝Xcache-3.2.0

#wget http://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz

#tar -zxf xcache-3.2.0.tar.gz

#cd xcache-3.2.0

#./configure --enable-xcache --with-php-c>

#make

#make install

#mkdir /etc/php.d

#cp xcache.ini /etc/php.d/

#vim /etc/php.d/xcache.ini

找到xcache.count =  改為你當(dāng)前服務(wù)器cpu個(gè)數(shù)(cat /proc/cpuinfo |grep -c processor查看個(gè)數(shù))

#/etc/init.d/httpd restart

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評(píng)論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: