首页 > 代码库 > mac 10.9.4下配置apache
mac 10.9.4下配置apache
mac 10.9.x已经自带了apache,可按如下步骤开启:
1、启动
sudo apachectl start
启动后,访问 http://localhost/ 应该能看到"It works!"的初始页面,如果对初始页面的内容感到好奇,可以打开"/etc/apache2/httpd.conf",197行可以看到如下代码片段:
1 <Directory "/Library/WebServer/Documents"> 2 # 3 # Possible values for the Options directive are "None", "All", 4 # or any combination of: 5 # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 6 # 7 # Note that "MultiViews" must be named *explicitly* --- "Options All" 8 # doesn‘t give it to you. 9 #10 # The Options directive is both complicated and important. Please see11 # http://httpd.apache.org/docs/2.2/mod/core.html#options12 # for more information.13 #14 Options Indexes FollowSymLinks MultiViews15 16 #17 # AllowOverride controls what directives may be placed in .htaccess files.18 # It can be "All", "None", or any combination of the keywords:19 # Options FileInfo AuthConfig Limit20 #21 AllowOverride None22 23 #24 # Controls who can get stuff from this server.25 #26 Order allow,deny27 Allow from all28 29 </Directory>
It works的内容,就在/Library/WebServer/Documents/index.html.en这个文件里,这是apache的默认页,相当于windows下IIS的C:\inetpub\wwwroot\iisstart.htm
2、重启/停止
sudo apachectl restart
sudo apachectl stop
3、创建个人站点目录
cd ~/
mkdir Sites
echo "hello" >> index.html
sudo apachectl restart
然后再访问 http://localhost/~jimmy/ 应该就能看到"hello"的个人目录初始页面(注:~jimmy需换成~你的用户名)
如果失败,请检查"/etc/apache2/users"目录下,是否有名为“jimmy.conf”的配置文件(同样:jimmy需换成你的用户名),如果没有,手动创建一个,内容参考下面:
1 <Directory "/Users/jimmy/Sites/">2 Options FollowSymLinks Indexes MultiViews3 AllowOverride All4 Order allow,deny5 Allow from all6 </Directory>
如果好奇目录名为什么是Sites? 可以查看"/etc/apache2/extra/httpd-userdir.conf"
1 # Settings for user home directories 2 # 3 # Required module: mod_userdir 4 5 # 6 # UserDir: The name of the directory that is appended onto a user‘s home 7 # directory if a ~user request is received. Note that you must also set 8 # the default access control for these directories, as in the example below. 9 #10 UserDir Sites11 12 #13 # Users might not be in /Users/*/Sites, so use user-specific config files.14 #15 Include /private/etc/apache2/users/*.conf16 <IfModule bonjour_module>17 RegisterUserSite customized-users18 </IfModule>
第10行就是答案
4、启用虚拟主机
默认情况下,apache的虚拟主机功能是关闭的,在“/etc/apache2/httpd.conf”中找到下面这行:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
将前面的#去掉,然后再打开“/etc/apache2/extra/httpd-vhosts.conf”,内容修改成类似下面的样子:
1 NameVirtualHost *:80 2 3 <VirtualHost *:80> 4 DocumentRoot "/Users/jimmy/Sites" 5 ServerName www.yjmyzz.com 6 ErrorLog "/Users/jimmy/Sites/log/error.log" 7 CustomLog "/Users/jimmy/Sites/log/access.log" common 8 <Directory /> 9 Options Indexes FollowSymLinks MultiViews10 AllowOverride None11 Order deny,allow12 Allow from all13 </Directory>14 </VirtualHost>
注:
a) /Users/jimmy/Sites/log/ 日志文件目录,必须存在,否则apache启动将失败,而且不会有任何错误提示
b) 虚拟主机的站点根目录,建议放在~/Sites/下,否则mac环境中会报类似“无权限访问”的错误。
这段配置绑定了一个不存在的域名www.yjmyzz.com到站点http://localhost/~jimmy/,为了验证域名绑定的效果,手动修改下hosts文件
sudo vi /etc/hosts 以管理员身份打开hosts文件,追加一行
127.0.0.1 www.yjmyzz.com
保存退出,重启apache,再次浏览 http://www.yjmyzz.com 应该ok了,看到的内容跟http://localhost/~jimmy/ 一样
5、URL转发
先打开httpd.conf,确保下面这二行没有被注释掉:
1 LoadModule proxy_module libexec/apache2/mod_proxy.so2 LoadModule proxy_http_module libexec/apache2/mod_proxy_http.so
然后在httpd.conf最后加上
1 ProxyPass /HelloApp http://localhost:8080/HelloApp/2 ProxyPassReverse /HelloApp http://localhost:8080/HelloApp/
这样访问 http://localhost/HelloApp、http://ip/HelloApp、http://www.yjmyzz.com/HellpApp 都相当于访问 http://localhost:8080/HelloApp