Mac Os X yosemite 中配置Apache

启动Apache

Mac自带Apache环境,所需要做的就是启动它然后进行相应的配置。

打开「终端(terminal)」,输入 「sudo apachectl -v」 ,可以看到Mac Os X Yosemite中内置的 Apache 版本。

在「终端」中输入 「sudo apachectl start」 启动 Apache。

打开浏览器在地址栏中输入「http://localhost」,会出现「It works!」页面。

页面默认的位置在 /Library/WebServer/Documents/ 下,这是 Apache 的默认根目录。 Apache 默认安装在 /etc/apache2/ 。

设置虚拟主机

修改 Apache 配置文件, 在 「/etc/apache2/httpd.conf」 ,使用 vim 进行编辑。

找到 「#include /private/etc/apache2/extra/httpd-vhosts.conf」 ,将其注释「#」删除。

重新启动 Apache , 「sudo apachectl restart」,虚拟主机设置开启。

配置虚拟主机,用 vim 打开 「/etc/apache2/extra/httpd-vhosts.conf」 进行配置,配置代码如下。


  DocumentRoot "/Library/WebServer/Documents"
  ServerName localhost
  ErrorLog "/private/var/log/apache2/localhost-error_log"
  CustomLog "/private/var/log/apache2/localhost-access_log" common



  DocumentRoot "/Users/yourLoginName/Sites"
  ServerName sites
  ErrorLog "/private/var/log/apache2/sites-error_log"
  CustomLog "/private/var/log/apache2/sites-access_log" common
  
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order deny,allow
    Require all granted
  

配置完成后保存文件,在hosts配置文件「/etc/hosts」中增加「127.0.0.1 sites」,重新启动Apache,中浏览器中输入「http://sites」即可正常访问。