macOS 中已经内置了 PHP、Python、Ruby、Perl 等常用的脚本语言,以及 Apache HTTP 服务器,所以使用起来非常方便。本文以最新的 macOS Sierra 10.12 配置 MAMP 开发环境为例,本文同样适用其它 macOS 版本的配置,macOS Sierra 10.12 中内置的 PHP 版本为 5.6。
一、配置Apache
#.
macOS Sierra 已内置 Apache 服务器,可以通过如下命令进行开启、关闭以及重启:
$ sudo apachectl start | stop | restart
开启后,打开浏览器,访问 ”http://localhost/index.html.en“
如果出现 ”It works!“
,则 Apache 可以正常使用。
#.
配置文件在 /etc/apache2/httpd.conf 文件中,如果需要配置,修改该文件重启即可。比如咱们对如下配置项进行修改:
默认 Web 根目录配置项为:
DocumentRoot "/Library/WebServer/Documents"
我们修改为当前宿主目录下的webroot目录下(我的账号为sean),修改后为:
DocumentRoot "/Users/sean/webroot"
还可以修改目录的相关权限:
<Directory /> Options FollowSymLink s AllowOverride None Order deny,allow Deny from all </Directory> <Directory "/Users/sean/webroot"> Options Indexes FollowSymLink s AllowOverride All Order allow,deny Allow from all </Directory>
还可以修改以当前用户运行Apache,省的以后老是需要修改权限,只需要做如下修改:
# User _www User sean
当然这个配置只能在开发环境配置,正式服务器配置成这样会存在安全隐患。
配置好后重启 Apache,在 /Users/sean/webroot 目录下新建 index.html,内容为 ”Hello World!“,访问 ”http://localhost“,如果出现 ”Hello World!“,则配置成功。
二、配置 PHP
macOS Sierra 已内置了 PHP 5.6,因此我们只需要在 Apache 的配置中加载 PHP 模块即可,打开 Apache 配置文件”/etc/apache2/httpd.conf“, 找到如下代码,去掉前面的注释(#):
#LoadModule php5_module libexec/apache2/libphp5.so
重启 Apache 后,在 /Users/sean/webroot 目录下新建 phpinfo.php,内容如下
<?php phpinfo();
打开浏览器,访问 ”http://localhost/phpinfo.php“
,如果出现 PHP 的相关信息,则配置成功。
默认没有生成 php.ini 配置文件,运行如下命令生成,也可以直接拷贝改名字:
sudo cp /etc/php.ini.default /etc/php.ini
三、安装 Mysql
macOS Sierra 没有内置 Mysql,所以需要自己安装,到 Mysql 官网下载 .dmg 文件安装即可,下载地址为:http://dev.mysql.com/downloads/mysql/,本文下载的版本为 Mysql 5.7.16。
下载后,双击安装即可,安装完成后,会设置一个初始的密码,如下图:
Mysql 的所有命令都在 /usr/local/mysql/bin 目录中,下面我们使用 mysqladmin 命令把 root 用户的初始密码修改为 root123,进入该目录,在命令行执行以下命令:
$ ./mysqladmin -u root -p password root123
回车后输入上图中的初始密码“h#.a%=reR3)=”即可修改成功。
下面我们用root账号在命令行中登录 Mysql,命令如下:
$ ./mysql -uroot -proot123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 23 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql>
登录后如果出现上图的界面,则 Mysql 安装成功。顺便推荐一个 macOS 中非常好用的 Mysql 客户端工具: Sequel Pro
总结
这样我们就配置好了 MAMP 开发环境,下面大家就可以愉快的玩耍了!