Mac下配置Apache

一. 打开终端,开启Apache:

1.mac已经自带Apache:我们只需要终端输入以下命令就可以完成:开启、重启、关闭操作

//开启apache:  sudo apachectl start

//重启apache:  sudo apachectl restart

//关闭apache:  sudo apachectl stop

2.验证Apache是否开启成功:在浏览器输入 127.0.0.1  或者localhost,显示以下输出就算成功了


3.Apache服务器的地址:

/Library/WebServer/Documents  我们可以打开文件夹按下Command+Shift+G输入文件路径快速定位到服务器文件夹,"It works!"就在index.html.en里

二、自定义Apache地址配置

1.在/etc/apache2/users 下查看是否有{用户名称}.conf的文件,如果没有新建一个,不要忘记chmod修改依稀权限,不然编辑不了内容,输入一下内容保存

<Directory "/Users/电脑的用户名称/手动建一个文件夹名称随意/">
    Options Indexes MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>


2.在手动建一个文件夹里面创建一个文件  index.php 文件 内容为

<?php   
    echo "sss";
    echo phpinfo();
 ?>

3.在/etc/apache2下的httpd.conf文件里找到下面两句把前面的#号去掉

LoadModule php5_module libexec/apache2/libphp5.so

LoadModule userdir_module libexec/apache2/mod_userdir.so


4.输入http://localhost/~luoyaosheng/  ,luoyaosheng是我的用户名,修改成你的用户名称。跳出以下页面就成功了



5.如果你想输入http://localhost就可以访问自定义服务器地址,需要在/etc/apache2下的httpd.conf文件把默认地址修改成自己设置的地址

#DocumentRoot "/Library/WebServer/Documents"
DocumentRoot "/Users/luoyaosheng/Sites"
#<Directory "/Library/WebServer/Documents">
<Directory "/Users/luoyaosheng/Sites">
建议不要直接删除,#号注释掉就可以了

但是请注意:这时候你如果重启了Apache服务通常情况下会遇上403权限错误,因为我们需要修改另外一些的配置项。

修改的主要有两方面的内容,也可以参考我的配置

<Directory />
   Options Indexes FollowSymLinks
   AllowOverride None
   Order deny,allow
   Allow from all
</Directory>
#DocumentRoot "/Library/WebServer/Documents"
DocumentRoot "/Users/luoyaosheng/Sites"
#<Directory "/Library/WebServer/Documents">
<Directory "/Users/luoyaosheng/Sites">
    Options FollowSymLinks Multiviews
    Options Indexes FollowSymLinks
    MultiviewsMatch Any
    Require all granted
</Directory>

6.输入sudo apachectl restart重启下apache 

打开网页输入http://localhost/index.php  收工

猜你喜欢

转载自blog.csdn.net/hyq_java/article/details/80118123