@qidiandasheng
2021-04-22T22:42:42.000000Z
字数 2166
阅读 2533
Web服务
打开终端(terminal),输入 sudo apachectl -v。会显示Apache的版本。
接着输入 sudo apachectl start,这样Apache就启动了。打开Safari浏览器地址栏输入 “http://localhost”,可以看到内容为“It works!”的页面。
Apache默认根目录(Web页面)
/Library(资源库)/WebServer/Documents/
Apache的安装目录(相关配置)
/etc/apache2/
sudo apachectl start
sudo apachectl restart
sudo apachectl stop
apache默认不需要虚拟主机是可以正常访问的,虚拟主机主要是解决多个域名访问的问题。 比如你的站点要配置多个域名,可以通过虚拟主机实现,如果只是一个域名,就不需要配置虚拟主机了
在/etc/apache2/目录下找到httpd.conf文件,打开并找到#Include /private/etc/apache2/extra/httpd-vhosts.conf,去掉#然后保存。
打开/etc/apache2/extra/httpd-vhosts.conf文件。其中默认开启了两个作为例子的虚拟主机。
<VirtualHost *:80>ServerAdmin webmaster@dummy-host.example.comDocumentRoot "/usr/docs/dummy-host.example.com"ServerName dummy-host.example.comErrorLog "/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.comDocumentRoot "/usr/docs/dummy-host2.example.com"ServerName dummy-host2.example.comErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common</VirtualHost>
而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:
Forbidden
You don't have permission to access /index.php on this server
最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。
在/etc/apache2/extra/httpd-vhosts.conf文件中加入以下两个虚拟主机
<VirtualHost *:80>DocumentRoot "/Library/WebServer/Documents"ServerName localhostErrorLog "/private/var/log/apache2/localhost-error_log"CustomLog "/private/var/log/apache2/localhost-access_log" common</VirtualHost><VirtualHost *:80>DocumentRoot "/Library/WebServer/Test"ServerName mysites.comErrorLog "/private/var/log/apache2/sites-error_log"CustomLog "/private/var/log/apache2/sites-access_log" common<Directory "/Library/WebServer/Test">Options Indexes FollowSymLinks MultiViewsAllowOverride AllRequire all granted</Directory></VirtualHost>
然后在/etc/hosts文件中加入127.0.0.1 mysites.com,保存重新启动Apache。
最后在浏览器中输入localhost显示的就是/Library/WebServer/Documents目录下的index.html。输入mysites.com显示的就是/Library/WebServer/Test目录下的index.html。
