Configure the Apache httpd.conf file

Apache's configuration directives are divided into two categories: one is core directives, which are usually provided by core modules, and these implementations must be written in httpd.conf, otherwise Apache may not work, and the other is provided by standard modules or third-party modules Provides, such as the dav functionality provided by the mod_dav module.

 

The most important of all Apache modules are core_module, so_module, http_module and mpm module. Except for so_module, which can be turned off or disabled like other modules, nothing else will work.

 

The configuration of Apache is very simple. The main configuration file is httpd.conf. To configure virtual hosts, you need to load and modify the corresponding virtual host configuration files. Other configuration files are referenced in httpd.conf. Assuming that our Apache is compiled and installed in /usr/local/apache, the configuration that Apache needs to be referenced is placed in the conf/extra directory of this path, usually dav configuration, virtual host configuration, ssl configuration, etc.

 

The configuration in httpd.conf can be divided into 3 parts: the main server part, the container part, and the server extension part.

 

1. The main server part

1. ServerName command

Used to configure Apache default hostname, the value is your site name or IP address. The full IP address is recommended. E.g:

ServerName www.orache.com or ServerName 192.168.171.133, if you do not use this command to specify the default host name of Apache, you will see a warning message when starting with # ./apachectl start, the system prompts that the domain name cannot be found, Therefore, only 127.0.0.1 can be used as the default address of the server, which of course can only be accessed by this machine. If the full site name is used, then Apache will decide whether to look for the IP corresponding to the site name in the thread-local hostname list file /etc/hosts based on the configuration of the host's host.conf file.

Query DNS for the IP corresponding to the site name.

 

2. ServerRoot instruction

Used to define the directory where the server is located. This path is usually specified at compile time by the --prefix=ServerRoot path option. If you installed with binary packages, ServerRoot is located under /etc/apache2.

You can also use the -d parameter to specify an unknown ServerRoot when starting Apache. This approach is only used to test the configuration of the same version of Apache in multiple environments.

 

3. DocumentRoot instruction

That is, the root directory of the URL request received by Apache. The path can only be an absolute path. If there are spaces in the path, quotation marks are required.

 

4. ServerAdmin command

Used to define the mailbox used to notify administrators when an error occurs on the server.

ServerAdmin [email protected]

 

5. ScriptAlias ​​and Alias ​​instructions

They function similarly and are both used to map directories. It's just that ScriptAlias ​​recognizes the mapped directory as a CGI script directory. Alias, on the other hand, is only mapped to an ordinary directory. Documents in the target directory specified by ScriptAlias ​​are treated as applications, and the server will run them instead of sending them as documents to the client. Alias: Maps web paths into file system paths for accessing content that is not under the Document Root.

 

6. User and Group instructions

ps aux | grep httpd to view it

 

7. Listen command

listening port. Range value: 0~65535, but 0~1024 are reserved for system services.

 

8. LoadModule instruction

Used to load modules or object files

 

9. ErrorDocument instruction

 There are 3 ways to use it, define text messages, use scripts, specify a page. E.g:

ErrorDocument 500 "unknown error"

ErrorDocument 404 "/srv/www/cgi-bin/missing_404.pl"

ErrorDocument 402 http://www.jonsk.com/info_402.html

 

10. Options command

This directive determines which server features are used in which directories

 

2. The container part

1、<IfModule>容器

它作用于模块,它先判断模块是否载入,再决定是否进行处理。

<IfModule !mpm_netware_module>的意思是如果载入了mpm_netware_module模块则不执行容器内的指令。IfModule指令可以嵌套。

<IfModule dir_module>

    DirecoryIndex index.html

</IfModule>

 

2、<IfDefine>容器

<IfDefine Proxy>

    LoadModule proxy_module modules/libproxy.so

</IfDefine>

下面的启动命令定义了一个名字Proxy,可以在IfDefine容器中使用

# httpd -D Proxy

 

3、<Directory>与<DirectoryMatch>容器

<Directory>容器的作用就是让它所封装的指令在指定的目录及它的子目录中起作用,这个目录必须是一个

完整的路径,当然你也可以使用通配符 * ? [],但这3者都不能匹配/。

禁止对/srv/apache/html/目录的访问权限,请求被拒绝:

<Directory /srv/apache/html/>

Order Deny,Allow

Deny from All

</Directory>

下面的利用到了正则表达式:

<Directory ~ "^/srv/apache[0-9]{2}/html/">

Order Deny,Allow

Allow from All

</Directory>

 

<DirectoryMatch>与Directory作用类似,都是作用于目录,只不过<DirectoryMatch>用到正则表达式时,

不需要 ~ ,例如:

<DirectoryMatch "^/srv/apache[0-9]{2}/html/">

Order Deny,Allow

Allow from All

</DirectoryMatch>

 

4. <Files>与<FilesMatch>容器

匹配所有的以.ht文件结尾的文件并允许所有用于访问

<Files "^\.ht">

Order Deny,Allow

Allow from All

<Files>

 

5、<Location>与<LocationMatch>容器

它们是对URL进行访问控制

<Location /cgi>

............................

</Location>

还可以将URL请求映射到apche模块处理器上,例如,使用Apache自带的mod_status模块:

<Location /server-status>

SetHandler server-status

</Location>

 

三、服务器扩展部分

在Apache的默认配置文件夹中有一个extra目录,这个目录用来存放Apache其他模块的配置文件。

Include指令可以通过通配符来加载多个文件,而这个顺序则是按照字母顺序加载,如果指向一个目录,

则会按字母顺序加载这个目录下的所有文件,对于加载文件路径,可以使用绝对路径,也可以使用

相对于ServerRoot目录的相对路径,如:

Include /home/apache/conf/httpd-vhosts.conf

Include extra/httpd-vhosts.conf

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326830924&siteId=291194637