首页 技术 正文
技术 2022年11月15日
0 收藏 849 点赞 2,722 浏览 1576 个字

1. 修改 apache 服务器指向的根目录

macOS 默置了 apache,有以下几个常用命令:

sudo apachectl -v // 查看 apache 版本httpd -v // 同上sudo apachectl startsudo apachectl stopsudo apachectl restartsudo apachectl -t // 查看当前 apache 运行情况

当我们启动 apache 后,在浏览器地址栏输入 localhost,看到 "It works!" 字样就对了。这个 html 文件位于 /Library/Webserver/Documents/ 下,该目录也就相当于 wampserver 下的 www 目录。如果不修改默认指向的目录,以后所有的文件都必须放在该目录下,不大方便,我们可以修改 /etc/apache2/httpd.conf 文件,搜索 DocumentRoot,进行修改(sudo vi http.conf)。(因为接下来的每个虚拟主机配置都会设定 DocumentRoot,所以修改这里的 DocumentRoot 看起来并不是必须的)

DocumentRoot "/Users/fish/www"<Directory "Users/fish/www">

比如我把根目录指向了 /Users/fish/www 目录下,这时启动 apache,输入 localhost,打开的就是该目录下的 index 文件了。

2. 配置文件中开启 virtual hosts

上一步骤中的 httpd.conf 文件中,打开 vhosts 开关(去掉行首注释 #)

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

3. 修改 httpd-vhosts.conf 文件

该文件位于 /etc/apache2/extra/ 文件夹中。现在比如我在 /Users/fish/www/ 目录中新建了个名为 test 的文件夹,想用 www.test.com 去访问,可以在该文件中添加如下:

<VirtualHost *:80>    DocumentRoot "/Users/fish/www/test"    ServerName www.test.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>

这里注意修改的话都要用管理员权限 (sudo vi xxx)

4. 修改 hosts 文件

sudo vi /etc/hosts

添加一行 127.0.0.1 www.test.com

5. localhost 不能访问了?

这个时候,我们启动或者重启下 apache,便能访问 www.test.com,但是访问 localhost 却出错了,这时我们需要再次修改 httpd-vhosts.conf 文件,添加如下:

<VirtualHost *:80>    DocumentRoot "/Users/fish/www/"    ServerName localhost    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common</VirtualHost>

6. 其他

如果出现这样错误 AH00557: httpd: apr_sockaddr_info_get() failed for fishdeMacBook-Pro.local,在 hosts 文件中添加 127.0.0.1 fishdeMacBook-Pro.local 即可。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,910
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,435
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,250
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,061
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,693
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,731