httpd详解

简介:

httpd

1、

httpd的安装方式通常有两种,一直是直接利用rpm包进行直接安装,另一种是源码编译安装,我们先用rpm包直接安装就行了


[root@zj602 ~]# rpm -ql httpd


安装好之后我们看下生成了哪些文件


[root@zj602 ~]# rpm -ql httpd

/etc/httpd

/etc/httpd/conf

/etc/httpd/conf.d

/etc/httpd/conf.d/README

/etc/httpd/conf.d/welcome.conf

/etc/httpd/conf/httpd.conf

/etc/httpd/conf/magic

/etc/httpd/logs

/etc/httpd/modules

/etc/httpd/run

/etc/logrotate.d/httpd

/etc/rc.d/init.d/htcacheclean

/etc/rc.d/init.d/httpd

/etc/sysconfig/htcacheclean

/etc/sysconfig/httpd

/usr/lib64/httpd

......


配置文件:

/etc/httpd/conf/httpd.conf

扩展配置文件:

/etc/httpd/conf.d/*.conf

服务脚本:

/etc/rc.d/init.d/httpd

脚本配置文件:

/etc/sysconfig/httpd

 

模块目录:

/etc/httpd/modules:   链接文件

/usr/lib64/httpd/modules

主程序:

/usr/sbin/httpd: prefork 

/usr/sbin/httpd.event: event

/usr/sbin/httpd.worker: worker

日志文件目录:

/var/log/httpd

access_log: 访问日志

error_log: 错误日志



测试配置和查看的命令

service httpd configtest        #测试配置文件有没有语法错误

httpd -t                         #同上

httpd -l                        #当前服务器所使用的模型及开启模块

httpd -D DUMP_MODULES           #当前服务器支持的模块

service httpd reload            #重新加载配置文件

service httpd restart           #重启httpd服务



相关的指令:

  Timeout 60                  在客户端和服务器端TCP三次握手的时候,当客户端发起请求,服务器端响应请求之后,服务器端等待客户端确认的时间,如果客户端在时间内未确认,则服务器将关闭该次TCP握手。

KeepAlive Off|On            持久连接是否启用

MaxKeepAliveRequests 100    服务器单个持久连接最大的请求数,超过即断开

KeepAliveTimeout 15         单个持久连接最大连接时长,超过即断开



2、

MPM:Multipath Processing Module    #多处理模块


prefork:一次一个进程响应一个请求

   worker:一个进程生成多个线程,一个线程响应一个请求

   event:基于事件驱动,一个进程响应多个请求


这里面的参数尤其关键,务必要搞清楚每个参数的意义。


与prefork相关的配置指令:

<IfModule prefork.c>

StartServers       8 在服务刚启动时,预先fork几个子进程;

MinSpareServers    5  最小预留的空闲子进程的数量;

MaxSpareServers   20  最多预留的空闲子进程的数量;

ServerLimit      256  为MaxClients指定的最大值;

MaxClients       256  最大的并发访问量;

MaxRequestsPerChild  4000   每个子进程最多可以接受的请求数,超过即KILL

</IfModule>


与worker相关的配置指令:

<IfModule worker.c>

StartServers         4 在服务刚启动时,预先fork几个子进程;

MaxClients         300  并行访问的上限;

MinSpareThreads     25  最小预留的空闲线程数量;

MaxSpareThreads     75 最大预留的空闲线程数量;

ThreadsPerChild     25  每个进程管理的最大线程数量;

MaxRequestsPerChild  0  每个子进程处理的最大请求数量;

</IfModule>


3、配置服务器支持keep-alived(长连接)

KeepAlive {On|Off}          #是否支持长连接

KeepAliveTimeout 2          #长连接超时时间

MaxKeepAliveRequests 50     #超时时间内允许请求的次数


4、配置加载模块

# LoadModule foo_module modules/mod_foo.so

#

LoadModule auth_basic_module modules/mod_auth_basic.so

LoadModule auth_digest_module modules/mod_auth_digest.so

LoadModule authn_file_module modules/mod_authn_file.so

LoadModule authn_alias_module modules/mod_authn_alias.so


5、配置网站根目录

DocumentRoot "/var/www/html"    #指定网站的主目录

<Directory "/var/www/html">     #容器内定义站点的访问权限

</Directory>

<Location "URL">                #容器内限定用户的访问方法

</Location>


6、配置页面文件的访问属性

<Directory "/var/www/html">

    Options

    Indexes         #是否允许索引页面文件,建议关闭

    FollowSymLinks  #是否跟随软连接文件

    SymLinksifOwnerMatch #跟随符号链接,只允许访问运行apache的用户有属主权限的文件

    ExecCGI:        #是否允许执行CGI脚本;

    All

    None

</Directory>


7、基于客户端访问控制

系统默认允许所有人访问

<Directory "/var/www/html">

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

Order allow,deny

Allow from all    #所有人可以访问

</Directory>

配置允许172.16.0.0/16访问,但不允许172.16.3.1访问

<Directory "/var/www/html">

Order allow,deny

Deny from 172.16.3.1        #禁用一个IP访问

Allow from 172.16.0.0/16    #允许一个网段访问

</Directory>


8、userdir个人站点

<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    #开启此项

</IfModule>


重新加载配置文件后用:http://HOST/~username/

    例如:http://172.16.6.2/~centos/


9、定义默认主页

  DirectoryIndex index.php index.jsp index.html


10、配置日志功能

日志有两类:访问日志(格式自定义)、错误日志


错误日志:

    ErrorLog "/path/to/error_log_file"  

访问日志:

    CustomLog "/path/to/custom_log_file" logformat

    日志格式:{combined|common|agent}

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              #主机名

%l              #远程登录名

%u              #用户名

%t              #请求到达的时间

%r              #请求报文的起始行,方法

%s              #

%b              #响应报文的大小

%{Foobar}i      #显示从发那个站点跳转过来

%{User-Agent}i  #用户代理


11、支持的字符集

  AddDefaultCharset UTF-8


12、路径别名

可以隐藏网站的真实目录,

Alias /test/ "/www/test/"            #配置别名

<Directory "/www/test">         #可以给别名目录定义访问权限

    Options None

    AllowOverride None

    Order allow,deny

    Allow from 172.16.0.0/16

</Directory>


13、脚本路径别名

调用服务器上的脚本程序,但不希望脚本放在网站的目录下

ScriptAlias /cgi-bin/ "/www/cgi-bin/"     #定义cgi脚本路径别名

# vi /wwww/cgi-bin/test.sh                #写一个测试脚本

#!/bin/bash

#cat << EOF

Content-Type:text/html

                                 #这里要有一个空行否则可能会测试不成功

<pre>

The hostname is: `hostname`.              #输出当前系统的主机名

The timeis: `date`.                       #显示时间

</pre>

EOF


14、基于用户访问控制

当你的网站或者站点的某个路径只想让你授权的用户访问时,就可以使用基于用户的访问控制

这里使用htpasswd命令建立用户帐号文件

htpasswd

    -c  #第一次使用-c创建新文件,不是第一次不要使用此选项

    -m  #用户密码使用MD5加密后存放

    -s  #用户密码使用SHA加密后存放

    -p  #用户密码不加密

    -d  #禁用一个账户

    -e  #启用一个账户

例如:

# htpasswd -c -m /etc/httpd/conf/.htpass  tom

   命令    选项  生成的用户文件路径及文件名 用户名

# htpasswd -m /etc/httpd/conf/.htpass jerry

# 再次添加用户时就不要使用-c选项了,否则会覆盖之前内容先生成

(1)建立用户帐号文件

# htpasswd -c -m /etc/httpd/conf/.htpass  tom

(2)修改主配置文件

# vi /etc/httpd/conf/httpd.con

DocumentRoot "/www/html"    #此时网站根目录就是/www/html

<Directory "/www/html">

Optins Indexes

AllowOverride AuthConfig

AuthName "Oaly for employees."    #登录提示信息,可自定义

AuthType Basic                    #认证方式

AuthUserFile /etc/httpd/conf/.htpass    #用户帐号文件

Require valid-user                #允许的用户


15、配置虚拟主机

(1)注释主服务器,添加虚拟主机

# DocumentRoot "/www/html"         #注释主服务器 

NameVirtualHost 172.16.3.1:80      #开启虚拟主机

<VirtualHost 172.16.3.1:80>

     DocumentRoot /www/web1        #虚拟主机的网站根目录

     ServerName web1.lyd.com       #主机名

</VirtualHost>

<VirtualHost 172.16.3.1:80>

     DocumentRoot /www/web2

     ServerName web2.lyd.com

</VirtualHost>


基于IP地址的虚拟主机示例:

/etc/httpd/conf.d/vhosts-www1.conf

<VirtualHost 172.16.69.1:80>

DocumentRoot "/myweb/vhosts/www1"

ServerName www1.qhdlink.com

</VirtualHost>


/etc/httpd/conf.d/vhosts-www2.conf

<VirtualHost 172.16.69.2:80>

DocumentRoot "/myweb/vhosts/www2"

ServerName www2.qhdlink.com

</VirtualHost>


基于PORT的虚拟主机配置示例:

前提:需要让httpd事先监听于指定的额外端口;

/etc/httpd/conf.d/vhosts-www3.conf

Listen 60080

<VirtualHost 172.16.69.1:60080>

DocumentRoot "/myweb/vhosts/www3"

ServerName www3.qhdlink.com

</VirtualHost>


基于FQDN的虚拟主机配置示例:

/etc/httpd/conf.d/vhosts-www4.conf

NameVirtualHost 172.16.69.1:80

<VirtualHost 172.16.69.1:80>

DocumentRoot "/myweb/vhosts/www4"

ServerName www4.qhdlink.com

</VirtualHost>


(2)给虚拟主机提供测试文件

mkdir /www/web{1,2}

echo web1.lyd.com > /www/web1/index.html

echo web2.lyd.com > /www/web2/index.html

(3)配置完后用 httpd -t 测试配置文件有没有错误

(4)重启服务 service httpd restart

(5)验证,修改验证客户端的hosts文件,

windows客户端

开始-->运行-->输入"c:\windows\system32\drivers\etc\hosts"用记事本打开并添加以下行

172.16.3.1web1.lyd.com

172.16.3.1web2.lyd.com


16、配置支持https

(1)安装httpd支持ssl模块

   yum install mod_ssl -y

(2)自建CA

   # cd /etc/pki/CA  

 # (umask 077; openssl genrsa -out private/cakey.pem 2048)

 # openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365

(3)生成私钥

 cd /etc/httpd/conf/

 mkdir ssl

 cd ssl

 (umask 077; openssl genrsa -out httpd.key 1024)

(4)生成证书申请

   openssl req -new -key httpd.key -out httpd.csr

(5)ca签署证书

   openssl ca -in httpd.csr -out httpd.crt -days 365

(6)修改httpd的ssl配置文件

   vi /etc/httpd/conf.d/ssl.conf

<VirtualHost 172.16.3.1:443>

DocumentRoot "/www/web1"

ServerName www.lyd.com

SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt

SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key

(7)将ca证书导入到客户端可信任证书服务器,并验证


17、相关命令

curl命令

curl是基于URL语法在命令行方式下工作的文件传输工具,它支持FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE及LDAP等协议。curl支持HTTPS认证,并且支持HTTP的POST、PUT等方法, FTP上传, kerberos认证,HTTP上传,代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。


curl  [options]  [URL...]

curl的常用选项:

    -A/--user-agent <string> 设置用户代理发送给服务器

    --basic 使用HTTP基本认证

    --tcp-nodelay 使用TCP_NODELAY选项

    -e/--referer <URL> 来源网址

    --cacert <file> CA证书 (SSL)

    --compressed 要求返回是压缩的格式

    -H/--header <line>自定义首部信息传递给服务器

    -I/--head 只显示响应报文首部信息

    --limit-rate <rate> 设置传输速度

    -u/--user <user[:password]>设置服务器的用户和密码

    -0/--http1.0 使用HTTP 1.0

用法:curl [options] [URL...]


elinks命令

elinks  [OPTION]... [URL]...

-dump: 不进入交互式模式,而直接将URL的内容输出至标准输出; 


ab命令

 Apache Bench,压力测试工具;

-c:并发连接数

-n:请求总数

-k:使用keepalive



本文转自 Runs_ 51CTO博客,原文链接:http://blog.51cto.com/12667170/1960453,如需转载请自行联系原作者

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
14天前
|
弹性计算 运维 Shell
一键编译安装Httpd-2.4.53
【4月更文挑战第30天】
13 1
|
Apache 开发工具 网络安全
|
网络安全
httpd服务的配置
httpd服务的配置
260 0
|
Linux 应用服务中间件 PHP
yum 6.8 nginx php-fpm
centos6.8 yum安装 php-fpm 1.检查当前安装的PHP包 yum list installed | grep php 如果有安装的PHP包,先删除他们 yum remove php.x86_64 php-cli.x86_64 php-common.x86_64 php-gd.x86_64 php-ldap.x86_64 php-mbstring.x86_64 php-mcrypt.x86_64 php-mysql.x86_64 php-pdo.x86_64   配置yum源 追加CentOS 6.5的epel及remi源。
870 0
|
关系型数据库 MySQL PHP
httpd 的坑
Httpd服务器的坑 在/etc/httpd/conf/httpd.conf中的配置信息, 有时注释到的内容仍然会生效 配置Auth时, 允许htpasswd规定的文件中的所有的用户, Require valid-uesr, 允许特定的用户Require user user1 user2 user3 .
917 0
|
机器学习/深度学习 Shell Apache
httpd.conf 配置
# # This is the main Apache server configuration file. It contains the # configuration directives that give the server its instructions.
1038 0
|
Web App开发 应用服务中间件 PHP