一、安装httpd

二、相关文件路径

三、配置文件详解

四、转载比较详细的HTTP配置中英文对照  


一、安装httpd

 

1、安装方式:yum源、rpm包、源码包编译安装,这里为了方便使用yum源安装

[root@test3 html]# yum install -y httpd

 

可以安装本地帮助手册

[root@test3 conf]# yum install -y httpd-manual

访问地址http://httpd主机ip/manual/

 

2、开启服务

[root@test3 vsftpd]# chkconfig --list httpd
httpd              0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@test3 vsftpd]# chkconfig httpd on
[root@test3 vsftpd]# chkconfig --list httpd
httpd              0:off    1:off    2:on    3:on    4:on    5:on    6:off

 

3、测试

(1)、可以暂时关闭iptables,在浏览器地址栏中输入服务器主机ip即可成功访问到httpd欢迎页面,欢迎页面配置文件/etc/httpd/conf.d/welcome.conf                                 

(2)、可以在/var/www/html/下创建默认主页面文件index.html,那么以主机ip访问时,将不显示欢迎信息,而是index文件内容

[root@test3 html]# vim /var/www/html/index.html
编辑内容如下
<html>
        <title>Test Page</title>
        <body>
                <h1>Hello world.</h1>
                Test information.
	</body>
</html>

p_w_picpath


HTTP的响应码:

    1xx: 信息性状态码

    2xx: 成功状态码

    3xx: 重定向状态码

        301: 永久重定向, Location响应首部的值仍为当前URL,因此为隐藏重定向; 

        302: 临时重定向,显式重定向, Location响应首部的值为新的URL

        304:Not Modified

4xx: 客户端错误状态码

404: Not Found

5xx: 服务器端错误状态码

500: Internal Server Error

502: Bad Gateway

504:Gateway Timeout


二、相关文件路径

服务脚本:/etc/rc.d/init.d/httpd

运行目录:/etc/httpd(安装目录)

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

扩展配置:/etc/httpd/conf.d/*.conf

监听socket: http,80/tcp   https,443/tcp

网页文件目录(DocumentRoot):/var/www/html/

CGI格式的脚本存放位置:/var/www/cgi-bin/

 

查看httpd核心组建

[root@test3 conf]#  httpd -l

获取httpd命令简单帮助

[root@test3 conf]# httpd -h

查看所有装载的模块

[root@test3 conf]# httpd -t -D DUMP_MODULES

 

三、配置文件详解

配置文件由3部分组成,主服务器和虚拟主机不能同时启用

[root@test3 html]# grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment    #全局环境,对主服务器和虚拟主机都生效,有些配置是服务器自身工作属性
### Section 2: 'Main' server configuration    #主服务器配置,主站配置    
### Section 3: Virtual Hosts    #虚拟主机配置

 

修改配置文件后,使用配置文件语法测试,大多数配置service httpd reload即可生效,但是改了端口设置则需要restart

[root@test3 conf]# httpd -t
Syntax OK    #表示没有问题
或者
[root@test3 conf]# service httpd configtest
Syntax OK

 

 

1、配置监听的地址和端口

Listen 80

Listen 10.1.1.112:80

 

2、配置所选用的MPM的属性,MPM:多道处理模块,定义apache响应多个用户同时请求的工作模型

(1)、prefork:一个进程响应一个请求(默认使用)

(2)、worker:一个进程生成多个线程,没有线程响应一个请求

启用该模块时,需要编辑/etc/sysconfig/httpd,去掉#号

#HTTPD=/usr/sbin/httpd.worker

 

# prefork MPM

<IfModule prefork.c>  
StartServers       8    #启动web服务时,启动8个空闲进程  
MinSpareServers    5    #最小空闲进程数  
MaxSpareServers   20    #最大空闲进程数  
ServerLimit      256    #允许最大并发连接请求数  
MaxClients       256    #允许同时连接的客户端数  
MaxRequestsPerChild  4000    #单个子进程在其生命周期内处理的总请求数限制,当某个子进程处理过的总请求数到达这个限制后这个进程就会被回收,如果设为0,那么这个进程永远不会过期(这样如果有内存泄露的话就会一直泄露下去……)  
</IfModule>

# worker MPM  
<IfModule worker.c>  
StartServers         4    #启动web服务时,启动4个空闲线程  
MaxClients         300    #允许同时连接的客户端数  
MinSpareThreads     25    #最少空闲线程数  
MaxSpareThreads     75    #最大空闲线程数  
ThreadsPerChild     25    #每个进程允许生成的线程数量  
MaxRequestsPerChild  0    #  
</IfModule>  

 

3、配置服务器开启持久连接keep-alived,空闲服务器可以开启,繁忙的服务器关闭该功能比较好

KeepAlive Off    #关闭    KeepAlive On    #打开

KeepAliveTimeout 15    #超时时长,单位秒,一个连接超过15秒就断开

MaxKeepAliveRequests 100    #一次持久连接中,最多请求100个资源就断开

 

4、配置加载的模块信息,不需要加载就用#号注释掉

# Example:

    LoadModule 模块名称 相对路径,相对于这个路径/etc/httpd  
# LoadModule foo_module modules/mod_foo.so

 

5、定义站点名称

例如:

ServerName localhost:80

ServerName 127.0.0.1:80

ServerName www.example.com:80

 

6、配置站点根目录

DocumentRoot "/var/www/html"

<Directory "FS_PATH">  
</Directory>

<Location "URL">  
</Location>  

 

7、配置页面文件访问属性

<Directory "/var/www/html">


    Options Indexes FollowSymLinks


    AllowOverride None    #允许优先,用来控制指令


    Order allow,deny  
    Allow from all

</Directory>  

 

Indexes    是否允许索引页面,生产环境中不是下载站点,建议关闭

FollowSymLinks    跟随软链接文件

SymLinksifOwnerMatch    跟随软链接文件,但属主需匹配,意味着apache用户是文件的属主

ExecCGI    是否允许运行CGI脚本

All    全部开启

None    全部关闭

 

8、访问控制

 

(1)、基于客户端访问控制

     Order allow,deny
    Allow from all               #默认deny,但是下面用allow定义了允许所有,所以整个控制意为放行

----------------------------

     Order allow,deny
    Allow from 10.1.1. 0/8              #默认deny,但是下面用allow定义允许访问的地址段,所以整个控制对定义的地址段放行

---------------------------

    Order allow,deny  
    Deny from 10.1.1. 0/8              #默认deny,但是下面用deny定义拒绝访问的地址段,所以整个控制就是拒绝所有访问

---------------------------

Order:定义allow和deny哪个为默认法则,写在后面的为默认法则,写在前面的指令没有显示定义的即受后面的控制

 

(2)、基于用户访问控制

DocumentRoot "/var/www/html"

<Directory "/PATH/TO/DocumentRoot_SUBDIR">      指定访问路径,通常为目录为站点根目录的子目录  
    Options None    选项  
    AllowOverride AuthConfig    是否允许覆盖、启用用户访问控制  
    AuthName "Realm"    认证名称,“”内是说明信息  
    AuthType Basic    认证类型  
    AuthUserFile /path/to/passwords    用户帐号文件  
    Require jerry tom    指明可以登录的用户,所有用户使用valid-user  
</Directory>

 

这里我们在/etc/httpd/conf/下创建一个隐藏的帐号文件:

htpasswd -c –m /etc/httpd/conf/.htpass jerry

添加另一个用户

htpasswd –m /etc/httpd/conf/.htpass tom

 

创建用户帐号文件,使用命令htpasswd,可用htpasswd -h获取帮助信息

[root@test3 httpd]# htpasswd -h
Usage:
	htpasswd [-cmdpsD] passwordfile username
	htpasswd -b[cmdpsD] passwordfile username password

	htpasswd -n[mdps] username
	htpasswd -nb[mdps] username password
 -c  Create a new file.    #第一次创建时,使用该选项,再次像文件写入时不要使用,会被覆盖
 -n  Don't update file; display results on stdout.
 -m  Force MD5 encryption of the password.    #用户密码是使用md5加密的
 -d  Force CRYPT encryption of the password (default).
 -p  Do not encrypt the password (plaintext).    #不加密,明文存放
 -s  Force SHA encryption of the password.    #用户密码是使用SHA加密的
 -b  Use the password from the command line rather than prompting for it.
 -D  Delete the specified user.
On Windows, NetWare and TPF systems the '-m' flag is used by default.
On all other systems, the '-p' flag will probably not work.

(3)、基于组的访问控制

<Directory "/PATH/TO/DocumentRoot_SUBDIR">      指定访问路径,通常为目录为站点根目录的子目录  
    Options None    选项  
    AllowOverride AuthConfig    
    AuthName "Realm"    认证名称,“”内是说明信息  
    AuthType Basic    认证类型  
    AuthGroupFile /path/to/groupfile    用户帐号文件  
    Require GRP_NAME    指明可以登录的用户,所有用户使用valid-user  
</Directory>

其中groupfile是一个文本文件,文件内容是用户名

 

9、userdir,让用户有个自己的站点

访问方式:http://HOST/~username/

<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 disabled

    #  
    # To enable requests to /~user/ to serve the user's public_html  
    # directory, remove the "UserDir disabled" line above, and uncomment  
    # the following line instead:  
    #  
    #UserDir public_html    启用该项,在用户家目录下新建目录public_html,并且apache用户要有访问权限,setfacl –m u:apache:x /home/username/public_html

</IfModule>  

 

10、定义默认主页面

DirectoryIndex index.html index.jsp index.php index.html.var  

有多个文件名时,会从左至右找到目录中符合条件的主页面文件

 

11、文件访问控制

<Files ~ "^\.ht">    该符号~表示模式匹配  
    Order allow,deny  
    Deny from all  
    Satisfy All  
</Files>

 

12、配置日志功能,apache的日志是自己管理的

日志有两类:错误日志、访问日志

[root@test3 conf]# ls /etc/httpd/logs/
access_log  error_log
[root@test3 httpd]# cd /etc/httpd/
[root@test3 httpd]# ls -l
total 8
drwxr-xr-x. 2 root root 4096 May 17 00:28 conf
drwxr-xr-x. 2 root root 4096 May 16 23:52 conf.d
lrwxrwxrwx. 1 root root   19 May 16 17:18 logs -> ../../var/log/httpd    #日志目录实际位置在/var目录下

错误日志:ErrorLog logs/error_log    无需指定日志格式  

访问日志:CustomLog logs/access_log common    需指定日志格式

日志格式

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  

%h:客户端主机ip

%l:客户端登录的主机地址,通常为匿名访问,日志记录为-,表示空

%u:认证提供的用户名,通常为匿名访问,日志记录为-,表示空

%t:请求到达时间

%r:请求报文的起始行,内容有请求连接的方法get、请求的资源、使用的协议版本

%>s:%s显示内部完成重定向以后的状态码,%>s为用户请求到达的原始响应的状态码

%b:响应报文的大小,单位bytes,不包含HTTP首部

%{Referer}i:定义由哪个超链接跳转

%{User-Agent}i:所使用的浏览器

 

13、设定默认字符集

AddDefaultCharset UTF-8

 

14、路径别名

Alias /error/ "/var/www/error/"

两个文件夹都在站点默认根目录下,使用别名后,可以定义访问路径为http://www.test.com/error/时,显示的是/var/www/error/下的内容  

15、脚本路径别名:定义CGI的执行目录

<Directory "/var/www/html">


    Options Indexes FollowSymLinks ExecCGI    #启用CGI模块,添加ExecCGI


    AllowOverride None


    Order allow,deny  
    Allow from all

</Directory>  

注意:为了安全我们可以不在上面启用,而是使用别名

ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"    页面访问路径为http://www.test.com/cgi-bin/test.sh时,实际访问的是/var/www/cgi-bin/中的CGI脚本

 

测试文件test.sh

#!/bin/bash
#
cat << EOF
Content-Type: text/html
<pre>
The hostname is: `/bin/hostname`.
The time is: `date`.
EOF
# end of cgi script file

 

16、虚拟主机

使用虚拟主机时,不能使用主服务器,关闭主服务器:注释主服务器的DocumentRoot

(1)、类型:

基于端口的虚拟主机:监听不同的端口,使用相同的ip

基于IP的虚拟主机:监听相同端口,使用不同ip地址

基于主机名的虚拟主机:监听相同端口,使用相同ip地址,使用不同的主机名,开启NameVirtualHost *:80

 

(2)、每个虚拟主机的定义

#<VirtualHost *:80>    *指所有地址的80端口上,也可以是ip  
#    ServerAdmin webmaster@dummy-host.example.com  
#    DocumentRoot /www/docs/dummy-host.example.com  
#    ServerName dummy-host.example.com  
#    ErrorLog logs/dummy-host.example.com-error_log  
#    CustomLog logs/dummy-host.example.com-access_log common  
#</VirtualHost>  
 

(3)、前文中适用于主服务器配置的配置信息也可以用于配置虚拟主机

 

17、httpd status   

httpd状态信息输出模块

LoadModule status_module modules/mod_status.so

 

ExtendedStatus On    是否开启该功能

#<Location /-status>    定义访问status信息时,是通过哪个url,例如http://10.1.1.112/status  
#    SetHandler server-status    处理器名称server-status  
#    Order deny,allow  
#    Deny from all  
#    Allow from .example.com  
#</Location>

 

下面配置信息中,未启用Order来控制主机访问,所以不需要使用AllowOverride

<Location /status>  
    SetHandler server-status  
    AuthName "Status"  
    AuthType Basic  
    AuthUserFile /etc/httpd/conf/.statuspass  
    Require valid-user  
</Location>

 

18、https的实现

SSL/TLS会话的建立仅能基于IP地址进行,只有1个IP时,只能给一个虚拟主机提供SSL;访问端口443/tcp

前提:已安装openssl(系统默认已安装),安装httpd模块mod_ssl

[root@test3 httpd]# yum install -y mod_ssl


步骤

(1)、生成私钥

(2)、生成证书申请

(3)、使用自建CA签署证书

以上操作参考http://64314491.blog.51cto.com/2784219/1651785

(4)、配置文件/etc/httpd/conf.d/ssl.conf

常用配置项

<VirtualHost _default_:443>    _default_表示默认虚拟主机,可以直接指定主机ip

#DocumentRoot "/var/www/html"    虚拟主机工作目录

#ServerName www.example.com:443    虚拟主机主机名和访问端口

SSLEngine on    是否开启SSL安全认证功能

SSLProtocol all -SSLv2    支持除SSLv2外所有的SSL协议

SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW    SSL支持的加密套件,!表示不支持

SSLCertificateFile /etc/pki/tls/certs/localhost.crt    证书文件路径

SSLCertificateKeyFile /etc/pki/tls/private/localhost.key    私钥文件路径


以上配置好后,重启服务,windows上安装签署的证书然后用浏览器打开站点https://www.test.com或者使用命令测试


测试命令,指定连接的主机及端口、CA证书

#openssl s_client -connect www.test.com:443 -CAfile /etc/pki/CA/cacert.pem

#GET / HTTP/1.1    使用GET连接方式,指定url“/”,指定使用的访问协议“HTTP/1.1”

#Host:www.test.com  指定主机名后,敲两次回车,显示页面信息及OK提示,表示测试成功


19、其他

TypesConfig /etc/mime.types    默认获取文件时,文件资源的类型

 

<IfModule mod_mime_magic.c>    模块允许服务器充文件内容来确定文件类型  
#   MIMEMagicFile /usr/share/magic.mime  
    MIMEMagicFile conf/magic    配置文件位置/etc/httpd/conf/magic  
</IfModule>  

#AddType    新增支持的文件类型,以某个后缀识别为什么格式的文件

AddType application/x-compress .Z  
AddType application/x-gzip .gz .tgz  
AddType application/x-x509-ca-cert .crt  
AddType application/x-pkcs7-crl    .crl  


四、转载比较详细的HTTP配置中英文对照

http://www.cnblogs.com/adamite/p/apache_configuration.html