macOS 搭建 NMP 环境

使用 brew 命令进行安装

安装 MySql

1
brew search mysql

搜索一下,看看有什么版本,大家可以根据自己的需要选择版本,我这选择 5.7

1
brew install mysql@5.7

静静等待安装,安装完成之后启动服务

1
brew services start mysql

根据安装完之后的信息提示

1
2
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation

/usr/local/Cellar/mysql@5.7/5.7.28/bin 目录下执行

1
./mysql_secure_installation

按照信息提示一步一步完成。然后记得把 mysql 添加到环境变量中去

~/.zshrc 中添加

1
export PATH=$PATH:/usr/local/Cellar/mysql@5.7/5.7.28/bin

后面是自己 mysql 的安装目录

执行一下命令使其生效

1
source ~/.zshrc

因为我用的是 zsh,所以是在 .zshrc ,如果你是使用默认的 bash,那同样在 ~/.bash_profile 添加环境变零,使其生效即可

安装 Nginx

1
brew install nginx

等待安装完成,编辑 /usr/local/etc/nginx/nginx.conf 配置文件,将默认监听的端口 8080 改成 80

1
2
3
4
5
6
7
8
9
http {
............

server {
listen 80;
server_name localhost;

#charset koi8-r;
.......

启动服务

1
brew services start nginx

访问 http://localhost ,就可以看到 Nginx 的欢迎页面了

安装 PHP

同样使用

1
brew search php

找你所需要的版本安装

1
brew install php

这样,它会直接安装最新版本的 php。查看 php 版本发现还是系统自带的 7.1 版本,这个是因为还没把我们安装的最新版本加到环境变量中去。

1
2
3
4
5
6
7
8
9
10
11
### 使用自带的 bash
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.bash_profile
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.bash_profile

source ~/.bash_profile

### 使用 zsh
echo 'export PATH="/usr/local/opt/php@7.2/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="/usr/local/opt/php@7.2/sbin:$PATH"' >> ~/.zshrc

source ~/.zshrc

配置 PHP 和 Nginx

/usr/local/etc/nginx/servers 目录下添加 default.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
listen 80;
server_name default.me ; ## 根据自己需要填写
root "/usr/local/var/www/default"; ## 根据自己的目录
index index.html index.htm index.php;

location / {
#autoindex on;
try_files $uri $uri/ /index.php?$query_string;
}

location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}

再次检查 /usr/local/etc/nginx 目录下是否存在 fastcgi.confnginx.conf

如果没有的话,复制默认的

1
2
cp nginx.conf.default nginx.conf
cp fastcgi.conf.default fastcgi.conf

重启服务

1
2
_ php-fpm -d
nginx -s reload

就可以开心的玩耍了。

安装扩展

redis

这里可以使用 brew 轻松安装服务

1
brew instll redis

接下来就是重点,安装 php 扩展,到 http://pecl.php.net/package/redis 选择自己的需要的版本

下载源码包

1
wget http://pecl.php.net/get/redis-5.1.0RC2.tgz

解压源码包,进入目录

1
2
tar -xzvf redis-5.1.tgz
cd redis-5.1.0

执行一下 phpize

1
2
3
// php目录
/usr/local/Cellar/php@7.2/7.2.24/bin/phpize
./configure --with-php-config=/usr/local/Cellar/php@7.2/7.2.24/bin/php-config

编译安装

1
make && make install

修改 php.ini 配置。在 /usr/local/etc/php/7.2/php.ini 文件中添加

1
extension=redis.so

重启 php-fpm 就可以看到 redis 扩展了

memcached

使用 brew 安装服务

1
brew install memcached

接下来是安装 php 扩展,到 http://pecl.php.net/package/memcached 选择自己需要的版本下载。

安装 libmemcached

这里需要注意,memcached 扩展需要依赖客户端 libmemcached API,所以这里先安装 libmemcached,到 https://launchpad.net/libmemcached/+download 选择自己需要的版本下载,开始下面的编译安装

1
2
// 创建目录
sudo mkdir -p /usr/local/libmemcached

解压,进入目录,进行编译安装

1
2
3
4
5
6
7
8
9
// 解压,进入目录
tar zxvf libmemcached-1.0.18.tar.gz
cd libmemcached-1.0.18

// 编译
./configure --prefix=/usr/local/libmemcached --with-memcached

// 安装
make && make install

这个过程中可能会遇到两个错误

问题1:

1
2
3
4
libmemcached/byteorder.cc:66:10: error: use of undeclared identifier 'ntohll'
return ntohll(value);
libmemcached/byteorder.cc:75:10: error: use of undeclared identifier 'htonll'
return htonll(value);

解决方案:

编辑 libmemcached/byteorder.cc 文件

1
2
3
4
5
sudo vi libmemcached/byteorder.cc
#include "libmemcached/byteorder.h" 下面增加以下内容:
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif

编辑 clients/memflush.cc 文件

1
2
3
sudo vi clients/memflush.cc
将两处 if (opt_servers == false)
替换成 if (opt_servers == NULL)

改完重新编译。

问题2:

权限问题

1
2
3
4
mkdir: /usr/local/libmemcached/lib: Permission denied
make[2]: *** [install-libLTLIBRARIES] Error 1
make[1]: *** [install-am] Error 2
make: *** [install] Error 2

解决方案

那就修改刚刚自己创建的目录的权限,用户组都行

1
2
3
4
5
// 用户组,你自己当前的用户组
chown -R zh***n:admin libmemcached

// 修改权限
chmod -R 0777 libmemcached

安装 memcached PHP 扩展

安装完上的,接下来就可以安装 PHP 扩展了

1
2
3
4
5
6
7
8
9
10
// 解压前面下载 memcached,进入目录
tar zxvf memcached-3.1.3.tar
cd memcached-3.1.3

// 执行phpize命令生成configure建立php外挂模块
/usr/local/Cellar/php/7.2.12_2/bin/phpize
./configure --with-php-config=/usr/local/Cellar/php@7.2/7.2.24/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached

// 安装
make && make install

修改php.init增加如下代码

1
2
#添加 
extension=memcached.so

问题1:**

1
error: memcached support requires ZLIB. Use --with-zlib-dir=<DIR> to specify the prefix where ZLIB headers and library are located

解决方案

安装 zlib

1
brew install zlib

遇到的问题

访问时,网页一直显示 502

1
[error] 38689#0: *6 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: blog.me, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "blog.me"

因为没有启动 php-fpm服务,启动就行了。

不对之处,请多指教

您的支持将鼓励我继续创作!