Apache configurationfiletutorial

LearningApacheconfigurationfile structure and basicconfiguration指令

查看tutoriallist

Apache configurationfilestructure

Apache configurationfile is managementserverbehavior core, Understand其structure and basicconfiguration指令 for 于 has 效地management and 定制Apacheserver至关 important . 本文将详细介绍Apacheconfigurationfile structure, 主要configurationfile and basicconfiguration指令.

configurationfile 位置

Apache configurationfile位于不同 位置, 具体取决于operationsystem:

operationsystem 主configurationfile位置 configurationTable of Contents
Ubuntu/Debian /etc/apache2/apache2.conf /etc/apache2/
CentOS/RHEL /etc/httpd/conf/httpd.conf /etc/httpd/
Windows ApacheinstallationTable of Contents/conf/httpd.conf ApacheinstallationTable of Contents/conf/

主要configurationfile

主configurationfile

  • httpd.conf: in CentOS/RHEL and Windowssystem on , 这 is Apache 主configurationfile, package含全局configuration指令.
  • apache2.conf: in Ubuntu/Debiansystem on , 这 is Apache 主configurationfile, 相当于httpd.conf.

辅助configurationfile

in Ubuntu/Debiansystem on , Apache configurationfile被分割成 many 个辅助file, 存放 in 不同 Table of Contentsin:

  • sites-available/: 存放虚拟主机configurationfile Table of Contents.
  • sites-enabled/: 存放已启用 虚拟主机configurationfile 符号链接.
  • mods-available/: 存放moduleconfigurationfile Table of Contents.
  • mods-enabled/: 存放已启用 moduleconfigurationfile 符号链接.
  • conf-available/: 存放otherconfigurationfile Table of Contents.
  • conf-enabled/: 存放已启用 otherconfigurationfile 符号链接.

configurationfile package含relationships

主configurationfile通常会throughInclude or IncludeOptional指令package含otherconfigurationfile, 形成一个完整 configuration体系. 例such as, in Ubuntu/Debiansystem on , apache2.conf会package含sites-enabled/Table of Contents under 所 has configurationfile.

configurationfile 语法

basic语法规则

  • 指令格式: configuration指令通常由指令名称 and 一个 or many 个parameter组成, 指令名称 and parameter之间用空格分隔.
  • 行尾: 每条指令必须单独占一行, 不需要分号结尾.
  • comment: 以#开头 行被视 for comment.
  • big small 写: 指令名称 big small 写不敏感, 但parameter通常 big small 写敏感.
  • 空格: many 余 空格 and 制表符会被ignore.
  • long 行: 可以using反斜杠\将 long 行分成 many 行.

指令块

某些指令需要 in specific 指令块inusing, 指令块由开始tag and 结束tag组成:

<指令块名称 parameter> # 指令块 in configuration指令 </指令块名称>

例such as, Directory指令块用于configurationspecificTable of Contents behavior:

<Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

basicconfiguration指令

全局configuration指令

ServerRoot

指定Apache installationTable of Contents, other相 for path configuration指令都基于此Table of Contents.

# Ubuntu/Debian ServerRoot "/etc/apache2" # CentOS/RHEL ServerRoot "/etc/httpd" # Windows ServerRoot "C:/Apache24"
Listen

指定Apache监听 IP地址 and 端口.

# 监听所 has IP地址 80端口 Listen 80 # 监听specificIP地址 specific端口 Listen 192.168.1.100:8080 # 监听IPv6地址 Listen [::]:80
ServerAdmin

指定servermanagement员 电子email地址, 当server遇 to issues时, 会 in error页面in显示此地址.

ServerAdmin admin@example.com
ServerName

指定server 域名 or IP地址, 用于creation重定向URL and 显示 in errormessagein.

ServerName www.example.com:80
DocumentRoot

指定网站file 存放Table of Contents, 即网站 根Table of Contents.

# Ubuntu/Debian DocumentRoot "/var/www/html" # CentOS/RHEL DocumentRoot "/var/www/html" # Windows DocumentRoot "C:/Apache24/htdocs"

Table of Contentsconfiguration指令

Directory指令块用于configurationspecificTable of Contents behavior:

<Directory /var/www/html> # 允许Table of Contentsindex and 跟随符号链接 Options Indexes FollowSymLinks # 不允许.htaccessfile覆盖configuration AllowOverride None # 允许所 has 访问 Require all granted </Directory>

logconfiguration指令

ErrorLog

指定errorlogfile 位置.

# Ubuntu/Debian ErrorLog ${APACHE_LOG_DIR}/error.log # CentOS/RHEL ErrorLog "logs/error_log" # Windows ErrorLog "logs/error.log"
CustomLog

指定访问logfile 位置 and 格式.

# Ubuntu/Debian CustomLog ${APACHE_LOG_DIR}/access.log combined # CentOS/RHEL CustomLog "logs/access_log" combined # Windows CustomLog "logs/access.log" combined

moduleconfiguration指令

LoadModule指令用于加载Apachemodule:

# 加载mod_sslmodule LoadModule ssl_module modules/mod_ssl.so # 加载mod_rewritemodule LoadModule rewrite_module modules/mod_rewrite.so # 加载mod_proxymodule LoadModule proxy_module modules/mod_proxy.so

configurationfile management

configurationfile 编辑

编辑Apacheconfigurationfile时, 应注意以 under 几点:

  • backup: in modifyconfigurationfile之 before , 始终backup原始file.
  • permission: 确保你 has 足够 permission编辑configurationfile ( in Linux on 通常需要rootpermission) .
  • 语法: 确保configuration指令 语法正确, 避免拼写error and 语法error.
  • test: modifyconfiguration after , usingapachectl configtest or apache2ctl configtestcommandstestconfigurationfile 语法.
  • 重启: testthrough after , 重启Apacheservice使configuration生效.

configurationfile package含

for 了更 good 地组织configuration, Apache允许usingInclude指令package含otherconfigurationfile:

# package含单个file Include /etc/apache2/conf-available/security.conf # package含Table of Contentsin 所 has file Include /etc/apache2/conf-enabled/*.conf # 可选package含 (such as果file不存 in 也不会报错) IncludeOptional /etc/apache2/conf-available/*.conf

Notes

in usingInclude指令时, 应注意configurationfile 加载顺序, 因 for after 加载 configuration会覆盖先加载 configuration (除非using了specific 指令, such as<IfModule>) .

实践case: configurationbasic网站

步骤1: creation网站Table of Contents

首先creation一个用于存放网站file Table of Contents:

sudo mkdir -p /var/www/example.com/public_html

步骤2: 设置Table of Contentspermission

设置适当 Table of Contentspermission:

sudo chown -R $USER:$USER /var/www/example.com/public_html sudo chmod -R 755 /var/www

步骤3: creationtest页面

creation一个 simple HTMLtest页面:

echo '<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Example.com</title> </head> <body> <h1>欢迎访问 Example.com!</h1> <p>这 is a test网站. </p> </body> </html>' > /var/www/example.com/public_html/index.html

步骤4: creation虚拟主机configurationfile

in Ubuntu/Debian on , creation虚拟主机configurationfile:

sudo nano /etc/apache2/sites-available/example.com.conf

添加以 under configuration:

<VirtualHost *:80> ServerAdmin admin@example.com ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/public_html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

步骤5: 启用虚拟主机

启用虚拟主机并testconfiguration:

sudo a2ensite example.com.conf sudo apache2ctl configtest sudo systemctl restart apache2

步骤6: test网站

in 浏览器in访问http://example.com, 你应该能看 to 你creation test页面.

互动练习

练习1: modifyApache默认端口

modifyApache configurationfile, 将默认端口 from 80改 for 8080, 然 after testconfiguration并重启Apacheservice, verificationservice is 否 in new 端口 on 正常run.

练习2: configuration自定义documentation根Table of Contents

creation一个 new Table of Contentsserving asApache documentation根Table of Contents, modifyconfigurationfile指向这个 new Table of Contents, 然 after creation一个test页面并verification网站 is 否正常访问.

练习3: configurationTable of Contents访问控制

modifyApache configurationfile, for specificTable of Contents设置访问控制, 禁止直接访问该Table of Contents in 容 (禁用Table of Contentsindex) , 然 after testconfiguration并verification效果.