search
HomeBackend DevelopmentPHP Tutorialmac下配置Apache+PHP+MySQL环境

  最近做一个项目,前后端交互的api文档一直以excel和word的形式管理,觉得很不方便,于是就是想找个更直观的方式。在网上找了一圈,有一些看起来很不错(没用过,所以只能说看起来很不错)的开源api管理平台,比如EasyAPI。

  但是,我又不想将api放在别人的服务器上,继续寻找,发现了showdoc。这是个可以部署到自己服务器的开源api管理项目,使用的php。于是,我开始在自己的mac上折腾Apache+PHP+MySQL,下面进入正题。

Apache+PHP配置

  mac osx系统都自带了Apache和PHP环境,只是默认情况下没有开启,我个人设备环境如下:

  • PHP版本:5.5.27
  • Apache版本:2.4.16 (Unix)
  • 系统版本:OS X EI Capitan(OS X 10.11)

开启Apache

  打开终端,输入如下开启命令。

  • 开启Apache:sudo apachectl start
  • 关闭Apache:sudo apachectl stop
  • 重启Apache:sudo apachectl restart

  在浏览器中输入localhost,如果出现如下默认的“It works!”界面,则表示Apache开启成功。

localhost-osx-apache.png

开启PHP

  开启PHP,需要修改Apache配置文件,方法如下:

  1. 打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
  2. 找到#LoadModule php5_module libexec/apache2/libphp5.so,去掉注释(删除前面的井号)。

  mac下Apache的默认文件夹为/Library/WebServer/Documents,在该目录下创建一个名为index.php文件,在文件中添加如下内容:。删除原目录下的index.html文件,然后在浏览器中输入localhost,如果出现如下PHP的info页,则表示PHP开启成功,如果不成功,用前面的命令重启Apache再试。

QQ20160219-0@2x.png

修改Apache目录

  上面说到了mac下Apache的默认文件夹为/Library/WebServer/Documents,该目录默认是隐藏的,操作不是很方便,我们可以将其修改成自定义的目录。

  1. 打开终端,输入命令:sudo vim /etc/apache2/httpd.conf
  2. 找到如下两处  DocumentRoot "/Library/WebServer/Documents"  
  3. 将两处中引号中的目录替换为自定义的目录

  完成以上三步后,重启Apache,将之前创建的index.php文件拷贝到自定义目录中,然后在浏览器中输入localhost,如果出现PHP的info页,则表示目录修改成功。

MySQL安装

  OSX 10.11没有自带MySQL,需要我们自己安装。这里只介绍最简单的方式,到MySQL官网下载最新的dmg安装包,我本人下载的是mysql-5.7.11-osx10.10-x86_64.dmg。进入下载页面后,会提示你登陆或注册,这里不必理会,直接点击底部的“No thanks, just start my download.”即可开始下载。  双击下载的dmg安装,一直下一步安装,最后安装完成后会弹出一个提示,告诉你MySQL为root@localhost账号生成了一个临时密码,务必将这个密码保存下来。

mysql-root-password.png

启动MySQL

  1. 进入系统偏好设置,点击MySQL。

    QQ20160219-2@2x.png

  2. 点击Start MySQL Server

    QQ20160219-1@2x.png

修改root用户的密码

  打开终端,输入命令(将temppassword替换为之前保存的临时密码):/usr/local/mysql/bin/mysqladmin -u root -p'temppassword' password 'newpassword'

修复“2002 MySQL Socket error”

  如果出现“2002 MySQL Socket error”错误,表示无法找到支持MySQL的c/s通信的socket。原因是MySQL将其放在 /tmp 目录,而OSX将其放在 /var/mysql 目录。所以我们只需要创建一个软链接,输入以下两个命令即可:创建目录:sudo mkdir /var/mysql创建软链接:sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

phpMyAdmin

  完成以上两步,MySQL就算安装配置完成了,不过为了方便,有必要安装一个可视化的工具,我选择phpMyAdmin。  直接到phpMyAdmin官网下载最新的zip包,建议下载多语言版本,支持简体中文。下载完成后,将解压后的文件夹重命名为phpMyAdmin,然后放置到自定义的Apache路径的根目录下。  在浏览器中输入localhost/phpMyAdmin,出现MySQL的登陆页面,在该页面可以设置语言,然后用root和之前设置的密码登陆。看到如下两个页面,就大功告成了!!

QQ20160219-3@2x.png

QQ20160219-4@2x.png

参考资料

  1. Mac下配置Apache + Php + Mysql环境
  2. Get Apache, MySQL, PHP and phpMyAdmin working on OSX 10.11 El Capitan
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment