Mac下用Homebrew安装配置nginx+php+mysql(最新实践版)

首先安装必要插件

1 安装Xcode Command Line Tools

xcode-select --install

注意

  1. 确保这步要完成,也可以直接安装xcode,装完之后要先打开一次
  2. Tools 包含了 mac 下所有的常用开发包。

2 安装XQUARZ(可忽略)

[https://www.xquartz.org](https://www.xquartz.org)

curl http://xquartz-dl.macosforge.org/SL/XQuartz-2.7.5.dmg -o /tmp/XQuartz.dmg
open /tmp/XQuartz.dmg

3 可以安装Homebrew了

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

4 成功后检查一下

brew doctor

5 更新到最新版本

brew update 
brew upgrade

注意: 类似 Centos 下的 yum 和 ubuntu 下的 apt-get

安装PHP

1 准备工作

因为brew默认的formula中没有PHP-FPM,所以需要添加一下

brew tap homebrew/dupes
brew tap homebrew/php

2 开始安装

brew install --without-apache --with-fpm --with-mysql php70
(如果安装 7.1就是 php71)
正常情况下生成很多摘要,如下

==> ./configure --prefix=/usr/local/Cellar/php70/7.0.1 --localstatedir=/usr/local/var --sysconfdir=/usr/local/etc/php/7.0
==> make
==> make install
==> /usr/local/Cellar/php70/7.0.3/bin/pear config-set php_ini /usr/local/etc/php/7.0/php.ini
==> Caveats
The php.ini file can be found in:
/usr/local/etc/php/7.0/php.ini
✩✩✩✩ PEAR ✩✩✩✩

If PEAR complains about permissions, 'fix' the default PEAR permissions and config:
chmod -R ug+w /usr/local/Cellar/php70/7.0.3/lib/php
pear config-set php_ini /usr/local/etc/php/7.0/php.ini

✩✩✩✩ Extensions ✩✩✩✩

If you are having issues with custom extension compiling, ensure that
you are using the brew version, by placing /usr/local/bin before /usr/sbin in your PATH:

  PATH="/usr/local/bin:$PATH"
PHP56 Extensions will always be compiled against this PHP. Please install them
using --without-homebrew-php to enable compiling against system PHP.

✩✩✩✩ PHP CLI ✩✩✩✩

If you wish to swap the PHP you use on the command line, you should add the following to ~/.bashrc,
~/.zshrc, ~/.profile or your shell's equivalent configuration file:

  export PATH="$(brew --prefix homebrew/php/php70)/bin:$PATH"
✩✩✩✩ FPM ✩✩✩✩

To launch php-fpm on startup:

* If this is your first install:
    mkdir -p ~/Library/LaunchAgents
    cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
* If this is an upgrade and you already have the homebrew.mxcl.php70.plist loaded:
    launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
    cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php56.plist ~/Library/LaunchAgents/
    launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
The control script is located at /usr/local/Cellar/php70/7.0.3/sbin/php70-fpm

Mountain Lion comes with php-fpm pre-installed, to ensure you are using the brew version you need to make sure /usr/local/sbin is before /usr/sbin in your PATH:

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

You may also need to edit the plist to use the correct "UserName".

Please note that the plist was called 'homebrew-php.josegonzalez.php56.plist' in old versionsof this formula.

To have launchd start php70 at login:
ln -sfv /usr/local/opt/php70/*.plist ~/Library/LaunchAgents
Then to load php70 now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php70.plist
==> Summary
🍺 /usr/local/Cellar/php70/7.0.3: 500 files, 41M, built in 4.1 minutes

3 参数调整

设置自定义php.ini文件
mkdir /usr/local/etc/php/7.0/conf.d
vim /usr/local/etc/php/7.0/conf.d/myphp.ini
把参数写进去
post_max_size = 100M
upload_max_filesize = 100M
max_execution_time = 120
short_open_tag = On

opcache
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

4 开机启动

版本参数根据自己实际版本配置。按 tab补全即可。

按以上摘要的内容,设置开机启动
1. mkdir -p ~/Library/LaunchAgents

2. cp /usr/local/Cellar/php70/7.0.3/homebrew.mxcl.php70.plist ~/Library/LaunchAgents/

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

5 检查端口

这时可以看到php已经运行在9000端口了
lsof -Pni4 | grep LISTEN | grep php

6 PHP组件安装

一般情况下,只要这样就可以了
brew install php70-mongo

安装NGINX

1 开始安装

brew install nginx
#开机启动需要ROOT权限
sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/

sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

2 创建文件夹

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 /data/www
sudo chown :staff /data/www
sudo chmod 775 /data/www

3 conf

vim /usr/local/etc/nginx/nginx.conf
worker_processes 1;
error_log /usr/local/etc/nginx/logs/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/etc/nginx/logs/access.log  main;
sendfile            on;
keepalive_timeout   65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}

4 php-fpm

vim /usr/local/etc/nginx/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;
    fastcgi_param CI_ENV development;
    include fastcgi_params;
}

5 SSL支持

创建一个文件夹

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

生成 4096位 RSA 密钥和自签名:

openssl req -new -newkey rsa:4096 -days 365 -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 -days 365 -nodes -x509 -subj "/C=US/ST=State/L=Town/O=Office/CN=blue.com" -keyout /usr/local/etc/nginx/ssl/blue.com.key -out /usr/local/etc/nginx/ssl/blue.com.crt

6 新建一个默认主机

vim /usr/local/etc/nginx/sites-available/default
server {
listen 80;
server_name localhost;
root /data/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;
}

7 新建一个SSL主机

vim /usr/local/etc/nginx/sites-available/default-ssl
server {
listen 443;
server_name localhost;
root /data/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;
}

把需要启用的主机配置文件链接到sites-enabled目录下:

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

8 启动NGINX

sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

安装mysql

  1. 安装mysql5.7

5.6版本没坑。直接装就行。忽略最后更改密码这步。
在 mysql 官网上下载 dmg包:mysql下载包直达

  1. 配置
    安装完后,会有个弹窗,给出临时密码。

  2. 配置 PATH 变量

    • 打开/user/(你的用户名)/.bash_profile文件
    • 加上export PATH=$PATH:/usr/local/mysql/bin
    • 在终端中输入source ./.profile命令刷新一下
    • 在终端中输入$PATH检查一下
  3. 更改密码

之前的临时密码可以登录一次,更改密码即可。但经常改不了。说权限不足。用以下方法更改。

*  打开一个终端窗口
*  输入 sudo /usr/local/mysql/support-files/mysql.server stop
*  输入 sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables
*  这时不要关窗口,再打开一个终端窗口
*  输入 sudo /usr/local/mysql/bin/mysql -u root
*  这时候会出现mysql>了,输入use mysql
*  最后输入 update user set authentication_string=password('新密码') where user='root';

常用命令

1 NGINX

启动/停止/重启 Nginx:

nginx.start
nginx.stop
nginx.restart

快速查看相关日志:

nginx.logs.access
nginx.logs.default.access
nginx.logs.default-ssl.access
nginx.logs.error

检查配置:

[sudo] nginx -t

2 PHP-FPM

启动/停止/重启 PHP-FPM:

php-fpm.start
php-fpm.stop
php-fpm.restart

检查配置:

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

推荐阅读更多精彩内容