mac搭建apache服务器

注意,mac自带httpd,可以卸载,也可以直接用。

新版mac的原生httpd在: /usr/local/bin/httpd ,conf文件在 /usr/local/etc/httpd 。  如果忘记了可以自己到user目录find。

配置conf文件时需要配置两个路径, 

DocumentRoot "/usr/local/var/www"     //网页路径                                                                                                                              

<Directory "~/Sites">   //文件夹路径

参考:

https://www.jianshu.com/p/a617691a7f4f

https://docle.github.io/2017/08/03/Configuring-personal-Sites-using-Apache-in-Mac/

查看错误

tail -f /var/log/apache2/error_log

 

1.本地开启服务器:

sudo su

查看当前版本

 httpd -v

配置文件与网站根目录默认所在位置

/etc/apache2/httpd.conf //配置文件
/Library/WebServer/Documents //网站根目录

服务基本操作

sudo apachectl start // 开启Apache
sudo apachectl stop // 关闭Apache
sudo apachectl restart // 重启Apache

开启目录浏览

 

修改/etc/apache2/httpd.conf,把Options FollowSymLinks Multiviews改成Options Indexes FollowSymLinks Multiviews

<Directory />
     AllowOverride none
     Require all granted
     Allow from all
</Directory>
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
    Options Indexes FollowSymLinks Multiviews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

其它操作:去除配置文件的空行与以#开头的行

cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.back //备份
grep -Ev "^$|[#;]" /etc/apache2/httpd.conf > /etc/apache2/httpd.conf  /去除空行与#号行

总结:
apache出现403 Forbidden主要有两个原因:
1是DocumentRoot目录或以下的文件没有权限,如:把文档目录设成/root/abcd,因为root目前默认没有X权限导致403,又或者具体文件没有644 权限

#查看目录权限
ll /root
drwxrwx---.  11 root root 4096 Feb 24 02:18 root
#通过以下命令修改
chmod 641 /root 
#查看文件权限
-rw-r-----  1 root root  52532 Feb 24 02:37 index.html
#通过以下命令修改
chmod 644 index.html
-rw-r--r--  1 root root  52532 Feb 24 02:37 index.html

目录权限修改说明: R=4 ,W=2 ,X=1 (R表示读,W表示写,X表示执行)
rwx 相加就是 7
2是apache配置文件问题,需要修改http.conf文件。

2.

 

3.开启目录访问

Mac下利用apache配置个人站点

 发表于 2017-08-03 |  分类于 Note

最近在折腾学习HTML5游戏制作,为了测试游戏就需要在本地运行,于是把文件放在 /Libraray/WebServer/Ducuments/ 下,对应的网址:http://localhost/ ,但是慢慢的有些问题就出现了,比如每次到达目录麻烦,懒癌晚期表示不能忍啊啊啊,还有就是读写权限的问题。然后网上搜索得知Mac OS X 是有两个目录可以直接运行web程序的。

  1. 一个是系统级的根目录:/Libraray/WebServer/Ducuments

    对应网址: http://localhost/

  2. 另一个是用户级的根目录:~/Sites

    对应网址:http://localhost/~user/

    (*注意,user是你的用户名,下同)

创建Sites目录

在用户主页目录下是没有这个Sites目录的,需要我们自己创建,打开terminal,在根目录~下运行以下命令

1
mkdir Sites

打开Finder会发现Sites跟一般的文件夹不一样,上面有一个Safari一样的图标呢!至于为什么目录名是Sites??查看/etc/apache2/extra/httpd-userdir.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Settings for user home directories
#
# Required module: mod_authz_core, mod_authz_host, mod_userdir

#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.  Note that you must also set
# the default access control for these directories, as in the example below.
#
UserDir Sites

#
# Control access to UserDir directories.  The following is an example
# for a site where these directories are restricted to read-only.
#
Include /private/etc/apache2/users/*.conf
<IfModule bonjour_module>
       RegisterUserSite customized-users
</IfModule>

第十行就是答案。

检查用户配置文件

建立Sites文件夹之后,检查一下 /etc/apache2/users/ 下面是否有user.conf这个文件,应该会有的,如果没有就自己建一个,我的这个文件内容如下

1
2
3
4
<Directory "/Users/abel/Sites/">
	Options Indexes MultiViews
	Require all granted
</Directory>

需要把abel替换为你的用户名user。

修改apache配置文件

接着修改/etc/apache2/httpd.conf,找到以下代码行并去掉句首的#使代码行生效

1
2
3
4
# LoadModule authz_core_module libexec/apache2/mod_authz_core.so
# LoadModule authz_host_module libexec/apache2/mod_authz_host.so
# LoadModule userdir_module libexec/apache2/mod_userdir.so
# Include /private/etc/apache2/extra/httpd-userdir.conf

其次,修改/etc/apache2/extra/httpd-userdir.conf,找到以下代码行并去掉句首的#使代码行生效

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

重启apache

在Sites里创建一个测试文件index.html,内容随意,重启apache

1
sudo apachectl restart

Sites下需要有日记文件目录log,否则apache重启可能会失败且不会有错误提示。重启apache后访问 http://loaclhost/~user/ 就可以看到刚才的index.html 页面了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值