Apache 配置详解

目录索引配置

使用模块:mod_autoindex

  1. 确认 LoadModule autoindex_module libexec/apache22/mod_autoindex.so 开启
  2. 将 Include etc/apache22/extra/httpd-autoindex.conf 启用
  3. 目录的 Options 必需包含 Indexes
/usr/local/etc/apache22/extra/httpd-autoindex.conf (FreeBSD)

IndexOptions FancyIndexing HTMLTable VersionSort FoldersFirst ScanHTMLTitles
IndexOptions NameWidth=35 DescriptionWidth=39 IconHeight=16 IconWidth=16
IndexOptions Charset=utf-8

目录别名配置

如将“D:\Documents\My Dropbox\wiki”路径下的wiki目录映射到网站根目录的/wiki/

修改httpd.conf,添加字段:

<Directory "D:/Documents/My Dropbox/wiki">
    Options Indexes FollowSymlinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

搜索到<IfModule alias_module>模块,在其中添加

Alias /wiki/ "D:/Documents/My Dropbox/wiki/"

最后重启 Apache 服务

目录密码配置

apache 中禁止一般用户访问后台特定目录

假定 /data/update/admin 为后台管理目录,那么必须设定对该目录访问需要验证

编辑 httpd.conf

<Directory "/data/update/admin"> 
  authtype basic 
  authname "Private" 
  authuserfile /usr/local/www/apache22/auth.dat  //保存认证信息的文件
  require user username  //进行认证的用户名
  Options Indexes FollowSymlinks MultiViews 
  AllowOverride None 
</Directory>

运行命令htpasswd -c auth.dat username 进行密码的设置。

重载apache配置

(For Linux) /etc/init.d/apache2 reload
(For Ubuntu) service apache2 reload

Apache 回查特性

httpd.conf

<Directory "D:/htdocs">
  AcceptPathInfo On
  <Files articles>
    ForceType application/x-httpd-php
  </Files>
</Directory>

articles 文件

// echo $_SERVER['PATH_INFO'];
list($category, $id) = explode("/", $_SERVER['PATH_INFO']);

// another example
$qs = $_SERVER['QUERY_STRING'];
$pos = strrpos($qs, '://');
$pos = strpos($qs, '/', $pos + 4);
$_SERVER['REQUEST_URI'] = substr($qs, $pos);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include('index.php');

Ubuntu 特定

配置文件列表

路径 /etc/apache2

apache2.conf  envvars          mods-available/  sites-available/
httpd.conf    mods-enabled/    sites-enabled    conf.d/
magic         ports.conf

启用与禁用模块

a2enmod rewrite
a2dismod rewrite

启用与禁用站点

a2ensite www.example.com
a2disstie useless.example.com