httpd:高度模块化
core + modules
DSO:Dynamic Shared Object
MPM:Multi-Processing Module 多线程处理模块,不是指一个模块
prefork:多个子进程,一个子进程一个请求
worker:多个子进程,一个进程多个线程,一个线程一个请求
event:(2.4支持)
httpd的功能特性
路径别名:alias
用户认证:authentication
虚拟主机:virtual host
反向代理:
负责均衡:
用户站点:
CGI:common Gateway interface
Apache安装目录结构
/etc/httpd/conf/ :配置文件目录
/etc/httpd/conf.d/ :配置文件子目录
/etc/httpd/logs :日志目录,软连接到/var/log/httpd
/var/www/html :文档根目录
/var/www/cgi-bin/ :CGI目录
/etc/httpd/conf/magic :记录对多媒体类型(MIME)的支持
httpd.conf文件的格式
全局配置:
主机配置:用于仅提供一个站点时
虚拟配置:用于提供多个站点时
注:主机配置和虚拟配置不能同时启用,配置文件修改完毕,要做配置文件语法测试,httpd -t
区分全局配置、主机配置、虚拟主机配置
grep "Section" /etc/httpd/conf/httpd.conf
vim /etc/sysconfig/httpd ---> HTTPD=/usr/sbin/httpd.worker 使用不同的MPM模块
global区域
ServerRoot "/etc/httpd" :定义工作目录
KeepAlive Off :是否启用保持连接
main区域
DocumentRoot "/var/www/html" :访问网页的根目录
DirectoryIndex index.html :在浏览器中不指定uri路径,默认呈现的网页文件名字
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined :访问日志路径
Alias /images/ "/data/imgs/" :/images为浏览器要访问的路径,/data/imgs/服务器真实的目录
AddDefaultCharset UTF-8
<Directory /> :用于访问控制,支持正则表达式
Options
FollowSymLinks:允许软连接当做网页文件
indexes:缺少指定的默认页面时,允许将目录中的所有文件以列表的形式返回给用户
none:所有先选都不生效
ExecCGI:允许使用mod_cgi模块执行CGI脚本;
includes:允许使用mod_include模块
MultiViews:允许使用mod_negotiation实现内容协商
AllowOverride None/auth :底下访问控制是否被禁用,和是否开启认证
</Directory>
用户目录
如果希望让每个用户都可以创建个人站点:http://IP/~Username/
<IfModule mod_userdir.c>
UserDir disabled/public_html_dir
</IfModule>
setfacl -m u:apache:rx /home/hadoop
DSO模块加载方式
loadModule module_name /path/to/module
如:loadModule php5_module /usr/lib64/httpd/modules/php5.so
如果使用相对路径,则对于ServerRoot所定义的位置而言的;
httpd -l:列出所有非DSO(shared)的模块
httpd -M: 列出所有模块
配置日志功能
/var/log/httpd/
access.log:访问日志 ;其记录内容需要自定义 CustomLog
error.log:错误日志
脚本路径别名
ScriptAlias /url "/path/to/somewhere"
CGI:common Gateway Interface 协议
CGI需要调用SUID,SGID来实现,很不安全
脚本一:(Content-Type下必须有空行)
#!/bin/sh
cat << EOF
Content-Type:text/html
<pre>
The hostname is:`hostname`.
The time is:`date`.
</pre>
EOF
脚本二:
#!/bin/sh
echo "Content-Type:text/html"
echo -e "\n"
echo "<p1>The hostname is:$(hostname).</p1>"
echo "<p2>The time is:$(date).</p2>"
基于用户的控制
虚拟用户
基于文件:/etc/httpd/conf/.htpasswd
sql数据库:
ldap:轻量级目录访问协议
模块以auth开头的代表认证类型:
模块以authn开头的代表认证提供者:
模块以authz开头的代表授权机制:
<Directory "/www/htdocs/fin">
Options None
AllowOverride AuthConfig :启用认证
AuthType Basic :认证类型
AuthName "ti shi xin xi" :提示信息
AuthBasicProvider file
AuthUserFile /etc/httpd/conf/.htpasswd :认证文件
Require valid-user :过滤用户
</Directory>
htpasswd -c -m /etc/httpd/conf/.htpasswd tom
htpasswd -m /etc/httpd/conf/.htpasswd jerry
虚拟主机
关闭中心主机,即注释Section2的 #DocumentRoot
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin
DocumentRoot /www/htdocs
ServerName www.a.com
ErrorLog logs/a.com.error_log
CustomLog logs/a.com.access_log common
</VirtualHost>
服务器status页面
内生的status信息,且此信息可以通过web预以显示
配置文件系统路径访问属性
<Directory[Match] "">=<Directory [~]"">
<File[Match] "">=<File [~]"">
基于URL访问属性
<Location[Match] "">=<Location [~]"">
注:Match启用正则
如果某要配置其属性的URL能映射到某具体的文件系统路径时,建议使用<Directory>
启用status需要的模块
grep status_mod /etc/httpd/conf/httpd.conf
httpd -M | grep status
vim /etc/httpd/conf/httpd.conf
<Location /status>
SetHandler server-status
Order deny,allow
allow from all
</Location>
使用mod_deflate模块压缩页面优化传输速度
httpd -M | grep deflate
vim /etc/httpd/conf.d/deflate.conf
SetOutputFilter DEFLATE
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)默认为6
DeflateCompressionLevel 9
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29620572/viewspace-1773071/
,如需转载,请注明出处,否则将追究法律责任。