全新安装Mac OS Sierra (10.12)并使用HomeBrew安装ZSH + MNMP (Mac + Nginx + Mysql + Php) 开发环境(二)

安装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> 

安装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.12)并使用HomeBrew安装ZSH + MNMP (Mac + Nginx + Mysql + Php) 开发环境(三)

你可能感兴趣的:(全新安装Mac OS Sierra (10.12)并使用HomeBrew安装ZSH + MNMP (Mac + Nginx + Mysql + Php) 开发环境(二))