Mac OS上搭建LNMP开发环境

安装Homebrew

安装Homebrew

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

检查是否存在冲突

brew doctor

对Homebrew更新升级

brew update && brew upgrade


安装Nginx

安装nginx

brew install nginx

安装oh-my-zsh

mac 自带zsh,在终端中输入cat/etc/shells

# List of acceptable shells for chpass(1).

# Ftpd will not allow users to connect who are not using

# one of these shells.

/bin/bash

/bin/csh

/bin/ksh

/bin/sh

/bin/tcsh

/bin/zsh

输入zsh --version,可以查看zsh版本。

输入brew install zsh,可以安装最新版的zsh。

输入 chsh -s /bin/zsh,并重启终端,可以将系统默认的终端shell改为zsh。

然后安装oh-my-zsh

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"


安装php-fpm

添加源

brew tap homebrew/dupes

brew tap homebrew/php

安装php-fpm

brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm


将PHP、PHP-FPM加入环境变量

在终端中

vim .zshrc

找到export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"这一行,在其后面添加

homebrew下载php、php-fpm路径配置,使其如下所示

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"

export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"

export PATH="$(brew --prefix homebrew/php/php56)/sbin:$PATH"

$(brew --prefix homebrew/php/php56)/bin是使用homebrew下载的php

$(brew --prefix homebrew/php/php56)/sbin是使用homebrew下载的php-fpm

/usr/bin是MAC自带的php

/usr/sbin是MAC自带php-fpm

然后使其立即生效

source .zshrc

查看$PATH,最前面的路径先后后面的路径

~echo $PATH

/usr/local/opt/php56/sbin:/usr/local/opt/php56/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

查看php、php-fpm版本

~php -v

PHP 5.6.19 (cli) (built: Mar 24 2016 20:55:30) (DEBUG)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

~php-fpm -v

PHP 5.6.19 (fpm-fcgi) (built: Mar 24 2016 20:55:33) (DEBUG)

Copyright (c) 1997-2016 The PHP Group

Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

~/usr/bin/php -v

PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10)

Copyright (c) 1997-2015 The PHP Group

Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

~/usr/sbin/php-fpm -v

PHP 5.5.31 (fpm-fcgi) (built: Feb 20 2016 20:33:26)

Copyright (c) 1997-2015 The PHP Group

Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies

从上面可见,配置的环境变量是正确的,homebrew先于系统自带的执行。

//启动php-fmp

php-fmp

参考

MAC 设置环境变量path的几种方法

mac系统如何显示和隐藏文件

MySQL PHP Nginx Redis Memcache

使用内置的 PHP

安装MySQL

brew install mysql

执行完如上命令后,有如下输出结果

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

mysql_secure_installation

To connect run:

mysql -uroot

To have launchd start mysql at login:

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

Then to load mysql now:

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

Or, if you don't want/need launchctl, you can just run:

mysql.server start

从上面的一段文字来看,安装mysql后,用户名是root,但是没有密码。如果设置密码的话,执行mysql_secure_installation命令。如果不想设置密码的话,可以执行mysql.server start,然后mysql -uroot,如下图所示

nhf:~ nanhangfei$mysql.server start

Starting MySQL

. SUCCESS!

nhf:~ nanhangfei$ mysql -uroot

Welcome to the MySQL monitor.Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.11 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>

如果想要设置root的密码的话,执行下面的命令,做些相关的设置。

mysql_secure_installation


安装phpmyadmin

brew install phpmyadmin


配置Nginx

首先,为我们的配置文件创建一些文件夹

mkdir -p /usr/local/etc/nginx/logs

mkdir -p /usr/local/etc/nginx/sites-available

mkdir -p /usr/local/etc/nginx/sites-enabled

mkdir -p /usr/local/etc/nginx/conf.d

mkdir -p /usr/local/etc/nginx/ssl

sudo mkdir -p /var/www

sudo chown :staff /var/www

sudo chmod 775 /var/www


修改nginx配置文件

vim /usr/local/etc/nginx/nginx.conf

替换其内容为

worker_processes  1;

error_log  /usr/local/etc/nginx/logs/error.log debug;

events {

worker_connections 256;

}

http {

include            mime.types;

default_type        application/octet-stream;

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log  /usr/local/etc/nginx/logs/access.log  main;

sendfile            on;

keepalive_timeout  65;

index index.html index.php;

include /usr/local/etc/nginx/sites-enabled/*;

}


然后创建php-fpm配置文件:

vim /usr/local/ect/nginx/conf.d/php-fpm

其内容如下,这个文件的意思是nginx中的php文件交由php-fmp解析

location ~ \.php$ {

try_files      $uri = 404;

fastcgi_pass  127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

include        fastcgi_params;

}

搞不清FastCgi与PHP-fpm之间是个什么样的关系


创建默认虚拟主机default:

vim /usr/local/etc/nginx/sites-available/default

其内容如下所示

server {

listen      80;

server_name  localhost;

root      /var/www/;

access_log  /usr/local/etc/nginx/logs/default.access.log  main;

location / {

include  /usr/local/etc/nginx/conf.d/php-fpm;

}

location = /info {

allow  127.0.0.1;

deny    all;

rewrite (.*) /.info.php;

}

error_page  404    /404.html;

error_page  403    /403.html;

}


创建ssl默认虚拟主机default-ssl

vim /usr/local/etc/nginx/sites-available/default-ssl

其内容如下所示:

server {

listen      443;

server_name  localhost;

root      /var/www/;

access_log  /usr/local/etc/nginx/logs/default-ssl.access.log  main;

ssl                  on;

ssl_certificate      ssl/localhost.crt;

ssl_certificate_key  ssl/localhost.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;

location / {

include  /usr/local/etc/nginx/conf.d/php-fpm;

}

location = /info {

allow  127.0.0.1;

deny    all;

rewrite (.*) /.info.php;

}

error_page  404    /404.html;

error_page  403    /403.html;

}


创建phpmyadmin虚拟主机

vim /usr/local/etc/nginx/sites-available/phpmyadmin

其内容如下所示

server {

listen      306;

server_name  localhost;

root    /usr/local/share/phpmyadmin;

error_log  /usr/local/etc/nginx/logs/phpmyadmin.error.log;

access_log  /usr/local/etc/nginx/logs/phpmyadmin.access.log main;

ssl                  on;

ssl_certificate      ssl/phpmyadmin.crt;

ssl_certificate_key  ssl/phpmyadmin.key;

ssl_session_timeout  5m;

ssl_protocols  SSLv2 SSLv3 TLSv1;

ssl_ciphers  HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers  on;

location / {

index  index.html index.htm index.php;

include  /usr/local/etc/nginx/conf.d/php-fpm;

}

}


设置SSL

mkdir-p /usr/local/etc/nginx/ssl

openssl req -new -newkey rsa:4096-days365-nodes -x509 -subj"/C=US/ST=State/L=Town/O=Office/CN=localhost"-keyout /usr/local/etc/nginx/ssl/localhost.key -out /usr/local/etc/nginx/ssl/localhost.crt

openssl req -new -newkey rsa:4096-days365-nodes -x509 -subj"/C=US/ST=State/L=Town/O=Office/CN=phpmyadmin"-keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt


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

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

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

ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin


Mac OS上搭建LNMP开发环境

MySQL PHP Nginx Redis Memcache

安装php-fpm、nginx、mysql、phpmyadmin是参考的这两篇文章, 但在安装过程中,会出现几个问题,但原文章并没有说明。下面是碰到的问题及解决方案。

启动php-fmp、nginx、mysql


php-fmp

确认php-fpm监听9000端口

~sudo lsof -Pni4 | grep LISTEN | grep php

Password:

php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)


启动nginx

sudo nginx

重新加载配置|重启|停止|退出

nginx -s reload|reopen|stop|quit

确认nginx在监听80端口

~sudo lsof -Pni4 | grep LISTEN | grep nginx

或者

index.html复制到nginx的根目录/var/www中去

~curl -IL http://localhost/index.html

HTTP/1.1 200 OK

Server: nginx/1.8.1

Date: Sun, 27 Mar 2016 03:18:30 GMT

Content-Type: text/html

Content-Length: 611

Last-Modified: Thu, 24 Mar 2016 23:16:35 GMT

Connection: keep-alive

ETag: "56f47553-263"

Accept-Ranges: bytes


启动mysql

mysql.server start

先登录

mysql -uroot -p

确定mysql端口

mysql> show global variables like 'port';

+---------------+-------+

| Variable_name | Value |

+---------------+-------+

| port| 3306|

+---------------+-------+ 


如果安装PHP时出现如下错误

checking if the location of ZLIB install directory is defined... no

configure: error: Cannot find libz

解决方式是在终端中执行如下命令

xcode-select --install

参考 

configure: error: Cannot find libz


启动php-fmp时的问题


~/usr/local/opt/php56/sbin/php-fpm

[26-Mar-2016 12:15:40] NOTICE: PHP message: PHP Warning:PHP Startup: mcrypt: Unable to initialize module

Module compiled with build ID=API20131226,NTS

PHPcompiled with build ID=API20131226,NTS,debug

These options need to match

in Unknown on line 0


Warning:PHP Startup: mcrypt: Unable to initialize module

Module compiled with build ID=API20131226,NTS

PHPcompiled with build ID=API20131226,NTS,debug

These options need to match

in Unknown on line 0

Unknown(0) : Warning - PHP Startup: mcrypt: Unable to initialize module

Module compiled with build ID=API20131226,NTS

PHPcompiled with build ID=API20131226,NTS,debug

These options need to match

[26-Mar-2016 12:15:40] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root

[26-Mar-2016 12:15:40] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

[26-Mar-2016 12:15:40] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)

[26-Mar-2016 12:15:40] ERROR: FPM initialization failed

解决办法,在终端执行如下命令

brew reinstall php56-mcrypt --build-from-source

参考 PHP Warning: PHP Startup: ????????: Unable to initialize module

上一步完成后,又出现如下问题

~sudo /usr/local/opt/php56/sbin/php-fpm

Password:

[26-Mar-2016 12:40:10] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)

[26-Mar-2016 12:40:10] ERROR: FPM initialization failed


查看谁占用了9000端口

~sudo lsof -i:9000

COMMAND PID USERFDTYPEDEVICE SIZE/OFF NODE NAME

php-fpm 707 root7uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

php-fpm 708 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

php-fpm 709 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

php-fpm 710 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)

杀掉进程id 707、708、709、710

~sudo kill -9 707

~sudo kill -9 708

~sudo kill -9 709

~sudo kill -9 710


~sudo /usr/local/opt/php56/sbin/php-fpm

[26-Mar-2016 12:55:29] NOTICE: fpm is running, pid 32706

[26-Mar-2016 12:55:29] NOTICE: ready to handle connections


确认php-fpm监听9000端口

~sudo lsof -Pni4 | grep LISTEN | grep php

Password:

php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)

~


~php-fpm -D

[25-Mar-2016 20:23:28] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)

[25-Mar-2016 20:23:28] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'

[25-Mar-2016 20:23:28] ERROR: FPM initialization failed

~

解决方法: 

php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf

又出现了如下错误

~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf

[25-Mar-2016 20:24:52] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)

[25-Mar-2016 20:24:52] ERROR: failed to post process the configuration

[25-Mar-2016 20:24:52] ERROR: FPM initialization failed

~

vim /usr/local/etc/php/5.6/php-fpm.conf

然后添加,error_log = /usr/local/var/log/php-fpm.log 保存

~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf

[25-Mar-2016 20:29:54] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root

[25-Mar-2016 20:29:54] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root

[25-Mar-2016 20:29:54] NOTICE: fpm is running, pid 1026

[25-Mar-2016 20:29:54] NOTICE: ready to handle connections

参考

拣点芝麻-Mac Nginx Php配置

Nginx的网站目录

配置文件路径: /usr/local/etc/nginx/nginx.conf

默认的网站根目录: /usr/local/opt//nginx/html

启动nginx问题

nginx: [error] open() “/usr/local/var/run/nginx.pid” failed

参考http://www.inbeijing.org/archives/1290

~sudo nginx

nginx: [warn] 1024 worker_connections exceed open file resource limit: 256

因为ulimit -n的值为256,所以将/usr/local/etc/nginx/nginx.conf配置文件中的worker_connections设置为256

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342

推荐阅读更多精彩内容