编译安装 apache(httpd);测试环境 vmware workstation 10,操作系统Centos6.5. 

工作IP172.16.26.5  作为CA服务器的ip 172.16.26.2

网关172.16.0.1

准备好安装包[因为httpd2.4.6需要高版本的apr支持,而yum源中的apr都是1.5版本以下的,不适用]

apr-1.5.0.tar.bz2, apr-util-1.5.2.tar.bz2,httpd-2.4.6.tar.bz2

放置在/tmp下操作

tar -xvf apr-util-1.5.2.tar.bz2  

tar -xvf apr-1.5.0.tar.bz2

tar -xvf httpd-2.4.6.tar.bz2

[root@localhost tmp]# ls 
apr-1.5.0 apr-util-1.5.2 httpd-2.4.6 ks-script-5pkrEC.log

httpd编译安装与配置_服务器

在安装apr包前请先检查 "Server Platform Development"   "Development tools" 这两个包有没有装上,如果没有执行以下命令安装
  yum groupinstall Server Platform Development

  yum groupinstall Development tools

[root@localhost apr-1.5.0]# yum grouplist  ==>查看组,如果上述两组在以下分类中,即已安装

Installed Groups:    =>安装的组

Available Groups:    =>有效的组

进入apr-1.5.0/目录

[root@localhost apr-1.5.0]# pwd 
/tmp/apr-1.5.0 

 [root@localhost apr-1.5.0]#./configure --prefix=/usr/local/apr1.5.0 =>配置检验

  [root@localhost apr-1.5.0]# make && make install    =>安装

进入/tmp/apr-util-1.5.2安装目录

[root@localhost apr-util-1.5.2]# pwd 
/tmp/apr-util-1.5.2

[root@localhost apr-util-1.5.2]# ./configure --prefix=/usr/local/apr-util1.5.2 --with-apr=/usr/local/apr1.5.0/  =>配置检验

[root@localhost apr-util-1.5.2]# make && make instal      =>安装

进入httpd-2.4.6安装目录

[root@localhost httpd-2.4.6]# pwd 
/tmp/httpd-2.4.6

--enable-so DSO 是否动态加载模块,其实默认是自动的 

 --enable-ssl 是否支持ssl

 --enable-cgi  CGI:Common Gateway Interface   简化版的http
 --enable-rewrite  url重写功能 =>依赖于 pcre 正则表达式解析器

--with-zlib  支持压缩

--with-pcre    正则表达式解析器

--with-apr 不支持的系统,需要依赖apr

-enable-mpms-shared=all  所有的mpms都支持

--with-mpm=event    [如果不用event的话,还有prefork.c,  worker.c 的可以选择]

--enable-modules=most  =>安装大部分的模块,也可以选择all

因为等会需要配置https安全访问,需要依赖 mod_ssl模块

[root@localhost httpd-2.4.6]# yum install mod_ssl    ==>安装mod_ssl

同时httpd依赖于pcre的开发包,

[root@localhost ~]# yum install pcre-devel    ==>安装pcre的开发包

如果openssl版本不足以支持新版的httpd2.4.6那么也要安装, 

[root@localhost httpd-2.4.6]# yum install openssl-devel

所有准备工作就绪,开始配置httpd-2.4.6

[root@localhost httpd-2.4.6]#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr1.5.0 --with-apr-util=/usr/local/apr-util1.5.2/ --enable-mpms-shared=all --with-mpm=event --enable-modules=most

[root@localhost httpd-2.4.6]#make && make install ==>配置完毕,可以安装了

httpd编译安装与配置_服务器_02

将apachectl所在的目录加入环境变量

在/etc/profile.d/下新建一个文件 httpd.sh,写入以下内容

PATH=/usr/local/apache/bin:$PATH

加入man命令 编辑/etc/man.config

[root@localhost bin]# vim /etc/man.config

MANPATH /usr/local/apache/man    ==>加入内容s

加入启动脚本    注意红色字体,关键位置修改

 #!/bin/bash

. /etc/rc.d/init.d/functions

HTTPD_LANG=${HTTPD_LANG-"C"}

INITLOG_ARGS=""

apachectl=/usr/local/apache/bin/apachectl

httpd=${HTTPD-/usr/local/apache/bin/httpd}

prog=httpd

pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}

lockfile=${LOCKFILE-/var/lock/subsys/httpd}

RETVAL=0

STOP_TIMEOUT=${STOP_TIMEOUT-10}

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 ${STOP_TIMEOUT} $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=6

        echo $"not reloading due to configuration syntax error"

        failure $"not reloading $httpd due to configuration syntax error"

    else

        # Force LSB behaviour from killproc

        LSB=1 killproc -p ${pidfile} $httpd -HUP

        RETVAL=$?

        if [ $RETVAL -eq 7 ]; then

            failure $"httpd shutdown"

        fi

    fi

    echo

}

case "$1" in

  start)

 start

 ;;

  stop)

 stop

 ;;

  status)

        status -p ${pidfile} $httpd

 RETVAL=$?

 ;;

  restart)

 stop

 start

 ;;

  condrestart|try-restart)

 if status -p ${pidfile} $httpd >&/dev/null; then

  stop

  start

 fi

 ;;

  force-reload|reload)

        reload

 ;;

  graceful|help|configtest|fullstatus)

 $apachectl $@

 RETVAL=$?

 ;;

  *)

 echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"

 RETVAL=2

esac

exit $RETVAL

[root@localhost ~]# service httpd start  ==>启动httpd服务器
Starting httpd: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain. Set the 'ServerName' directive globally to suppress this message 
[ OK ]

httpd编译安装与配置_服务器_03

apache httpd服务安装好了.

下面讲讲apache的优化配置工作.

1,持久连接设置.

在 /etc/httpd24/httpd.conf中

Include /etc/httpd24/extra/httpd-default.conf 引入这个配置文件

KeepAlive On  =>这一项改为On , 启用持久连接

MaxKeepAliveRequests 100 在使用持久连接时,最大的请求个数

KeepAliveTimeout 5    在这里配置最长保持连接时间        这两个参数,在生产环境中,按具体需求配置.

2、MPM参数: 

  MaxKeepAliveRequests 100   最大请求资源数 [ 建议30 ] 

  KeepAliveTimeout 15    最大请求时间 [ 建议 2s ]

 <IfModule prefork.c> ==>加载不同的模块,所使用不同的配置

     StartServers 8                =>启动时就创建空闲进程数

      MinSpareServers 5       =>最小空闲进程

      MaxSpareServers 20    =>最大空闲进程

        ServerLimit 256            =>最多启动的进程数

      MaxClients 256            =>最大客户端进程

     MaxRequestsPerChild 4000    =>每个进程能处理的请求

  </IfModule>

 <IfModule worker.c>

  StartServers 4                    =>启动时就创建空闲进程数

  MaxClients 300                 ==>最大用户请求

  MinSpareThreads 25        =>最小空闲线程

  MaxSpareThreads 75        =>最大空闲线程

  ThreadsPerChild 25           =>每个子进程可以启动的线程

  MaxRequestsPerChild 0    =>每个线程能处理的请求 0是不做限制

  </IfModule>

3、指定监听的地址和端口 
Listen [IP:]PORT  ==>如果ip参数省略,说明是监听所有的地址
此指令可重复指定多次; 

4、DSO机制装载的模块 显示所加载的模块,可以使用命令 httpd -D DUMP_MODULES 

httpd编译安装与配置_服务器_04

使用格式:LoadModule Module_Name /path/to/Module_File

例如,要加载mod_dir.so ,将引入mod前的#反注释就OK

LoadModule dir_module modules/mod_dir.so

5、指定站点根目录 
DocumentRoot "/path/to/somewhere"  格式

DocumentRoot "/usr/local/apache/htdocs" =>系统默认的根目录

例如,我们对系统指定的网站服务根目录做修改,改为 /usr/local/apache/www

 mkdir /usr/local/apache/www    ==>创建目录

  touch /usr/local/apache/www/show.html

  touch /usr/local/apache/www/news.html

  echo 'this is show'>/usr/local/apache/www/show.html

  echo 'this is news'>/usr/local/apache/www/news.html

  service httpd restart 

别外在服务根目录下创建两个文件,重启服务测试一下.

httpd编译安装与配置_服务器_05

OK,服务根目录修改成功.

6、站点路径访问控制

很明显,像刚才那样的访问,因为服务根目录下,没有默认的index.html页面,所以会显示一个资源列表,如果不是作为下载站的话,是相当不安全的,那么我们就要对其做路径访问控制.

   基于本地文件系统路径:用法如下

    <Directory "/path/to/somewhere">

    </Directory>

以刚才新建的服务根路径为例,我们配置一下访问控制

在<Directory "/path/to/somewhere"></Directory>标签对中,有丙个很重要的控制选项.

    

(1) Options 
Indexes: 当访问的路径下无默认的主页面,将所有资源以列表形式呈现给用户;危险,慎用; 
FollowSymlinks: 跳跃符号链接是否允许     -FollowSymLinks  就是功能不启用了 
(2) AllowOverride 
支持在每个页面目录下创建.htaccess用于实现对此目录中资源访问时的访问控制功能。  

AllowOverride None  =>是否启用                 用于实现对目录中资源访问时的访问控制功能 

本例配置如下,

<Directory "/usr/local/apache/www">  
      Options -Indexes -FollowSymLinks  不允许资源列表形式展现,不允许符号链接跳转
     AllowOverride None  不允许使用.htaccess对此目录做访问控制
     Require all granted 允许所有主机访问本站资源
</Directory>

   基于URL访问路径做访问控制

    <Location "/path/to/URL">

    </Location> 

将参数修改后,不指定所访问的资源后,访问将不再被允许

httpd编译安装与配置_操作系统_06

httpd编译安装与配置_操作系统_07

如何做到精细控制,例如不允许某ip访问呢?

<Directory "/usr/local/apache/www">  
      Options -Indexes -FollowSymLinks  不允许资源列表形式展现,不允许符号链接跳转
     AllowOverride None  不允许使用.htaccess对此目录做访问控制
     Require all granted 允许所有主机访问本站资源
</Directory>

修改 Require选项,要在外面套上RequireAll标签对,

 <RequireAll>

     require all granted    =>允许所有的主机访问

     require not ip 172.16.26.1    =>除了此ip

    附:如果想阻止整个网段的访问,怎么办?  这样就好了 172.16.0.0/16 

 </RequireAll>

更改完配置,重读httpd配置文件

httpd编译安装与配置_操作系统_08

访问不OK了. 为方便接下来测试,把这个限制先取消

7,定义默认的主页面,

如,上述条件中,不允许对不确定的资源访问,如果我们把默认的主页面中,加入show.html选项,看能自动访问到不?


httpd编译安装与配置_操作系统_09

httpd编译安装与配置_服务器_10

8、配置日志功能 
ErrorLog "/path/to/error_log"  错误日志
LogLevel {debug|info|notice|warn|error|crit|alert|emerg} 错误日志级别

LogFormat 
CustomLog "/path/to/access_log" LogFormat_Name     访问日志配置

%h: 客户端地址 
%l: 远程登录名,通常为- 
%u: 认证时输入用户名,没有认证时为- 
%t: 服务器收到 用户请求时的时间 
%r:请求报名的起始行 
%>s: 响应状态码 
%b: 响应报文的长度,单位是字节 
%{HEADER_NAME}i: 记录指定首部对应的值 

9、路径别名  
实现URL路径的映射,从而所访问的资源不再依赖于站点根目录;  
Alias /URL/ "/path/to/somewhere/" 但是通常站点开发不这么做,这里不再详述

10做用户访问控制

如站点中,有一个比较敏感的目录,需要权限才能访问的,那怎么控制呢?

11,cgi-bin脚本控制

/etc/httpd24/extra/httpd-ssl.conf配置中,有一行是

<Directory "/usr/local/apache/cgi-bin">指明了其路径,在路径中按规则写入sh脚本,就可以执行了

12、基于用户访问控制

  用户认证:

   基本认证: Basic

   摘要认证:digest

下面示意一个文件认证的方式

 <Directory "/web/a/houtai">

         Options none

 AllowOverride AuthConfig

 AuthType Basic

 AuthName "please input you key"

 AuthBasicProvider file

 AuthUserFile   /etc/httpd24/.htpasswd robert

 Require valid-user

 </Directory>

创建认证文件

htpasswd

    -c: 如果此文件事先不存在,则创建;注意,只能在创建第一个用户时使用;

    -m:以md5的格式编码存储用户的密码信息

    -D:删除指定用户

[root@localhost extra]# htpasswd -m -c /etc/httpd24/.htpasswd robert

此时重读httpd配置文件,访问页面时,就需要权限才能访问了

httpd编译安装与配置_操作系统_11

httpd编译安装与配置_服务器_12

当然访问认证还有很多种方式,不过这类场景一般不这样用,通常这类功能由程序开发提供,此处不再深入探讨.

13配置虚拟主机,以及可以使其使用https安全访问

下面配置 物理机可以以 https://www.a.com访问

配置虚拟主机 www.a.com 

将172.16.26.2作为CA证书服务器, 向其申请证书,配置为https安全访问

附建立CA服务器过程

一、建立CA服务器:

   1、生成密钥

  (umask 077; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)   2048是密钥的长度

==>没错,就这个命令,在 /etc/pki/CA/private 生成一个密钥文件cakey.pem [因为配置文件就是叫这个名字,所以必须生成一个叫cakey.pem的文件]

   2、自签证书

   # openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365      (cacert.pem  这个文件名也是配置文件定义的)

自签证书时,指定私钥文件,会自动提取出公钥文件, 并可指定有效期限 365天

req 子命令 

    -new 生成新请求

    -x509  创建自签署证书   这是专用选项,只有自签才有用  

    -key 指明私钥文件 /path/from/somefile

    -out 证书存入位置    /path/to/somefile

   -days 证书有效期, 只有在和x509一起用才有效,因为申请的证书不能自己决定有效时长,除非是自己给自己发证书

[root@www private]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 365  执行此命令
                                             签署     新请求  自签选项   来源私钥文件                               生成的证书文件

You are about to be asked to enter information that will be incorporated 
into your certificate request. 
What you are about to enter is what is called a Distinguished Name or a DN. 
There are quite a few fields but you can leave some blank 
For some fields there will be a default value, 
If you enter '.', the field will be left blank. 
----- 
Country Name (2 letter code) [XX]:cn 
State or Province Name (full name) []:hn 
Locality Name (eg, city) [Default City]:zz 
Organization Name (eg, company) [Default Company Ltd]:robert 
Organizational Unit Name (eg, section) []:cto 
Common Name (eg, your name or your server's hostname) []:ca2.robert.com 
Email Address []:admin@robert.com                 填入各种信息 证书生成


[root@www CA]# pwd 
/etc/pki/CA ==>在证书目录下
[root@www CA]# ls 
cacert.pem certs crl index.txt newcerts private serial    ==>果然有证书文件

   3、初始化工作环境      索引文件  序列号文件

   # touch /etc/pki/CA/{index.txt,serial}    ==>要想工作,就得有这两个文件 index.txt 索引文件

   # echo 01 > /etc/pki/CA/serial    ==>序列号文件,要指明从哪里开始

在172.16.26.5上建立密钥对

[root@localhost ssl]# openssl genrsa -out /etc/httpd24/ssl/httpd.key 2048

[root@localhost ssl]# pwd     
/etc/httpd24/ssl         ==>在此目录下,生成证书签署请求
[root@localhost ssl]# openssl req -new -key ./httpd.key -out ./httpd.csr    =>生成证书签署请求

在172.16.26.2主机上,将 172.16.26.2上的证书请求拷过来

[root@ca2 csr]# scp root@172.16.26.5:/etc/httpd24/ssl/httpd.csr ./httpd.csr

[root@ca2 csr]# openssl ca -in ./httpd.csr -out ./httpd.crt -days 300 ==>为其签署证书

开始配置https

[root@localhost ssl]# vim /etc/httpd24/httpd.conf

[root@localhost extra]# vim /etc/httpd24/httpd.conf     编辑配置文件,开启以下三个模块

LoadModule rewrite_module modules/mod_rewrite.so

LoadModule ssl_module modules/mod_ssl.so

LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

开启 Include /etc/httpd24/extra/httpd-ssl.conf

安装 mod_ssl模块 

[root@localhost ssl]# yum install mod_ssl -y

编辑/etc/httpd24/extra/httpd-ssl.conf

[root@localhost ssl]# vim /etc/httpd24/extra/httpd-ssl.conf

关键位置修改

<VirtualHost _default_:443>  
# General setup for the virtual host 
DocumentRoot "/web/a" 
ServerName www.a.com

SSLCertificateFile /etc/httpd24/ssl/http.crt     ==>证书

# Server Private Key:

# If the key is not combined with the certificate, use this

# directive to point at the key file. Keep in mind that if

# you've both a RSA and a DSA private key you can configure

# both in parallel (to also allow the use of DSA ciphers, etc.)

SSLCertificateKeyFile /etc/httpd24/ssl/http.key    ==>私钥

#为了节约带宽,我们一般会将页面内容压缩后,再向前端发送

我们做一个测试,在/web/a/index.html中,添加许多文字内容.

访问测试, Content-Length:250359

httpd编译安装与配置_服务器_13

修改/etc/httpd24/httpd.conf

LoadModule deflate_module modules/mod_deflate.so ==>引入该模块

#,在一个公共区域添加以下内容

AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/x-javascript

AddOutputFilterByType DEFLATE text/javascript

AddOutputFilterByType DEFLATE text/css

# 以上是所要压缩的格式内容

# Level of compression (Highest 9 - Lowest 1)

DeflateCompressionLevel 9  

# Netscape 4.x has some problems.

BrowserMatch ^Mozilla/4 gzip-only-text/html  

# Netscape 4.06-4.08 have some more problems

BrowserMatch ^Mozilla/4\.0[678] no-gzip  

# MSIE masquerades as Netscape, but it is fine

BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

添加完成,重启httpd服务

[root@localhost ~]# service httpd restart 
Stopping httpd: [ OK ] 
Starting httpd: [ OK ]

访问测试, Content-Length:856 资源节约相当明显

httpd编译安装与配置_服务器_14