httpd篇

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpd篇相关的知识,希望对你有一定的参考价值。

学习用途


httpd为Apache HTTP服务器程序。直接执行程序可启动服务器的服务。

语法
httpd [-hlLStvVX][-c<httpd指令>][-C<httpd指令>][-d<服务器根目录>][-D<设定文件参数>][-f<设定文件>]

参数说明

  • -c<httpd指令> 在读取配置文件前,先执行选项中的指令。
  • -C<httpd指令> 在读取配置文件后,再执行选项中的指令。
  • -d<服务器根目录> 指定服务器的根目录。
  • -D<设定文件参数> 指定要传入配置文件的参数。
  • -f<设定文件> 指定配置文件。
  • -h 显示帮助。
  • -l 显示服务器编译时所包含的模块。
  • -L 显示httpd指令的说明。
  • -S 显示配置文件中的设定。
  • -t 测试配置文件的语法是否正确。
  • -v 显示版本信息。
  • -V 显示版本信息以及建立环境。
  • -X 以单一程序的方式来启动服务器。

<http://www.runoob.com/linux/linux-comm-httpd.html>


 

[[email protected] ~] httpd -h
Usage: httpd [-D name] [-d directory] [-f file]
             [-C "directive"] [-c "directive"]
             [-k start|restart|graceful|graceful-stop|stop]
             [-v] [-V] [-h] [-l] [-L] [-t] [-T] [-S] [-X]
Options:
  -D name            : define a name for use in <IfDefine name> directives
  -d directory       : specify an alternate initial ServerRoot
  -f file            : specify an alternate ServerConfigFile
  -C "directive"     : process directive before reading config files
  -c "directive"     : process directive after reading config files
  -e level           : show startup errors of level (see LogLevel)
  -E file            : log startup errors to file
  -v                 : show version number
  -V                 : show compile settings
  -h                 : list available command line options (this page)
  -l                 : list compiled in modules
  -L                 : list available configuration directives
  -t -D DUMP_VHOSTS  : show parsed vhost settings
  -t -D DUMP_RUN_CFG : show parsed run settings
  -S                 : a synonym for -t -D DUMP_VHOSTS -D DUMP_RUN_CFG
  -t -D DUMP_MODULES : show all loaded modules 
  -M                 : a synonym for -t -D DUMP_MODULES
  -t                 : run syntax check for config files
  -T                 : start without DocumentRoot(s) check
  -X                 : debug mode (only one worker, do not detach)

 

yum 安装(后续会有编译安装)

# 初次安装httpd
yum install -y httpd
# 查看安装的版本
httpd -v
# 查看编译的模块
httpd -l
# 查看httpd指令说明
httpd -L>1.log |tail -n 20 1.log
# 二进制文件路径
which httpd
# 查看httpd 的运行状态
systemctl status httpd.service
# 添加到开机启动
chkconfig --lever 35 on
# 启动httpd
systemctl enable httpd.service
# 查看默认端口占用情况
netstat -lntcp |grep 80
#检查是否能正常访问80端口

curl http://127.0.0.1:80 > /dev/null

 

配置文件和相关信息路径:

注:本文只是简单的展示了基本的配置和查看命令。

# /etc/httpd 目录树结构
[[email protected] httpd]# tree
.
├── conf
│   ├── httpd.conf 
│   └── magic
├── conf.d
│   ├── autoindex.conf
│   ├── README
│   ├── userdir.conf
│   └── welcome.conf
├── conf.modules.d
│   ├── 00-base.conf
│   ├── 00-dav.conf
│   ├── 00-lua.conf
│   ├── 00-mpm.conf
│   ├── 00-proxy.conf
│   ├── 00-systemd.conf
│   └── 01-cgi.conf
├── logs -> ../../var/log/httpd
├── modules -> ../../usr/lib64/httpd/modules
└── run -> /run/httpd

或者通过 httpd –V 查看已配置项

[[email protected] conf]# httpd -V
AH00558: httpd: Could not reliably determine the servers fully qualified domain name, using 192.168.136.140. Set the ServerName directive globally to suppress this message
Server version: Apache/2.4.6 (CentOS)
Server built:   Nov 14 2016 18:04:44
Servers Module Magic Number: 20120211:24
Server loaded:  APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture:   64-bit
Server MPM:     prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="/run/httpd/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

 

httpd.conf 主配置文件

目录:/etc/httpd/conf/httpd.conf

# 输出默认配置
egrep -v "[#|$]" /etc/httpd/conf/httpd.conf

进一步通过sed命令处理,查找出监听的端口

egrep -v "[#|$]" /etc/httpd/conf/httpd.conf|sed -n /Listen/p

打印默认配置文件的具体参数

默认端口:80

默认用户:apache 组:apache

默认根目录:/var/www/html

# 或者使用 nl 添加行号 egrep -v "[#|$]" /etc/httpd/conf/httpd.conf|sed -n / /p| nl
[[email protected] conf]# egrep -v "[#|$]" /etc/httpd/conf/httpd.conf|sed -n / /p
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin [email protected]
<Directory />
    AllowOverride none
    Require all denied
DocumentRoot "/var/www/html"
<Directory "/var/www">
    AllowOverride None
    Require all granted
<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
<IfModule dir_module>
    DirectoryIndex index.html
<Files ".ht*">
    Require all denied
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
EnableSendfile on
IncludeOptional conf.d/*.conf

查看是用户已自动创建

[[email protected] httpd]# cat /etc/passwd |sed -n /apache/p
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin

 

autoindex.conf 自动索引配置

[[email protected] httpd]# egrep -v ^"[#|$]" $PWD/conf.d/autoindex.conf
IndexOptions FancyIndexing HTMLTable VersionSort

Alias /icons/ "/usr/share/httpd/icons/"

<Directory "/usr/share/httpd/icons">
    Options Indexes MultiViews FollowSymlinks
    AllowOverride None
    Require all granted
</Directory>

AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip

AddIconByType (TXT,/icons/text.gif) text/*
AddIconByType (IMG,/icons/image2.gif) image/*
AddIconByType (SND,/icons/sound2.gif) audio/*
AddIconByType (VID,/icons/movie.gif) video/*

AddIcon /icons/binary.gif .bin .exe
AddIcon /icons/binhex.gif .hqx
AddIcon /icons/tar.gif .tar
AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
AddIcon /icons/a.gif .ps .ai .eps
AddIcon /icons/layout.gif .html .shtml .htm .pdf
AddIcon /icons/text.gif .txt
AddIcon /icons/c.gif .c
AddIcon /icons/p.gif .pl .py
AddIcon /icons/f.gif .for
AddIcon /icons/dvi.gif .dvi
AddIcon /icons/uuencoded.gif .uu
AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
AddIcon /icons/tex.gif .tex
AddIcon /icons/bomb.gif /core
AddIcon /icons/bomb.gif */core.*

AddIcon /icons/back.gif ..
AddIcon /icons/hand.right.gif README
AddIcon /icons/folder.gif ^^DIRECTORY^^
AddIcon /icons/blank.gif ^^BLANKICON^^

DefaultIcon /icons/unknown.gif


ReadmeName README.html
HeaderName HEADER.html

IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

userdir.conf

技术分享

 

welcome.conf 配置默认页面

首页配置文件/etc/httpd/conf.d/welcome.conf

# 备份配置文件

cp welcome.conf  welcome.conf.bak

vim welcome.conf

技术分享

 

# 这里指定了目录 /usr/share/httpd/noindex
cd /usr/share/httpd/noindex
# 指定显示2层结构树
[[email protected] noindex]# tree -L 2
.
├── css
│   ├── bootstrap.min.css
│   ├── bootstrap-theme.min.css
│   ├── fonts
│   └── open-sans.css
├── images
│   ├── apache_pb.gif
│   └── poweredby.png
└── index.html

修改默认页面为自定义页面

技术分享

查看日志文件

# 默认日志路径
/var/log/httpd

#  带占用空间大小的日志结构树输出
[[email protected] httpd]# tree -C --du -h
.
├── [ 26K]  access_log
└── [4.2K]  error_log

  30K used in 0 directories, 2 files

检查日志

[[email protected] httpd]# tail -n 3 /var/log/httpd/*
==> /var/log/httpd/access_log <==
192.168.136.1 - - [13/Dec/2016:17:23:24 +0800] "GET /noindex/css/fonts/SemiboldItalic/OpenSans-SemiboldItalic.ttf HTTP/1.1" 404 258 "http://192.168.136.140/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
192.168.136.1 - - [13/Dec/2016:17:23:24 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.136.140/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393"
::1 - - [13/Dec/2016:17:23:30 +0800] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"

==> /var/log/httpd/error_log <==
[Tue Dec 13 16:23:12.122570 2016] [autoindex:error] [pid 84883] [client 192.168.136.1:60341] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Tue Dec 13 16:23:40.637128 2016] [autoindex:error] [pid 84167] [client 192.168.136.1:60357] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive
[Tue Dec 13 17:23:24.469463 2016] [autoindex:error] [pid 84885] [client 192.168.136.1:61453] AH01276: Cannot serve directory /var/www/html/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive

 

传送门

Apache HTTP 服务器 2.4 文档 http://httpd.apache.org/docs/current/

技术分享

 

问题:

  1. 是否可存在多个httpd?
  2. httpd配置如何合理化管理(安装、配置、变更、删除)?
  3. 如何在对log归档和删除?log日志文件中快速有效找到有助于web排错和优化的记录?如何基于数据分析,自动生成一定时间内的运行趋势报表?
  4. 如何规范web文件的存放?
  5. httpd和IISadmin服务有什么相同之处?
  6. 如何升级?

以上是关于httpd篇的主要内容,如果未能解决你的问题,请参考以下文章

配置 VScode 编辑器 (前端篇)

httpd篇

Python代码阅读(第19篇):合并多个字典

Python代码阅读(第26篇):将列表映射成字典

Python代码阅读(第41篇):矩阵转置

Linux+Apache+PHP+MySQL服务器环境配置(CentOS篇)