怎样在MAC下通过brew安装LNMP

摘要:摘要: 介绍如何在MAC下安装LNMP环境

在开始介绍怎样在MAC中安装LNMP环境之前,我只想说一句话,这个过程主要看人品,有的人不用浪费太多时间就能很轻松的完成,但是某些人(例如:myself)实在是一个不愿回忆的惨痛时光。好了不说废话,切入主题......

 

1 (71).jpg

 

一、安装Homebrew

Homebrew是一个依赖管理工具,通过brew命令就可以快速的帮助你安装工具。安装brew命令行之前要确保你的电脑上已经安装了Xcode,然后执行以下命令:

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

安装完成之后,在命令行中输入

username$brew #下面是返回的内容 Eampleusage: brew[info|home|options][FORMULA...] brewinstallFORMULA... brewuninstallFORMULA... brewsearch[foo] brewlist[FORMULA...] brewupdate brewupgrade[FORMULA...] brewpin/unpin[FORMULA...] Troubleshooting: brewdoctor brewinstall-vdFORMULA brew[--env|config] Brewing: brewcreate[URL[--no-fetch]] brewedit[FORMULA...] https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Formula-Cookbook.md Furtherhelp: manbrew brewhome

这是你就可以根据返回的帮助信息进行下一步操作了,再此就不一一介绍每个命令了。只重点说一下 install和unstall

#安装FORMULA==需要安装的工具名称如:php、mysql、nginx username$brewinstallFORMULA #卸载FORMULA==需要安装的工具名称如:php、mysql、nginx username$brewuninstallFORMULA

 

二、安装PHP

首先,添加Homebrew官方的几项软件源。

brewtaphomebrew/dupes brewtaphomebrew/versions brewtaphomebrew/php

安装的时候可以根据需要自己的需要安装不同版本的PHP环境,网上大多说安装的说5.6和5.5.

使用以下命令就可以安装PHP环境(php56 => PHP5.6),配置项可以自己制定(--with-debug 可以考虑不要)。

#安装PHP5.6 brewinstallphp56 --without-snmp --without-apache --with-debug --with-fpm --with-intl --with-homebrew-curl --with-homebrew-libxslt --with-homebrew-openssl --with-imap --with-mysql --with-tidy

在安装的过程中可能会出现报错,如下

#出现问题 checkingforZLIBsupport...yes checkingifthelocationofZLIBinstalldirectoryisdefined...no configure:error:Cannotfindlibz

笔者搜索了网上的一些资料,根据https://github.com/Homebrew/homebrew-php/issues/2549上介绍的解决方法,执行一条命令就可以解决这个报错

xcode-select--install

除了这个报错之外,还会遇到跟中权限问题,主要是文件夹没有写入权限,根据提示的文件夹,逐一进行修改即可。

建议使用b "rew doctor" 命令进行错误检查,它会返回一些很有价值的问题信息,并有解决问题的提示。

MAC都会自动带有PHP和Apache环境,因此我们要添加环境变量,优先执行我们使用brew安装的PHP环境。

#增加环境变量 username$echo\'exportPATH="/usr/local/bin:/usr/local/sbib:$PATH"\'>>~/.bash_profile username$source~/.bash_profile

在安装PHP5.6环境后(如果幸运的你能顺利的来到这一步),就要继续安装PHP的扩展了,可以直接安装扩展,无需重新编译PHP,我们可以通过

#使用brew查找PHP username$brewsearchphp56

此命令可以帮助我们查看当前PHP5.6环境下有哪些扩展可以安装,这样我们就可以自主选择扩展进行安装。下面是我安装的扩展。

#安装PHP扩展 username$brewinstallphp56-gearmanphp56-msgpackphp56-memcachephp56-memcachedphp56-mongophp56-phalconphp56-redisphp56-xdebug

在这个过程之中,可能会遇到一个错误如下,导致redis安装失败

Error:Nosuchfileordirectory-/usr/local/opt/igbinary/include/igbinary.h

解决方法是取网上下载igbinary-1.1.1.tar

#进入到igbinary-1.1.1.tar所在目录 username$tarzxvfigbinary-1.1.1.tar username$cdigbinary-1.1.1 username$/usr/local/Cellar/php56/5.6.17/bin/phpize username$./configure--with-php-config=/usr/local/Cellar/php56/5.6.17/bin/php-config username$make username$makeinstall username$vim/usr/local/etc/php/php.ini 加上: extensions=igbinary.so

在igbinary-1.1.1文件夹里面会有igbinary.h这个文件,将文件放到错误提示中的目录下即可解决,然后重新安装没有安装完成的扩展。

注意,扩展安装了,那对应的软件也要安装,同样是实用brew命令进行安装。然后使用PHP命令查看一下PHP是否安装成功,它会返回PHP的版本和安装的扩展等信息。

#查看PHP信息 username$php-v

接下来修改php-fpm配置文件,vim /usr/local/etc/php/5.6/php-fpm.conf,找到pid相关大概在25行,去掉注释pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在/usr/local/var/run/php-fpm.pid,下面要安装的Nginx pid文件也放在这里。

我们还可以使用php-fpm命令来测试。

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

启动关闭重启php-fpm

#启动 username$php-fpm-D #关闭 username$kill-INT`cat/usr/local/var/run/php-fpm.pid` #重启 username$php-fpmkill-USR2`cat/usr/local/var/run/php-fpm.pid`

启动php-fpm之后,确保它正常运行监听9000端口

username$lsof-Pni4|grepLISTEN|grepphp php-fpm30907calvin9uIPv40xf11f9e8e8033a2a70t0TCP127.0.0.1:9000(LISTEN) php-fpm30917calvin0uIPv40xf11f9e8e8033a2a70t0TCP127.0.0.1:9000(LISTEN) php-fpm30918calvin0uIPv40xf11f9e8e8033a2a70t0TCP127.0.0.1:9000(LISTEN) php-fpm30919calvin0uIPv40xf11f9e8e8033a2a70t0TCP127.0.0.1:9000(LISTEN) #正常情况,会看到上面这些进程

为了使用方便,将php-fpm设为开机自启动。

username$ln-sfv/usr/local/opt/php56/*.plist~/Library/LaunchAgents launchctlload~/Library/LaunchAgents/homebrew.mxcl.php56.plist

经过一番周折应该可以顺利的完成PHP环境的安装与配置了,如果你在安装的过程中出现了其他问题,请耐心的在网上进行搜索,此文也许只能解决你一部分的问题,good luck!

三、安装Nginx

在经历了安装PHP环境的曲折之后,还没有得到很好的休息,我们又来安装Nginx,说起Nginx还是有有必要多说几句的,一般情况下有些人会使用Mac自带的Apahce,这样就不需要安装Nginx了,但为了更好的开发环境还是建议去安装一下Nginx,有益无害。

同样,我们还说使用brew命令来安装Nginx,命令如下

username$brewinstallnginx

这个安装过程相当顺畅一些,也许还是会出现文件夹权限写入问题,逐一进行排查就能够解决。不要忘记使用 "brew doctor" 去检查是否有错误或警告产生在安装的过程中。

安装完成后去检查Nginx

#测试配置是否有语法错误 username$nginx-t nginx:theconfigurationfile/usr/local/etc/nginx/nginx.confsyntaxisok nginx:configurationfile/usr/local/etc/nginx/nginx.conftestissuccessful

常用的Nginx命令

#打开nginx username$sudonginx #重新加载配置|重启|停止|退出nginx username$nginx-sreload|reopen|stop|quit

Nginx默认监听的端口说8080,而不是通常认为的80,所以要使用root执行权限让Nginx去监听80端口

#将Nginx的端口由8080改为80,1.8.1是我安装的版本,可以根据安装情况去替换 username$sudochownroot:wheel/usr/local/Cellar/nginx/1.8.1/bin/nginx username$sudochmodu+s/usr/local/Cellar/nginx/1.8.1/bin/nginx

将Nginx设为开机自启

#开机自启 user$ln-sfv/usr/local/opt/nginx/*.plist~/Library/LaunchAgents launchctlload~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

到此Nginx安装完成。

四、Nginx+PHP-FPM配置

单单是安装完成Nginx和PHP是不够的,需要将两者结合起来才能满足我们的常规开发模式。

创建需要用到的目录,注意要有权限才可以执行命令。

#创建目录 username$mkdir-p/usr/local/var/logs/nginx username$mkdir-p/usr/local/etc/nginx/sites-available username$mkdir-p/usr/local/etc/nginx/sites-enabled username$mkdir-p/usr/local/etc/nginx/conf.d username$mkdir-p/usr/local/etc/nginx/ssl username$sudomkdir-p/var/www username$sudochown:staff/var/www username$sudochmod775/var/www

接下来配置nginx.conf

#打开配置文件进入编辑状态 username$vim/usr/local/etc/nginx/nginx.conf

输入以下内容并保存

worker_processes1; error_log/usr/local/var/logs/nginx/error.logdebug; pid/usr/local/var/run/nginx.pid; events{ worker_connections256; } http{ includemime.types; default_typeapplication/octet-stream; log_formatmain\'$remote_addr-$remote_user[$time_local]"$request"\' \'$status$body_bytes_sent"$http_referer"\' \'"$http_user_agent""$http_x_forwarded_for"\'; access_log/usr/local/var/logs/access.logmain; sendfileon; keepalive_timeout65; port_in_redirectoff; include/usr/local/etc/nginx/sites-enabled/*; }

设置 Nginx php-fpm 配置文件

#编辑配置文件 username$vim/usr/local/etc/nginx/conf.d/php-fpm #proxythephpscriptstophp-fpm location~.php${ try_files$uri=404; fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; fastcgi_intercept_errorson; include/usr/local/etc/nginx/fastcgi.conf; }

准备Nginx虚拟主机

#创建info.phpindex.html404.html403.html文件到/var/www下面,如果没有写入权限去调整一下 username$vi/var/www/info.php username$vi/var/www/index.html username$vi/var/www/403.html username$vi/var/www/404.html

创建默认虚拟主机

#编辑文件 username$vim/usr/local/etc/nginx/sites-available/default #输入以下内容 server{ listen80; server_namelocalhost; root/var/www/; access_log/usr/local/var/logs/nginx/default.access.logmain; location/{ indexindex.htmlindex.htmindex.php; autoindexon; include/usr/local/etc/nginx/conf.d/php-fpm; } location=/info{ allow127.0.0.1; denyall; rewrite(.*)/.info.php; } error_page404/404.html; error_page403/403.html; }

创建虚拟主机软连接并开启虚拟主机

username$ln-sfv/usr/local/etc/nginx/sites-available/default/usr/local/etc/nginx/sites-enabled/default

这时我们就可以在浏览器中访问我们localhost,去访问我们的主页了(当然,你要事先在index.html/index.php 编辑好内容,才能在localhost中显示出来)。

到此,我们就将Nginx和PHP配置完成,如果发现localhost不能被访问到的话,可以试一下localhost:8080,如果可以输出的话,那么就说明你的80端口没有开放给Nginx。

四、安装MySQL

在整个安装的过程中,个人感觉MySQL比起上面两个环境在安装的过程中出现问题的概率更大一些。我选择的是5.6版本,由于默认的版本的5.7,但是在安装和配置的过程中出现了一些问题没有成功的解决,所以就选择了低一点的版本。

#mysql56表示5.6版本,可以选择其他版本进行安装 username$brewinstallmysql56

整个过程还是比较顺利的,但是不要高兴的太早,如果你用mysql命令的时候,它就会报错,如果没有报错那么恭喜你,下面的内容就可以跳过了并且可以放心去使用mysql了。

#执行mysql命令后的错误提示 username$mysql-uroot-p(初始密码为空,建议修改) ERROR2002(HY000):CannotconnecttolocalMySQLserverthroughsocket\'/tmp/mysql.sock\'(2)

出现这个错误的原因是mysql服务没有启动。

#开始解决上一步出现的错误 username$unsetTMPDIR #然后进入mysql的安装目录 username$cd/usr/local/Cellar/mysql56/5.6.27/ #执行 username$mysql_install_db--verbose--user=`whoami`--basedir="$(brew--prefixmysql)"--datadir=/usr/local/var/mysql--tmpdir=/tmp #检查问题 username$mysqld--initializespecified #如果出现问题,有可能是目录不存在,去创建对应的目录即可 #启动mysql username$mysql.serverstart #进入mysql username$mysql-uroot-p #如果能成功的进入mysql证明mysql已经安装成功了

将mysql设为开机自启

username$mkdir-p~/Library/LaunchAgents username$cp/usr/local/Cellar/mysql56/5.6.27/homebrew.mxcl.mysql56.plist~/Library/LaunchAgents/

一切顺利的话,到了这里你就已经将mysql环境安装完成了。

 

结束语:安装过程中可能会出现各种奇葩的问题,只要有足够的耐心就可以将问题解决,本问仅供参考并不具有权威性,如果可以解决你所遇到的问题,那么非常荣幸。请不要吝啬分享好的文章给其他人。