写在前面:如果此文有幸被某位朋友看见并发现有错的地方,希望批评指正。如有不明白的地方,愿可一起探讨。



解决依赖关系


httpd-2.4.9依赖于较新版本的apr和apr-util,接下来就编译安装apr和apr-util

1、编译安装apr

# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# make && make install

2、编译安装apr-util

# tar xf apr-util-1.5.3.tar.bz2
# cd apr-util-1.5.3
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install


编译安装httpd-2.4.9


httpd-2.4.9编译过程中还需依赖于pcre-devel软件包,因此也需将其安装才行

提示:挂载光盘,切换到光盘下的Package目录执行

# rpm -ivh pcre-devel-7.8-6.el6.x86_64.rpm

1、编译安装httpd-2.4.9

# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/httpd24 --sysconfdir=/etc/httpd24 --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-modules=most --enable-mpms-shared=all --with-mpm=event
# make && make install

2、编译/etc/httpd24/httpd.conf配置文件,添加如下行即可:

    PidFile "/var/run/httpd.conf"

    PidFile用于指定记录httpd进程号(PID)的文件位置

3、提供SysV服务脚本/etc/rc.d/init.d/httpd24

# vim /etc/rc.d/init.d/httpd24

脚本内容如下:

#!/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/httpd24/bin/apachectl
httpd=${HTTPD-/usr/local/httpd24/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/httpd24

将http24加入服务列表

# chkconfig --add httpd24
# chkconfig httpd24 on

4、启动http24服务并测试

# /etc/rc.d/init.d/httpd24 start

在浏览器中键入编译安装httpd-2.4.9的主机IP地址,如:10.170.2.1

wKiom1QNVOGiuY_bAACzUUtvtak267.jpg

此处需要说明的是,当你编译安装完httpd-2.4.9后,其DocumentRoot的默认路径为/usr/local/httpd24/htdocs,在此目录下,有一个index.html文件,更多详细内容请查看/etc/httpd24/httpd.conf配置文件


配置虚拟主机


在正式配置虚拟主机之前,需要进行说明的是:以前的httpd版本,配置虚拟主机、配置基于用户的访问控制以及配置httpd工作于https方式等只在主配置文件httpd.conf中,而编译安装的httpd-2.4.9将这些功能的配置分离出来形成了新的配置文件并利用Include包含的主配置文件中

1、编辑/etc/httpd24/httpd.conf配置文件,启动下面这项内容

Include /etc/httpd24/extra/httpd-vhosts.conf

2、编辑/etc/httpd24/extra/httpd-vhosts.conf文件

# cd /etc/httpd24/extra/
# vim httpd-vhosts.conf

将文件中的两个VirtualHost的所用内容注释掉,然后编辑如下内容

<VirtualHost *:80>
    DocumentRoot "/web/hosta"
    ServerName www.muluhe.org
    <Directory "/web/hosta">
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/web/hostb"
    ServerName mail.muluhe.com
    <Directory "/web/hostb">
        Require all granted
    </Directory>
</VirtualHost>

3、编辑网页文件

# mkdir -pv /web/hosta
# mkdir -pv /web/hostb
# vim /web/hosta/index.html
    内容为:<h1>Hello,www.muluhe.org</h1>
# vim /web/hostb/index.html
    内容为:<h1>Hello,mail.muluhe.com</h1>

4、重启http24服务并测试

# /etc/rc.d/init.d/httpd24 restart

由于没有安装DNS服务器,因此利用本地文件对虚拟主机名进行解析

打开Windows主机上的C:\Windows\System32\drivers\etc\hosts文件,并添加对应IP地址与虚拟主机名,添加的内容为:

10.170.2.1 www.muluhe.com
10.170.2.1 mail.muluhe.com
10.170.2.1 www.muluhe.org

在浏览器中分别键入www.muluhe.com、www.muluhe.org、mai.muluhe.com,得到如下结果:

wKioL1QNUoKjH2P6AADB3dwNOhs534.jpg

wKiom1QNU7TA39K9AADamG8Hky4381.jpg

wKiom1QNU8LTtkDHAADdgACfJpU478.jpg


配置基于用户访问控制


1、编译配置文件/etc/httpd24/httpd.conf,启动下面的选项

Include /etc/httpd24/extra/httpd-userdir.conf

关闭下面的选项

Include /etc/httpd24/extra/httpd-vhosts.conf

2、编辑配置文件/etc/httpd24/extra/httpd-userdir.conf

# cd /etc/httpd24/extra/
# vim httpd-userdir.con

将文件中相对应的内容修改为:

UserDir "/usr/local/httpd24/htdocs/admin"
<Directory "/usr/local/httpd24/htdocs/admin">
    AllowOverride AuthConfig
    Options none
    AuthType Basic
    AuthName "Admin Area."
    AuthUserFile /etc/httpd24/.htpasswd
    Require valid-user
</Directory>

3、提供相关文件

# cd /usr/local/httpd24/htdocs/
# mkdir admin
# vim index.html
    内容为:<h1>Hello,muluhe,Welcome to admin directory</h1>
# htpasswd -c -m /etc/httpd24/.htpasswd muluhe
New password: 
Re-type new password: 
Adding password for user muluhe

4、重启httpd24服务并测试

# /etc/init.d/httpd24 restart
Stopping httpd:                                            [  OK  ]
Starting httpd: AH00526: Syntax error on line 10 of /etc/httpd24/extra/httpd-userdir.conf:
Invalid command 'UserDir', perhaps misspelled or defined by a module not included in the server configuration
                                                           [FAILED]

呀哈,竟然出现错误,其大概的意思是说没有加载支持'UserDir'命令的模块,查看/etc/httpd24/extra/httpd-userdir.conf可以找到以下内容:

# Required module: mod_authz_core, mod_authz_host, mod_userdir

与配置文件/etc/httpd24/httpd.conf中的LoadModule中启动的模块作对比,发现没有启动LoadModule userdir_module modules/mod_userdir.so这个模块,将其启动起来再重启服务

# /etc/init.d/httpd24 restart
Stopping httpd:                                            [FAILED]
Starting httpd:                                            [  OK  ]

在浏览器中键入www.muluhe.com/admin,可以得到如下结果

wKiom1QNY4fR8FfaAAFjGtgnwUA995.jpg

输入用户名和密码可以得到如下结果

wKiom1QNY7Ly99oYAAGXfmRiEjQ716.jpg


配置httpd24工作于https


1、安装mod_ssl模块并查看

# yum -y install mod_ssl
# rpm -ql mod_ssl

2、为服务器端生成私钥,并为其提供证书

建立CA

# cd /etc/pki/CA
# (umask 077; openssl genrsa -out private/cakey.pem 2048)
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
# touch serial index.txt
# echo 01 > serial

生成证书请求

# mkdir /etc/httpd/ssl
# cd /etc/httpd/ssl
# (umask 077; openssl genrsa -out httpd.key 1024)
# openssl req -new -key httpd.key -out httpd.csr

签署证书

# openssl ca -in httpd.csr -out httpd.crt -days 1000

3、编辑配置文件/etc/httpd24/httpd.conf,启动下面的选项

Include /etc/httpd24/extra/httpd-ssl.conf
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so

4、编辑配置文件/etc/httpd24/extra/httpd-ssl.conf,修改下面的选项

DocumentRoot "/usr/local/httpd24/htdocs/ssl"
ServerName www.muluhe.com
SSLCertificateFile "/etc/httpd24/ssl/httpd.crt"
SSLCertificateKeyFile "/etc/httpd24/ssl/httpd.key"

5、安装证书

复制/etc/pki/CA/cacert.pem文件到Windows桌面并重命名为cacert.crt,其具体的安装步骤为:

双击桌面上"cacert.crt"-->点击安装"安装证书(I)..."-->点击"下一步"-->选择"将所有的证书放入下列存储(P)"-->点击"浏览(R)..."-->点击"下一步"-->点击"完成"-->在新的窗口中点击"yes"-->在新的窗口点击"确定"

6、编辑网页文件

# mkdir /usr/local/httpd24/htdocs/ssl
# cd /usr/local/httpd24/htdocs/ssl
# vim index.html
    其内容为:<h1>Hello,SSL, I'm here!</h1>

7、重启httpd24服务并测试

# /etc/rc.d/init.d/httpd24 restart

在浏览器中键入www.muluhe.com,可以得到如下结果

wKiom1QNvoiTz6GuAADB3dwNOhs734.jpg

在浏览器中键入https://www.muluhe.com,可以得到如下结果

wKioL1QNvszwgwTOAADlfGmf6Oc160.jpg