本次編譯安裝LAMP環(huán)境版本:
Linux:Centos6x-el6.x86_64
Apache:2.4.23
Mysql:5.6.33 (rpm,通用二進(jìn)制,源碼)
PHP:5.6.9
Xcache:3.2.0
安裝順序:Aapche-->Mysql-->PHP-->Xcache
首先關(guān)閉selinux
#vim /etc/sysconfig/selinux
把里邊的一行改為
SELINUX=disabled //重啟生效
#setenforce 0 臨時生效
一、編譯安裝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
補充:
(1)構(gòu)建MPM為靜態(tài)模塊
在全部平臺中,MPM都可以構(gòu)建為靜態(tài)模塊。在構(gòu)建時選擇一種MPM,鏈接到服務(wù)器中。如果要改變MPM,必須重新構(gòu)建。為了使用指定的MPM,請在執(zhí)行configure腳本 時,使用參數(shù) --with-mpm=NAME。NAME是指定的MPM名稱。編譯完成后,可以使用 ./httpd -l 來確定選擇的MPM。 此命令會列出編譯到服務(wù)器程序中的所有模塊,包括 MPM。
(2)構(gòu)建 MPM 為動態(tài)模塊
在Unix或類似平臺中,MPM可以構(gòu)建為動態(tài)模塊,與其它動態(tài)模塊一樣在運行時加載。 構(gòu)建 MPM 為動態(tài)模塊允許通過修改LoadModule指令內(nèi)容來改變MPM,而不用重新構(gòu)建服務(wù)器程序。在執(zhí)行configure腳本時,使用--enable-mpms-shared選項即可啟用此特性。當(dāng)給出的參數(shù)為all時,所有此平臺支持的MPM模塊都會被安裝。還可以在參數(shù)中給出模塊列表。默認(rèn)MPM,可以自動選擇或者在執(zhí)行configure腳本時通過--with-mpm選項來指定,然后出現(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ī)啟動
#chkconfig --level 35 httpd on //設(shè)定啟動級別
#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
添加站點,編輯 /etc/httpd/extra/httpd-vhosts.conf文件即可,新建站點格式如下:
<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如果提示如下錯誤:
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),它通過一個HTML頁面展示了當(dāng)前服務(wù)器的統(tǒng)計數(shù)據(jù)。這些數(shù)據(jù)通常包括但不限于:
(1) 處于工作狀態(tài)的worker進(jìn)程數(shù);
(2) 空閑狀態(tài)的worker進(jìn)程數(shù);
(3) 每個worker的狀態(tài),包括此worker已經(jīng)響應(yīng)的請求數(shù),及由此worker發(fā)送的內(nèi)容的字節(jié)數(shù);
(4) 當(dāng)前服務(wù)器總共發(fā)送的字節(jié)數(shù);
(5) 服務(wù)器自上次啟動或重啟以來至當(dāng)前的時長;
(6) 平均每秒鐘響應(yīng)的請求數(shù)、平均每秒鐘發(fā)送的字節(jié)數(shù)、平均每個請求所請求內(nèi)容的字節(jié)數(shù);
啟用狀態(tài)頁面的方法很簡單,只需要在主配置文件中添加如下內(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可以對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可對相應(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)建一個邏輯卷存放數(shù)據(jù)庫數(shù)據(jù),方便以后擴(kuò)展,也可以mkdir一個目錄,存放。(數(shù)據(jù)量不大,可以省略此步驟,直接在現(xiàn)有磁盤新建目錄存放)
#fdisk /dev/vdb //創(chuàng)建一個20G的分區(qū) 分區(qū)id 為8e
#partprobe /dev/vdb //讓內(nèi)核識別分區(qū) cat /proc/partitions 查看,如果命令無法識別,重啟服務(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ī)啟動,默認(rèn)2345級別啟動
#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)境變量、頭文件、庫文件、幫助手冊設(shè)置(可省略)
#vim /etc/profile.d/mysql.sh //設(shè)置環(huán)境變量
添加export PATH=$PATH:/usr/local/mysql/bin
#vim /etc/man.config //設(shè)置幫助手冊
添加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
三、編譯安裝php-5.6.9
1、安裝依賴擴(kuò)展文件
#yum install -y libxml2 libxml2-devel bzip2 bzip2-devel
如果想讓編譯的php支持mcrypt擴(kuò)展,此處還需要下載如下兩個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 --with-apxs2=/usr/local/httpd/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts
說明:
1、這里為了支持apache的worker或event這兩個MPM,編譯時使用了--enable-maintainer-zts選項。
2、如果使用PHP5.3以上版本,為了鏈接MySQL數(shù)據(jù)庫,可以指定mysqlnd,這樣在本機(jī)就不需要先安裝MySQL或MySQL開發(fā)包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認(rèn)設(shè)置了。
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
#make
#make test
#make install
#cp php.ini-production /etc/php.ini
3、配置apache支持php
#vim /etc/httpd/httpd.conf
找到AddType application/x-gzip .gz .tgz添加如下兩行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
找到DirectoryIndex index.html修改為:
DirectoryIndex index.php index.html
#/etc/init.d/httpd restart
四、編譯安裝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個數(shù)(cat /proc/cpuinfo |grep -c processor查看個數(shù))
#/etc/init.d/httpd restart