Respect the author: Reprinted from: http://dancewithnet.com/2010/05/09/run-apache-php-mysql-in-mac-os-x/
Mac OS X has built-in Apache and PHP, which is very convenient to use. This article takes Mac OS X 10.6.3 and 10.8.1 as examples. The main contents include:
There are two methods:
sudo apachectl start
" and enter your account and password, so Apache will run. sudo apachectl -v
", you will see the Apache version information of Mac OS X, such as in 10.8.1:
<code>Server version: Apache/2.2.22 (Unix) Server built: Jun 20 2012 13:57:09 </code>
In this way, enter "http://localhost" in the browser, you can see a page with the content "It works!", which is located under "/Library (resource library)/WebServer/Documents/", this This is the default root directory of Apache.
Note: Turning on Apache means turning on "Web Sharing". At this time, Internet users will access the "/Library (resource library)/WebServer/Documents/" directory through "http://[local IP]/" , access the "/Users/[user name]/Sites/" directory through "http://[local IP]/~[user name]". It is worth noting that when Mac OS X canceled "Web Sharing" in 10.8, it also removed the "/Users/[username]/Sites/" directory, so accessing "http: //[Local IP]/~[Username]" will display "403 Forbidden", but http://[Local IP]/ can still be accessed. You can go to "System Preferences" -> "Security" -> "Firewall", turn on the firewall, and then check "Block all incoming connections (block)" in "Firewall Options" all incoming connections)". You can also set httpd.conf to only allow localhost and 127.0.0.1 to access "/Library (resource library)/WebServer/Documents/".
<code><directory> <span>...... # # Controls who can get stuff from this server. #</span> Order allow,deny <span>#Allow from all</span> Allow from 127.0.0.1 Allow from localhost </directory> </code>
sudo vi /etc/apache2/httpd.conf
" in the terminal to open the Apache configuration file. (If you are not used to operating the terminal and vi, you can set it to display all system hidden files in Finder. Remember to restart Finder after setting up, and then you can find the corresponding files and edit them as you like. Please note that some files still need to be modified. Open the root account, but in general it is safer to use sudo
on the terminal to temporarily obtain root permissions) #LoadModule php5_module libexec/apache2/libphp5.so
", remove the # sign in front, save (enter :w
on the command line) and exit vi (enter :q
on the command line). sudo cp /etc/php.ini.default /etc/php.ini
", so you can run <code>sudo vi /etc/php.ini</code> to edit php.ini to configure various functions. for example:
<code><span>;通过下面两项来调整PHP提交文件的最大值,如phpMyAdmin中导入数据的最大值</span> upload_max_filesize = 2M post_max_size = 8M <span>;通过display_errors来控制是否显示PHP程序的报错信息,这在调试PHP程序时非常有用</span> display_errors = Off </code>
sudo apachectl restart
" and restart Apache so that PHP can be used. sudo cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php
", that is, copy the index.html.en file in the root directory of Apache and rename it to info.php. sudo vi /Library/WebServer/Document/info.php
" in the terminal so that you can edit the info.php file in vi. Add “<?php phpinfo(); ?>
” after “It’s works!” 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. 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 for Mac OS X.
/usr/local
下的同名文件夹下。如运行“mysql-5.5.27-osx10.6-x86_64.dmg
”会把MySQL安装到“/usr/local/mysql-5.5.27-osx10.6-x86_64
”中,一路默认安装完毕。(注意,从10.8开始Mac OS X的权限更加严格,直接点击会提示“mysql-5.5.27-osx10.6-x86_64.pkg can’t be opened because it is from an unidentified developer. Your security preferences allow installation of only apps from the Mac App Store and identified developers.”阻止了安装,你可以使用双指单击该安装文件,在弹出菜单中选择“用…打开(open with)”,再选择“安装(Installer)”就可以接着安装了。)sudo vi /etc/bashrc
”,在bash配置文件中加入mysqlstart
、mysql
和mysqladmin
的别名(注意:修改完毕之后需要退出“终端(Terminal)”之后重新进入,这些命令才会生效):
<code>#mysql alias mysqlstart='sudo /Library/StartupItems/MySQLCOM/MySQLCOM restart' alias mysql='/usr/local/mysql/bin/mysql' alias mysqladmin='/usr/local/mysql/bin/mysqladmin' </code>
这样就可以在终端中比较简单地通过命令进行相应的操作。由于开始安装MySQLStartupItem.pkg到“/Library/StartupItems/MySQLCOM/
”来控制MySQL的运行、自动运行、停止、关闭之类。在MySQL没有启动时,直接运行mysql
或mysqladmin
命令会提示“Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
”,所以我们可以通过控制面板或者直接运行mysqlstart
命令来启动MySQL,之后再运行mysql
或mysqladmin
命令就正常了。比如安装完毕后MySQL的root
默认密码为空,如果要设置密码可以在终端运行“mysqladmin -u root password "mysqlpassword"
”来设置,其中mysqlpassword即root的密码。更多相关内容可以参考B.5.4.1. How to Reset the Root Password。
注意:Mac OS X的升级或其他原因可能会导致MySQL启动或开机自动运行时,在MySQL操作面板上会提示“Warning:The /usr/local/mysql/data directory is not owned by the 'mysql' or '_mysql'
”,这应该是某种情况下导致/usr/local/mysql/data
的宿主发生了改变,只需要运行“sudo chown -R mysql /usr/local/mysql/data
”即可。
另外,使用PHP连接MySQL可能会报错“Can’t connect to local MySQL server through socket ‘/var/mysql/mysql.sock’”,或使用localhost无法连接MySQL而需要127.0.0.1,原因是连接时php默认去找/var/mysql/mysql.sock
了,但MAC版的MYSQL改动了文件位置,放在/tmp下了。处理办法是按如下修改php.ini:
<code>mysql.default_socket = /tmp/mysql.sock</code>
phpMyAdmin是用PHP开发的管理MySQL的程序,非常的流行和实用。能够使用phpMyAdmin管理MySQL是检验前面几步效果的非常有效方式。
/Library/WebServer/Documents/
”中,并改名为phpmyadmin。
<code>sudo tar -xf ~/Downloads/phpMyAdmin-3.5.2.2-all-languages.tar.bz2 -C /Library/WebServer/Documents/ sudo mv /Library/WebServer/Documents/phpMyAdmin-3.5.2.2-all-languages /Library/WebServer/Documents/phpmyadmin </code>
/Library/WebServer/Documents/phpmyadmin/
”中的config.sample.inc.php,并命名为config.inc.php<code><span>用于Cookie加密,随意的长字符串</span> $cfg['blowfish_secret'] = 'a8b7c6d'; <span>当phpMyAdmin中出现“#2002 无法登录 MySQL 服务器(#2002 Cannot log in to the MySQL server)”时, 请把localhost改成127.0.0.1就ok了, 这是因为MySQL守护程序做了IP绑定(bind-address =127.0.0.1)造成的</span> $cfg['Servers'][$i]['host'] = 'localhost'; <span>把false改成true,这样就可以访问无密码的MySQL了, 即使MySQL设置了密码也可以这样设置,然后登录phpMyAdmin时输入密码</span> $cfg['Servers'][$i]['AllowNoPassword'] = false; </code>
http://localhost/phpmyadmin
访问phpMyAdmin了。此时会看到一个提示“无法加载 mcrypt 扩展,请检查您的 PHP 配置。(The mcrypt extension is missing. Please check your PHP configuration.)”,这会涉及到下一节安装MCrypt扩展了。MCrypt是一个功能强大的加密算法扩展库,它包括有22种算法,phpMyAdmin依赖这个PHP扩展库。但在Mac OS X下的安装却不那么友善,具体如下:
./configure --disable-posix-threads --enable-static
会报错,运行make
会提示命令不存在,此时还需要打开Xcode,然后在Xcode的软件“配置(Preference…)”)-> “下载(Downloads)” 中安装 “命令行工具(Command Line Tools)”:
<code>cd ~/Downloads tar -zxvf libmcrypt-2.5.8.tar.bz2 cd libmcrypt-2.5.8 ./configure --disable-posix-threads --enable-static make sudo make install</code>
phpize
命令:
<code>cd ~/Downloads tar -zxvf php-5.3.13.tar.bz2 cd php-5.3.13/ext/mcrypt curl -O http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz tar -zxvf autoconf-latest.tar.gz cd autoconf-2.69 ./configure make sudo make install cd .. phpize ./configure make sudo make install </code>
<code>sudo vi /etc/php.ini</code>
在php.ini中加入如下代码,并保存后退出,然后重启Apache
<code>extension=mcrypt.so</code>
当你再访问http://localhost/phpmyadmin
时,会发现“无法加载 mcrypt 扩展,请检查您的 PHP 配置。”提示没有了,这就表示MCrypt扩展库安装成功了。如果还不能加载,尝试把php.ini中的加入的extension
修改为:
<code>extension=/usr/lib/php/extensions/no-debug-non-zts-20090626/mcrypt.so</code>
Mac OS X下安装MCrypt扩展的确比较复杂,而且稍微不小心会有各种小问题出现,大家还可以参考How to Install mcrypt for php on Mac OSX Lion 10.8 & 10.7 Development Server和Adding MCRYPT to your OSX Lion PHP install
sudo vi /etc/apache2/httpd.conf
”,打开Apche的配置文件#Include /private/etc/apache2/extra/httpd-vhosts.conf
”,去掉前面的“#
”,保存并退出。sudo apachectl restart
”,重启Apache后就开启了虚拟主机配置功能。sudo vi /etc/apache2/extra/httpd-vhosts.conf
”,就打开了配置虚拟主机文件httpd-vhost.conf,配置虚拟主机了。需要注意的是该文件默认开启了两个作为例子的虚拟主机:
<code><virtualhost> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "/usr/docs/dummy-host.example.com" ServerName 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 </virtualhost> <virtualhost> ServerAdmin webmaster@dummy-host2.example.com DocumentRoot "/usr/docs/dummy-host2.example.com" ServerName dummy-host2.example.com ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common </virtualhost> </code>
而实际上,这两个虚拟主机是不存在的,在没有配置任何其他虚拟主机时,可能会导致访问localhost时出现如下提示:
<code>Forbidden You don't have permission to access /index.php on this server</code>
最简单的办法就是在它们每行前面加上#,注释掉就好了,这样既能参考又不导致其他问题。
<code><virtualhost> DocumentRoot "/Library/WebServer/Documents" ServerName localhost ErrorLog "/private/var/log/apache2/localhost-error_log" CustomLog "/private/var/log/apache2/localhost-access_log" common </virtualhost> <virtualhost> DocumentRoot "/Users/[用户名]/Sites" ServerName sites ErrorLog "/private/var/log/apache2/sites-error_log" CustomLog "/private/var/log/apache2/sites-access_log" common <directory></directory> Options Indexes FollowSymLinks MultiViews AllowOverride None Order deny,allow Allow from all </virtualhost> </code>
保存退出,并重启Apache。
sudo vi /etc/hosts
”,打开hosts配置文件,加入"127.0.0.1 sites
",这样就可以配置完成sites虚拟主机了,可以访问“http://sites”了,在10.8之前Mac OS X版本其内容和“http://localhost/~[用户名]”完全一致。ErrorLog "/private/var/log/apache2/sites-error_log"
”也可以删掉,但记录日志其实是一个好习惯,在出现问题时可以帮助我们判断。如果保留这些log代码,一定log文件路径都是存在的,如果随便修改一个不存在的,会导致Apache无法服务而没有错误提示,这个比较恶心。这里利用Mac OS X 10.6.3和10.8.1中原生支持的方式来实现的配置,也可以参考“Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(一) ”和“Mac OS X Leopard: 配置Apache, PHP, SQLite, MySQL, and phpMyAdmin(二) ”。实际上,还可以使用XAMPP或MacPorts这种第三方提供的集成方案来实现简单的安装和使用。