[关闭]
@qidiandasheng 2021-04-22T22:42:42.000000Z 字数 2166 阅读 2000

Apache(❎)

Web服务


简介

Mac 配置Apache

启动Apache

打开终端(terminal),输入 sudo apachectl -v。会显示Apache的版本。

接着输入 sudo apachectl start,这样Apache就启动了。打开Safari浏览器地址栏输入 “http://localhost”,可以看到内容为“It works!”的页面。

相关目录

相关命令

启动Apache

sudo apachectl start

重启Apache

sudo apachectl restart

停止Apache

sudo apachectl stop

Apache 配置虚拟主机

为什么配置虚拟主机

apache默认不需要虚拟主机是可以正常访问的,虚拟主机主要是解决多个域名访问的问题。 比如你的站点要配置多个域名,可以通过虚拟主机实现,如果只是一个域名,就不需要配置虚拟主机了

启动虚拟主机

/etc/apache2/目录下找到httpd.conf文件,打开并找到#Include /private/etc/apache2/extra/httpd-vhosts.conf,去掉#然后保存。

配置虚拟主机

打开/etc/apache2/extra/httpd-vhosts.conf文件。其中默认开启了两个作为例子的虚拟主机。

  1. <VirtualHost *:80>
  2. ServerAdmin webmaster@dummy-host.example.com
  3. DocumentRoot "/usr/docs/dummy-host.example.com"
  4. ServerName dummy-host.example.com
  5. ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
  6. CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
  7. </VirtualHost>
  8. <VirtualHost *:80>
  9. ServerAdmin webmaster@dummy-host2.example.com
  10. DocumentRoot "/usr/docs/dummy-host2.example.com"
  11. ServerName dummy-host2.example.com
  12. ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
  13. CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
  14. </VirtualHost>

而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:
Forbidden
You don't have permission to access /index.php on this server

最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。

增加自定义的虚拟主机

/etc/apache2/extra/httpd-vhosts.conf文件中加入以下两个虚拟主机

  1. <VirtualHost *:80>
  2. DocumentRoot "/Library/WebServer/Documents"
  3. ServerName localhost
  4. ErrorLog "/private/var/log/apache2/localhost-error_log"
  5. CustomLog "/private/var/log/apache2/localhost-access_log" common
  6. </VirtualHost>
  7. <VirtualHost *:80>
  8. DocumentRoot "/Library/WebServer/Test"
  9. ServerName mysites.com
  10. ErrorLog "/private/var/log/apache2/sites-error_log"
  11. CustomLog "/private/var/log/apache2/sites-access_log" common
  12. <Directory "/Library/WebServer/Test">
  13. Options Indexes FollowSymLinks MultiViews
  14. AllowOverride All
  15. Require all granted
  16. </Directory>
  17. </VirtualHost>

然后在/etc/hosts文件中加入127.0.0.1 mysites.com,保存重新启动Apache。

浏览器显示

最后在浏览器中输入localhost显示的就是/Library/WebServer/Documents目录下的index.html。输入mysites.com显示的就是/Library/WebServer/Test目录下的index.html

参考

Mac OS X中配置Apache
Apache HTTP服务器

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注