Mac 下 Nginx、MySQL、PHP-FPM 的安装配备

php -m | grep "hprose"

sudo brew services restart php@7.1

第一是设置修正root密码(设置过了能够毫无安装,略过)、删除无名访谈、删除root网络访谈、删除test数据库。指令实施完后,登录mysql就必要密码验证了

安装Mysql

先物色下mysql

➜  ~ brew search mysql
automysqlbackup            mysql-cluster              mysql-sandbox
mysql                      mysql-connector-c          mysql-search-replace
mysql++                    mysql-connector-c++        mysqltuner
homebrew/php/php53-mysqlnd_ms            homebrew/versions/mysql56
homebrew/php/php54-mysqlnd_ms            Caskroom/cask/mysql-connector-python
homebrew/php/php55-mysqlnd_ms            Caskroom/cask/mysqlworkbench
homebrew/php/php56-mysqlnd_ms            Caskroom/cask/navicat-for-mysql
homebrew/versions/mysql55                Caskroom/cask/sqlpro-for-mysql

看一下mysql的版本新闻

➜  ~ brew info mysql     
mysql: stable 5.7.16 (bottled)
Open source relational database management system
# 下面信息省略

上面就用brew install mysql安装下吧,安装时的新闻有那般一句话We've installed your MySQL database without a root password. To secure it run:mysql_secure_installation,那就来安装下root的密码

# 第一步:打开mysql服务
mysql.server start
# 第二步:执行mysql_secure_installation
mysql_secure_installation # 执行后按照提示信息进行设置,慢慢看下英文,都能看懂的

好了,进入mysql看下

➜  ~ mysql -u root -p         
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16 Homebrew
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

要是你前边运用了 homebrew/php 实行设置的,提出你先删除以前的版本。


安装php

一向brew search php 莫有用,你能够试下看看提醒音讯,要先增多php扩张

brew update # 安装软件前都要习惯的更新下brew源
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php

今后brew search php就足以了,出来一群版本的php,我们就安装php7.0版本吧,小编用php7.1本子在跑laravel的时候,出现过错误,作者那时候未有消除掉,装回php7.0还不荒谬。

能够先brew options php70拜望你安装的时候要如何选项,笔者选拔了下边那些,因为背后我们会用nginx作为反向代理,所以就毫无用--with-apache选项了,也毫无加多--without-fpm选项

brew install php70 --with-debug --with-gmp --with-homebrew-curl --with-homebrew-libressl --with-homebrew-libxml2 --with-homebrew-libxslt --with-imap --with-libmysql --with-mysql

出于Mac自带了php和php-fpm,由此要求加上系统情状变量PATH来替代自带PHP版本,大家用的是zsh,所以放进.zshrc中,如若您用的shell是bash,那么能够把下部的消息写入到~/.bash_profile文件中,借使那么些文件未有,你和谐建三个就可以。

echo 'export PATH="$(brew --prefix php70)/bin:$PATH"' >> ~/.zshrc  #for php
echo 'export PATH="$(brew --prefix php70)/sbin:$PATH"' >> ~/.zshrc  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.zshrc #for other brew install soft
source ~/.zshrc

测量检验下效果

# brew安装的php
➜  ~ php -v    
PHP 7.0.12 (cli) (built: Oct 24 2016 00:06:38) ( NTS DEBUG )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

# brew安装的php-fpm
➜  ~ php-fpm -v
PHP 7.0.12 (fpm-fcgi) (built: Oct 24 2016 00:06:45) (DEBUG)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

# Mac自带的php, 我以前的项目有些依赖不支持php7,所以这个我是留着的,随时切换使用
➜  ~ /usr/bin/php -v     
PHP 5.6.24 (cli) (built: Aug  8 2016 16:58:37) 
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

# Mac自带php-fpm
➜  ~ /usr/sbin/php-fpm -v
PHP 5.6.24 (fpm-fcgi) (built: Aug  8 2016 16:58:54)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
➜  ~ 

下边先来看下php-fpm的配置文件,路线在/usr/local/etc/php/7.0/php-fpm.conf, 我们应该都猜的到。大家根本看下pid和log文件会放在何地

 13 ; [global]
 14 ; Pid file
 15 ; Note: the default prefix is /usr/local/var
 16 ; Default Value: none
 17 ;pid = run/php-fpm.pid
 18 
 19 ; Error log file
 20 ; If it's set to "syslog", log is sent to syslogd instead of being written
 21 ; in a local file.
 22 ; Note: the default prefix is /usr/local/var
 23 ; Default Value: log/php-fpm.log
 24 ;error_log = log/php-fpm.log

投机看下下边包车型大巴消息,去掉17行和24行前面包车型客车分行,使用php-fpm -t测量试验下布署是或不是准确,按提醒音信是无论它也得以,默许正是在/usr/local/var路线下的,可是依然设置下啊;

➜  7.0 php-fpm -t
[24-Oct-2016 11:20:31] NOTICE: configuration file /usr/local/etc/php/7.0/php-fpm.conf test is successful

php-fpm的一些管制:

#测试php-fpm配置
php-fpm -t

#启动php-fpm
php-fpm -D

#关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`

#重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

#也可以用上文提到的brew命令来管理php-fpm
brew services start|stop|restart php70

#还可以用这个命令来管理php-fpm
php70-fpm start|stop|restart 

开发银行php-fpm之后,确认保证它符合规律运作监听9000端口:

➜  ~ lsof -Pni4 | grep LISTEN | grep php
php-fpm   18381  zjp    8u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   18382  zjp    0u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   18383  zjp    0u  IPv4 0xbca78421d968b30f      0t0  TCP 127.0.0.1:9000 (LISTEN)

[全新安装Mac OS Sierra (10.12State of Qatar并动用HomeBrew安装ZSH + MNMP (Mac + Nginx

实际上在默许情状下,安装了 Php@7.1后还要也设置了 Pecl,我们再安装扩大的时候,只必要经过 pecl 来安装就 OK 啦。

要改良配置 php 大概 php-fpm 的话,能够校勘"/usr/local/etc/php/5.4/php.ini" 、 "/usr/local/etc/php/5.4/php-fpm.conf"。

/usr/local/etc/php/7.1/conf.d/ext-swoole.ini

1
2
sudo chown root:wheel /usr/local/Cellar/nginx/1.2.6/sbin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.2.6/sbin/nginx
[swoole]extension=/usr/local/Cellar/php@7.1/7.1.17/pecl/20160303/swoole.so

这里是 PHP 5.4.28 源代码地址出错的标题,改进文件 /usr/local/Library/Taps/josegonzalez/homebrew-php/Formula/abstract-php-version.rb 下 Php54Defs 的源码地址 PHP_SRC_TARBALL 的值,这里改用 这几个地址。

譬如是安装 xdebug 扩张,在启用时索要用 Zend 扩充方式加载。

1
2
3
mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/mysql/5.5.28/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
pecl install hprose xxtea swoole

brew 施行完后,nginx 就设置好了。能够用以下指令对 nginx 进行操作:

// 是否启用 Debug 跟踪调试// 启用后,会在命令行中输出 Debug 跟踪 log,我一般禁用此项。enable debug/trace log support? [no] : no// 是否启用 PHP Sockets 支持// 如果你需要用 PHP 编写 Sockets 服务,可以启用此项。enable sockets supports? [no] : yes// 是否启用 OpenSSL 支持// 是否启用 SSL 加密,如果你是通过 Swoole 提供对外的 HTTPS 服务,则需要启用此项。// 我的 Mac 是本地开发用,所以不启用。enable openssl support? [no] : no// 是否启用 HTTP2 支持// 了解 HTTP2 新特性 https://zh.wikipedia.org/wiki/HTTP/2enable http2 support? [no] : yes// 是否启用异步 Redis 支持// Swoole 实现了一套支持异步的 Redis 服务端框架// https://wiki.swoole.com/wiki/page/p-redis_server.htmlenable async-redis support? [no] : yes// 是否启用 MySQL 原生支持enable mysqlnd support? [no] : yes// 是否启用 PgSQL 协程客户端支持enable postgresql coroutine client support? [no] : yes

但是试了刹那间,不是超级客户登录,而是普通客户登入,何况监听的端口在1024以下的(比方把暗中认可的8080端口改为了80端口),nginx 开机是运行不了。因而,要 nginx 开机运维以来,需求授予它管理员权限:

[xdebug]zend_extension=/usr/local/Cellar/php@7.1/7.1.17/pecl/20160303/xdebug.so
1
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
[xxtea]extension=/usr/local/Cellar/php@7.1/7.1.17/pecl/20160303/xxtea.so

开机运营 php-fpm(上边包车型客车 5.4.9 是当前设置 php 的具体版本号):

安装 Php 7.1 版本:

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
#!/bin/sh
 
 
param=$1
 
start()
{
    fpms=`ps aux | grep -i "php-fpm" grep -v grep awk '{print $2}'`
    if [ ! -n "$fpms" ]; then
        php-fpm
        echo "PHP-FPM Start"
    else
        echo "PHP-FPM Already Start"
    fi
}
 
stop()
{
    fpms=`ps aux | grep -i "php-fpm" grep -v grep awk '{print $2}'`
    echo $fpms | xargs kill -9
 
    for pid in $fpms; do
        if echo $pid | egrep -q '^[0-9]+$'then
            echo "PHP-FPM Pid $pid Kill"
        else
            echo "$pid IS Not A PHP-FPM Pid"
        fi
    done
}
 
case $param in
    'start')
        start;;
    'stop')
        stop;;
    'restart')
        stop
        start;;
    *)
        echo "Usage: ./phpfpm.sh start|stop|restart";;
esac

brew 1.5 版本之后将 Php 转至 core 维护。

1)初叶安装 mysql 的风流罗曼蒂克部布满局数据库(比如:information_schema、mysql)

[hprose]extension=/usr/local/Cellar/php@7.1/7.1.17/pecl/20160303/hprose.so

OK,php-fpm安装完成。

echo 'export PATH="/usr/local/opt/php@7.1/bin:$PATH"' >> ~/.zshrcecho 'export PATH="/usr/local/opt/php@7.1/sbin:$PATH"' >> ~/.zshrc
1
mysql -u root -p

Today I’d like to announce Homebrew 1.6.0. The most significant changes since 1.5.0 are `brew install python` installing Python 3, the deprecation of Homebrew/php and various formula API deprecations.

1
source ./.profile

设置后,brew 会自动在php.ini 中增添扩充,这样特别不便利管理和对扩充举办配置,大家删除 /usr/local/etc/php/7.1/php.ini 中 brew 增添的恢弘,在首先行 -__-!! .

1
2
3
4
5
Error: SHA256 mismatch
Expected: 3fe780e5179e90c4d37276e79acc0d0692f1bc0911985af694b92c664c0ef3c4
Actual: f9dbbb4e5ecd98010a3c4686d0713dcda6a77223fb9d05537089b576ab8f7fdd
Archive: /Library/Caches/Homebrew/php54-5.4.28
To retry an incomplete download, remove the file above.

/usr/local/etc/php/7.1/conf.d/ext-xxtea.ini

用了3年多的图书罢工,近来新动手了后生可畏台 rmbp,一批专门的学问情状要配置,LNMP 里的 NMP 是正规要安装的,适逢其会也是第二回在 mac 上设置配备 nginx、mysql、php,所以顺便做个记录,免得以后忘了能够查看,不用随地翻。

添加原委:

1
export PATH="$(brew --prefix php54)/bin:$PATH"
vim /usr/local/etc/php/7.1/conf.d/ext-hprose.inivim /usr/local/etc/php/7.1/conf.d/ext-xxtea.inivim /usr/local/etc/php/7.1/conf.d/ext-swoole.ini
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
[client]
port = 3306
 
 
 
[mysqld]
port = 3306
server_id = 1
character-set-server = utf8
default-storage-engine = INNODB
socket = /tmp/mysql.sock
skip-external-locking
 
key_buffer = 16K
query_cache_limit = 256K
query_cache_size = 4M
max_allowed_packet = 1M
table_open_cache = 128          # 表缓存的数目,一般是 max_connections 的倍数
 
max_connections = 32
thread_concurrency = 2
 
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
 
#skip-bdb
 
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
 
 
 
[mysqldump]
quick
max_allowed_packet = 16M
 
 
 
[mysql]
no-auto-rehash
default-character-set = utf8
 
 
[isamchk]
key_buffer = 8M
sort_buffer_size = 8M
 
 
 
[myisamchk]
key_buffer = 8M
sort_buffer_size = 8M
 
 
 
[mysqlhotcopy]
interactive-timeout

/usr/local/etc/php/7.1/conf.d/ext-hprose.ini

为了方便,写了个运营、关闭、重启 php-fpm 的 shell 脚本: