码迷,mamicode.com
首页 > Web开发 > 详细

Apache httpd服务——常用配置

时间:2018-06-24 17:03:07      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:out   symlink   web3   div   控制   share   admin   comm   exe   

 

技术分享图片

 

httpd 2.4 常用配置

yum安装后默认配置文件

 1 ~]# cat /etc/httpd/conf/httpd.conf
 2 ServerRoot "/etc/httpd"
 3 Listen 80
 4 Include conf.modules.d/*.conf
 5 User apache
 6 Group apache
 7 ServerAdmin root@localhost
 8 ServerName www.example.com:80
 9 <Directory />
10     AllowOverride none
11     Require all denied
12 </Directory>
13 DocumentRoot "/var/www/html"
14 <Directory "/var/www">
15     AllowOverride None
16     Require all granted
17 </Directory>
18 <Directory "/var/www/html">
19     Options Indexes FollowSymLinks
20     AllowOverride None
21     Require all granted
22 </Directory>
23 <IfModule dir_module>
24     DirectoryIndex index.html
25 </IfModule>
26 <Files ".ht*">
27     Require all denied
28 </Files>
29 ErrorLog "logs/error_log"
30 LogLevel warn
31 <IfModule log_config_module>
32     LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
33     LogFormat "%h %l %u %t \"%r\" %>s %b" common
34     <IfModule logio_module>
35       LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
36     </IfModule>
37     CustomLog "logs/access_log" combined
38 </IfModule>
39 <IfModule alias_module>
40     ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
41 </IfModule>
42 <Directory "/var/www/cgi-bin">
43     AllowOverride None
44     Options None
45     Require all granted
46 </Directory>
47 <IfModule mime_module>
48     TypesConfig /etc/mime.types
49     AddType application/x-compress .Z
50     AddType application/x-gzip .gz .tgz
51     AddType text/html .shtml
52     AddOutputFilter INCLUDES .shtml
53 </IfModule>
54 AddDefaultCharset UTF-8
55 <IfModule mime_magic_module>
56     MIMEMagicFile conf/magic
57 </IfModule>
58 EnableSendfile on
59 IncludeOptional conf.d/*.conf

配置格式:directive value;directive 不区分字符大小写;value 为路径时,是否区分大小写,取决于文件系统。

1、显示服务器版本信息

ServerTokens Major|Minor|Min[imal]|Prod[uctOnly]|OS|Full

ServerTokens Prod  #建议关闭显示服务器版本号

2、修改监听的IP和Port

Listen [ip:]port 省略ip表示本机所有IP都监听,至少要有一个监听,此指令可重复出现多次

Listen 80

3、持久连接

Persistent Connection:连接建立,每个资源获取完成后不会断开连接,而是继续等待其它的请求完成,默认关闭持久连接

KeepAlive On  #启用长连接功能
KeepAliveTimeout 15  #保持连接15秒
MaxKeepAliveRequests 100  #断开条件

4、MPM多路处理模块

~]# httpd -M |grep mpm
 mpm_prefork_module (shared)  #默认prefork处理模式
~]# vim /etc/httpd/conf.modules.d/00-mpm.conf  #在此文件中配置使用那种处理模块
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
#LoadModule mpm_worker_module modules/mod_mpm_worker.so
#LoadModule mpm_event_module modules/mod_mpm_event.so

5、DSO加载动态模块配置

配置指定实现模块加载格式: LoadModule <mod_name> <mod_path>

示例:

~]# vim /etc/httpd/conf.modules.d/00-base.conf
LoadModule auth_basic_module modules/mod_auth_basic.so

6、定义‘Main‘ Server的文档页面路径

DocumentRoot "/path" 指向的路径为URL路径的起始位置

DocumentRoot "/var/www/html"
<Directory "/var/www/html">
    Require all granted  #授权可以访问
</Directory>

7、定义站点主页面

<IfModule dir_module>
    DirectoryIndex index.html index.php
</IfModule>

8、站点访问控制常见机制

? 基于文件系统路径:

<Directory  “/path">  #控制文件夹
    ...
</Directory>

<File  “/path/file”>  #控制指定文件
    ...
</File>

<FileMatch  "PATTERN">  #支持正则表达式
    ...
</FileMatch>

? 基于URL路径:

<Location  "">
    ...
</Location>

<LocationMatch "PATTERN">
    ...
</LocationMatch>

1)Options [+|-]option [[+|-]option] ... :后跟1个或多个以空白字符分隔的选项列表在选项前的 +,- 表示增加或删除指定选项,默认Options FollowSymlinks

  • Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户
  • FollowSymLinks:允许访问符号链接文件所指向的源文件
  • All:全部允许
  • None:全部禁用

2)AllowOverride All|None|directive-type [directive-type] ... :与访问控制相关的哪些指令可以放在指定目录下的.htaccess(由AccessFileName指定)文件中,覆盖之前的配置指令;只对<directory>语句有效

  • All: 所有指令都有效
  • None:.htaccess 文件无效
  • AuthConfig Indexes 除了AuthConfig 和Indexes的其它指令都无法覆盖

3)Order Deny,Allow :定义生效次序;写在后面的表示默认法则,2.4版本不再支持

  • Allow from和Deny from:定义客户端地址,拒绝或允许

例:拒绝访问站点下所有以.conf结尾的文件

DocumentRoot "/data/website"
<Directory "/data/website">
    Require all granted
</Directory>
<Files "*.conf">
    Require all denied
</Files>

? 例:允许访问符号链接文件所指向的源文件,但是不允许返回索引列表给用户

<Directory "/data/website">
    Require all granted
    Options -Indexes +FollowSymLinks
</Directory>

9、<Directory>中“基于源地址”实现访问控制

? 不允许指定的主机访问

DocumentRoot "/data/website"
<Directory "/data/website">
    <RequireALL>
        Require all granted
        Require not ip 192.168.0.2  #不允许0.2的主机访问
    </RequireALL>
</Directory>

? 只允许指定主机访问

DocumentRoot "/data/website"
<Directory "/data/website">
    <RequireAny>
        Require all denied
        Require ip 192.168.0.2  #只允许0.2主机访问
    </RequireAny>
</Directory>

10、日志设定

~]# vim /etc/httpd/conf/httpd.conf
ErrorLog "logs/error_log"  #错误日志记录文件
LogLevel warn  #默认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>

错误日志的 LogLevel 可选:debug, info, notice, warn, error,crit, alert, emerg

访问日志:

  • %h 客户端IP地址
  • %l 远程用户,启用mod_ident才有效,通常为减号“-”
  • %u 验证(basic,digest)远程用户,非登录访问时,为一个减号“-”
  • %t 服务器收到请求时的时间
  • %r First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
  • %>s 响应状态码
  • %b 响应报文的大小,单位是字节;不包括响应报文http首部
  • %{Referer}i 请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的
  • %{User-Agent}i 请求报文中首部“User-Agent”的值;即发出请求的应用程序

建议:自定义日志记录格式

<IfModule log_config_module>
    LogFormat "%h %l %u %{%F %T}t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" custlog
    CustomLog "logs/access_log" custlog
</IfModule>

11、设定默认字符集

AddDefaultCharset utf-8  #设置默认字符集为utf-8,默认为AddDefaultCharset Off
<Directory "/data/website">
    Require all granted
    AddDefaultCharset utf-8
</Directory>

12、定义路径别名

格式: Alias /URL/ "/PATH/"

Alias /web2 /data/website2
<Directory "/data/website2">
    Require all granted
</Directory>

访问:http://192.168.0.7/web2 则是访问/data/website 这个目录下的站点

13、基于用户的访问控制

允许账号文件中的所有用户登录访问:Require valid-user

例:基于单用户认证

? 1)定义安全域

Alias /admin "/data/admin"
<Directory "/data/admin">
    AuthType Basic
    AuthName "please input your user and password!"
    AuthUserFile "conf.d/.htuser"
    Require user admin
</Directory>

? 2)提供账号和密码存储(文本文件)

~]# htpasswd -mc /etc/httpd/conf.d/.htuser admin
~]# cat /etc/httpd/conf.d/.htuser
admin:$apr1$Yfglmncl$BC1hebCpPjn1Sn.azt/Zu.
~]# systemctl restart httpd

? 3)测试访问 :http://192.168.0.7/admin/,输入用户名密码即可访问

例:基于组账号进行认证

1)定义安全域

Alias /admin "/data/admin"
<Directory "/data/admin">
    AuthType Basic
    AuthName "please input your user and password!"
    AuthUserFile "conf.d/.htuser"
    AuthGroupFile "conf.d/.htgroup"
    Require group gadmin gadmin2
</Directory>

2)提供账号和密码存储(文本文件)

~]# htpasswd -c /etc/httpd/conf.d/.htuser tom
~]# htpasswd /etc/httpd/conf.d/.htuser jerry
~]# htpasswd /etc/httpd/conf.d/.htuser maria
~]# echo ‘gadmin: tom jerry‘ > /etc/httpd/conf.d/.htgroup
~]# echo ‘gadmin2: tom maria‘ >> /etc/httpd/conf.d/.htgroup

3)测试访问 :http://192.168.0.7/admin/,输入用户名密码即可访问

14、基于模块mod_userdir.so实现用户家目录的http共享

~]# vim /etc/httpd/conf.d/userdir.conf
<IfModule mod_userdir.c>
    #UserDir disabled
    UserDir public_html
</IfModule>
<Directory "/home/user1/public_html">
    AuthType Basic
    AuthName "user1 home dir"
    AuthUserFile "conf.d/.htuser"
    Require user user1
</Directory>
~]# htpasswd -c /etc/httpd/conf.d/.htuser user1
~]# systemctl restart httpd
~]# su -user1
~]$ mkdir public_html
~]$ echo "user1 home dir" > public_html/index.html
~]$ setfacl -m u:apache:x /home/user1/

访问:http://192.168.0.7/~user1/ 站点,输入密码即可登录

15、ServerSignature On | Off | EMail

? 当客户请求的网页并不存在时,服务器将产生错误文档,如果于打开了ServerSignature选项,错误文档的最后一行将包含服务器的名字、Apache的版本等信息;如果不对外显示这些信息,就可以将这个参数设置为Off;设置为Email,将显示ServerAdmin 的Email提示;2.4版本默认值关闭,2.2版本默认开启

16、status页面

LoadModule status_module modules/mod_status.so 确认此模块已加载

httpd]# vim conf.d/myhttp.conf
<Location "/status">
    SetHandler server-status
    Require all granted
</Location>
~]# systemctl restart httpd

访问http://192.168.0.7/status查看服务器状态信息

  • "**_**" Waiting for Connection 等待的连接
  • "S" Starting up
  • "R" Reading Request
  • "W" Sending Reply 有回应的连接
  • "K" Keepalive (read)
  • "D" DNS Lookup
  • "C" Closing connection
  • "L" Logging
  • "G" Gracefully finishing
  • "I" Idle cleanup of worker
  • "." Open slot with no current process 空闲sock个数

17、虚拟主机

注意:一般虚拟机不要与main主机混用;因此,要使用虚拟主机,一般先禁用main主机;注释中心主机的DocumentRoot指令即可。

2.4版本基于FQDN的虚拟主机不再需要NameVirutalHost指令

data]# mkdir website{1..3}
data]# echo ‘<h1>website 1</h1>‘ > website1/index.html
data]# echo ‘<h1>website 2</h1>‘ > website2/index.html
data]# echo ‘<h1>website 3</h1>‘ > website3/index.html
~]# vim /etc/httpd/conf/httpd.conf
#Listen 80
#DocumentRoot "/var/www/html"

三种实现方案:

  • 基于PORT:为每个虚拟主机使用至少一个独立的PORT
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 81
Listen 82
Listen 83
<Directory "/data">
    Require all granted
</Directory>
<VirtualHost *:81>
    DocumentRoot "/data/website1"
    ServerName www.web1.com
    ErrorLog "logs/web1_error_log"
    TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost *:82>
    DocumentRoot "/data/website2"
    ServerName www.web2.com
    ErrorLog "logs/web2_error_log"
    TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost *:83>
    DocumentRoot "/data/website3"
    ServerName www.web3.com
    ErrorLog "logs/web3_error_log"
    TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd

分别访问:http://192.168.0.7:81 和 http://192.168.0.7:82 和 http://192.168.0.7:83

  • 基于IP:为每个虚拟主机准备至少一个IP地址
~]# ip a a 192.168.0.11/24 dev eth0
~]# ip a a 192.168.0.12/24 dev eth0 
~]# ip a a 192.168.0.13/24 dev eth0 
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 80
<Directory "/data">
    Require all granted
</Directory>
<VirtualHost 192.168.0.11:80>
    DocumentRoot "/data/website1"
    ServerName www.web1.com
    ErrorLog "logs/web1_error_log"
    TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost 192.168.0.12:80>
    DocumentRoot "/data/website2"
    ServerName www.web2.com
    ErrorLog "logs/web2_error_log"
    TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost 192.168.0.13:80>
    DocumentRoot "/data/website3"
    ServerName www.web3.com
    ErrorLog "logs/web3_error_log"
    TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd

分别访问:192.168.0.11192.168.0.12192.168.0.13

  • 基于FQDN:为每个虚拟主机使用至少一个FQDN
~]# vim /etc/httpd/conf.d/virtualhost.conf
Listen 80
<Directory "/data">
    Require all granted
</Directory>
<VirtualHost *:80>
    DocumentRoot "/data/website1"
    ServerName www.web1.com
    ErrorLog "logs/web1_error_log"
    TransferLog "logs/web1_access_log"
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/data/website2"
    ServerName news.web2.com
    ErrorLog "logs/web2_error_log"
    TransferLog "logs/web2_access_log"
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "/data/website3"
    ServerName bbs.web3.com
    ErrorLog "logs/web3_error_log"
    TransferLog "logs/web3_access_log"
</VirtualHost>
~]# systemctl restart httpd

客户端测试:

~]# vim /etc/hosts  添加以下内容
192.168.0.7 www.web1.com news.web2.com bbs.web3.com
~]# curl www.web1.com
~]# curl news.web2.com
~]# curl bbs.web3.com

18、使用mod_deflate模块压缩页面优化传输速度

LoadModule deflate_module modules/mod_deflate.so 模块默认已经加载

# httpd -M |grep deflate
 deflate_module (shared)
conf.d]# vim myhttpd.conf
# 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
DeflateCompressionLevel 9  #压缩比为 9

19、启用Sendfile功能

~]# vim /etc/httpd/conf/httpd.conf
EnableSendfile On

 

Apache httpd服务——常用配置

标签:out   symlink   web3   div   控制   share   admin   comm   exe   

原文地址:https://www.cnblogs.com/Gmiaomiao/p/9220813.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!