最近想接触一下php相关知识,需要在mac上搭配php运行环境。其实Mac系统已经内置了Apache和PHP,使用起来非常方便。
启动Apache
$ sudo apachectl start //由于sudo是系统指令,需要输入电脑密码
$ sudo apachectl -v
//接下来会看见Mac中Apache的版本信息,如10.11.5中
Server version: Apache/2.4.18 (Unix)
Server built: Feb 20 2016 20:03:19
//验证Apache服务器是否启动:打开浏览器,在网址处输入
http://localhost
或 http://127.0.0.1
如果出现It works! 则启动成功。
Apache的默认安装路径:/private/etc/apache2

Apache的服务部署路径: /Library/WebServer/Documents/
修改部署路径
在/private/etc/apache2 目录下用记事本打开httpd.conf文件,找到DocumentRoot就可以修改路径

此时显示

复制后修改,然后再替换原文件
Apache服务端口号修改
在/private/etc/apache2 目录下打开httpd.conf文件,找到Listen 80修改

启动PHP
在终端中输入命令
$ sudo vim/ etc/apache2/httpd.conf
找到 #LoadModule php5_module libexec/apache2/libphp5.so 将#去掉
然后重启Apache
sudo apachectl restart
进入/Library/WebServer/Documents 新建一个php文件
sudo vim test.php
在text.php中输入一下代码
<?php phpinfo(); ?>

在浏览器中国打开localhost/test.http,测试php是否可用

安装MySql
与Apache、PHP不同,Mac原生没有带MySql,需要自己安装。先到mysql官网(http://dev.mysql.com/downloads/mysql/)下载安装包。MySql有社区版(community)和企业版(enterprise),如果个人学习及开发的话,选择community即可。

网友评论