开启Mac自带apache和PHP开发环境

来源:互联网 发布:怎么查看mac运行程序 编辑:程序博客网 时间:2024/06/05 15:45

一、开启自带apache

在Terminal(终端)中输入

[plain] view plain copy
  1. sudo apachectl start    

上面代码功能是启动apache,在浏览器中输入http://localhost,你能看到 “ It works”的页面,也就是你启动成功了;

apache默认虚拟主机(可理解为网站根目录)目录:/Library/WebServer/Documents/

二、在apache中开启php

在修改httpd.conf先备份。httpd.conf.bank,在修改httpd.conf先备份。httpd.conf.bank。在修改httpd.conf先备份。httpd.conf.bank

在Terminal(终端)中输入

[plain] view plain copy
  1. sudo vim /etc/apache2/httpd.conf  
上面命令是打开httpd.conf(要用sudo命令,因为修改httpd.conf文件需要较高的权限,一般用户无法修改。vim命令在修改时先要按“i”键,表示insert,才能允许输入;保存时,先按"esc",在按":wq")
找到

[plain] view plain copy
  1. #LoadModule php5_module libexec/apache2/libphp5.so  

去掉注释符号(#)

复制一份/Library/WebServer/Documents/index.html.en,命名为info.php,和index.html.en放在同一目录下。修改info.php内容

[html] view plain copy
  1. <html>  
  2. <body>  
  3.     <h1>It works!</h1>  
  4.     <?php phpinfo(); ?>  
  5. </body>  
  6. </html>  

在浏览器中输入http://localhost或http://localhost/info.php,出现如下画面就表示默认apache和php搭配成功了。


三、设置虚拟主机和虚拟目录

虚拟主机和虚拟目录都可以理解为网站的根目录或子目录。

默认开启的apache站点根目录是/Library/WebServer/Documents/,也就是说你要把代码文件放到这个目录下浏览器才会解析到。

设置自己的网站的根目录(虚拟主机)

在Terminal(终端)中输入

[plain] view plain copy
  1. sudo vim /etc/apache2/httpd.conf  

去掉下面代码最前面的#

[plain] view plain copy
  1. #LoadModule authn_core_module libexec/apache2/mod_authn_core.so  
  2. #LoadModule authz_host_module libexec/apache2/mod_authz_host.so  
  3. #LoadModule authz_core_module libexec/apache2/mod_authz_core.so  
  4. #LoadModule dir_module libexec/apache2/mod_dir.so  
  5. #LoadModule userdir_module libexec/apache2/mod_userdir.so  
  6. #LoadModule alias_module libexec/apache2/mod_alias.so  
找到

[plain] view plain copy
  1. DocumentRoot "/Library/WebServer/Documents"  
  2. <Directory "/Library/WebServer/Documents">  
把/Library/WebServer/Documents修改为自己的网站根目录,我的是/Users/user/Desktop/Project/PHPWorkspace

[plain] view plain copy
  1. DocumentRoot "/Users/user/Desktop/Project/PHPWorkspace"  
  2. <Directory "/Users/user/Desktop/Project/PHPWorkspace">  

找到

[plain] view plain copy
  1. #Include /private/etc/apache2/extra/httpd-userdir.conf  
  2. #Include /private/etc/apache2/extra/httpd-vhosts.conf  
[plain] view plain copy
  1. #Include /private/etc/apache2/other/*.conf  
去掉前面的#

保存退出编辑

在Terminal(终端)中输入

[plain] view plain copy
  1. <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="plain"><pre name="code" class="plain"><pre name="code" class="plain">sudo vim /etc/apache2/extra/httpd-vhosts.conf  

去掉下面一行的#

[plain] view plain copy
  1. #Include /private/etc/apache2/users/*.conf  

保存退出编辑

创建虚拟主机

在Terminal(终端)中输入

[plain] view plain copy
  1. sudo vim /etc/apache2/extra/httpd-vhosts.conf  
用#注释掉原有的两个VirtualHost

[plain] view plain copy
  1. #<VirtualHost *:80>  
  2. #    ServerAdmin webmaster@dummy-host.example.com  
  3. #    DocumentRoot "/usr/docs/dummy-host.example.com"  
  4. #    ServerName dummy-host.example.com  
  5. #    ServerAlias www.dummy-host.example.com  
  6. #    ErrorLog "/private/var/log/apache2/dummy-host.example.com-error_log"  
  7. #    CustomLog "/private/var/log/apache2/dummy-host.example.com-access_log" common  
  8. #</VirtualHost>  
  9.   
  10. #<VirtualHost *:80>  
  11. #    ServerAdmin webmaster@dummy-host2.example.com  
  12. #    DocumentRoot "/usr/docs/dummy-host2.example.com"  
  13. #    ServerName dummy-host2.example.com  
  14. #    ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"  
  15. #    CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common  
  16. #</VirtualHost>  

添加

[plain] view plain copy
  1. <VirtualHost *:80>  
  2.     DocumentRoot "/Users/user/Desktop/Project/PHPWorkspace"  
  3.     ServerName phpworkspace  
  4.     ErrorLog "/private/var/log/apache2/phpworkspace-error_log"  
  5.     CustomLog "/private/var/log/apache2/phpworkspace-access_log" common  
  6. <Directory />  
  7.     Options Indexes FollowSymLinks MultiViews  
  8.     AllowOverride None  
  9.     Order deny,allow  
  10.     Allow from all  
  11. </Directory>  
  12. </VirtualHost>  
保存

四、添加dns解析

在Terminal(终端)中输入

[plain] view plain copy
  1. sudo vi /etc/hosts  
添加
[plain] view plain copy
  1. 127.0.0.1        phpworkspace  
把第二步中的info.php拷贝到自己网站的根目录(我的是/Users/user/Desktop/Project/PHPWorkspace),在浏览器中输入自己的创建的主机名(我的是phpworkspace,你可以输入http://phpworkspace或http://phpworkspace/info.php),出现我上面贴的图表示成功。如果遇到403错误吗,请看下面

五、遇到的问题

打开网站错误码403

Forbidden

You don't have permission to access /HelloMac.htm on this server.

网上有很多的解决方法,我这里讲两种

1、没有设置默认页面或根站点下没有页面文件

(1)、打开httpd.conf
修改

[plain] view plain copy
  1. DirectoryIndex index.html  
为如下

[plain] view plain copy
  1. DirectoryIndex index.html index.php index.jsp index.htm  

这是为站点设置默认页面,这时在网站的根目录(我的是/Users/user/Desktop/Project/PHPWorkspace)下必须要有index.html index.php index.jsp index.htm这四种文件之一,否者还是可能会出现。那是因为你在设置虚拟主机时是这样写的

[plain] view plain copy
  1. <Directory />  
  2.     Options FollowSymLinks MultiViews  
  3.     AllowOverride None  
  4.     Order deny,allow  
  5.     Allow from all  
  6. </Directory>  
要改成

[plain] view plain copy
  1. <Directory />  
  2.     Options Indexes FollowSymLinks MultiViews  
  3.     AllowOverride None  
  4.     Order deny,allow  
  5.     Allow from all  
  6. </Directory>  


2、找到自己设置的网站根目录文件夹(我的是/Users/user/Desktop/Project/PHPWorkspace),右键"显示简介",勾选共享的文件夹。

网上说用

sudo chmod -R 775 /Users/user/Desktop/Project/PHPWorkspace

试了一下没有效果。


阅读全文
0 0
原创粉丝点击