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 see
11 # http://httpd.apache.org/docs/2.2/mod/core.html#options
12 # for more information.
13 #
14 Options Indexes FollowSymLinks MultiViews
15
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 Limit
20 #
21 AllowOverride None
22
23 #
24 # Controls who can get stuff from this server.
25 #
26 Order allow,deny
27 Allow from all
28
29 </Directory>
It works的内容,就在/Library/WebServer/Documents/index.html.en这个文件里,这是apache的默认页,相当于windows下IIS的C:\inetpub\wwwroot\iisstart.htm
另外,如果要监听多端口,可以修改http.conf,类似下面这样:
Listen 80
Listen 81
这样就同时监听了80,81二个端口
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 MultiViews
3 AllowOverride All
4 Order allow,deny
5 Allow from all
6 </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 Sites
11
12 #
13 # Users might not be in /Users/*/Sites, so use user-specific config files.
14 #
15 Include /private/etc/apache2/users/*.conf
16 <IfModule bonjour_module>
17 RegisterUserSite customized-users
18 </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 MultiViews
10 AllowOverride None
11 Order deny,allow
12 Allow from all
13 </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/ 一样
tips: 如果站点根目录,想放到其它位置,在httpd.conf中找到
1 <Directory />
2 Options FollowSymLinks
3 AllowOverride None
4 Order deny,allow
5 Deny from all
6 </Directory
把4,5行删除掉就行了
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
6、端口转发
假如服务器上有一个应用 http://x.x.x.x:8080/ ,如果想通过类似 http://www.yjmyzz.com 的域名来直接访问,就需要做端口转发,仍然打开httpd.conf
1 LoadModule proxy_connect_module modules/mod_proxy_connect.so2 LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
在"5、URL转发"的基础上,再打开这二项
然后修改extra/httpd-vhosts.conf
1 NameVirtualHost *:80
2
3 <VirtualHost *:80>
4 ProxyPreserveHost On
5 ServerName www.yjmyzz.com
6
7 ProxyPass / http://www.yjmyzz.com:8000/
8 ProxyPassReverse / http://www.yjmyzz.com:8000/
9
10 ServerAdmin webmaster@localhost
11 </VirtualHost>
这样就相当于把 80端口转发到8080端口上了