欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

马哥 21_03 _httpd属性配置 有大用

[root@localhost ~]# cd /etc/httpd/conf

[root@localhost conf]# pwd

/etc/httpd/conf

[root@localhost conf]# ls

httpd.conf  httpd.conf.bak  magic

[root@localhost conf]# grep "Section" httpd.conf

### Section 1: Global Environment

### Section 2: 'Main' server configuration

### Section 3: Virtual Hosts

主Server 2段和虚拟主机 3段不能同时出现,只能出现一个

主Server 2段 意味着只提供一个 web 服务器,不提供 虚拟主机的

虚拟主机 3段 意味着禁用了中心主机(或者说 'Main' server ) 而启用了虚拟主机,就意味着一台物理主机将提供多个不同的站点

所以说它俩不能同时启用就是因为这个原因

绝大多数能够在主Server中配置的属性都可以在虚拟主机中使用,(虽然并非每一个)

[root@localhost conf]#

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

................

................

### Section 2: 'Main' server configuration

#

# The directives in this section set up the values used by the 'main'

# server, which responds to any requests that aren't handled by a

# <VirtualHost> definition.  These values also provide defaults for

# any <VirtualHost> containers you may define later in the file.

#

# All of these directives may appear inside <VirtualHost> containers,

# in which case these default settings will be overridden for the

# virtual host being defined.

#


#

# ServerAdmin: Your address, where problems with the server should be

# e-mailed.  This address appears on some server-generated pages, such

# as error documents.  e.g. admin@your-domain.com 

#

ServerAdmin root@localhost        #一般而言,每一个站点,我们都要配置一下 服务器管理员 ServerAdmin  

# 如果打开不存在的页面时,可能这里会出现 管理员的邮箱地址(不过,因为没有配置,所以这里未看到)

# 邮箱地址其实就是ServerAdmin 所显示出来的地址 叫 服务器管理员 (Server服务管理员),这里的 服务器管理员 可以是每个虚似主机都可以定义的,每一个站点都需要一个 (不使用虚拟主机,只有一个中心主机,它也需要ServerAdmin )

image.png

#

# ServerName gives the name and port that the server uses to identify itself.

# This can often be determined automatically, but we recommend you specify

# it explicitly to prevent problems during startup.

#

# If this is not set to valid DNS name for your host, server-generated

# redirections will not work.  See also the UseCanonicalName directive.

#

# If your host doesn't have a registered DNS name, enter its IP address here.

# You will have to access it by its address anyway, and this will make

# redirections work in a sensible way.

#

#ServerName www.example.com:80        #服务器名字,如果不启用这个指令的话,意味着服务器在启动的时候,它会去反解,去反向解析你的ip地址到某一个主机名,能够能够解析成功,就把那个主机名当作你的服务器的名字,否则,可能会报错的,报错说你当前的那台主机没有主机名,因此我们使用localhost当作主机名,给你来个warning(警告),一旦发生这种情况的话,那就给本地主机一个主机名就可以了(可以打开 /etc/hosts 让 192.168.1.45 ip地址 有个主机名)

image.png

或者 

[root@localhost conf]# dig -x 192.168.1.45            (没结果)

[root@localhost conf]# dig -x 192.168.1.45 @127.0.0.1           (没结果)

假如有结果的话,也不会报错的

image.png

# 反正不管怎么的,能够决定你当前主机的主机名web服务器启动时就不会报错,否则可能会报错,如果不想它报错,给它个 ServerName  就可以了,但是在虚拟主机当中,尤其是基于主机名的虚拟主机当中,ServerName是必须的,不然的话,怎么能够根据主机名来定位虚拟主机呢

#[root@localhost conf]# ifconfig eth0

image.png

#eth0      Link encap:Ethernet  HWaddr 00:0C:29:81:DB:A1

#             inet addr:192.168.1.45  Bcast:192.168.1.255  Mask:255.255.255.0

#             inet6 addr: fe80::20c:29ff:fe81:dba1/64 Scope:Link

#             UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

#             RX packets:2808 errors:0 dropped:0 overruns:0 frame:0

#             TX packets:6311 errors:0 dropped:0 overruns:0 carrier:0

#             collisions:0 txqueuelen:1000

#             RX bytes:233346 (227.8 KiB)  TX bytes:1174652 (1.1 MiB)

#             Interrupt:67 Base address:0x2000





#

# UseCanonicalName: Determines how Apache constructs self-referencing

# URLs and the SERVER_NAME and SERVER_PORT variables.

# When set "Off", Apache will use the Hostname and Port supplied

# by the client.  When set "On", Apache will use the value of the

# ServerName directive.

#

UseCanonicalName Off        # ( Canonical 正式的) web服务器自身如何能够在url上实现自身的自我引用的,马哥说不说了,对我们来讲意义不大


#

# DocumentRoot: The directory out of which you will serve your

# documents. By default, all requests are taken from this directory, but

# symbolic links and aliases may be used to point to other locations.

#

DocumentRoot "/web/html"        #文档根目录,很关键,网页文件的存放位置,自己可以完全改一个位置

# 建一个 /web/html 目录  把 DocumentRoot  的路径改成 "web/html"

#

#[root@localhost conf]# mkdir /web/html -pv

image.png

#



#[root@localhost ~]# service httpd restart            重启下服务就可以了

#停止 httpd:                                               [确定]

#启动 httpd:                                               [确定]

# 找不到 以前的a.html (在 /var/www/html 下的 a.html)了  http://192.168.1.45/a.html 

#image.png

# 访问默认路径      http://192.168.1.45/

原来让列表 让选择根目录下的文件(比如 a.html)

现在呢?禁止访问,没权限访问这个服务器

image.png

# 再改回来,重新httpd服务器

image.png

# 重启服务 service httpd restart

# 现在再刷新 http://192.168.1.45/

image.png

# 事实上,我们每一个所定义的网页所在的位置路径目录里面的网页能够被哪些主机所访问,而且如何被访问,是有其属性定义的

http://192.168.1.45/var/www/html 为什么不是这个,对于网站本身来讲,根已经指向了 /var/www/html  所以这个根就相当于(相对于) /var/www/html  ,

#因此url路径和本地文件系统路径并不是一码事 一般而言,URL是相对于 DocumentRoot 的路径而言的,在我们的配置文件当中我们可以定义 DocumentRoot  对应路径下每一个文件能够被哪些ip地址的客户端访问,以及如何被访问,况且,它还可以定义,在访问的时候,你是否需要提供账号和密码 

# Each directory to which Apache has access can be configured with respect

# to which services and features are allowed and/or disabled in that

# directory (and its subdirectories).

#

# First, we configure the "default" to be a very restrictive set of

# features.

#

<Directory />

    Options FollowSymLinks

    AllowOverride None

</Directory>


#

# Note that from this point forward you must specifically allow

# particular features to be enabled - so if something's not working as

# you might expect, make sure that you have specifically enabled it

# below.

#


#

# This should be changed to whatever you set DocumentRoot to.

#

image.png

<Directory "/var/www/html">#这是一个容器, 指定这个路径,应该如何被访问,里面定义了一大堆指令,因为这是个容器,所以还要有结尾</Directory> ,,里面的参数(或指令) 是限定这个路径 /var/www/html 如何被访问的  所以上面 如果是 DocumentRoot "/web/html" 就不匹配了(这里就不生效了), 如果是  DocumentRoot "/var/www/html"  就匹配(就生效了),

因此image.png如果 DocumentRoot "/web/html" 因为没有定义启用任何选项,所以就不允许 Indexes 索引了,因为Options是默认没有任何选项的 可以重新定义一下 <Directory "/web/html"> 或把 把 <Directory "/var/www/html"> 改成 <Directory "/web/html">   就可以运行索引  Indexes 了

我 现在改成 如下 

image.png

#<Directory "/var/www/html">

 <Directory "/web/html">

# 然后重启服务  [root@localhost ~]# service httpd restart

image.png

# 可以看到 能够 Indexes了   http://192.168.1.45/

image.png


# 改成 none 吧,什么都不允许,比较安全     Options none

image.png

#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    Options Indexes FollowSymLinks        #   Options  就是定义 /var/www/html 下面的所有网页文档(网页文件) 能够在被访问的时候访问属性的    Options后面可以跟多个值,多个值可以用空格隔开, 

# Indexes 索引,就是没有首页,它把所目录下面的所有的文件都列出来,让你自己去选一个,把整个网页根目录下( /var/www/html 路径下)的每一个文件都列出来,这很便捷,但是很不安全(别人可以通通拿走你的网页),所以将来在生产环境中一定不要允许 Indexes ,但是默认配置为了让我们配置起来更为简单,所以它尽可能做得开放了,因此默认配置不适合我们生产环境中使用,所以Indexes选项先须要关掉(尤其在生产环境中,但是在有些时候可以不用管,把它作为下载站用的时候,应该把它启用起来,里面放一大堆rar文件,可以允许别人下载的,除非这种场景,要关掉Indexes

image.png )

# 把 Options 后面的 Indexes 去掉就是不允许的,因为默认是不允许的,默认每一个选项都没有

# Options

           #       None: 每一个选项都没有,任何选项都不支持

          #        Index: 允许索引目录

         #         FollowSymLinks:允许访问这个符号链接真正指向的那个文件  事实上,建议这个功能是不允许的 这个功能关掉,可以提高服务器的性能的    (FollowSymLinks  (Follow 跟随 跟踪 ) (Sym symbol 符号) (link 链接) 跟随符号链接的 ) 如果网页路径下有一个文件是符号链接,允许追踪符号链接文件,那就是允许访问这个符号链接真正指向的那个文件(符号链接指向的源文件)

          #        Includes 包含,这表示允许执行服务器端包含的,叫SSI格式(Server Sed Include)的网页文件,这事实上也是不安全的,一定不要允许支持,除非你知道自己在做什么

        #         SymLinksifOwnerMatch 是对 FollowSymLinks 更为进一步的限定,允许你执行符号链接,但是属主必须要跟执行httpd进程的那个用户相匹配,这一项意义不大,而且还要进行额外的检查的,这一项也不应该开放的,

        #         ExecCGI   (exec execute 执行)    是否允许执行cgi脚本的,这一项有些时候会有用,

        #         MultiViews 这是内容协商的一种机制,叫多视图,多功能视图,这是内容协商的一种机制,根据客户端来的语言和文字和判定我应该显示哪一种网页给你(所以可能有多个默认页,不同的用户通过不同的地方地域来访问的时候,显示的内容不一样),内容协商很消耗资源,除非你是国际化的站点网站,而且的确提供了这种功能,否则MultiViews是没必要显示的,对我们来讲,没有太大的意义

        #        All 表示启用所有选项,支持所有选项,在生产环境中没人这么玩

  

        

# [root@localhost conf]# cd /var/www/html/

# [root@localhost html]# ln -sv

# [root@localhost html]# ln -sv /etc/fstab /var/www/html/b.html

# 创建指向“/etc/fstab”的符号链接“/var/www/html/b.html”

# [root@localhost html]# ll

#总计 4

##-rw-r--r-- 1 root root 82 02-24 17:04 a.html

#lrwxrwxrwx 1 root root 10 02-25 04:00 b.html -> /etc/fstab        # 软链接 b.html指向了  /etc/fstab 

打开 http://192.168.1.45/b.html 由下图说明是允许访问这个链接文件的 

    image.png

#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride None        #允许覆盖,允许超越,允许改写  覆盖下面这两项 ,下面这两项是定义服务器的访问控制列表的

                                                                 #    Order allow,deny  (allow逗号deny,之间不能有空格)顺序 列表下面有一大堆,可以允许哪些客户端访问,不允许哪些客户端访问  先allow 后deny 意思是 deny 优先,是默认值(就是Deny from all)  最后一个有决定权  只要没有允许的都通通deny拒绝

                                                                 #    Allow from all        #允许所有人访问


#  Order:用于定义基于主机的访问控制功能的 能够基于IP,网络地址,主机名限制,定义访问控制功能的机制

               # 但是Order本身并不是定义访问控制机制的,Order本身是定义谁是默认原则的

                   #         Order allow,deny     #  allow  deny  是根据实际需要定义的

                   #         Allow from XXXXXX

                    #        Deny from XXXXXX

下面是一个例子

                                                            order allow,deny

                                                            deny from 220.181.51.110

                                                            deny from 111.206.36.17

                                                            deny from 220.181.51.42

                                                            deny from 123.125.67.216

                                                            deny from 111.206.36.143

                                                            deny from 111.206.36.145

                                                            deny from 111.206.36.8

                                                            deny from 111.206.36.14

                                                            deny from 220.181.51.58

                                                            deny from 123.125.67.216

                                                            deny from 61.132.207.194

                                                            deny from 61.148.18.138

                                                            allow from all                                                         


#

#                            当前这个网站仅允许  192.168.1.0/24访问 (正确的写法,只要写下下面两行,没定义的通通拒绝)

                                                                                    Order allow,deny

                                                                                    Allow from 192.168.1.0/24

                               当前这个网站不允许  192.168.1.0/24访问 (下面这两行的话,其实是不对的,因为拒绝了192.168.1.0/24,剩下的通通取默认值,也是通通拒绝)

                                                                                    Order allow,deny

                                                                                    Deny from 192.168.1.0/24

                                当前这个网站不允许  192.168.1.0/24访问 (正确的写法应该是如下 )(拒绝了了192.168.1.0/24, 剩下的通通取默认值,也是通通允许)

                                                                                    Order deny,allow

                                                                                    Deny from 192.168.1.0/24

                                当前这个网站不允许  192.168.1.0/24访问 (正确的写法的正确的写法 )(拒绝了了192.168.1.0/24, 剩下的通通取默认值,也是通通允许)                                

                                                                            order allow,deny

                                                                            deny from 192.168.1.0/24

                                                                            allow from all

                                当前这个网站不允许  192.168.1.100     172.16.100.177 访问          

                                                                            order deny ,allow       

                                                                            deny from  192.168.1.100  172.16.100.177         

                                                                        image.png                                  


自己测试 拒绝 49.77.214.93 正确一  默认拒绝,先允许所有,然后拒绝

order allow,deny

Allow from All

deny from 49.77.214.93

自己测试 拒绝 49.77.214.93 正确二 

order allow,deny

deny from 49.77.214.93

Allow from All

自己测试 拒绝 49.77.214.93 正确三

order deny,allow

deny from 49.77.214.93


自己测试 拒绝 49.77.214.93 错误一   都允许了,因为允许优先级高 All 包含了49.77.214.93 

order deny,allow

Allow from All

deny from 49.77.214.93

自己测试 拒绝 49.77.214.93 错误二 都允许了,因为允许优先级高 All 包含了49.77.214.93 

order deny,allow

deny from 49.77.214.93

Allow from All



# 记住  测试 httpd.conf文件的语法正确性的 用 # httpd -t 

                        image.png

# 重启服务器 #  service httpd restart

image.png

# vim /web/html/index.html 建一个 index.html 文档

image.png

# 可以发现不能访问了 http://192.168.1.45/     不允许列出列表,就是拒绝了 还是 通过Order 来禁用这个ip的?应该是通过Order禁用ip的吧

image.png

# elinks 纯文本的浏览器,在纯文本界面下浏览网页的一个工具

#  elinks URL        它能通过交互式打开一个网页,削去了所有图片的浏览器界面

#        -dump   把网页显示出来之后退出了,不再显示交互模式 (-dump 和  -source 一般不会同时用 )

#        -source  显示网页的源代码(html格式的文档源码)

@           [root@localhost ~]# man elinks

#            image.png

image.pngimage.png

#  比如  elinks http://192.168.1.45/            

[root@localhost ~]# elinks http://192.168.1.45        就可以 打开一个页面了 

image.png

[root@localhost ~]# elinks -dump http://192.168.1.45

   it works

[root@localhost ~]# elinks -source http://192.168.1.45

<html>

it works

</html>

[root@localhost ~]#

image.png




# Controls who can get stuff from this server.

#

    Order allow,deny

    Allow from all


</Directory>


#

# UserDir: The name of the directory that is appended onto a user's home

# directory if a ~user request is received.

#

# The path to the end user account 'public_html' directory must be

# accessible to the webserver userid.  This usually means that ~userid

# must have permissions of 711, ~userid/public_html must have permissions

# of 755, and documents contained therein must be world-readable.

# Otherwise, the client will only receive a "403 Forbidden" message.

#

# See also: http://httpd.apache.org/docs/misc/FAQ.html#forbidden

#

#




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

.....................

.....................


#<Directory "/var/www/html">

<Directory "/web/html">


#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    #Options Indexes FollowSymLinks

    Options none

#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride None   #指的是我们是不是不把下面两行生效了,我们还有别的控制法则,

                                                            #    Order allow,deny

                                                            #    Allow from all

                                        #通常后面跟的值是  AuthConfig    Limit    或者   FileInfo    等等

                                        #一般用得最多的就是 AuthConfig ,意味着我们需要基于一个文件中的用户名和账号需要认证以后

                                        #才允许访问,httpd的账号和密码不是/etc/passwd和/etc/shadow中的账号和密码,而是我们自己

                                        #建立的一个文件中的账号和密码 (比如我们在 /etc/httpd/conf 目录下建一个文件 一个口令文件,这个文件中放有用户的账号和密码,这个文件并不存在,需要手动建的,建完以后,我们只允许这个文件中的某个账号登录访问,我们在访问的时候,需要输一个账号和密码就能够访问了,所以这样子我们可以限定我们某些比较敏感的站点只能够让那些可以通过我们访问认证的用户才能够访问)(只要这里 AllowOverride AuthConfig ),并且提供一些额外选项就行了

                                        #加上如下的指令

                                        #        AuthType Basic                 # 表示我们使用基本用户认证类型(有两种一种是Basic,一种是Degist)

                                        #        AuthName "Restricted Files"            #表示在用户认证的时候,给它显示个名字,告诉你这是个什么东西,为什么需要认证

                                        #        AuthUserFile /usr/local/apache/passwd/passwords        #认证用户的时候,用户的账号密码文件是哪一个

                                        #        Require user marion        #表示只有 marion 这个用户才能登录的

                                        #

                                        #

                                        #

                                        #        AuthType Basic                 # 表示我们使用基本用户认证类型(有两种一种是Basic,一种是Degist)

                                        #        AuthName "Restricted Files"            #表示在用户认证的时候,给它显示个名字,告诉你这是个什么东西,为什么需要认证

                                        #        AuthBasicProvider file                            

                                        #        AuthUserFile /usr/local/apache/passwd/passwords        #认证用户的时候,用户的账号密码文件是哪一个

                                        #        AuthGroupFile /usr/local/apache/passwd/htgroups      #认证用户的时候,组的账号密码文件是哪一个,组的文件建立很简单,建一个组名,后面冒号一大堆用空格隔开的用户名就可以了

                                        #        Require group GroupName        #表示只有 GroupName 组中的用户才能够登录的

                                        #        

                                        #        Require valid-user                 #只要出现AuthUserFile /usr/local/apache/passwd/passwords 账号中的用户都可以登录的

                                        #

                                        #

                                        #

                                        #





                                                           


#

# Controls who can get stuff from this server.

#

    Order allow,deny  #先恢复原样,让正常情况下是可以访问的

    Allow from all

    #Order deny,allow

   #  Allow from all

    #Deny from 192.168.1.100 172.16.100.177



#  


#      [root@localhost ~]# service httpd restart

#      停止 httpd:                                               [确定]

#      启动 httpd:                                               [确定]

image.png

#          http://192.168.1.45/index.html  可以正常访问

image.png






</Directory>



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

....................

....................



....................

....................


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

..........................................

..........................................


<Directory "/web/html">


#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    #Options Indexes FollowSymLinks

    Options none


#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride AuthConfig  #改成  AuthConfig ,使用认证配置去覆盖用户的请求  

    AuthType Basic #认证类型是什么

    AuthName "Restricted Site..."               #让别人知道,到底是为了认证什么

    AuthUserFile  "/etc/httpd/conf/htpasswd"    #这个文件不能让其它用户可以看到里面的内容                 # 用户认证的时候,使用的文件叫什么  认证的时候,要求提供的(配置?)文件是什么,如果基于组来认证的话,也可以基于组来提供配置文件

    Require valid-user  # Require  user XXXX  到底这个文件中哪些用户(XXXX)可以登录呢,表示允许这个文件中是允许哪些用户登录的,如果允许这个文件中的所有用户都能登录的话,就使用 Require valid-user


 # 建立 /etc/httpd/conf/htpasswd 这个文件吧,使用命令 htpasswd ,这个命令就是帮我们去创建这个文件,并且在这个文件中添加用户,并为其指定密码的,当然也可以从这个文件中删除用户,,第一次创建文件的时候,使用-c (Create)选项,如果这个文件存在的话 再使用 -c ,会把这个文件的内容清空掉的,-m 表示用户的密码以md5加密的方式存放的

htpasswd [ -c ] [ -m ] [ -D ] passwdfile username   (passwdfile 指的是文件名)(username   指的是添加进来的用户)

  # man htpasswd

        [root@localhost ~]# man htpasswd

image.png

image.png


[root@localhost ~]# htpasswd -c -m /etc/httpd/conf/htpasswd hadoop

New password:

Re-type new password:

Adding password for user hadoop


[root@localhost ~]# htpasswd  -m /etc/httpd/conf/htpasswd tom        (注意因 /etc/httpd/conf/htpasswd文件己存在,所以建第二个用户的时候千万不能有-c )

New password:

Re-type new password:

Adding password for user tom

[root@localhost ~]# cat /etc/httpd/conf/htpasswd

hadoop:$apr1$mVmjx/..$Sdev/Wd1UPR.c9EMSkW2C.        #$加密方式$盐$加密结果   密码含有salt盐,

tom:$apr1$6hNDn/..$nKG28JFZ/WeVPmqY3Tg/w0

[root@localhost ~]# httpd -t            (-t test  表示测试吧)

[root@localhost ~]# service httpd restart

停止 httpd:                                               [确定]

启动 httpd:                                               [确定]

[root@localhost ~]#

#  打开浏览器    http://192.168.1.45/

image.png

# 输入用户名密码 就可以登录了

image.png

 # 所以我们在访问某些站点的时候,也可以要求用户在认证以后,才能访问的,

 # 换一个浏览器看看  http://192.168.1.45/

image.png

# Controls who can get stuff from this server.

#

    Order allow,deny

    Allow from all

#    Order deny,allow

   #  Allow from all

 #   Deny from 192.168.1.100 172.16.100.177


</Directory>

..........................................

..........................................



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

..........................................

..........................................

<Directory "/web/html">


#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    #Options Indexes FollowSymLinks

    Options none


#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride AuthConfig

    AuthType Basic

    AuthName "Restricted Site..."

    AuthUserFile  "/etc/httpd/conf/htpasswd"

    #Require valid-user

    Require user hadoop

#

# Controls who can get stuff from this server.

#

    Order allow,deny

    Allow from all

#    Order deny,allow

   #  Allow from all

 #   Deny from 192.168.1.100 172.16.100.177


</Directory>

image.pngimage.png

..........................................

..........................................


[root@localhost ~]# !ser

service httpd restart

停止 httpd:                                               [确定]

启动 httpd:                                               [确定]

[root@localhost ~]#

此时 hadoop 用户能正常访问

image.png

此时 tom  用户不能访问

image.png


基于组合来设定访问权限

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

..........................................

..........................................

<Directory "/web/html">


#

# Possible values for the Options directive are "None", "All",

# or any combination of:

#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews

#

# Note that "MultiViews" must be named *explicitly* --- "Options All"

# doesn't give it to you.

#

# The Options directive is both complicated and important.  Please see

# http://httpd.apache.org/docs/2.2/mod/core.html#options

# for more information.

#

    #Options Indexes FollowSymLinks

    Options none


#

# AllowOverride controls what directives may be placed in .htaccess files.

# It can be "All", "None", or any combination of the keywords:

#   Options FileInfo AuthConfig Limit

#

    AllowOverride AuthConfig

    AuthType Basic

    AuthName "Restricted Site..."

    AuthUserFile  "/etc/httpd/conf/htpasswd"

    AuthGroupFile  "/etc/httpd/conf/htgroup"

    Require group myuser

image.png

..........................................

..........................................


新一个组名文件

[root@localhost ~]# vim /etc/httpd/conf/htgroup

myusers: hadoop tom   #组名冒号后面 加进来几个用户就可以了(这些用户之间是用空格隔开的),这些用户必须事先创建密码文件的


[root@localhost ~]# httpd -t    # 检查文件错误,没问题

Syntax OK

[root@localhost ~]# !ser        # 重启服务

service httpd restart

停止 httpd:                                               [确定]

启动 httpd:                                               [确定]

[root@localhost ~]#

若有错,要以看报错的日志 错误日志很关键,通过查看错误日志,你就知道你的服务器到底配置错在哪里了

[root@localhost ~]# tail /var/log/httpd/error_log

image.png

# 可以打开了 输用户名密码 可以进去了  http://192.168.1.45

image.png


马哥 把 AuthGroupFile  "/etc/httpd/conf/htgroup" 路径错写成 /etc/httpd/confi/htgroup  就报错了 500 错误,服务器端错误 (Internal Server Error 服务器端错误)

image.png


[root@localhost ~]# man htpasswd

                      htpasswd    -D    (-D  delete ) 删除用户的  从我们事先建立好的配置文件中删除某一个用户

                                        (-d 是加密用的)

image.png

image.png


Apache所支持的认证机制不仅仅是 Basic,也支持基于 md5 摘要 digest方式的认证机制

[root@localhost ~]# ht

htcacheclean  htdigest      htpasswd      httpd.event   httxt2dbm

htdbm         htmlview      httpd         httpd.worker

htdbm 只是保证我们文件的存放位置不同,但是htdigest是另外一种认证机制,它们都比较麻烦,我们用的并不是特别的多,一般来讲 ,简单的Basic认证足够用了,所以我们有简单认证的实现方式就OK了,对我们来讲就够用了,         

Order allow,deny

allow from

deny from 

地址的表示方式

    IP

    network/netmask

    HOSTNAME 主机名   (例如 www.a.com)                     允许或拒绝这个域名内的所有主机访问 

    DOMAINNAME 域名  (例如  a.com magedu.com)      允许或拒绝这个域名内的所有主机访问

    Partial IP: 使用线状?的ip地址?    比如部分IP  (例如  172.16  表示172.16.0.0/16  还是 表示 172.16开头的ip )

    

在 http 2.4的版本当中,不再使用 allow , deny 这种机制的,到时候讲 2.4源码包的时候再说 



[root@localhost ~]#




<IfModule mod_userdir.c>        #判断某个模块是否存在了 判断这个模块到底有没有 使用 # httpd -l 

# mod_userdir就是是否支持用户在自己的家目录下建立各人的网页文件的,默认是不允许的

# 如果允许的话,意味着每一个用户 比如tom  可以使用 http://192.168.1.45/~tom/  访问自己家目录下的网页了,每个人都可以在自己家目录下的某个目录里面建立网站,然后使用 http://192.168.1.45/~用户名/  来访问  

    #

    # UserDir is disabled by default since it can confirm the presence

    # of a username on the system (depending on home directory

    # permissions).

    #

    UserDir disable  #默认是不允许的  我们想使启用的话, 把UserDir disable 注释掉,然后 把  #UserDir public_html 注释去掉   明确指定在用户的家目录下的哪一个目录里面放网页文件,可以当作用户的个人的网站访问入口,默认是  public_html,所以每一个用户只要在他的家目录下建一个public_html目录,在里面放网页文件,就要以访问到了,但是有个前提,用户的家目录别人是进不来的,(每个用户的家目录都是700的权限),任何其它人访问不到,所以包括我们运行httpd进程的apache用户也访问不到,

所以apache用户也访问不到这个页面,因此你得让用户家目录(还是家目录下的public_html?) 让其它用户具有进去的权限(也就是x权限)


    #

    # To enable requests to /~user/ to serve the user's public_html

    # directory, remove the "UserDir disable" line above, and uncomment

    # the following line instead:

    #

    UserDir public_html


</IfModule>


[root@localhost ~]# httpd -l   #可以看出 没有 mod_userdir 这个模块

Compiled in modules:

  core.c

  prefork.c

  http_core.c

  mod_so.c

[root@localhost ~]#

[root@localhost ~]# httpd -M        # 这是apache所支持的所有的额外装的模块

Loaded Modules:

 core_module (static)

 mpm_prefork_module (static)

 http_module (static)

 so_module (static)

 auth_basic_module (shared)

 auth_digest_module (shared)

 authn_file_module (shared)

 authn_alias_module (shared)

 authn_anon_module (shared)

 authn_dbm_module (shared)

 authn_default_module (shared)

 authz_host_module (shared)

 authz_user_module (shared)

 authz_owner_module (shared)

 authz_groupfile_module (shared)

 authz_dbm_module (shared)

 authz_default_module (shared)

 ldap_module (shared)

 authnz_ldap_module (shared)

 include_module (shared)

 log_config_module (shared)

 logio_module (shared)

 env_module (shared)

 ext_filter_module (shared)

 mime_magic_module (shared)

 expires_module (shared)

 deflate_module (shared)

 headers_module (shared)

 usertrack_module (shared)

 setenvif_module (shared)

 mime_module (shared)

 dav_module (shared)

 status_module (shared)

 autoindex_module (shared)

 info_module (shared)

 dav_fs_module (shared)

 vhost_alias_module (shared)

 negotiation_module (shared)

 dir_module (shared)        #这里有

 actions_module (shared)

 speling_module (shared)

 userdir_module (shared)

 alias_module (shared)

 rewrite_module (shared)

 proxy_module (shared)

 proxy_balancer_module (shared)

 proxy_ftp_module (shared)

 proxy_http_module (shared)

 proxy_connect_module (shared)

 cache_module (shared)

 suexec_module (shared)

 disk_cache_module (shared)

 file_cache_module (shared)

 mem_cache_module (shared)

 cgi_module (shared)

 version_module (shared)

 perl_module (shared)

 php5_module (shared)

 proxy_ajp_module (shared)

 python_module (shared)

 ssl_module (shared)

Syntax OK

image.png

[root@localhost ~]#



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


<IfModule mod_userdir.c>

    #

    # UserDir is disabled by default since it can confirm the presence

    # of a username on the system (depending on home directory

    # permissions).

    #

    #UserDir disable


    #

    # To enable requests to /~user/ to serve the user's public_html

    # directory, remove the "UserDir disable" line above, and uncomment

    # the following line instead:

    #

    UserDir public_html


</IfModule>

image.png


[root@localhost ~]# useradd hadoop

useradd:用户 hadoop 已存在

[root@localhost ~]#

[root@localhost ~]# useradd hadoop

useradd:用户 hadoop 已存在

[root@localhost ~]# su - hadoop

[hadoop@localhost ~]$ mkdir public_html

[hadoop@localhost ~]$ cd public_html

[hadoop@localhost public_html]$ pwd

/home/hadoop/public_html

[hadoop@localhost public_html]$ vim index.html

<html>

<body>

Hadoop's Site

</body>

</html>

[hadoop@localhost public_html]$ cd ..

[hadoop@localhost ~]$ exit

logout

[root@localhost ~]#

[root@localhost ~]# chmod o+x /home/hadoop/        (一定要让hadoop的加目录具体执行权限,换句话说,别的用户能进去)

[root@localhost ~]#

http://192.168.0.55/

image.png

http://192.168.0.55/~hadoop/            将来访问很多的互联网项目的时候,它的网站站点的格式都是这样子的,这种方式用户可以定义自己的站点的,这种方式并不是特别的安全

image.png


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


#<Directory /home/*/public_html>    #这里定义了家目录下的每一个用户的public_html

#    AllowOverride FileInfo AuthConfig Limit                # 1)  把它改成None

#    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec  2)把它改成None

#    <Limit GET POST OPTIONS>        # 这里限制 GET POST OPTIONS 为允许所有        3)把这一行,及下面的共8行启用起来  1) 2) 3) 都做了,会尽可能的安全的,否则的话 不要启用它

#        Order allow,deny

#        Allow from all

#    </Limit>

#    <LimitExcept GET POST OPTIONS>        # 除了 GET POST OPTIONS 方法之外的其它任何方法 都拒绝,是为了加强安全

#        Order deny,allow

#        Deny from all

#    </LimitExcept>

#</Directory>



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


DirectoryIndex index.html index.html.var        #目录索引,这里指的是主页,这里是定义,我们如果在访问路径的时候,没指定访问哪个页面,所默认访问的页面,,,(如果两个文件都有 自左而右匹配,左边最优先)(如果两个文件都没有的话 如果上面的Options 是 Indexes 那么就是列表,列出目录下的所有文件)(如果Options 为 不允许索引,那么就是 Forbidden 禁止访问)


AccessFileName .htaccess        # apache支持每个目录的访问控制, 在网站的任何一个路径下,创建个子目录,这个子目录下的网页文件,如何被访问,访问控制权的设置,可以单独定义访问控制法则的,只要在这个目录下建  .htaccess 文件,把访问控制机制写进去,,,,,,,但是这个东西,让apache的执行效率极低,一旦你定义了这个文件以后,访问控制法则是自上而下执行的,(如果父目录定义了某控制法则,子目录就会自动定义)(如果在父目录里面定义了 .htaccess 文件,如果它还有子目录,访问子目录的时候,就要搜索到父目录的.htaccess文件,所以这非常麻烦,,)(因此一般情况下,在一个真正的生产环境服务器上,这个功能应该是禁用的,我们也很少这样子去定义一个目录的访问控制权限,所以直接注释掉它,禁用它就可以了)


<Files ~ "^\.ht">  # 这是一个容器,是只对 .ht开头的文件生效的,这里波浪线  ~  意味着对  "^\.ht" 进行模式匹配的(正则表达式的匹配)  也就意味 你的文件被  "^\.ht" 所匹配到, (我们的很多文件,如 .htaccess .htpasswd .htgroup 等等是不允许别人通过网站来访问的,不然的话你的.ht开头的代码都被别人拿到了 )

    Order allow,deny

    Deny from all

</Files>


TypesConfig /etc/mime.types        #多功能多用途邮件扩展,这是让我们的http协议能够传输多媒体文档的(非文本格式的文件吧)   /etc/mime.types 这里面定义了我们支持哪些非文本文件吧(哪些mime类型)

DefaultType text/plain        #如果不指类型 (比如 aa  没有后缀名 或aa.xxx 这种格式我们没见过,)我们把它当作文本下的纯文本信息(简单的明文文本信息)

<IfModule mod_mime_magic.c> #如果 mod_mime_magic 存在的话, MIMEMagicFile  就是 conf/magic ,这个马哥不解释了,可能不重要,不什么好讲的

#   MIMEMagicFile /usr/share/magic.mime

    MIMEMagicFile conf/magic

</IfModule>


#

# HostnameLookups: Log the names of clients or just their IP addresses

# e.g., www.apache.org (on) or 204.62.129.132 (off).

# The default is off because it'd be overall better for the net if people

# had to knowingly turn this feature on, since enabling it means that

# each client request will result in AT LEAST one lookup request to the

# nameserver.

#

HostnameLookups Off        # Off 表示关闭主机名,只记录ip地址,  On就是打开主机名,记录主机名,不记录ip地址,(ip地址反解成主机名来记录,这样做极其浪费资源,会速度慢),

#EnableMMAP off  # 不解释

#EnableSendfile off  # 对提高系统性能很关键,以后讲到系统优化的时候再讲


ErrorLog logs/error_log        # 定义错误日志, 应该是在相对于  ServerRoot "/etc/httpd" 这个路径下吧

 

LogLevel warn        #日志级别,跟错误日志相关

   

# LogFormat  日志格式,定义的是访问日志的格式  百分号后面的字符到底什么意思? 表示宏

#  %h 客户端地址 %l 以哪个用户的身份 %u apache的用户(应该是.htpasswd (这个文件名字是可以自己自定义的)里面定义的用户) %t  什么时间 %r (reference) 来自于哪个网站  %>s 访问的什么页面

#  %{User-Agent}i 使用的浏览器是什么        http://apache.chinahtml.com/mod/mod_log_config.html#logformat 

#  http://httpd.apache.org/docs/2.2/mod/mod_log_config.html#logformat  有解释

image.png






image.png

image.pngimage.png

[root@localhost ~]# tail /var/log/httpd/access_log

%h (Remot host) 客户端地址

%l  (Remote logname) 远程主机上的用户登录的主机的登录名称 远程客户端主机 (windows客户端,使用的账号.这个账号不能随便传递给别人,所以这个账号大多数是 中短横线 - ,) 远程主机上登录的用户名,不是我们访问服务器网站登录的用户名

%u (Remot user) 我们的网站如果需要认证的话,用户比如 hadoop tom 等等,通过账号密码才能登录,这就表示登录网站的时候需要使用的用户名,如果没做认证,用户就为空

%t ( time ) 日志生成的时间,日志事件生成的时间

\"%r\%"    ( First line of request )反斜杠引号,表示显示为引号本身的,(反斜线表示转义,)  请求报文的第一行 (请求的方法 url获取的资源 版本号 )

%>s   (%s Status. 状态码  For requests that got internally redirected, this is the status of the *original* request  原始请求的状态码 --- %>s for the last.  %>s 最后(最近)一次请求的对应的内容的状态码)(一次请求一个页面,一个页面里面的请求有n个,这个不懂)

%b  (bytes)( Size of response in bytes 响应报文的字节大小 )

\"%{Referer}i\"        一个网站里面点一下链接,到另一个页面里面去,Referer表示是从哪个页面来的,Referer就是?还是就在报文首部,image.png, 在报文首部里面,Referer项那一行信息里面响应的是什么内容    所以它就表示我们的网站用户访问的时候来自于什么地方,上一个页面 (如果是在浏览器中直接输入的话,那就为空吧)

\"%{User-Agent}i\"       又是个首部,  i 表示发过来的是什么内容?    User-Agent指的是客户端浏览器的类型(用户代理)    Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/6 9.0.3486.0 Safari/537.36  表示是其中一个,它也判断不出来,,    (Comodo dragon 克莫多 龙 是一种chrome上(google 浏览器)开发的二次开发版本) IE浏览器应该能判断得出来,IE浏览器访问的话,显示的是Mozilla吧?

image.png



LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined  #每一个logformat 后面都有名字 combined  表示混合模式

LogFormat "%h %l %u %t \"%r\" %>s %b" common  # common  通用模式 普通模式

LogFormat "%{Referer}i -> %U" referer  #   referer   只记录客户端访问地址来源的

LogFormat "%{User-agent}i" agent #  agent 只记录客户端的浏览器类型的


CustomLog logs/access_log combined    # CustomLog 访问日志 是用来定义访问日志的日志文件及日志类型的


# 可以通过日志里面的访问记录来判断里面的页面访问量的  某一天某个页面被来自于不同地址的客户端访问了多少次,来自于哪个客户端倒底访问了多少页面,

PV: Page View   某一天(某小时)同一个页面被来自于不同地址的客户端访问了多少次,是网站统计里面常用的指标 页面访问量,一般PV是每天来统计的,今天的PV多少,昨天的PV是多少

UV:User View 也是网站统计里面常用的指标,客户访问量 来自于同一个IP地址的客户端访问了多少次    今天有多少个独立IP来访问,昨天有多少个独立IP来访问,  每天的独立IP量

记录的时候,只记录页面,不记录image,js,css,中记录完整意义上的页面

同一个页面连续出现了两次(打开后,再刷新)能不能算为两次访问,一般来讲不可以的算为两次访问,刷新的话,按道理来讲是不算的,不算有效的


我们的工作80%是与web相关的,有一部分是与游戏相关的,这样的工作大概就两大类,网站运维和游戏运维

也有很少是做项目集成的(承包一个项目,构建一个项目,要设计好,用多少台服务器,要装什么软件,,打点好,接下来就不用管了)

面临的工作主要就这三大类


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


ServerSignature On         # 先不忙说


Alias /icons/ "/var/www/icons/"        #别名

# 网站根目录  /web/html

#      http://192.16.100.1/bbs/a.html            # bbs 就是   /web/html 下面的一个目录

#      http://192.16.100.1/bbs/images/logo.jpg       bbs/images 这个目录 就在  /web/html 下面

#          所以 url 路径是相对于 网站根目录  /web/html 而言的

#  把 /www/forum    定义了一个别名 bbs ,那么 此时访问 http://192.16.100.1/bbs/images/logo.jpg 意味着 在  /www/forum   目录下 有个 images 目录    此时bbs不再是对应的(网站根目录  /web/html) 下的一个目录,bbs是映射到 /www/forum  这个目录的一个别名,这个路径 /www/forum可以不在我们的网页根目录下

<Directory "/var/www/icons">

    Options Indexes MultiViews

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>


建一个目录  /bbs/forum

[root@localhost ~]# mkdir -pv /bbs/forum

mkdir: 已创建目录 “/bbs”

mkdir: 已创建目录 “/bbs/forum”

[root@localhost ~]#

我们期望用户在访问路径的时候,http://ip/luntan 就能访问 /bbs/forum 目录下的所有文件了


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


Alias /icons/ "/var/www/icons/"    #/icons  后面有斜线,/var/www/icons也应该有  ,要带的话,就都带

Alias /luntan "/bbs/forum"        # /luntan后面没斜线,/bbs/forum也应该没有  ,要不带的话,就都不带

image.png

[root@localhost ~]# cd /bbs/forum/

[root@localhost forum]# vim index.html             (在/bbs/forum/ 目录下建一文件  index.html) 

<html>

        <body>/bbs/forum</body>

</html>



[root@localhost forum]# service httpd restart            (改了配置文件要重启服务)

停止 httpd:                                               [失败]

启动 httpd:                                               [确定]

[root@localhost forum]#

访问  http://192.168.1.45/luntan/   可以看到结果

image.png


定义网站文档根目录

定义网站文档的访问选项: Options  及其意义

基于主机(ip)的访问控制,

基于用户或组的访问控制,

用户个人站点的配置

错误日志

日志格式

访问日志        PV    UV

路径别名


上面的内容必须要能够自己去定义和实现,并尝试调整此前我们讲过的prefork模块或work模型的某一个参数值

(比如最少启动的空闲进程,最多启动的空闲进程怎么调整)




                                        #


普通分类: