我配置的服务器根目录下有两个文件,一个学习用的thinkPHP5,一个是之前自学时敲的一些小demo.我一直想通过域名来访问服务器,也可以用域名来访问服务器指定目录下的文件。文件结构如下,sites是我的根目录,我想通过www.lrg.com来访问sites,通过www.tp5.com 来访问 sites/tp5/public,通过www.phplearn.com来访问sites/PHPLearn/,效果图如下:
完成效果图,下面就是配置过程。
/private/etc/apache2/extra/httpd-vhosts.conf
找到这个文件配置虚拟主机,可以用vim编辑,也可以用文本编辑器打开,拖出来,编辑好,拷贝再粘贴到上层文件夹,输下密码就可以了。
# Virtual Hosts
#
# Required modules: mod_log_config
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin 1051099153@qq.com
ServerName localhost.com
ServerAlias localhost.com
DocumentRoot "/Users/LRG/sites/"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1051099153@qq.com
ServerName www.lrg.com
ServerAlias www.lrg.com
DocumentRoot "/Users/LRG/sites/"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 1051099153@qq.com
ServerName www.phplearn.com
ServerAlias www.phplearn.com
DocumentRoot "/Users/LRG/sites/PHPLearn/"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 105109913@qq.com
ServerName www.tp5.com
ServerAlias www.tp5.com
DocumentRoot "/Users/LRG/sites/tp5/public/"
</VirtualHost>
问题:其实我早就应该装好了,但是呢,我中途出了点问题,可以用这个命令检查你的apachectl配置有没有出错
apachectl configtest
我碰到的问题:1、填的网易邮箱,可能是邮箱里面有下划线,报错。
2、DocumentRoot那一行有两个引号,第一个是英文字母,第二个却是汉子的双引号,用上面的命令一查,就知道错哪了。
配置好这个文件后,需要配置这个文件,将主机ip映射到对应域名:
/private/etc/hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 www.lrg.com
255.255.255.255 broadcasthost
127.0.0.1 localhost
127.0.0.1 www.phplearn.com
127.0.0.1 www.tp5.com
修改后,用configtest检查一下配置,没问题,重启一下apache,就大公告成
sudo apachectl start
sudo apachectl restart
sudo apachectl stop
网友评论