Apache官网:http://www.apache.org/
Apache 命令:
启动Apache:sudo apachectl start
关闭Apache:sudo apachectl stop
重启Apache:sudo apachectl restart
配置 PHP:
1、打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
2、找到 #LoadModule php5_module libexec/apache2/libphp5.so,去掉注释(删除前面的 #)。
注:创建 phpinfo.php,添加 内容 <?php phpinfo(); ?>。可通过浏览器 访问 phpinfo.php 获取 php 信息。
配置 Virtual hosts:
1.、打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
2、找到下面的配置,去掉 #Include /private/etc/apache2/extra/httpd-vhosts.conf 的注释(删除前面的 #)
原始配置:
# Virtual hosts
#Include /private/etc/apache2/extra/httpd-vhosts.conf
修改后的配置:
# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf
3、找到下面的配置并修改保存退出
原始的配置:
<Directory />
AllowOverride none
Require all denied
</Directory>
修改后的配置:
<Directory />
Options Indexes FollowSymLinks
AllowOverride None
Order deny,allow
Allow from all
</Directory>
4、找到下面的配置,注释掉 AllowOverride None,添加 AllowOverride All
原始配置:
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 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>
修改后的配置:
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 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
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
5、打开终端,输入命令:sudo vim /etc/apache2/extra/httpd-vhosts.conf
6、注释掉下列配置:
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host.example.com
# DocumentRoot "/usr/docs/dummy-host.example.com"
# ServerName dummy-host.example.com
# ServerAlias www.dummy-host.example.com
# ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
# CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
#</VirtualHost>
#<VirtualHost *:80>
# ServerAdmin webmaster@dummy-host2.example.com
# DocumentRoot "/usr/docs/dummy-host2.example.com"
# ServerName dummy-host2.example.com
# ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
# CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
#</VirtualHost>
7、添加下列配置,这里添加了 localhost 和 project.com 两个虚拟主机;"/Library/WebServer/Documents" 为默认目录,"/Users/mac168/sites" 为自定义目录。
<VirtualHost *:80>
DocumentRoot "/Library/WebServer/Documents"
ServerName localhost
ErrorLog "/private/var/log/apache2/localhost-error_log"
CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "~/sites"
ServerName project.com
ErrorLog "/private/var/log/apache2/sites-error_log"
CustomLog "/private/var/log/apache2/sites-access_log" common
<Directory "~/sites">
Options Indexes FollowSymLinks MultiViews
AllowOverride none
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
8、打开终端,输入命令:sudo vim /etc/hosts
9、修改为下列配置
127.0.0.1 localhost
127.0.0.1 project.com
255.255.255.255 broadcasthost
::1 localhost
::1 project.com
注意事项:
1、DocumentRoot 根目录如果没有 index 索引,访问 localhost 会提示 403 Forbidden。
2、自定义 DocumentRoot 根目录时,根目录的每一级都要有访问权限,否则同样提示 403 Forbidden。可通过 终端命令 chmod 修改,也可通过 command + i,显示目录简介,在 共享与权限 中修改(修改为 everyone 修改为 只读即可)。