centos 6.7之httpd配置

前言:

http在osi中的位置:

在osi的七层模型中,http(hyper text tranfer protocol)位于第七层应用层,是一种本地与网络主机连接传输的协议,其部分基于位于第四层传输层的TCP协议(还有UDP协议),而TCP协议又是基于第三层网络层的IP协议。


http与httpd的关系

实现httpd应用协议的web服务器现在主流有三种:

httpd,也就是大家熟知的apache服务中的主程序

ngnix

lighttpd


-------------------------分割线--------------------------


centos 6默认httpd版本为2.2版本,centos 7默认httpd为2.4版本


一、httpd的安装

 yum -y install httpd

 a.通过rpm -ql httpd命令可以观察

    1.配置文件:/etc/httpd/conf/httpd.conf

        其中额外的配置文件目录:/etc/httpd/conf.d/*.conf,适用配置虚拟主机等

    2.程序文件:/usr/sbin/httpd

    3.日志文件:/var/log/httpd

    4.模块文件:/usr/lib64/httpd/modules

 b.httpd服务相关状态及启用

    service httpd start  //启动httpd服务

    service httpd restart //重启httpd服务

    service httpd reload  //重读httpd服务配置

    chkconfig httpd on|off //开机自启或关闭httpd服务


二、httpd的基本配置

 a.监听端口修改

    1.vim /etc/httpd/conf/httpd.conf

    2.搜索Listen

    3.修改格式Listen [IP:] PORT

    4.实例:Listen 172.16.45.67:8080

    5.httpd -t检查语法

    6.service httpd reload|restart

    注意点:Listen中的如果要添加ip,一定为本机ip

        修改ip后,一定要进行service restart

        同一个端口不能出现两个不同的ip


 b.DSO:Dynamic shared objects动态共享模块

    1.vim /etc/httpd/conf/httpd.conf

    2.搜索LoadModule

    3.不需要的模块可进行注释

    4.模块切换:修改/etc/sysconfig/httpd中的HTTPD值

        HTTPD=/usr/sbin/httpd|httpd.worker|httpd.event

    5.查看静态编译模块:httpd -l


 c.站点访问控制

    1.基于ip地址的访问控制

        vim /etc/httpd/conf/httpd.conf

        搜索DocumentRoot找到根目录

         然后添加如下代码

<Directory "PATH/TO/SOME_DIR">  //PATH路径为根目录地址
        Order allow,deny
    Allow from 172.16
    Deny from 172.16.45.72
    Deny from all
</Directory>

    其中ip来源请求是遵循最佳匹配法则机制

    如果上实例:172.16.45.72不可访问,

           172.16.45.01可以访问

           192.168.1.10不可访问

    2.基于文件系统和用户进行控制

        vim /etc/httpd/conf/httpd.conf

        搜索DocumentRoot找到根目录

         然后添加如下代码

<Directory "/PATH/TO/SOME_DIR">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "SOME_STRING_HERE"  //显示给用户的信息
    AuthUserFile "/PATH/TO/HT_PASSWD_FILE"     //AuthUserFile "/etc/httpd/conf/.htpasswd "
    Require user user1 user2...   //也可以使用Require valid-user 表示所有用户都合法
</Directory>

         其中用户密码可以使用htpasswd命令来生成

        htpasswd [options] /PATH/TO/HT_PASSWD_FILE USERNAME

    3.基于组账号进行控制

        设置同用户账号设置,但在代码中新加入了组的控制

<Directory "/PATH/TO/SOME_DIR">
    Options None
    AllowOverride None
    AuthType Basic
    AuthName "SOME_STRING_HERE"
    AuthUserFile "/PATH/TO/HT_PASSWD_FILE"
    AuthGroupFile "/PATH/TO/HT_GROUP-FILE"    //authgroupfile "/etc/httpd/conf/.groupwd"
            //然后在/etc/httpd/conf/.groupwd编写 mygroup:user1 user2
    Require group grp1 grp2...      //grp1填写mygroup,与上述文件中内容的名称一致
</Directory>


 d.定义站点别名

        vim /etc/httpd/conf/httpd.conf

        别名定义格式:Alias /URL/ "/PATH/TO/SOME_DIR/"

        实例:Alias /images/ "/var/www/html/pictures/"

            其中"/var/www/html"为DocumentRoot值


说明:其中images并非为系统具体目录,即/var/www/html/pictures/存在一个logo.jpg的文件时,使用172.16.45.67/images/logo.jpg可以访问到该文件,images即相当于是/var/www/html/pictures


 e.日志设定

   日志格式:LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

    

%h:remote host
%l:remote logname(from identd)
%u:remote user(from auth)
%t:time the request was received(stand english format)
"%r":first line request
%s:status code for request tha got internally redirected
%b:size of response in bytes,excluding HTTP headers
"%{Referer}i":The contents of Referer header line(s) in the request sent to the server
        //发送到服务器的请求引用者的首部的内容 i表示取其值
"%{User-Agent}i":The contents of User-Agent header line(s) in the request sent to the server
        //客户端的浏览器类型


 f.虚拟主机设定

    虚拟主机设定有用种:基于ip,基于端口,基于FQDN,三种设定基本类似

    本文以基于ip作示例:

    1.vim /etc/httpd/conf.d/vhost1.conf  //不在conf/httpd.conf中直接修改

        修改NameVirtualHost值,其中ip监听地址为本机地址

    2.在vhost1.conf中加入以下代码

<VirtualHost 172.16.45.71:80>
    ServerName www1.magedu.com
    DocumentRoot /tmp/vhosts/www1
</VirtualHost>

    在/tmp/vhost/www1文件夹中加入index.html,并写入内容,访问172.16.45.71即可查看到内容








练习:写一个脚本,批量生成10个FQDN虚拟主机配置:

    要求配置文件为/etc/httpd/conf.d/virhost#.conf

#/bin/bash
#
#
Ip=$(ifconfig | head -n 3 | grep "inet addr" | awk -F: '{print $2}'| awk '{print $1}')
    //提取本机ip
echo "your IP is $Ip"
cp /etc/httpd/conf/httpd.conf{,.bak}
    //备份httpd配置文件
sed -i "s@.*\<NameVirtualHost\>.*@NameVirtualHost $Ip:80@" /etc/httpd/conf/httpd.conf
    //更改NameVirtualHost值,启用虚拟主机
Virtualhost() {
    echo "<VirtualHost $Ip:80>" >/etc/httpd/conf.d/virhost$1.conf
    echo -e "\tServerName www$1.chunlanyy.com" >>/etc/httpd/conf.d/virhost$1.conf
    echo -e "\tDocumentRoot /tmp/virhost/www$1" >>/etc/httpd/conf.d/virhost$1.conf
    mkdir -p /tmp/vhost/www$1
    echo "</VirtualHost>" >>/etc/httpd/conf.d/virhost$1.conf
}
        //函数进行配置文件的写入
Hostfile(){
    mkdir -p /tmp/virhost/www$1/
    touch /tmp/virhost/www$1/index.html
    echo "<h1>www$1 site</h1>" >/tmp/virhost/www$1/index.html
}
        //函数进行访问显示内容的写入
for I in {1..10};do
    Virtualhost $I
    Hostfile $I
done
echo "virtualhosts has setup"


修改172.16.45.72主机的/etc/hosts内容

172.16.45.67 www1.chunlanyy.com

172.16.45.67 www2.chunlanyy.com

...

依次将10个名称写完

然后再使用172.16.45.72主机进行访问,结果如下

[root@chunlanyy tmp]# curl www{1..10}.chunlanyy.com
<h1>www1 site</h1>
<h1>www2 site</h1>
<h1>www3 site</h1>
<h1>www4 site</h1>
<h1>www5 site</h1>
<h1>www6 site</h1>
<h1>www7 site</h1>
<h1>www8 site</h1>
<h1>www9 site</h1>
<h1>www10 site</h1>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章