参考链接: https://discussions.apple.com/docs/DOC-3083

PHP is not enabled in recent versions of OS X. To enable it, do:

1
sudo vi /etc/apache2/httpd.conf

Uncomment the following lines:

1
2
3
4
5
6
7
8
9
10
11
#LoadModule php5_module libexec/apache2/libphp5.so
to
LoadModule php5_module libexec/apache2/libphp5.so

#LoadModule userdir_module libexec/apache2/mod_userdir.so
to
LoadModule userdir_module libexec/apache2/mod_userdir.so

#Include /private/etc/apache2/extra/httpd-userdir.conf
to
Include /private/etc/apache2/extra/httpd-userdir.conf

继续修改配置

1
sudo vi /etc/apache2/extra/httpd-userdir.conf

Uncomment the following lines:

1
2
3
#Include /private/etc/apache2/users/*.conf
to
Include /private/etc/apache2/users/*.conf

While you are in /etc/apache2, double-check to make sure you have a user config file. It should exist at the path: /etc/apache2/users/.conf. That file may not be created in Lion and if you upgrade to Mountain Lion, you still won’t have it. It does appear to be created when you create a new user in Mountain Lion. If that file doesn’t exist, you will need to create it with:

1
sudo vi /etc/apache2/users/<your short user name>.conf
1
2
3
4
5
6
7
8
9
10
<Directory "/Users/<your short user name>/Sites/">
AddLanguage en .en
LanguagePriority en fr de
ForceLanguagePriority Fallback
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from localhost
Require all granted
</Directory>

重启apache

1
sudo apachectl restart

My site works

Now try PHP. Create a PHP info file with:

1
echo "<?php echo phpinfo(); ?>" > ~/Sites/info.php

And test it by entering the following into Safari’s address bar:

1
http://<your local host>/~<your short user name>/info.php