一、前言

首先恭喜选择了mac来做PHP开发环境,mac 对PHPer不错,内置了PHP的apache环境以及php开发语言;唯独缺少mysql数据库;所以只用安装mysql即可;

本文中的链接,该翻墙的还是要翻墙的;重要的细节说三遍:翻墙!翻墙!翻墙!

二、推荐软件

Homebrew是MacOS 缺失的软件包管理器,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便。
这里介绍几个简单常用的brew的语法即可;

常用命令

1.安装Homebrew命令

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2.简洁帮助命令

1
brew --help

3.安装软件命令

1
brew install 软件名称

4.卸载软件命令

1
brew uninstall 软件名称

5.查看软件信息

1
brew info 软件名称

6.搜索软件

1
brew search 软件名称

7.查看已安装软件列表

1
brew list

三、配置php环境

1.检查Mac中的apache以及php版本信息

因为我重装过mac系统了(Mac OS High Sierra),所以可能php的版本比较高;两个都在至少省去了你一大波麻烦的事情去做;刚拿到手的时候的php版本是5.6的样子;

1
2
3
4
5
6
7
8
apachectl -v
Server version: Apache/2.4.27 (Unix)
Server built: Jul 15 2017 15:41:46

php -v
PHP 7.1.7 (cli) (built: Jul 15 2017 18:08:09) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

2.简单介绍apache下http.conf 配置文件

httpd.conf文件所在位置\etc\apache2目录下;可以选择两种方式打开httpd.conf文件;

  • 命令行工具vim打开,一定要使用sudo命令打开,否则没有权限保存,切记一定要使用,并且会提示你输入你电脑的密码;
1
sudo vim /etc/apache2/httpd.conf
  • 用finder打开,使用快捷键:command+shift+g 这是个时候会弹出输入框,此时你只用输入apache所在目录即可:/etc/apache2 ;此时你便会找到httpd.conf文件,选择你想要打开的方式,并进行编辑即可;一般使用sublime text 3编辑;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# 你的apache软件安装的位置。其它指定的目录如果没有指定绝对路径,则目录是相对于该目录,mac原装的apache可以不用改;
ServerRoot "/usr"

# 监听端口
<IfDefine SERVER_APP_HAS_DEFAULT_PORTS>
Listen 8080
</IfDefine>
<IfDefine !SERVER_APP_HAS_DEFAULT_PORTS>
Listen 80
</IfDefine>

# 加载的相关模块
LoadModule authn_file_module libexec/apache2/mod_authn_file.so
#LoadModule authn_dbm_module libexec/apache2/mod_authn_dbm.so
#LoadModule authn_anon_module libexec/apache2/mod_authn_anon.so
#LoadModule authn_dbd_module libexec/apache2/mod_authn_dbd.so
#LoadModule authn_socache_module libexec/apache2/mod_authn_socache.so
LoadModule authn_core_module libexec/apache2/mod_authn_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule authz_groupfile_module libexec/apache2/mod_authz_groupfile.so
#......
LoadModule hfs_apple_module libexec/apache2/mod_hfs_apple.so

# 加载的php版本,多版本php也是类似的写法
PHPIniDir "/etc"
LoadModule php7_module libexec/apache2/libphp7.so

<IfModule unixd_module>
# 伪用户
User _www
# 伪用户组
Group _www
</IfModule>

#管理员的邮箱
ServerAdmin you@example.com

# 站点名称(域名)
ServerName www.example.com:80

# 这里是全局目录默认规则,拒绝所有
<Directory />
AllowOverride none
Require all denied
</Directory>

# 这个是源码包安装后,网站主目录位置这个是源码包安装后,网站主目录位置,mac默认站点文件,可以改成你需要的文件名称
# --- Options 目录权限
# None :没有任何额外权限
# All :所有权限
# Indexs :没有找到默认文章(如:index.php)就会返回当前文件夹下的所以文件列表

# --- AllowOverride子权限文件开关
# 定义是否允许目录下.htaccess文件中的权限生效,这里只列举以下两种常见的选项
# None:
.htaccess中权限不生效
# All: .htaccess文件中所有权限都生效

# --- Require访问控制管理
# Require all granted允许所有访问
# Require all denied拒绝所有访问
# Require ip 192.168.1.0/24 仅允许192.168.1.0/24网络的主机访问
# Require not ip 192.168.1.2 禁止192.168.1.2的主机访问,其它都可以
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
Options FollowSymLinks Multiviews
MultiviewsMatch Any
Require all granted
</Directory>

# 设置默认目录的默认文档
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>

#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
# 防止 .htaccess 和 .htpasswd 文件被从Web上访问
<FilesMatch "^\.([Hh][Tt]|[Dd][Ss]_[Ss])">
Require all denied
</FilesMatch>

# Apple specific filesystem protection.
# 苹果特定文件系统保护,全是拒绝访问,所以可以不用管
<Files "rsrc">
Require all denied
</Files>
<DirectoryMatch ".*\.\.namedfork">
Require all denied
</DirectoryMatch>

# apache错误日志记录 记录文件的路径,可以修改
ErrorLog "/private/var/log/apache2/error_log"

# 记录日志的等级,
LogLevel warn

<IfModule log_config_module>
# 日志格式
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common

<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
</IfModule>

# 访问 apache 访问日志
CustomLog "/private/var/log/apache2/access_log" common

#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
#CustomLog "/private/var/log/apache2/access_log" combined
</IfModule>

# 读取其他的配置
Include /private/etc/apache2/other/*.conf

配置完成后重启apache服务,打开浏览器输入localhost验证下;

1
2
3
sudo httpd -v #检查配置文件是否正确;
sudo apachectl start #启动 apache 服务;
sudo apachectl restart #重启 apache 服务;

【备注】
在配置中已经加载了php模块;所以php这个部分就不用太担心了;如果不放心;可以写一个php文件检查php是否正确;最好的方法,phpinfo;

1
2
3
<?php
phpinfo();
?>

3.php配置文件简介

这个地方我就偷下懒,发个链接,大家可以去看看,官方文档也有的哦;

4.数据库配置文件简介

  • 首先我们首选homebrew来安装数据库
1
2
3
4
5
6
7
8
9
10
11
brew search mysql # 可以选择搜索下数据库的版本,如果直接安装,默认安装的数据库版本为5.7 
==> Searching local taps... # 别人在执行搜索😳下面才是搜索的列表
mysql ✔ mysql-connector-c mysql-utilities
automysqlbackup mysql-connector-c++ mysql@5.5
mysql++ mysql-sandbox mysql@5.6
mysql-cluster mysql-search-replace mysqltuner
==> Searching taps on GitHub...
caskroom/cask/mysql-connector-python caskroom/cask/navicat-for-mysql
caskroom/cask/mysql-shell caskroom/cask/sqlpro-for-mysql
caskroom/cask/mysql-utilities
==> Searching blacklisted, migrated and deleted formulae...

对数据库版本没有要求直接执行安装命令即可

1
brew install mysql

安装完成后,执行下面语句

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
brew info mysql #下面一大段信息都是所安装的数据库的信息,以及如何开启数据库服务
mysql: stable 5.7.20 (bottled) #数据库版本
Open source relational database management system
https://dev.mysql.com/doc/refman/5.7/en/
Conflicts with:
mariadb (because mysql, mariadb, and percona install the same binaries.)
mariadb-connector-c (because both install plugins)
mysql-cluster (because mysql, mariadb, and percona install the same binaries.)
mysql-connector-c (because both install MySQL client libraries)
percona-server (because mysql, mariadb, and percona install the same binaries.)
/usr/local/Cellar/mysql/5.7.20 (324 files, 233.7MB) * #安装的地址;
Poured from bottle on 2017-11-06 at 22:59:01
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mysql.rb
==> Dependencies
Build: cmake ✘
Required: openssl ✔
==> Requirements
Required: macOS >= 10.7
==> Options #重要信息
--with-archive-storage-engine
To connect run:
#链接数据库 ,5.7mysql默认密码为空,所以直接可以链接上数据库
#当然你要使用数据库服务,肯定要启动数据库服务了;下面就是如何启动数据库服务,注意看注释
mysql -uroot
Compile with the ARCHIVE storage engine enabled
--with-blackhole-storage-engine
Compile with the BLACKHOLE storage engine enabled
--with-debug
Build with debug support
--with-embedded
Build the embedded server
--with-local-infile
Build with local infile loading support
--with-test
Build with unit tests
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation #这个是执行数据库初始配置的语句

MySQL is configured to only allow connections from localhost by default
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
# 这句话就是告诉你,不好意思数据库的配置,我们没有,你需要自己写(按照你的需求写);
# 数据库也有配置加载的优先级,具体怎么个优先级;下面会介绍
server starting up correctly.
# 下面这句话就是告诉你,如果你要数据库在开机的时候就启动呢,就执行下面的语句;
To have launchd start mysql now and restart at login:
brew services start mysql
# 下面这句话告诉我们,如果你不要开机的时候启动数据库服务,就执行下面的语句即可;按需启动数据库服务;
Or, if you don't want/need a background service you can just run:
mysql.server start

刚刚说到数据库配置文件是没有的,my.cnf这个文件你找不到的,以及数据库配置加载的优先级;

1
2
3
4
5
mysql --help
# 你可以看到一大段数据库帮助信息,这个时候你锁定一句
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf /usr/local/etc/my.cnf ~/.my.cnf
这句话,就是告诉你,数据库配置的加载顺序,所以你需要新建my.cnf文件,写入你的配置即可;当然,完成后,记得重启数据库服务;

环境配置到此结束;


参考链接:https://www.jianshu.com/p/488f18b56b97