Apache 的配置
- 定义apache服务器所在根目录
刚开始用windowscmd.exe
打开apache服务器时会报错
httpd: Syntax error on line 39 of E:/StudyFile/Apache24/conf/httpd.conf: ServerR
oot must be a valid directory
这是因为默认的是在c:/Apache24
(版本不同名称也不同)一般都需要自行修改。
找到apache文件目录下的conf文件夹下的httpd.conf
文件进行修改
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# Do not add a slash at the end of the directory path. If you point
# ServerRoot at a non-local disk, be sure to specify a local disk on the
# Mutex directive, if file-based mutexes are used. If you wish to share the
# same ServerRoot for multiple httpd daemons, you will need to change at
# least PidFile.
#下面路径改为自己存放apache文件夹的路径
Define SRVROOT "c:/Apache24"
- apache的网站根目录:默认 Apache 的网站根目录是安装目录中的
htdocs
文件夹,为了方便对网站文件的管理,一般我们会将其设置在
一个自定义目录中
如需修改:路径Apache24\conf
(版本不一样修改内容有少许差异)httpd.conf
文件
注意区分符号是/
而不是windows复制文件路径的\
。
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#修改apache网站根目录 两个都需要修改
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">
- 监听端口的修改(浏览器端口为 80)
监听端口可以随意修改为任意一个未被其他程序监听的端口,可以通过设置配置文件httpd.conf
中的 Listen指令后面的数字修改。
#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
#
#Listen 12.34.56.78:80
Listen 80
- 默认文档
当客户端访问的是一个目录而不是具体文件时,服务端默认返回这个目录下的某个文档(文件),这个文档称为默认文档。
配置文件httpd.conf
的 280多行的 DirectoryIndex ,默认文档可以配置多个(有前到后依次去找,找到为止,如果没找到任何一个则启用目录浏览)
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
如:加上php
#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
apache支持php(我的是php-7.3.4-Win32-VC15-x64)
php有个配置文件php7apache2_4.dll
(不同版本名字不一样)
我们需要把php的工作拿给apache来执行
配置apache的httpd.conf
文件引用 php7apache2_4.dll
在180行左右添加一行LoadModule
#=========================================================================================
# PHP的处理模块(包工头)
# 模块不是根据后缀名判断是否用 php 工作,而是根据 MINE Type 是不是 application/x-httpd-php 决定是否使用php
# 这里的路径根据自己实际目录而定
#此处如果路径有空格则需要放在“”内。
#如:LoadModule php7_module "E:/study file/PHP/php-7.3.4-Win32-VC15-x64/php7apache2_4.dll"
LoadModule php7_module E:/StudyFile/PHP/php-7.3.4-Win32-VC15-x64/php7apache2_4.dll
#=========================================================================================
然后在httpd.conf
文件的440行左右添加MIME类型的文件扩展名(<IfModule mime_module></IfModule>
内添加)
#
# Filters allow you to process content before it is sent to the client.
#
# To parse .shtml files for server-side includes (SSI):
# (You will also need to add "Includes" to the "Options" directive.)
#
#AddType text/html .shtml
#AddOutputFilter INCLUDES .shtml
AddType application/x-httpd-php .php
</IfModule>