歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> centos 6.7之httpd配置

centos 6.7之httpd配置

日期:2017/3/3 11:29:52   编辑:Linux技術

前言:

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>
本文出自 “少壯不努力,一生在內地” 博客,請務必保留此出處http://marility.blog.51cto.com/11643000/1825878

Copyright © Linux教程網 All Rights Reserved