Apache本地服务器搭建(Mac版)

由于Mac自带apache服务器,所以无需下载,apache默认处于开启状态。

可以在浏览器输入localhost,显示It works!,代表目前处于开启状态,默认文件目录为/Library/WebServer/Documents

开启apache:
sudo apachectl -k start

关闭apache:
sudo apachectl -k stop

重启apache:
sudo apachectl -k restart

----------------------------------------开启服务器报错----------------------------------------

开启服务器时,我们可能遇到以下报错:

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

解决办法:

  1. 进入apache的安装目录(根据自己的apache目录进入): 

    cd /etc/apache2/

  2. 编辑httpd.conf文件:

    sudo vim httpd.conf

  3. 查找'#ServerName'字符串(非编辑模式下输入/#ServerName,按回车即可查找),在其下面新增一行'ServerName localhost:80',如下图:

     

   4. 保存文件,重启服务器:

    sudo apachectl -k restart

----------------------------------------开启服务器报错----------------------------------------

-------------------------------------修改服务器默认文件夹--------------------------------------

apache默认的文件路径为'/Library/WebServer/Documents',如果我们想自己定义的话,可按如下配置:

  1. 编辑httpd.conf文件,搜索DocumentRoot "/Library/WebServer/Documents"字符串,把下面两个路径替换成自定义路径:

     

  2. 重启服务器即可

    sudo apachectl -k restart

 -------------------------------------修改服务器默认文件夹--------------------------------------

-------------------------------------访问服务器时Forbidden--------------------------------------

页面显示Forbidden,You don't have permission to access / on this server.

我的自定义路径是/Users/用户名/WebServer:

  1. 访问localhost时,WebServer文件夹下没有index.html文件,所以报Forbidden。

  2. 访问localhost/code时,code下没有找到index.html文件,所以报Forbidden。

  3. 访问localhost/code/test.html,不报错。

总结:直接访问某文件夹时,会自动找此文件夹下的index.html文件,如果没有就会报上面的错误。

-------------------------------------访问服务器时Forbidden--------------------------------------

猜你喜欢

转载自www.cnblogs.com/lqw007/p/12666150.html