Mac OS X中配置Apache

我使用的Mac OS X版本是10.8.2,Mac自带了Apache环境。

启动Apache
设置虚拟主机
 

启动Apache(可直接用root来操作)

打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密)。如下显示Apache的版本


 

接 着输入 sudo apachectl start,这样Apache就启动了。打开Safari浏览器地址栏输入 “http://localhost”,可以看到内容为“It works!”的页面。其位于“/Library(资源库)/WebServer/Documents/”下,这就是Apache的默认根目录。

Apache的安装目录在:/etc/apache2/,etc默认是隐藏的。有三种方式查看:

dock下右键Finder,选择"前往文件夹",输入"/etc"
在finder下----》前往---》前往文件夹,然后输入/etc
可以在terminal 输入 "open /etc"
 

设置虚拟主机

在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件
在httpd.conf中找到“#Include /private/etc/apache2/extra/httpd-vhosts.conf”,去掉前面的“#”,保存并退出。
运行“sudo apachectl restart”,重启Apache后就开启了虚拟主机配置功能。
运行“sudo vi /etc/apache2/extra/httpd-vhosts.conf”,就打开了配置虚拟主机文件httpd-vhost.conf,配置虚拟主机了。需要注意的是该文件默认开启了两个作为例子的虚拟主机:

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common
</VirtualHost>
 
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/usr/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
</VirtualHost>


而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:

Forbidden
You don't have permission to access /index.php on this server
最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。

增加如下配置

<VirtualHost *:80>
    DocumentRoot "/Library/WebServer/Documents"
    ServerName localhost
    ErrorLog "/private/var/log/apache2/localhost-error_log"
    CustomLog "/private/var/log/apache2/localhost-access_log" common
</VirtualHost>
 
<VirtualHost *:80>
    DocumentRoot "/Users/snandy/work"
    ServerName mysites
    ErrorLog "/private/var/log/apache2/sites-error_log"
    CustomLog "/private/var/log/apache2/sites-access_log" common
    <Directory />
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order deny,allow
                Allow from all
      </Directory>
</VirtualHost>

 


保存退出,并重启Apache。

运 行“sudo vi /etc/hosts”,打开hosts配置文件,加入"127.0.0.1 mysites",这样就可以配置完成sites虚拟主机了,可以访问“http://mysites”了,在10.8之前Mac OS X版本其内容和“http://localhost/~[用户名]”完全一致。
注意,记录log的“ErrorLog "/private/var/log/apache2/sites-error_log"”也可以删掉,但记录日志其实是一个好习惯,在出现问题时可以帮 助我们判断。如果保留这些log代码,一定log文件路径都是存在的,如果随便修改一个不存在的,会导致Apache无法服务而没有错误提示,这个比较恶 心。

 

#检查apache 配置文件是否有错误;命令:

zhaohongtaodeMacBook-Pro:extra root# apachectl 

AH00526: Syntax error on line 50 of /private/etc/apache2/extra/httpd-vhosts.conf:

Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration

解决方法

$>cd /etc/apache2/mods-enabled    切换到apache下的mods-enabled目录
$>sudo ln -s ../mods-available/rewrite.load rewrite.load   启用rewrite mod
$>sudo /etc/init.d/apache2 restart    重启apache服务器。
或者

在apache的配置文件httpd.conf中将#LoadModule rewrite_module modules/mod_rewrite.so前的#去掉,再重启服务器。(我用的是这个)

(转自:http://www.win4000.com/course_detail_27842.html、http://www.cnblogs.com/52linux/archive/2012/03/24/2415637.html) 

 

apache操作:

重启apache:sudo /usr/sbin/apachectl restart

关闭apache:sudo /usr/sbin/apachectl stop

开启apache:sudo /usr/sbin/apachectl start

 

问题:

关闭和启动apache的时候提示下面的东西。
[root@mail ~]# service httpd start/restart
Warning: DocumentRoot [/usr/local/apache2/docs/dummy-host.example.com] does not exist
Warning: DocumentRoot [/usr/local/apache2/docs/dummy-host2.example.com] does not exist

原因:

/usr/local/apache2/conf/extra/httpd-vhosts.conf文件了多了一些没有实质的文件。

解决方法:

将/usr/local/apache2/conf/extra/httpd-vhosts.conf的文件注释掉就可以了

(转自:http://www.yebihai.com/webservices/apache/675.html)

 

转自:http://www.cnblogs.com/snandy/archive/2012/11/13/2765381.html

posted @ 2015-04-26 12:32  zhhtao  阅读(221)  评论(0编辑  收藏  举报