Local access to PHP files and databases


English version steps

Basic configuration

  1. First, download MAMP from the official website: https://www.mamp.info/en/downloads/

  1. After downloading, follow the guide and finish the installation. Then open the free version of MAMP (no need to open Pro, which is paid, the free version is enough)

  1. Click Preference–>Click Ports–>Apache Port: 8888, Nginx Port: 8888, MySQL Port: 8889 (just use its default port number for these ports)

  1. Click Server–>Document root path: Users->your Mac local user->Sites->localhost (this is where you store the PHP files you want to run locally (my own path), you can customize the path here, but you may need to grant MAMP permission to access the desktop folder (If your path is a certain desktop folder) the default is Finder->Applications->MAMP->htdocs)
  • Tips: You can use command+shift+G in Finder to open Go navigation, then enter the full path to access the root or any location file/folder.

  1. If you are successful, a WebStart page will open automatically, and the “start” button will be green. Then you can access your PHP files in your browser: http://localhost:8888/YourFile.php

Connecting to the PhpMyAdmin database

  • If your PHP file has a connection to a database, then you also need to configure the database.

  1. First check if you have a database connection, remember the previous webStart page, scroll down the page, find MySQL, does it show “You can administer your MySQL databases with phpMyAdmin”, if it does, you can access your database by clicking phpMyAdmin directly.
  • If it doesn’t, then do the following checks:
    1. Go to
      /Applications/MAMP/bin/phpMyAdmin/config.inc.php
      and check if the port, username, and password in the configuration file are the same as your MAMP configuration, noting that if you have not configured the MySQl password before then the default is: username: root, password: root. I recommend changing it to your own password.
  1. The most convenient way to do this is to:
    1. Enter the following command in the terminal.
      /Applications/MAMP/Library/bin/mysqladmin -u root -p password
    2. Follow the prompts and enter the original password, then the new password, and finally confirm the new password to succeed. The following warning may appear, but it will not affect the password change.
      Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
    3. Go to the MAMP configuration file and change the MySQL password by opening
      /Applications/MAMP/bin/phpMyAdmin/config.inc.php
      Find line 87 or so and change it to the password you just changed.
    4. After these two steps, you have correctly changed the mysql password for MAMP, the integrated PHP environment for Mac.
    5. Restart the server, and you should see that phpMyAdmin is successfully connected when you enter the WebStart page, click it to enter.
  • Note: The port of phpMyAdmin is the port of MySQL configured in your MAMP: 8889. The code in your PHP code to connect to the database should be the same as the code provided in the MySQL page under its WebStart page.

中文版本步骤

基本配置

  1. 首先,官网下载MAMP: https://www.mamp.info/en/downloads/

  1. 下载完之后,按照指引,完成安装。然后打开MAMP免费版(不用开Pro,付费的,免费版足够)

  1. 点击Preference–>点击Ports–>Apache Port: 8888, Nginx Port: 8888, MySQL Port:8889 (这些端口号用它默认的就行)

  1. 点击Server–>Document root路径: Users->你的Mac本机用户->Sites->localhost (这里存放你要在本机运行的PHP文件(我自己的路径),这里可以自定义路径但是可能需要授予MAMP权限访问桌面文件夹(如果你的路径是桌面文件夹的话)默认的话是Finder—>Applications—>MAMP—>htdocs)
  • Tips: 你可以在Finder中使用 command+shift+G打开Go导航,然后输入完整路径以访问root或者任意位置的文件/文件夹。

  1. 设置完后回到主界面,web server 和 PHP version默认就行然后点击右上角start。成功的话 会有一个 WebStart页面自动打开,而且 start为绿色。那么你可以在浏览器中访问 你的PHP文件了: http://localhost:8888/YourFile.php

连接PhpMyAdmin数据库

  • 如果你的PHP文件中有连接数据库,那么还需要配置一下数据库。

  1. 首先查看是否有连接到数据库,记得之前的webStart页面吗,页面往下拉,找到MySQL,是否显示“You can administer your MySQL databases with phpMyAdmin”,如果有的话,直接点击phpMyAdmin就可以访问你的数据库了。
  • 如果说连接失败则进行如下检查:
    1. 去到
      /Applications/MAMP/bin/phpMyAdmin/config.inc.php
      地址检查配置文件中的端口,用户名,密码是否和你MAMP配置的相同,其中要注意的是 如果之前没有配置过MySQl密码那么默认的是:用户名:root,密码:root。建议更改成你自己的密码。
  1. 最方便的做法是:
    1. 终端输入以下命令:
      /Applications/MAMP/Library/bin/mysqladmin -u root -p password
    2. 按提示执行,先输入原密码,再输入新密码,最后确认新密码,即可成功。 可能会出现下面警告,但不影响密码更改。
      Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
    3. 进入MAMP配置文件修改MySQL密码,打开
      /Applications/MAMP/bin/phpMyAdmin/config.inc.php
      找到87行左右,修改成你刚才进行过修改的密码。
    4. 经过这两个步骤,就正确修改了Mac下PHP集成环境MAMP的mysql密码。
    5. 重启服务器,进入WebStart页面应该可以看到phpMyAdmin连接成功了,点击即可进入。
  • Note: phpMyAdmin 的端口就是你MAMP配置的MySQL的端口: 8889。你PHP代码里面的连接数据库的代码最好跟它WebStart页面下的MySQL页面下提供的代码一样。

Author: Anti-General
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Anti-General !
R
Comment
  TOC