macOS 下配置 MAMP 开发环境(Mac + Apache + Mysql + PHP)

macOS 中已经内置了 PHP、Python、Ruby、Perl 等常用的脚本语言,以及 Apache HTTP 服务器,所以使用起来非常方便。本文以最新的 macOS Sierra 10.12 配置 MAMP 开发环境为例,本文同样适用其它 macOS 版本的配置,macOS Sierra 10.12 中内置的 PHP 版本为 5.6。

一、配置Apache

macOS Sierra 已内置 Apache 服务器,不需要我们自己编译安装,只需开启 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 FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>

<Directory "/Users/sean/webroot">
    Options Indexes FollowSymLinks
    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 模块即可。php、phpize 等命令都在 /usr/bin 目录下。

加载 PHP 模块

打开 Apache 配置文件/etc/apache2/httpd.conf, 找到如下代码,去掉前面的注释(#):

#LoadModule php5_module libexec/apache2/libphp5.so

生成配置文件

默认没有生成 php.ini 配置文件,运行如下命令生成,也可以直接拷贝改名字:

$ sudo cp /etc/php.ini.default /etc/php.ini

测试是否加载成功

重启 Apache 后,在 /Users/sean/webroot 目录下新建 phpinfo.php,内容如下

<?php

phpinfo();

打开浏览器,访问http://localhost/phpinfo.php,如果出现 PHP 的相关信息,则配置成功。

三、安装 Mysql

macOS Sierra 没有内置 Mysql,所以需要自己下载安装。

下载 Mysql

Mysql 官网 下载 .dmg 文件安装即可,本文下载的版本为 Mysql 5.7.16,请按下图指示进行下载:

mysql-download.png

安装 Mysql

下载后,双击安装即可,安装完成后,会设置一个初始的密码,如下图:

mysql-password.png

修改密码

Mysql 的所有命令都在 /usr/local/mysql/bin 目录中,下面我们使用 mysqladmin 命令把 root 用户的初始密码修改为 root123,进入该目录,在命令行执行以下命令:

$ ./mysqladmin -u root -p password root123

回车后输入上图中的初始密码 “h#.a%=reR3)=“ 即可修改成功。

登录 Mysql

下面我们用 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 开发环境,下面大家就可以愉快的玩耍了!


本文首发于马燕龙个人博客,欢迎分享,转载请标明出处。
马燕龙个人博客:https://www.mayanlong.com
马燕龙个人微博:http://weibo.com/imayanlong
马燕龙Github主页:https://github.com/yanlongma

标签: mac, lamp

不错,不错,对我有帮助! 我要打赏他!GO ->

仅有一条评论

  1. 那个白块是什么??挡得那么准,完全没办法留言了~~

添加新评论