如何使用Mac本地服务器

我们经常性的需要使用局域网搭建 Web 服务器测试环境,如部署局域网无线安装企业应用,模拟某个接口的返回JSON的时候,Mac OS X 自带了 Apache 和 PHP 环境,我们只需要简单的启动它就行了。

启动 Apache

查看Apache版本

打开终端,输入

1
2
3
$ httpd -v
Server version: Apache/2.4.25 (Unix)
Server built: Feb 6 2017 19:34:08

启动Apache

在终端输入 sudo apachectl start 即可启动 Apache。
启动后,在浏览器中输入 http://127.0.0.1http://localhost 如果看到 It Works! 页面。那么 Apache 就启动成功了,站点的根目录为系统级根目录 /Library/WebServer/Documents。

启动后,你可以通过编辑 /etc/apache2/httpd.conf 文件来修改 Apache 配置

停止 Apache:sudo apachectl stop

重启 Apache:sudo apachectl restart

创建用户级根目录

我们也可以创建用户级根目录,更方便管理和操作。

  1. 在用户目录下创建 Sites 目录,cd; mkdir Sites; touch Sites/.localized,旧的 Mac 系统中如果该目录已存在,则略过。
  2. cd /etc/apache2/users 检查目录下是否存在 username.conf 文件,username 为当前用户名,如果没有则创建一个 sudo touch username.conf,并修改文件权限 sudo chmod 644 username.conf。

  3. 创建之后,打开 username.conf 文件,sudo vi username.conf 将下面的配置信息写入文件,username 依然为当前用户名:

1
2
3
4
5
6
7
<Directory "/Users/username/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
  1. 编辑 /etc/apache2/httpd.conf 文件,找到下列代码,并将前面的注释符号 # 删除:
1
2
Include /private/etc/apache2/extra/httpd-userdir.conf
LoadModule userdir_module libexec/apache2/mod_userdir.so
  1. 编辑 /etc/apache2/extra/httpd-userdir.conf 文件,找到下列代码,并将前面的注释符号 # 删除:

    Include /private/etc/apache2/users/*.conf

  2. 重启 Apache:sudo apachectl restart

在浏览器中输入 http://127.0.0.1/~usernamehttp://localhost/~username,即可测试用户目录是否工作。

文章目录
  1. 1. 启动 Apache
    1. 1.0.0.1. 查看Apache版本
    2. 1.0.0.2. 启动Apache
    3. 1.0.0.3. 停止 Apache:sudo apachectl stop
    4. 1.0.0.4. 重启 Apache:sudo apachectl restart
  • 1.1. 创建用户级根目录
  • 本站总访问量 本站访客数人次 ,