Mac OS X Yosemite 10.10 配置 Apache+PHP
查看当前系统的apache版本,终端下输入:httpd -v
1. 启动Apache 服务
sudo apachectl start
打开safari,访问:http://localhost,显示“It works!”表示Apache启动正常
当前访问的是Apache的默认目录,/Library/WebServer/Documents/
2. 配置用户访问目录
因为之前已经配置过用户目录,这里提一下,首先创建用户目录:mkdir ~/Sites 此时会在当前用户的根目录下创建一个Sites目录
可以在Sites目录下创建一个测试的html,或者cp /Library/WebServer/Documents/index.html.en ~/Sites/
修改当前用户目录的访问权限,这个与上一个版本一致没有变化,这里提一下修改方式:
cd /etc/apache2/users
sudo vim username.conf
注意:这里的username是当前用户名,请根据实际情况修改
输入以下内容:
<Directory "/Users/username/Sites/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
保存文件,重启apache ,sudo apachectl restart
修改当前文件的访问权限:sudo chmod 775 username.conf
此时打开safari,访问http://localhost/~username/ , 此时页面提示~username服务器不存在,我们还需要进入下一步的设置
3. 修改apache的httpd.conf文件
cd /etc/apache2/
sudo vim httpd.conf
找到以下信息,将其前面的#去掉:
LoadModule php5_module libexec/apache2/libphp5.so
LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
Include /private/etc/apache2/extra/httpd-userdir.conf
修改/etc/apache2/extra/httpd-userdir.conf
sudo vim /etc/apache2/extra/httpd-userdir.conf
找到一下信息修,将其前面的#去掉:
Include /private/etc/apache2/users/*.conf
此时访问http://localhost/~username/, 依然无法访问,提示:You don‘t have permission to access /~username/ on this server.
这里是就是新版本与老版本的区别了,需要重新打开httpd.conf文件,
sudo vim /etc/apache2/httpd.conf
找到
<Directory />
AllowOverride none
Require all denied
</Directory>
修改为:
<Directory />
AllowOverride none
Require all granted
</Directory>
如果需要.htaccess支持伪静态rewirte,需要在 httpd.conf 开启代码行:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
注:在 MAC OS 系统下 .htaccess 文件是不能直接新建的,而且是隐藏状态!
例如给wordpress添加伪静态,如下命令:
cd ~/Sites/Wordpress
touch .htaccess
vi .htaccess
添加如下内容:
RewriteEngine On
RewriteBase /~haibor/Wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~haibor/Wordpress/index.php [L]
记得给予权限及重启:
chmod 777 .htaccess
sudo apachectl restart
重启apache,sudo apachectl restart
此时访问:http://localhost/~username/,显示:“It works!”,恭喜你,Apache配置成功!
4.配置虚拟主机支持
编辑httpd.conf,找到
#Include conf/extra/httpd-vhosts.conf 取消这一行的注释
编辑/etc/apache2/extra/httpd-vhosts.conf
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/Users/hinet/Sites/shanxiang/trunk"
ServerName localhost
#ServerAlias www.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
<Directory "/Users/hinet/Sites/shanxiang/trunk"> #rewrite需指明路径
Options Indexes FollowSymLinks MultiViews
AllowOverride All #AllowOverride None改为AllowOverride All以支持.htaccess
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
#ServerAdmin phpmyadmin@dummy-host2.example.com
DocumentRoot "/Users/hinet/Sites/phpMyAdmin"
ServerName phpmyadmin.com
#ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log"
#CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
编辑/etc/hosts文件,添加
127.0.0.1 phpmyadmin.com
5. 配置PHP
cd /etc
sudo cp /etc/php.ini.default /etc/php.ini
sudo apachectl restart
创建info.php文件测试php是否安装成功
cd ~/Sites
vim info.php
输入以下信息:
<html>
<body>
<h1>It works!</h1>
<?php phpinfo(); ?>
</body>
</html>
打开浏览器,访问http://localhost/~username/info.php,显示一下信息表示php安装配置成功~