Mac搭建LNMP环境笔记

Brew

brew的安装方式如下:

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

brew常用选项

brew install xxx

brew uninstall xxx

brew list

brew update xxx


Nginx

使用Nginx也比较方便,首先安装

brew install nginx

使用80端口,需要将nginx加入root组当中:

默认目录,根据实际版本号填写

sudo chown root:wheel /usr/local/Cellar/nginx/1.12.1/bin/nginx

sudo chmod u+s /usr/local/Cellar/nginx/1.12.1/bin/nginx


nginx配置

nginx.conf文件配置

cd /usr/local/etc/nginx/

vi nginx.conf

worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log debug;

events {

worker_connections  1024;

}

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/var/log/nginx/access.log  main;

sendfile            on;

keepalive_timeout  65;

index index.html index.php;

include /usr/local/etc/nginx/servers/*.conf;

}

nginx目录下新建conf.d文件夹

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

conf.d里新建php-fpm配置

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;

}

servers文件夹里配置站点文件

server {

listen      80;

server_name  work.love;

root        /Users/admin/works/blog/public/;

access_log  /usr/local/var/log/nginx/host.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;

}

加入launchctl启动控制

mkdir -p ~/Library/LaunchAgents

cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

到这里,nginx基本上是完工了

运行nginx :

sudo nginx #打开 nginx

nginx -s reload|reopen|stop|quit  #重新加载配置|重启|停止|退出 nginx

nginx -t  #测试配置是否有语法错误


php

php 的安装相对nginx和mysql来说,要复杂点,因为brew 默认没有php的包

brew tap homebrew/dupes

brew tap homebrew/versions

brew tap homebrew/homebrew-php

关闭老版本的PHP进程

brew unlink php56

执行完上面命令后,这个时候才可以安装php,不过php有很多的版本,你可以用:

brew search php

来查看具体的版本

开始安装PHP7

brew install php71

开启PHP71进程

brew link php71

输入命令,查看是否成功

php -v

添加系统环境变量PATH来方便我们的使用

进入配置文件

sudo vim ~/.bash_profile

修改默认PHP版本

export PATH="$(brew --prefix php71)/bin:$PATH"

export PATH="$(brew --prefix php71)/sbin:$PATH"

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

终端运行

source ~/.bash_profile

注意:  如果你用的是zsh,那么你加入的配置文件是

~/.zshrc

配置php-fpm,修改php-fpm.conf文件

/usr/local/etc/php/7.1/php-fpm.conf


;pid = run/php-fpm.log

;error_log = log/php-fpm.log

修改为

pid = /usr/local/var/run/php-fpm.pid

error_log = /usr/local/var/log/php-fpm.log

启动php-fpm

sudo /usr/local/Cellar/php71/7.1.8_20/sbin/php-fpm -D

停止php-fpm

sudo killall php-fpm

安装扩展memcached

brew install php71-memcached

配置文件

/usr/local/etc/php/7.0/conf.d/ext-memcached.ini

安装扩展redis

brew install php71-redis

配置文件

/usr/local/etc/php/7.0/conf.d/ext-redis.ini

更多扩展通过以下命令搜索

brew search php

进行安装

brew install 扩展名

设置php-fpm的开机自启动

mkdir -p ~/Library/LaunchAgents

cp /usr/local/opt/php71/homebrew.mxcl.php71.plist ~/Library/LaunchAgents/

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php71.plist

输入命令查看php-fpm是否启动

lsof -Pni4|grep LISTEN | grep php

php版本

php -v

~/usr/local/opt/php71/bin/ -v

php-fpm版本

php-fpm -v

~/usr/local/opt/php71/sbin/php-fpm -v

配置文件

安装好后生成的配置文件都在 /usr/local/etc/php/7.0 目录里,分别如下:

php.ini

/usr/local/etc/php/7.1/php.ini

php-fpm.conf

/usr/local/etc/php/7.1/php-fpm.conf

php, phpize, php-config

ls /usr/local/opt/php71/bin

php-fpm

/usr/local/opt/php71/sbin/php-fpm


mysql

官网下载mac安装包安装

安装完成后终端命令

vi ~/.bash_profile

添加以下变量

PATH=$PATH:/usr/local/mysql/bin

Mac 下安装MySQL后, shell不能识别MySQL命令

MySQL -V

shell显示 command not found: mysql

原因是我用的zsh  输入以下命令完美解决

vi ~/.zshrc  插入  source ~/.bash_profile

终端执行 

source ~/.zshrc

为了方便使用,我们经常会创建任意连接的root用户:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MyNewPass4!' WITH GRANT OPTION;

刷新权限使命令生效:

flush privileges;

```

你可能感兴趣的:(Mac搭建LNMP环境笔记)