前言:LAMP :Linux+Apache+MySQL+PHP,是大家经常采用的组合,今天来做一下一个实验,看看他们如何组合在一起。我的平台:RedHat 5.8 ,Development Libraries,Development Tools,X Software Development已安装,计划安装的软件:

httpd-2.4.3.tar.bz2 ##它依赖的包有
apr
arp-utils
pcre-devel

php-5.4.8.tar.bz2 ##它依赖的包有
libmcrypt
libmcrypt-devel 
mysql-5.5.28-linux2.6-i686.tar.bz2 ##通用已编译包

一,编译安装httpd-2.4.3

1,安装它依赖的包apr   ##Apache portable Run-time libraries,Apache可移植运行库

   1:  tar -xvf apr-1.4.6.tar.bz2 
   2:  cd apr-1.4.6
   3:  ./configure --prefix=/usr/local/apr
   4:  make && make install

2,安装依赖包arp-utils  ##可移植库的一些工具

   1:  cd ..
   2:  cd apr-util-1.4.1
   3:  ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
   4:  make && make install
   5:  cd ..

3.安装pcre-devel    ##这个包在httpd编译的时候会用的到

   1:  yum -y install pcre-devel
 
4.编译安装httpd-2.4.3
   1:  tar -xvf httpd-2.4.3.tar.bz2 
   2:  cd httpd-2.4.3
   3:  ./configure --prefix=/usr/local/apache \       ##安装目录
   4:  > --sysconfdir=/etc/httpd \                  ##配置文件目录
   5:  > --enable-so \                    ##启用动态库,也就是实现模块的动态加载
   6:  > --enable-cgi \                   ##运行执行cgi程序
   7:  > --enable-ssl \                   ##允许基于ssl加密传输
   8:  > --enable-rewrite \            ##允许URL地址重写
   9:  > --with-zlib \                      ##支持压缩
  10:  > --with-pcre \                    ##支持perl语言的正则表达式
  11:  > --with-apr=/usr/local/apr \      ##apr的目录
  12:  > --with-apr-util=/usr/local/arp-util \   ##apr-util的目录
  13:  > --enable-mpms-shared=all \      ##实现mpm的动态切换
  14:  > --with-mpm=event                     ##默认加载的mpm
  
  15:
  
  16: make && make install     ##编译安装,这的大家懂得吧

5.配置httpd,简单建立两个虚拟主机

   1:  vi /etc/httpd/httpd.conf   ##编辑配置文件
   2:  #DocumentRoot "/usr/local/apache/htdocs" ##213行,欲建立虚拟主机,注释此行
   3:  Pidfile /var/run/httpd.pid   ##214行,不定义pid文件位置容易出错,建议设定
   4:  Include /etc/httpd/extra/httpd-vhosts.conf ##463左右,启用这行,启用配置virtualhost的配置文件
   1:  /etc/httpd/extra/httpd-vhosts.conf    ##如下所示
   2:  <VirtualHost *:80>
   3:      DocumentRoot "/www/laoguang.me/"     ##文档目录
   4:      ServerName www.laoguang.me              ##域名
   5:      ErrorLog "/www/logs/error_laoguang.log"     ##错误日志
   6:      CustomLog "/www/logs/access_laoguang.log" common  ##访问日志
   7:      <Directory /www/laoguang.me>   ##2.4必须要求对目录权限进行设置
   8:          AllowOverride none     ##不允许url重写
   9:          Options none               ##不启用所有选项
  10:          Require all granted      ##允许所有
  11:      </Directory>
  12:  </VirtualHost>
  13:  <VirtualHost *:80>
  14:      DocumentRoot "/www/magedu.com"       ##同上
  15:      ServerName www.magedu.com
  16:      ErrorLog "/www/logs/error_magedu.log"
  17:      CustomLog "/www/logs/access_magedu.log" common
  18:      <Directory /www/magedu.com>
  19:          AllowOverride none
  20:          Options none
  21:          Require all granted
  22:      </Directory>
  23:  </VirtualHost>

6,建立相应目录与文件,并修改权限

   1:  mkdir -pv /www/{laoguang.me,magedu.com,logs}
   2:  echo "Hello ,I'm LaoGuang" > /www/laoguang.me/index.html
   3:  echo "Hello ,I'm Magedu" > /www/magedu.com/index.html
   4:  chown -R deamon:deamon /www
   5:  ##启动httpd
   6:  /etc/local/apache/bin/apachectl start  ##没有提示代表一切正常
   7:  ##修改/etc/hosts文件,建立映射,添加
   8:  172.16.1.1 www.laoguang.me    ##我的IP是172.16.1.1
   9:  172.16.1.1 www.magedu.com 
  10:  ##打开浏览器,或者 elinks www.laoguang.me看看能否正常访问

7,启用基于ssl的https,编辑/etc/httpd/httpd.conf

   1:  LoadModule ssl_module modules/mod_ssl.so    ##126行左右取消注释,启用ssl模块   
   2:  LoadModule socache_shmcb_module modules/mod_socache_shmcb.so   ##取消注释,ssl模块依赖   
   3:  include /etc/httpd/extra/httpd-ssl.conf ##480行左右取消注释,启用httpd-ssl配置文件   
   4:  ##修改ssl配置文件,被修改的选项有  
   5:  DocumentRoot "/www/laoguang.me"       ##确定文档目录  
   6:  ServerName www.laoguang.me:443       ##域名为哪个          
   7:  SSLCertificateFile "/etc/httpd/laoguang.crt"    ##证书位置   
   8:  SSLCertificateKeyFile "/etc/httpd/laoguang.key"    ##私钥位置   
   9:  ##添加如下,否则2.4的httpd不让访问
  10:  <Directory /www/laoguang.me>
  11:          Options none
  12:          AllowOverride none
  13:          Require all granted
  14:  </Directory>
  15:   
  16:  ##建立相应的证书与私钥    看下面链接  11:  http://laoguang.blog.51cto.com/6013350/1035608

8,重启httpd,/etc/local/apache/bin/apachectl restart ,测试https://www.laoguang.me能否正常运行。为了方便更新PATH,省的每次都绝对路径启动服务,并添加帮助文档

   1:  echo "export PATH=$PATH:/usr/local/apache/bin" > /etc/profile.d/httpd.sh  ##更新PATH
   2:  vi /etc/man.config                                ##添加man文档
   3:  MANPATH=/usr/local/apache/man    48行左右

提供个shell脚本来控制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

而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/httpd

加入服务列表:
# chkconfig --add httpd


接下来就可以启动服务进行测试了。

 

二,安装MySQL-5.5.28-linux2.6-i686.tar.bz2

1,建立需要的用户与用户组mysql以安全运行服务,并修改权限

   1:  groupadd -r mysql
   2:  useradd -r -g mysql mysql
 

2,解压缩Mysql到 /usr/local 并修改权限

   1:  tar xvf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local
   2:  cd /usr/local
   3:  ln -sv mysql-5.5.28-linux2.6-i686/ mysql
   4:  chown -R mysql:mysql mysql 
 

3,我们把Mysql的数据放在/data下面,准备这个目录与权限

   1:  mkdir /data
   2:  chown -R mysql:mysql /data

4,安装初始化Mysql (由于这个包是编译好的绿色包)

   1:  cd /usr/local/mysql
   2:  ./scripts/mysql_install_db --datadir=/data --user=mysql
   3:  ## 拷贝控制脚本到/etc/init.d下
   4:  cp ./support-files/mysql.server /etc/init.d/mysqld 
   5:  ##拷贝配置文件
   6:  cp ./support-files/my-huge.cnf /etc/my.cnf
   7:  ##修改my.cnf
   8:  thread_concurrency=4      ##你的cpu数*2
   9:  datadir=/data        ##添加数据目录

5,把mysql提供的库文件,头文件添加到相应库里

   1:  ln -sv /usr/local/mysql/include/ /usr/include/mysql  ##把头文件链接头文件库
   2:  echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf   ##把库做一个附属配置文件,开机会自动加载
   3:  ldconfig -v 立刻加载库到缓存
   4:   
   5:  echo "export PATH=$PATH:/usr/local/mysql/bin " >/etc/profile.d/mysql.sh   ##更新PATH
   6:  vi /etc/man.config                                 ##添加帮助文档
   7:  MANPATH /usr/local/mysql/man   ##48行左右加入

6,添加到服务中实现开机启动

   1:  chkconfig --add mysqld   ##添加到service中
   2:  chkconfig mysqld on       ##添加到开机启动
   3:  service mysqld start       ##现在启动服务

如果没有报错,mysql就安装完毕了,你可以去设置mysqld了 

三,编译安装php-5.4.8.tar.bz2

1,先安装它的依赖包 libmcrypt-2.5.7-5.el5.i386.rpm,libmcrypt-devel-2.5.7-5.el5.i386.rpm

   1:  rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm libmcrypt-devel-2.5.7-5.el5.i386.rpm

2,编译安装php-5.4.8

   1:   tar -xvf php-5.4.8.tar.bz2 
   2:  cd php-5.4.8
   3:   ./configure --prefix=/usr/local/php \  ##安装目录你懂得
   4:  > --with-mysql=/usr/local/mysql \             ##跟mysql整合
   5:  > --with-openssl \                               ##支持ssl
    6: > --with-mysqli=/usr/local/mysql/bin/mysql_config \##支持mysqli函数,靠它与mysql交流
   7:  > --enable-mbstring \       ##支持像中文这样的多字符语言
   8:  > --with-freetype-dir \     ##这个是关于字体的
   9:  > --with-jpeg-dir \            ##支持jpeg你懂得
  10:  > --with-png-dir \             ##支持png你也懂得
  11:  > --with-zlib \                    ##支持压缩库
  12:  > --with-libxml-dir=/usr \  ##支持xml库
  13:  > --enable-xml \                ##支持xml
  14:  > --enable-sockets \          ##支持套接字
  15:  > --with-apxs2=/usr/local/apache/bin/apxs \  ##这是与apache沟通的选项
  16:  > --with-mcrypt \               ##支持加密
  17:  > --with-config-file-path=/etc \    ##配置文件目录
  18:  > --with-config-file-scan-dir=/etc/php.d \  ##还会扫描的配置文件目录
  19:  > --with-bz2 \                    ##支持bz2
  20:  > --enable-maintainer-zts   ##支持apache的worker或event这两个MPM

3, make &&make install       ##你懂得

4,生成配置php文件

   1:  cp php.ini-production /etc/php.ini    ##从安装目录下拷贝到/etc/下

5,修改 /etc/httpd/httpd.conf让httpd支持php

   1:   DirectoryIndex index.php index.html    ##248行左右添加php的主页支持
   2:  AddType application/x-httpd-php .php             ##合适位置添加对php类型文件的支持

6,修改主页测试运行

   1:  echo -e "<?php\nphpinfo()\n?>" > /www/laoguang.me/index.html
   2:  mv /www/laoguang.me/index.html /www/laoguang.me/index.php
   3:   
   4:  ##访问www.laoguang.me测试运行是否能调用phpinfo()这个函数
 
如果能运行,php就安装整合完毕了

四:编译安装xcache为php加速                 1.编译安装xcache 
 
1: tar xf xcache-2.0.0.tar.gz
 
   2:  cd xcache-2.0.0
   3:  /usr/local/php/bin/phpize
   4:  ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
   5:  make && make install

       安装结束时,会出现类似如下行:

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

      2.编译php.ini,整合php与xcache

 

   1:  ##将xcache提供的样例配置导入php.ini
   2:   mkdir /etc/php.d
   3:   cp xcache.ini /etc/php.d
   4:   
   5:  ##编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
   6:  zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so

所有整合完毕了。