Mac OS X中配置Apache

我使用的Mac OS X版本是10.10.2,Mac自带了Apache环境。

  • 启动Apache
  • 设置虚拟主机

启动Apache

打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密)。如下显示Apache的版本

接着输入 sudo apachectl start,这样Apache就启动了。打开Safari浏览器地址栏输入 “http://localhost”,可以看到内容为“It works!”的页面。其位于“/Library(资源库)/WebServer/Documents/”下,这就是Apache的默认根目录。

Apache的安装目录在:/etc/apache2/,etc默认是隐藏的。有三种方式查看:

dock下右键Finder,选择”前往文件夹”,输入”/etc” 
在finder下----》前往---》前往文件夹,然后输入/etc 
可以在terminal 输入 “open /etc”

设置虚拟主机

在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件 
在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出。 
运行“sudo apachectl restart”,重启Apache后就开启了虚拟主机配置功能。 
运行“sudo vi /etc/apache2/extra/httpd-vhosts.conf”,就打开了配置虚拟主机文件httpd-vhost.conf,配置虚拟主机了。需要注意的是该文件默认开启了两个作为例子的虚拟主机:

<code class="language-shell hljs avrasm has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><VirtualHost *:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>>
    ServerAdmin webmaster@dummy-host<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.example</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>
    DocumentRoot <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/docs/dummy-host.example.com"</span>
    ServerName dummy-host<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.example</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>
    ErrorLog <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/dummy-host.example.com-error_log"</span>
    CustomLog <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/dummy-host.example.com-access_log"</span> common
</VirtualHost>

<VirtualHost *:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">80</span>>
    ServerAdmin webmaster@dummy-host2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.example</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>
    DocumentRoot <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/usr/docs/dummy-host2.example.com"</span>
    ServerName dummy-host2<span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.example</span><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">.com</span>
    ErrorLog <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/dummy-host2.example.com-error_log"</span>
    CustomLog <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/dummy-host2.example.com-access_log"</span> common
</VirtualHost></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:

<code class="hljs vhdl has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">Forbidden
You don<span class="hljs-attribute" style="box-sizing: border-box;">'t</span> have permission <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">access</span> /index.php <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">on</span> this server</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li></ul>

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

增加如下配置

<code class="hljs apache has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"><VirtualHost *:80></span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">DocumentRoot</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/Library/WebServer/Documents"</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ServerName</span> localhost
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">ErrorLog</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/localhost-error_log"</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">CustomLog</span> <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"/private/var/log/apache2/localhost-access_log"</span> common
<span class="hljs-tag" style="color: rgb(0, 102, 102); box-sizing: border-box;"></VirtualHost></span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; background-color: rgb(238, 238, 238); top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right;"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li></ul>

保存退出,并重启Apache。

运行“sudo vi /etc/hosts”,打开hosts配置文件,加入”127.0.0.1 mysites”,这样就可以配置完成sites虚拟主机了,可以访问“http://mysites”了,在10.8之前Mac OS X版本其内容和“http://localhost/~[用户名]”完全一致。 
注意,记录log的“ErrorLog “/private/var/log/apache2/sites-error_log””也可以删掉,但记录日志其实是一个好习惯,在出现问题时可以帮助我们判断。如果保留这些log代码,一定log文件路径都是存在的,如果随便修改一个不存在的,会导致Apache无法服务而没有错误提示,这个比较恶心。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值