apache服务是一个最常用的服务之一,以为现在人们获取信息的主要方式主要是通过
 浏览网页,因此apache服务是服务中主要的一个应用。
 首先apache服务主要的安装
 源码安装或者rpm包安装
 一下使用的是rpm安装的方式:
 我这里使用本地的yum源安装
 一、简单的安装和配置
 #yum install httpd
 #service httpd start //启动http服务
 # echo "hello" > /var/www/html/index.html
 然后在浏览器中输入 http://IP地址就能看到一个默认的页面了
 如果你做了一个本地缓存DNS的话,也可以使用域名直接访问
 或者使用一下命令也能访问
 #curl http://IP or #curl http://domain
 #service httpd status //查看httpd的运行状态
 #netstat -tnl | grep :80 //查看使用的端口号
 #httpd -t or service httpd configtest 测试
 
 apache多道处理模块
   perfork(进程模式:每一个用户独立的进程)
   worker(线程模式:开两个进程,用户在进程区域中开辟新的线程)
   redhat常使用prefirk
 二、UserDir,用户可以访问自己家目录下的网站
   在家目录上给apache用户执行的权限,然后访问
   http://domain/~USERNAME就可以访问自己的家目录下的网站了
   #vim /etc/httpd/conf/httpd.conf
      UserDir enable redhat,student
      UserDir  public_html
   #mkdir /home/redhat/public_html
   #mkdir /home/student/public_html
   #echo "redhat userdir page test" >/home/redhat/public_html/index.html
   #echo "student userdir page test" >/home/student/public_html/index.html
   #setfacl -m u:apache:x /home/redhat
   #setfacl -m u:apache:x /home/student
   #setenforce 0 //关闭selinux
   在浏览器中输入 http://domain/~redhat就可以访问redhat用户家目录下的网页了
 三、Alias路径下的别名
   Alias /aa "/PATH/index.html"  
   然后通过 http://domain/aa来访问网页
   #vim /etc/httpd/conf/httpd.conf
    Alias /ro "/www/web/"
   #mkdir -pv /www/web
   #echo "alias test page" >/www/web/index.html
   #setenforce0
   #service httpd restart
   在浏览器中输入 http://domain/ro就能通过别名的方式访问
 四、基于用户名认证的网站
   web认证的方式:文本文件,表单,mysql,ldap;加密方式有basic,md5
   通过设置认证网站,来增强网站的安全性,只有通过认证的的用户才有权限访问
   网站。
   具体步骤如下:
   #vim /etc/httpd/conf/httpd.conf
    添加如下内容
     <Directory "/www/web">
       AllowOverride Authconfig
       AuthName "Auth web"                     //提示信息
       AuthType basic                          //加密方式
       AuthUserFile /etc/httpd/conf/.webuser   //认证用户文件
       Require User user1                      //允许认证的用户
     </Directory>
   #htpasswd -c -m /etc/httpd/conf/.webuser user1  //创建.webuser文件,添加用户
   输如新密码
   #htpasswd -m /etc/httpd/conf/.webuser user2  //添加用户
   输入新密码
   #setenforce 0
   如果想设置组认证,需要在文件中添加如下内容
    #vim /etc/httpd/conf/.webgrps
      mygrp: user1 user2 。。。
    #groupadd mygrp
      <Directory "/www/web">
       AllowOverride Authconfig
       AuthName "Auth web"                     //提示信息
       AuthType basic                          //加密方式
       AuthUserFile /etc/httpd/conf/.webuser   //认证用户文件
       AuthGroupFile /etc/httpd/conf/.webgrps
       Require group mygrp                      //允许认证的用户组
     </Directory>  
   由于我是借用上面的别名网站,所以直接在浏览器中输入 http://domain/ro就会
   提示用户输入帐号和密码
   关于htpasswd的用法:
     htpasswd   创建apache认证用户的帐号和密码
         -c 创建
         -m 使用MD5加密
         -D 删除用户
 五、虚拟主机
   1、基于端口的虚拟主机
      #vim /etc/httpd/conf/httpd.conf
        添加如下内容:
         </VirtualHost *:80>
          DocumentRoot "/www/web/web1"
          ServerName  www.luowei.com
         </VirtualHost>
         </VirtualHost *:8080>
          DocumentRoot "/www/web/web2"
          ServerName  www.luowei.com
         </VirtualHost>          
       同时把DocumentRoot "/var/www/html"给注释掉
       #mkdir -vp /www/web/web{1,2}
       #echo "web1" > /www/web/web1/index.html
       #echo "web2" >/www/web/web2/index.html
       然后在浏览器中访问 http://www.luowei.com:80和http://www.luowei.com:8080进行
       测试(注:我这做了DNS服务器,解析 www.luowei.com)
   2、基于IP的虚拟主机
       #vim /etc/httpd/conf/httpd.conf
        添加如下内容:
         </VirtualHost 192.168.1.103:80>
          DocumentRoot "/www/web/web1"
          ServerName  www.luowei.com
         </VirtualHost>
         </VirtualHost 192.168.1.104:80>
          DocumentRoot "/www/web/web2"
          ServerName  www.luowei.com
         </VirtualHost>          
       同时把DocumentRoot "/var/www/html"给注释掉
       #mkdir -vp /www/web/web{1,2}
       #echo "web1" > /www/web/web1/index.html
       #echo "web2" >/www/web/web2/index.html
       #ifconfig eth0:0 192.168.1.103/24
       #ifconfig eth0:1 192.168.1.104/24
       然后在浏览器中输入 http://192.168.1.103http://192.168.1.104进行测试
   3、基于域名的虚拟主机
      假如我这里使用luowei.com和luowei.cn两个主机名
      那么一下的配置如下:
      NameServer *:80
       #vim /etc/httpd/conf/httpd.conf
        添加如下内容:
         </VirtualHost 192.168.1.103:80>
          DocumentRoot "/www/web/web1"
          ServerName  web1.luowei.com
         </VirtualHost>
         </VirtualHost 192.168.1.104:80>
          DocumentRoot "/www/web/web2"
          ServerName  web2.luowei.com
         </VirtualHost>          
       同时把DocumentRoot "/var/www/html"给注释掉
       #mkdir -vp /www/web/web{1,2}
       #echo "web1" > /www/web/web1/index.html
       #echo "web2" >/www/web/web2/index.html     
       #service httpd reload
      这样在浏览其中分别输入 http://web1.luowei.comhttp://web2.luowei.com进行测试
      如果你没有DNS服务器的话,可以在C:\Windows\System32\drivers\etc 添加解析名字
      格式如下:
      IP DOMAINNAME
      192.168.1.103 web1.luowei.com
      192.168.1.103 web2.luowei.com
      #httpd -S 查看使用的虚拟主机情况
六、cgi 通用网关接口 
      #vim /etc/httpd/conf/httpd.conf
      找到ScriptAlias  /cgi-bin/ "/var/www/cgi-bin/" 这是系统默认定义好的
      一个,cgi的位置。
      接下来就在上面的目录中创建一个a.cgi的脚本
      #cd /var/www/cgi-bin/
      #vim a.cgi
        #!/bin/bash
        echo -e "Content-type: text/html\n\n"
       
        echo "The current time is:`date`"
      #chmoe +x a.cgi
      然后在浏览器中输入 http://192.168.1.103/cgi-bin/a.cgi测试
七、ssl实现https
      ssl机制应用到http支持https
      要想支持ssl现状模块mod_ssl
      #yum install mod_ssl
      创建一个ca
      #cd /etc/pki/CA
      #openssl genrsa 1024 > private/cakey.pem
      #chmoe 700 !$
      #vim /etc/pki/tls/openssl.cnf修改如下
       dir             = /etc/pki/CA 
      #openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
      #mkdir certs newcerts crl
      #touch index.txt
      #echo 01 > serial
      #cd /etc/httpd/conf/
      #mkdir ssl
      #cd ssl
      #(umask 077;openssl genrsa 1024 > httpd.key)
      #openssl req -new -key httpd.key -out httpd.csr  证书请求
      #
      #openssl ca -in httpd.csr -out httpd.crt -days 3650
      把上面生成的cacert.pem改名为cacert.crt并导入浏览器中
      “工具”-->Internet选项-->内容-->证书-->受信任的根证书颁发机构-->导入就行了
      #cd /etc/httpd/conf.d/
      #vim ssl.conf修改一下几项
        DocumentRoot "/www/web/web1"
        ServerName web1.luowei.com:443
        SSLCertificateFile /etc/httpd/ssl/httpd.crt
        SSLCertificateKeyFile /etc/httpd/ssl/httpd.key
      保存就可以了
      #httpd -S
      #netstat -ntul |grep :443查看端口是否启用
      然后在浏览器中输入 https://www.luowei.com就可以访问网页了,可以看到网页上有 一个小锁,呵呵,这就是效果,如果不把ca的证书导入到浏览器中,在你打开网页的时候 会有个提示的。