一、需要的软件包
httpd-2.4.4.tar.bz2
mysql-5.5.28-linux2.6-i686.tar.gz
php-5.4.13.tar.bz2
apr-1.4.6.tar.bz2
apr-util-1.5.2.tar.bz2
xcache-3.0.1.tar.bz2
确保开发环境已经安装上 Development Libraries和Development Tools 软件包组,如果没有安装要使用 # yum groupinstall “Development Libraries”“Development Tools”命令安装开发环境。
二、手动编译安装httpd软件包
因为httpd-2.4.4需要新版本的apr和apr-util,我们要先编译安装apr和apr-util。
1、编译安装apr-1.4.6
# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install
2、编译安装apr-util-1.5.2
# tar xf apr-util-1.5.2.tar.bz2
# cd apr-util-1.5.2
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install
3、编译安装httpd-2.4.4
编译过程需要依赖pcre-devel包,pcre-devel系统磁盘自带的有,我们可以用yum命令安装。
# yum install pcre-devel
安装好后解压httpd源码包,并编译安装
# tar xf httpd-2.4.4.tar.bz2
# cd httpd-2.4.4
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --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-module=most --enable-mpm-shared=most --with-mpm=event
# make && make install
./configure选项的意义:
--prefix=/...:安装路径
--with-apr=/...:依赖包的路径
--sysconfdir=/...:配置文件安装路径
--enable-modules=most:启用大多数模块
--enable-mods-shared=MODULE-LIST:是否启用共享模块(all,most,few,reallyall)
--enable-so:支持动态共享模块(默认支持,如果不支持httpd无法和php进行交互)
--enable-ssl:启用ssl功能(不然无法启用https功能)
--enable-cgi:以cgi的方式进行交互
--enable-cgid:在(worker,event)启用
--enable-mpms-shared:构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM
--with-mpm:默认为MPM
4、修改httpd的主配置文件,设置其pid文件路径
# vim /etc/httpd/httpd.conf
添加如下内容:
PidFile "/var/run/httpd.pid"
5、为httpd提供SysV服务脚本/etc/rc.d/init.d/httpd
# vim /etc/c.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/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
给予此脚本执行权限:
# vim chmod +x /etc/rc.d/init.d/httpd
将httpd加入服务列表
# chkconfig --add httpd
# chkconfig httpd on
修改PATH环境变量
# vim /etc/profile.d/httpd.sh
添加内容如下:
export PATH=$PATH:/usr/local/apache/bin
Apache服务到这里就编译安装完成了,可以启动服务测试。
三、编译安装mysql-5.5.28
1、提供一个存放数据的文件系统,也就是新建一个逻辑卷,将其挂载至某一目录下,作为数据存放目录。创建一个新的分区(这里新分区大小为5G),将其文件系统属性调整为8e。保存并用partprobe命令通知内核重读分区。然后就可以开始创建逻辑卷了。
# pvcreate /dev/sda5 创建物理卷
# vgcreate mysql /dev/sda5 创建卷组,名为mysql
# lvcreate -n lv_mysql -L 3G mysql 创建逻辑卷,大小为3G(注意:逻辑卷大小一定要小于分区大小),逻辑卷名为lv_mysql
2、新建mysql用户,以安全方式运行进程
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data
3、编译安装并初始化mysql
# tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.28-linux2.6-i686 mysql
# cd mysql
# chown -R mysql:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root . #将属主改为root用户是防止mysql用户密码泄漏后,数据库中文件被修改
4、为mysql提供主配置文件:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
并修改此文件中thread_concurrency的值为你的CPU个数乘以2:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data
5、为mysql提供sysv服务脚本:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
# chkconfig --add mysqld
# chkconfig mysqld on
为了方便以后使用man命令,我们在这里要将mysql的开发组件导出给系统。
6、输出mysql的man手册至man命令的查找路径:
编辑/etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
7、输出mysql的头文件至系统头文件路径/usr/include:
这可以通过简单的创建链接实现:
# ln -sv /usr/local/mysql/include /usr/include/mysql
8、输出mysql的库文件给系统库查找路径:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后让系统重新载入系统库:
# ldconfig
9、修改PATH环境变量,让系统可以直接使用mysql的相关命令。
# vim /etc/profile.d/mysql.sh
添加内容如下:
export PATH=$PATH:/usr/local/mysql/bin
这样mysql服务也编译安装完成了,下面我们就来编译安装php。编译安装php的时候需要等待很长的时间:
四、编译安装php
应为./configre 选项中启用了加密模块,我们要安装四个rpm包
libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm
mhash-0.9.2-6.el5.i386.rpm
mhash-devel-0.9.2-6.el5.i386.rpm
使用rpm -ivh *rpm就可以安装。
1、编译安装php-5.4.13
# tar xf php-5.4.13.tar.bz2
# 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
./configure选项作用:
--enable-maintainer-zts:支持apache的worker或event这两个MPM
其它选项功能和mysql编译安装时一样
# ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
# make
# make test #用来测试使用,这一步骤可以省略。
# make intall
为php提供配置文件:
# cp php.ini-production /etc/php.ini
2、编辑apache配置文件httpd.conf,以apache支持php
# vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html
修改为:
DirectoryIndex index.php index.html
3、提供php文件
# cd /usr/local/apache/htdocs
# mv index.html index.php
# vim index.php
<html><body><h1>It works!</h1></body></html>
<?php
$conn=mysql_connect('localhost','root','');
if ($conn)
echo "Success...";
else
echo "Failure...";
phpinfo();
?>
五、安装xcache,为php加速:
1、安装
# tar xf xcache-3.0.1.tar.gz
# cd xcache-3.0.1
# /usr/local/php/bin/phpize
# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
# make && make install
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
# mkdir /etc/php.d
# cp xcache.ini /etc/php.d
说明:xcache.ini文件在xcache的源码目录中。
编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
要确保新增的这一行在最前边。
到这里,手动编译安装LAMP就结束了。如果访问php网页的时候出错原因有:
1、SELinux没有调整到permissive,编辑/etc/selinux/config文件,将SELINUX后改为permissive即可;
2、php模块没有装载上,使用httpd -M 命令就可以查看装载的模块,看其中是否有php模块,模块总数为27;没有的话重新编译安装一次就可以了。