mac使用brew安装nginx php mysql_MAC使用brew安装nginx+php+mysql环境

更新时间:2019-05-14    来源:ruby    手机版     字体:

【www.bbyears.com--ruby】

这两天重新安装了下MAC系统下的nginx+php+mysql环境,今天整理一下安装步骤:

1、安装 homebrew

ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
就这么简单,一条命令就可以完成 homebrew 的安装。

参考资料:http://brew.sh/index_zh-cn.html

2、安装 nginx

brew install nginx
brew 执行完成后,nginx 就安装成功了。

nginx 的操作命令:

#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
#测试配置是否有语法错误
nginx -t
启动 nginx 后,默认的开启的是8080端口,可以通过修改配置文件来设置端口:
vim /usr/local/etc/nginx/nginx.conf
默认访问的目录:
/usr/local/Cellar/nginx/1.4.7/html
我这里安装的nginx版本为1.4.7

安装完 nginx 后,nginx 是不会随机启动的,需要设置一下。

设置 nginx 开机启动:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/nginx/1.4.7/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
设置权限:
sudo chown root:wheel /usr/local/Cellar/nginx/1.4.7/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.4.7/bin/nginx
OK,到这里我们已经成功安装了nginx并设置开机启动。

3、安装mysql

brew install mysql
配置mysql数据库
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
执行完成后就可以在终端中运行 mysql 命令了。

这里需要注意一下,我们可以不需要密码就可以进入 mysql,可以通过一些安全设置、设置用户密码来保证安全性。

设置 mysql 开机启动:

mkdir -p ~/Library/LaunchAgents/
cp /usr/local/Cellar/mysql/5.6.17/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
mysql 配置文件:
/usr/local/Cellar/mysql/5.6.17/my.cnf
至此,mysql 就安装完成了。

4、安装 php

MAC本来就自带了 php,但是很多扩展没有安装,所以选择了重新安装php。

首先,我们需要安装第三方程序包。www.111cn.net

brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
我们可以查看下 brew 下有那些 php 版本
brew search php
brew install php54 --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm
更多的php选项可以通过以下命令查看:
brew options php54
由于是重装php,之前系统预装的php还没卸载,因此在终端调用php时,还是以之前系统的php版本做解析,所以这里需要修改path,指定 php 的解析路径。在~/.bashrc(没有则创建)最后加入一行:
export PATH="$(brew --prefix php54)/bin:$PATH"
执行一下 source 使之生效
source ./.profile
php 配置文件:
/usr/local/etc/php/5.4/php.ini
php-fpm 配置文件:
/usr/local/etc/php/5.4/php-fpm.conf
启动 php-fpm 的话就直接在终端里执行 "php-fpm",默认打开 php-fpm 会显示一个状态 shell 出来,也可以把 php-fpm 的配置文件里的 "daemonize = no" 改为 "daemonize = yes",就会以后台守护进程的方式启动,对于刚修改的配置文件,可以执行 "php-fpm -t" 来检测配置有没有问题。

开机启动php-fpm:

mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/php54/5.4.26/homebrew-php.josegonzalez.php54.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist
5、设置 nginx 的 php-fpm 的配置

下面是自己配置虚拟机的nginx 配置文件:

server {
    listen       80;
    server_name  www.test.com;

    root   /Users/sumw/www/www.test.com;
    index  index.html index.htm index.php;

    location / {
            try_files $uri $uri/ /index.php;
    }

    location ~ .php$ {
            try_files $uri =404;

            include fastcgi.conf;
            fastcgi_pass 127.0.0.1:9000;
    }

}
至此,我们的 nginx+php+mysql 环境就安装完成了。

不过好像这里有个问题,MAC开机重启后,php又恢复到了自带的php版本,解决方法:

调整一下path顺序:

www.111cn.net

ls -al /usr/local/bin/php
/usr/local/bin/php -> ../Cellar/php54/5.4.19/bin/php
把/usr/local/bin调整到第一位
cat ~/.bash_profile
export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

本文来源:http://www.bbyears.com/jiaocheng/51102.html