MAC 默认是自带 Apache 服务器的,但是我们需要后天的去使用配置一下它。
// 开启
sudo apachectl -k start
// 关闭
sudo apachectl -k stop
// 重启
sudo apachectl -k restart
// 开启
sudo apachectl start
// 关闭
sudo apachectl stop
// 重启
sudo apachectl restart
// mac hosts 文件目录
/private/etc/hosts
// 配置文件路径
/etc/apache2/httpd.conf
// 备份配置文件
sudo cp httpd.conf httpd.conf.backup
// 还原配置(其实就是用备份文件覆盖)
sudo cp httpd.conf.backup httpd.conf
// 虚拟机配置路径
/etc/apache2/extra/httpd-vhosts.conf
// php.ini 路径
/etc/php.ini
// 将 php.ini.default 拷贝为 php.ini
sudo cp php.ini.default php.ini
sudo apachectl -k start
运行之后结果:
dengzemiaodeMacBook-Pro:~ dengzemiao$ sudo apachectl -k start
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using dengzemiaodeMacBook-Pro.local. Set the 'ServerName' directive globally to suppress this message
dengzemiaodeMacBook-Pro:~ dengzemiao$
咋们先不管警告啥的,因为咋们是新机器肯定啥都没有的,启动之后浏览器打开IP地址:http://localhost 或 http://127.0.0.1,如果浏览器输出 It works!,那就是启动成功了。
# 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.
#
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
没有加入Indexes的打开 http://127.0.0.1 效果:
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
#ServerName www.example.com:80 -》 默认配置就是 127.0.0.1
# 现在我们需要自定义一个 ServerName
ServerName 127.0.0.1
#LoadModule php7_module libexec/apache2/libphp7.so -》默认是被注释的
LoadModule php7_module libexec/apache2/libphp7.so
然后在配置文件中搜索关键字"DocumentRoot",将DocumentRoot与Directory后面的默认路径替换为上面的自定义路径。
# 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.
#
# /Library/WebServer/Documents -》 默认根目录
# /Users/dengzemiao/Sites -》 自定义根目录
DocumentRoot "/Users/dengzemiao/Sites"
<Directory "/Users/dengzemiao/Sites">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
我在自定义目录里面创建一个自定义index2.html(里面其他文件是我从默认路径那边拷贝过来的,这些都是没用的文件只是测试用一下):
然后在启动 `Apache` 之后访问路径:http://127.0.0.1/index2.html
注意了:以后开发写的php文件就是在这个 Sites 文件夹中进行创建文件或者项目开发,因为只有这里面你的php文件才会被执行成功,在其他地方则会显示源码不会被执行成功。
cd /etc
sudo cp php.ini.default php.ini
(这一步好像可以不用配置,我忘了之前的操作了) 并在httpd.conf中添加配置:
// 只需要添加路径即可,不需要带文件名
PHPIniDir /private/etc
sudo apachectl -k start
启动完成后,用浏览器访问127.0.0.1,可以看到放置在本地资源根目录下的文件。下面是一些 Apache 细节检测设置功能介绍,可选择性了解。
如果配置文件不小心改错或者写错东西
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
Listen 80
</IfDefine>
不小心改成
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
Listenss1 80
</IfDefine>
可以通过命令行
sudo apachectl -t
来检测配置文件是否有问题,上面改错之后会输出
dengzemiaodeMacBook-Pro:etc dengzemiao$ sudo apachectl -t
AH00526: Syntax error on line 56 of /private/etc/apache2/httpd.conf:
Invalid command 'Listenss1', perhaps misspelled or defined by a module not included in the server configuration
dengzemiaodeMacBook-Pro:etc dengzemiao$
如果没有问题,则会输出:
dengzemiaodeMacBook-Pro:etc dengzemiao$ sudo apachectl -t
Syntax OK