Mac OS X has built-in Apache and PHP, which are very convenient to use. This article uses Mac OS X 10.6.3 and 10.8.1 as examples.
The main contents include:
- start Apache
- run php
- Install MySQL
start Apache
There are two ways:
Open
系统设置偏好(System Preferences)
->共享(Sharing)
->Web共享(Web Sharing)
. Note, this is deprecated as of Mac OS X 10.8Web共享(Web Sharing)
.Open
终端(terminal)
, then (note that thesudo
required password is the systemroot
account password)Run
sudo apachectl start
, and then enter the account password, soApache
it runs.Run
sudo apachectl -v
, you will see the Apache version information for Mac OS X , such as 10.8.1:
Server version: Apache/2.2.22 (Unix)
Server built: Jun 20 2012 13:57:09
So enter " http://localhost " in the browser , you can see a page with the content "It works!", which is located /Library(资源库)/WebServer/Documents/
under the default root directory of Apache.
run php
Run in terminal
sudo vi /etc/apache2/httpd.conf
, openApache
configuration file. (If you are not used to operating the terminal and vi, you can set to display all system hidden files in the Finder. Remember to restart the Finder after setting, and then you can find the corresponding files and edit them as you like. You need to pay attention to the modification of some files. Open the root account, but it is generally safer to use sudo on the terminal to temporarily gain root privileges.)Find
#LoadModule php5_module libexec/apache2/libphp5.so
, remove the preceding#
number, save (input on the command line:w
) and exit vi (input on the command line:q
).Run
sudo cp /etc/php.ini.default /etc/php.ini
, so you can runsudo vi /etc/php.ini
to edit php.ini to configure various functions.Run
sudo apachectl restart
, restart Apache so PHP is ready to use.To run
sudo cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php
, copy the index.html.en file in the Apache root directory and rename it to info.php.- Run in a terminal
sudo vi /Library/WebServer/Document/info.php
so you can edit the info.php file in vi. Add it after "It's works!"<?php phpinfo(); ?>
and save it. In this way, you can see information about PHP in " http://localhost/info.php ", for example, the built-in PHP version number in 10.8 is 5.3.13.
Install MySQL
Mac OS X does not have built-in MySQL, so you need to install it manually. Currently, the most stable version of MySQL is 5.5. MySQL provides installation instructions under Mac OS X.