macOS下安装nginx+php+memcached+redis+mysql+yaf扩展环境教程

macOS 10.12.4
直接上命令,不多说了。
下载并安装各软件包均通过 Homebrew,因此,安装之前,需在 Mac 上下载并安装  Homebrew,在 CLI 中通过下面的命令可以完成:

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

怎么用brew不是这篇文章介绍的。我们直接说我本地的配置过程

这里安装默认的 Nginx:

brew install nginx

新版的macOS默认没了Launch目录,创建个

mkdir -p ~/Library/LaunchAgents

添加 Nginx 至开机启动:

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents

设置nginx的执行文件权限,这样就可以80端口运行了

sudo chown root:wheel /usr/local/Cellar/nginx/1.10.0/bin/nginx
sudo chmod u+s  /usr/local/Cellar/nginx/1.10.0/bin/nginx
sudo chown -R $USER /usr/local/var/log/nginx/

Nginx安装完成,开启和停止服务的命令:

sudo nginx         # Start
sudo nginx -s stop # stop

安装PHP

brew search php

我这儿安装php71 也就最新的7.1版本

brew install --without-apache --with-fpm --with-mysql php71

安装相关扩展(我自己用的,你们需要其它的自行查看对应的扩展名)

brew install php71-xdebug php71-memcached php71-swoole php71-redis php71-pdo-pgsql php71-mcrypt 

添加进开机启动

ln -sfv /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/
ln -sfv /usr/local/Cellar/memcached/1.4.36/homebrew.mxcl.memcached.plist  ~/Library/LaunchAgents/

yaf扩展安装
https://github.com/laruence/yaf下载最新的源码安装包并解压

unzip yaf-master.zip
cd yaf-master

找到我们的php的bin目录

echo $(brew --prefix homebrew/php/php71)

安装autoconf,否则会报错 Cannot find autoconf. Please check your autoconf installation and the

brew install autoconf

执行安装

/usr/local/opt/php71/bin/phpize
./configure --with-php-config=/usr/local/opt/php71/bin/php-config
make && make install

完事后会提示你扩展目录,像我的是

/usr/local/Cellar/php71/7.1.3_15/lib/php/extensions/no-debug-non-zts-20160303/

打开/usr/local/etc/php/7.1/conf.d目录,复制个ini文件,重命名ext-yaf.ini
内容改为

[yaf]
extension="/usr/local/Cellar/php71/7.1.3_15/lib/php/extensions/no-debug-non-zts-20160303/yaf.so"

mysql安装

brew install mysql

安装完了会有一些提示,默认密码为空,启动命令

mysql.server start

添加开机启动

ln -sfv /usr/local/opt/mysql/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/

启动mysql,然后初始化

mysql_secure_installation

会依次出现下面的各种提示

New password:                   //给root账号设置一个新密码. 输入密码后回车

Re-enter new password:      //确认刚才的密码

Remove anonymous users? (Press y|Y for Yes, any other key for No) :    //是否删除匿名账号
Success.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) :   //禁止远程登录

Remove test database and access to it? (Press y|Y for Yes, any other key for No) :  //删除test数据库

Reload privilege tables now? (Press y|Y for Yes, any other key for No) :  //立即刷新数据库权限,也就是刚才的设置立即生效

用你的mysql工具连接一下,我个人推荐sequel pro

安装redis

brew install redis

完成了会提示你

brew services start redis //启动命令
redis-server /usr/local/etc/redis.conf //配置等信息

开机启动

ln -sfv /usr/local/Cellar/redis/3.2.8/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/

配置环境

source ~/.profile
alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'

改下nginx的相关配置

vi /usr/local/etc/nginx/nginx.conf

内容大致如下

worker_processes  8;
events {
    worker_connections  1024000;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 2000M;
    include servers/*;
}

然后是建个php-fpm文件

location ~ \.php$ {
    try_files   $uri = 404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_intercept_errors  on;
    include fastcgi.conf;
}

进servers目录下建个默认站点default.conf

server {
    listen       80;
    server_name  _;
    root         /usr/local/var/www/;
    access_log  off;
    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        include     php-fpm;
    }
}

完事了重启一下机器 ... 收工

最后修改时间为:2017 年 04 月 12 日 10 时 22 分 PM
如果觉得我的文章对你有用,请随意赞赏

发表评论