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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

HTTP Method Verification in LaravelHTTP Method Verification in LaravelMar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

Discover File Downloads in Laravel with Storage::downloadDiscover File Downloads in Laravel with Storage::downloadMar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function