Mac OS X上安装配置Apache服务器

说明:Mac在安装完成Mac系统的时候它已经自带了Apache服务器,接下来就是配置和将它启动运行了。那么接下来要做的事情就是:

    1.配置apache的配置文件
    2.设置虚拟主机
启动并查看apache

打开终端输入以下命令:

    $sudo apachectl start
    $sudo apachectl -v
配置apache主配置文件

apache的主配置文件在路径/etc/apache2/下

    先将原来的文件备份
    $sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.backup
    修改主配置文件
    $vi /etc/apache2/httpd.conf
要修改的地方如下,为了便于参考默认配置,笔者将只是将修改的地方注释掉

    ...
    <Directory />
        #AllowOverride none
        # Require all denied
        Require all granted
        AllowOverride all
    </Directory>
    ...

    # Virtual hosts
    #Include /private/etc/apache2/extra/httpd-vhosts.conf
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    ...

Ps:某些老版本的配置中时allow什么的在网上能找到非常多的教程,在这就不加解释了。
设置虚拟主机

apache的默认的根目录在/Library/WebServer/下,配置虚拟主机后可以不用理会默认的网站根目录,根据自己的需要在合适的地方建立不同的网站目录。

修改httpd-vhosts.conf文件,文件位置在/etc/apache2/extra/下.

    备份原有的文件
    $sudo cp /etc/apache2/extra/httpd-vhosts.conf /etc/apache2/extra/httpd-vhosts.conf.backup

    $sudo vi /etc/apache2/extra/httpd-vhosts.conf
    设置虚拟主机(此站点本机访问域名是mysite.local,根目录是/var/wwwroot/abc)
    ...
    <VirtualHost *:80>
        ServerAdmin [email protected]
        DocumentRoot "/var/wwwroot/abc"
        ServerName mysite.local
        ErrorLog "/private/var/log/apache2/mysite.local-error_log"
        CustomLog "/private/var/log/apache2/mysite.local-access_log" common
    </VirtualHost>
    ...
修改hosts文件,文件位置在/etc/

    $sudo vi /etc/hosts
    将自定义的域名绑定到127.0.0.1
    ...
    127.0.0.1      localhost mysite.local
    ...
重新启动Apache服务器

    sudo apachectl restart

猜你喜欢

转载自www.linuxidc.com/Linux/2016-03/129524.htm