mac 10.10.x已经自带了apache,可按如下步骤开启
1、启动
sudo apachectl start
启动后,访问 http://localhost/ 应该能看到"It works!"的初始页面,如果对初始页面的内容感到好奇,可以打开"/etc/apache2/httpd.conf",197行可以看到如下代码片段:
<Directory "/Library/WebServer/Documents">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks MultiViews
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
另外,如果要监听多端口,可以修改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需换成你的用户名),如果没有,手动创建一个,内容参考下面:
<Directory "/Users/jimmy/Sites/">
Options FollowSymLinks Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
如果好奇目录名为什么是Sites? 可以查看"/etc/apache2/extra/httpd-userdir.conf"
# Settings for user home directories
#
# Required module: 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
#
# Users might not be in /Users/*/Sites, so use user-specific config files.
#
Include /private/etc/apache2/users/*.conf
<IfModule bonjour_module>
RegisterUserSite customized-users
</IfModule>
第10行就是答案
4、启用虚拟主机
默认情况下,apache的虚拟主机功能是关闭的,在“/etc/apache2/httpd.conf”中找到下面这行:
#Include /private/etc/apache2/extra/httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/Users/jimmy/Sites"
ServerName www.yjmyzz.com
ErrorLog "/Users/jimmy/Sites/log/error.log"
CustomLog "/Users/jimmy/Sites/log/access.log" common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
</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中找到
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory
把4,5行删除掉就行了
5、URL转发
先打开httpd.conf,确保下面这二行没有被注释掉:
1 LoadModule proxy_module libexec/apache2/mod_proxy.so
2 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.so
2 LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
在" 5、URL转发 "的基础上,再打开这二项
NameVirtualHost *:80
<VirtualHost *:80>
ProxyPreserveHost On
ServerName www.yjmyzz.com
ProxyPass / http://www.yjmyzz.com:8000/
ProxyPassReverse / http://www.yjmyzz.com:8000/
ServerAdmin webmaster@localhost
</VirtualHost>
这样就相当于把 80端口转发到8080端口上了
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。