Mac下Apache2本地域名配置

Mac是自带Apache的,怎么启用的教程网络上还是有很多的。一般配置完后可以用127.0.0.1或者localhost进行访问。

一般Apache的安装路径是在/etc/apache2/下,内部主要牵涉到配置域名的相关文件一个是主文件下的httpd.conf,另一个是大部分人采用的子文件extra下

的httpd-vhosts.conf.

一般启用Apache的时候都会推荐在/Users/username/下建立一个Sites文件夹(username是你的MAC用户名),具体原因可以参见httpd.conf文件

可以使用如下命令(一步步来)


sudo su
vim /etc/apache2/httpd.conf
/DocumentRoot
即可以找到下面的部分
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#
 
#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/Users/username/Sites"
<Directory "/Users/username/Sites">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options

上面的DocumentRoot后面跟着的就是你的服务器的根目录,下面的Directory标签应该是针对该文件进行权限设置。

所以要配置域名最好将指定域名的文件夹置于/Users/username/Sites文件夹内,以防权限不足,服务器禁止访问。

正式开始部分

然后在httpd.conf中找到

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

这有点类似于导入头文件,只不过前面的#代表注释,所以我们可以将注释打开,导入httpd-vhost.conf文件,具体操作如下

将光标调到#位置,然后按x(小写,英文输入法),然后输入  :wq,然后enter退出vim

然后打开httpd-vhost.conf:

vim /etc/apache2/extra/httpd-vhosts.conf

打开后会发现前面都是注释的,只有下面两个标签没有注释,那只是人家的demo,所以按i键进入插入模式,然后在每一行前面加#号

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot "/usr/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "/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 [email protected]
    DocumentRoot "/usr/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>

然后在最底下加入下面的代码:(具体域名和路径请自己修改)


NameVirtualHost *:80   #这句必须要加,可能是指定访问端口号的吧,我也没弄明白
 
 
#下面三个标签就是配置的域名  第一个是因为配完后发现首页访问不了了,就自己再配了个首页
<VirtualHost *:80>
        ServerAdmin [email protected]	#服务器账号,一般填邮箱账号,我是乱填的
        ServerName localhost.com	#服务器名字,可以自己命名
        ServerAlias localhost.com	#指定域名,自己慎重
        DocumentRoot "/Users/username/Sites/"	#服务器的文件位置,也就是该域名指向的位置(必须要有访问权限
#也就是必须要在httpd.conf文件中声明的文件内,否则会出现You don't have permission to access / on this server.)
</VirtualHost>
 
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName www.b.com
        ServerAlias www.b.com
        DocumentRoot "/Users/username/Sites/b"
</VirtualHost>
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName www.a.com
        ServerAlias www.a.com
        DocumentRoot "/Users/username/Sites/a"
</VirtualHost>
这就相当于配置好了,然后按ESC退出插入模式,输入 :wq退出当前文件

接下来就是该hosts文件了,打开/etc/hosts:

vim /etc/hosts

然后按i键进入插入模式,光标移动到最下方,在里面加入你刚刚添加的域名解析就好了,

具体格式如下


127.0.0.1       www.b.com
 
127.0.0.1       www.a.com
前面是Apache的指定主域名,后面是你想要添加的域名。然后按ESC退出插入模式,再输入 :wq 退出vim。

之后再重启Apache服务器就OK了!

退出Apache服务器,超级管理模式下   apachectl restart

非超级管理模式   sudo apachectl restart

猜你喜欢

转载自blog.csdn.net/fmyzc/article/details/81630742