linux服务之httpd

http://mirrors.cnnic.cn/apache/httpd/docs/  英文pdf文档下载

 

ServerRoot    指的是配置文件根
DocumentRoot    指的是网页文件根

 

定义了4个昵称

combined

common

referer

agent

combinedio



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

LogFormat "%h %l %u %t \"%r\" %>s %b" common

LogFormat "%{Referer}i -> %U" referer

LogFormat "%{User-agent}i" agent

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio



192.168.2.80 - - [07/Apr/2015:14:34:50 +0800] "GET /docs/ HTTP/1.1" 200 1664 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

192.168.2.80 - - [07/Apr/2015:14:34:53 +0800] "GET /docs/html/ HTTP/1.1" 200 17621 "http://192.168.2.84/docs/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

192.168.2.80 - - [07/Apr/2015:14:34:55 +0800] "GET /docs/html/manual.css HTTP/1.1" 404 296 "http://192.168.2.84/docs/html/basics.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

192.168.2.80 - - [07/Apr/2015:14:34:58 +0800] "GET /docs/html/graph_overview.html HTTP/1.1" 200 3693 "http://192.168.2.84/docs/html/basics.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

192.168.2.80 - - [07/Apr/2015:14:34:58 +0800] "GET /docs/html/manual.css HTTP/1.1" 404 296 "http://192.168.2.84/docs/html/graph_overview.html" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

192.168.2.80 - - [07/Apr/2015:14:35:41 +0800] "GET / HTTP/1.1" 200 1468 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36"

1.85.49.230 - - [07/Apr/2015:15:01:14 +0800] "GET / HTTP/1.1" 200 1468 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"

1.85.49.230 - - [07/Apr/2015:15:01:14 +0800] "GET /cacti/images/auth_login.gif HTTP/1.1" 200 21265 "http://125.76.228.16:2002/" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0

 

 

错误

11:57:05 111 /var/www/html/doc/mrtg2:#service httpd reload
Reloading httpd:                                           [FAILED]
11:57:07 112 /var/www/html/doc/mrtg2:#service httpd status
httpd dead but pid file exists
11:57:15 113 /var/www/html/doc/mrtg2:#rm -rf /var/run/httpd/httpd.pid
11:57:29 114 /var/www/html/doc/mrtg2:#service httpd start
Starting httpd:                                            [FAILED]
11:57:34 115 /var/www/html/doc/mrtg2:#service httpd status
httpd dead but subsys locked
12:00:16 121 /var/www/html/doc/mrtg2:#rm -rf /var/lock/subsys/httpd
12:00:44 122 /var/www/html/doc/mrtg2:#service httpd start
Starting httpd:                                            [  OK  ]

查看错误日志解决以上问题
12:08:57 130 /var/www/html/doc/mrtg2:#less /var/log/httpd/error_log
[Fri Apr 24 11:55:24 2015] [notice] SIGHUP received.  Attempting to restart
unable to start piped log program '/usr/local/apache/bin/rotatelogs /var/log/access_log 86400': No such file or directory
Unable to open logs
unable to start piped log program '/usr/local/apache/bin/rotatelogs /var/log/access_log 86400': No such file or directory
Unable to open logs

semaphore是一个内部用于与其子进程进行交流的工具," No space left on device: Couldn't create accept lock "的意思是说apache不能再创建新的semaphore进程。 用如下命令查看有多少 semaphore在运行。 #ipcs -s 你将会看到 ------ Semaphore Arrays -------- key semid owner perms nsems 0x00000000 68681743 apache 600 1 0x00000000 68714515 apache 600 1 0x00000000 68747291 apache 600 1 你可运行如下命令来安全的杀死每个 Semaphore(信号量) #ipcrm -s <semid> <semid>是上面ipcs -s输出的第二列值。 想要一次性将所有的Semaphore全部杀死,请运行如下命令: for semid in `ipcs -s |awk '{print $2}'`; do ip crm -s $semid; done 如果不能再创建更多的Semaphores: 有些时候你可能想改变系统允许创建 semaphores的数量。这就需要改变内核参数。 运行以下命令来查看当前参数: #ipcs -l 修改 /etc/sysctl.conf文件,增加如下两行: kernel.msgmni = 1024 kernel.sem = 250 256000 32 1024 运行命令# sysctl -p 使刚改的参数生效。 (kernel.msgmni 该文件指定消息队列标识的最大数目,即系统 范围内最大多少个消息队列。缺省设置 :16) 关于/var/lock/subsys目录 总的来说,系统关闭的过程(发出关闭信号,调用服务自身的进程)中会检查/var/lock/subsys下的文件,逐一关闭每个服务,如果某一运行的服务在/var/lock/subsys下没有相应的选项。在 系统关闭的时候,会像杀死普通进程一样杀死这个服务。通过察看/etc/rc.d/init.d下的脚本,可以发现每个服务自己操纵时都会去查看/var/lock/subsys下相应的服务。

 

This is the status code that the server sends back to the client. This information is very valuable, because it reveals whether the request resulted in a successful response (codes beginning in 2), a redirection (codes beginning in 3), an error caused by the client (codes beginning in 4), or an error in the server (codes beginning in 5). The full list of possible status codes can be found in the HTTP specification (RFC2616 section 10).

这是服务器返回给客户端的状态代码。这个信息非常有价值,因为它显示请求是否以一个成功应答(代码以2开头),一个重定向(代码以3开头),一个由客户端引起的错误(代码以4开头),或者一个服务端的错误(代码以5开头)为结果。一份可能的状态代码的完全列表能在HTTP规范中被发现。

      Status-Code    =

            "100"  ; Section 10.1.1: Continue

          | "101"  ; Section 10.1.2: Switching Protocols

          | "200"  ; Section 10.2.1: OK

          | "201"  ; Section 10.2.2: Created

          | "202"  ; Section 10.2.3: Accepted

          | "203"  ; Section 10.2.4: Non-Authoritative Information

          | "204"  ; Section 10.2.5: No Content

          | "205"  ; Section 10.2.6: Reset Content

          | "206"  ; Section 10.2.7: Partial Content

          | "300"  ; Section 10.3.1: Multiple Choices

          | "301"  ; Section 10.3.2: Moved Permanently

          | "302"  ; Section 10.3.3: Found

          | "303"  ; Section 10.3.4: See Other

          | "304"  ; Section 10.3.5: Not Modified

          | "305"  ; Section 10.3.6: Use Proxy

          | "307"  ; Section 10.3.8: Temporary Redirect

          | "400"  ; Section 10.4.1: Bad Request

          | "401"  ; Section 10.4.2: Unauthorized

          | "402"  ; Section 10.4.3: Payment Required

          | "403"  ; Section 10.4.4: Forbidden

          | "404"  ; Section 10.4.5: Not Found

          | "405"  ; Section 10.4.6: Method Not Allowed

          | "406"  ; Section 10.4.7: Not Acceptable

          | "407"  ; Section 10.4.8: Proxy Authentication Required

          | "408"  ; Section 10.4.9: Request Time-out

          | "409"  ; Section 10.4.10: Conflict

          | "410"  ; Section 10.4.11: Gone

          | "411"  ; Section 10.4.12: Length Required

          | "412"  ; Section 10.4.13: Precondition Failed

          | "413"  ; Section 10.4.14: Request Entity Too Large

          | "414"  ; Section 10.4.15: Request-URI Too Large

          | "415"  ; Section 10.4.16: Unsupported Media Type

          | "416"  ; Section 10.4.17: Requested range not satisfiable

          | "417"  ; Section 10.4.18: Expectation Failed

          | "500"  ; Section 10.5.1: Internal Server Error

          | "501"  ; Section 10.5.2: Not Implemented

          | "502"  ; Section 10.5.3: Bad Gateway

          | "503"  ; Section 10.5.4: Service Unavailable

          | "504"  ; Section 10.5.5: Gateway Time-out

          | "505"  ; Section 10.5.6: HTTP Version not supported

          | extension-code



      extension-code = 3DIGIT

      Reason-Phrase  = *<TEXT, excluding CR, LF>

 

需求:指定的ip访问status与info,非法ip在日志中留有记录

三步走就可以用这个地址访问了server-info与server-status的配置是一样的  http://172.16.1.44/server-status  http://172.16.1.44/server-info



改配置

1.开模块,

2.去注释,并允许要允许的主机。顺序为先拒绝后允许,拒绝所有,再允许个别。ExtendedStatus On只是为了在server-status中显示更多信息罢了

3.重启服务器生效

vi httpd.conf  修改三个地方

1.LoadModule status_module modules/mod_status.so

2.ExtendedStatus On

3.

<Location /server-status>

    SetHandler server-status

    Order deny,allow

    Deny from all

    Allow from 172.16.1.254

</Location>

4.service httpd reload



看日志

access_log

192.168.1.90 - - [24/Apr/2015:09:58:12 +0800] "GET /server-info HTTP/1.1" 403 290 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"

error_log

[Fri Apr 24 09:58:12 2015] [error] [client 192.168.1.90] client denied by server configuration: /var/www/html/server-info

 

需求:查看文件列表形式

注释掉这一行,就可以看到文件的列表形式

#Include conf.d/*.conf



<Directory "/var/www/html">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

 

需求:轮替日志

CustomLog "|/usr/sbin/rotatelogs /var/log/httpd/access_log 86400" common



上面这个可能不能正常使用,所以用下面的那一条,在httpd.conf中加入下面的一条指令,然后访问页面,日志就会在定义的时间内替换。



CustomLog "||/usr/sbin/rotatelogs /var/log/httpd/access_log 86400" common



[root@84-monitor httpd]# vi /etc/httpd/conf/httpd.conf

[root@84-monitor httpd]# service httpd reload

Starting httpd:                                            [  OK  ]



定义时间内没有访问,不会轮替。有访问,86400这个时间一到就轮替。



[root@84-monitor httpd]# ll

total 104

-rw-r--r--. 1 root root  1001 Apr  3 17:46 access_log

-rw-r--r--. 1 root root   950 Apr  3 18:00 access_log.1428055200

-rw-r--r--. 1 root root   950 Apr  3 18:01 access_log.1428055260

-rw-r--r--. 1 root root  2850 Apr  3 18:02 access_log.1428055320

-rw-r--r--. 1 root root   475 Apr  3 18:03 access_log.1428055380

 

cgi脚本  bash,perl,python,php,等

cgi脚本时,两种类型的报错
[Wed May 06 15:23:29 2015] [error] [client 192.168.1.88] (8)Exec format error: exec of '/var/www/cgi-bin/a' failed
[Wed May 06 15:23:29 2015] [error] [client 192.168.1.88] Premature end of script headers: a

[Wed May 06 15:41:08 2015] [error] [client 192.168.1.88] malformed header from script. Bad header=a.sh: a.sh


LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
确保以上两条指令开启

bash脚本
1
.修改cgi-bin目录的权限,然后在此目录下新建脚本文件 2.内容如下 vi b #! /bin/bash echo Content-type: text/html echo "" ifconfig 注意到上面的输出的第一行必须是输出“Content-type: text/html“否则Apache识别不了这个文本页面的输出。 3.浏览器访问 http://172.16.1.44/cgi-bin/b

python脚本
16:33:45 141 /var/www/cgi-bin:#vi a
#! /usr/bin/python
print("Content-type: text/html\n\n")
print("hello world")

 

http://www.ietf.org/rfc/rfc3875  cgi/1.1

CGI是一种接口的标准,并不区分编程语言,也就是说,CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。CGI会将标准输出重定向到给http的response,返回给浏览器。
CGI严格的介绍,公共网关接口CGI(Common GatewayInterface) 是WWW技术中最重要的技术之一,有着不可替代的重要地位。CGI是外部应用程序(CGI程序)与Web服务器之间的接口标准,是在CGI程序和Web服务器之间传递信息的规程。CGI规范允许Web服务器执行外部程序,并将它们的输出发送给Web浏览器,CGI将Web的一组简单的静态超媒体文档变成一个完整的新的交互式媒体。

CGI的输出结果需要是HTML形式或浏览器能显示的形式,否则其结果不会在浏览器中显示。 另外在CGI程序的所有输出前面必须有一个MIME类型的头,即HTTP头,对浏览器指明所接收内容的类型,大多数情况下,形如: Content
-type: text/html 网络访问CGI程序,浏览器中可能会发生四种情况: CGI程序的输出 太好了!这说明一切正常。 CGI程序的源代码或者一个"POST Method Not Allowed"消息 这说明Apache没有被正确配置以执行CGI程序,重新阅读配置Apache看看遗漏了什么。 一个以"Forbidden"开头的消息 这说明有权限问题。参考Apache error log和下面的文件的权限。 一个"Internal Server Error"消息 查阅Apache error log,可以找到CGI程序产生的出错消息"Premature end of script headers"。对此,需要检查下列各项,以找出不能产生正确HTTP头的原因。 文件的权限 记住,服务器不是以你的用户身份运行的,就是说,在服务器启动后,拥有的是一个非特权用户的权限-通常是``nobody''或者``www'' -而需要更大的权限以允许文件的执行。通常,给予``nobody''足够的权限以执行文件的方法是,对文件赋予everyone execute权限: chmod a+x first.pl 另外,如果需要对其他文件进行读取或写入,也必须对这些文件赋予正确的权限。 如果服务器被配置为使用su exec则是一个例外。这个程序允许CGI程序根据其所在虚拟主机或用户宿主目录的不同而以不同的用户权限运行。Su exec有极其严格的权限校验,任何校验失败都会使CGI程序运行失败而产生"Internal Server Error"。对此,需要检查su exec的日志文件以发现哪个安全校验出问题了。 路径信息 当你在命令行执行一个程序,某些信息会自动传给shell而无须你操心,比如一个路径,告诉shell你所引用的文件可以在哪儿找到。 但是,在CGI程序通过网站服务器执行时,则没有此路径,所以,你在CGI程序中引用的任何程序(如sendmail)都必须指定其完整的路径,使shell能找到它们以执行你的CGI程序。 一种普通的用法是,在CGI程序的第一行中指明解释器(通常是perl),形如: #!/usr/bin/perl 必须保证它的确指向解释器。 语法错误 多数CGI程序失败的原因在于程序本身有问题,尤其是在已经消除上述两种错误而CGI挂起的情况下。在用浏览器测试以前,先在命令行中执行你的程序,能够发现大多数的问题。 出错记录 出错记录是你的朋友。任何错误都会在出错记录中有记载,所以你应该首先查看它。如果你的网站空间提供者不允许访问出错记录,那么你应该考虑换一个空间提供者。学会阅读出错记录,可以快速找出问题并快速解决。

 

你可能感兴趣的:(linux)