Mac下用brew搭建LNMP开发环境

转载自: http://www.jianshu.com/p/164490d5e40f

Brew

brew对于mac,就像apt-get对于ubuntu,安装软件的好帮手,不能方便更多...

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

自启动设置

To reload nginx after an upgrade:
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

启动关闭Nginx的命令如下(如果想要监听80端口,必须以管理员身份运行)

#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit 
#测试配置是否有语法错误
nginx -t

配置Nginx

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

修改Nginx配置文件

vim nginx.conf

主要修改位置是最后的include

worker_processes  1;  
    error_log       /usr/local/var/log/nginx/error.log warn;
    pid        /usr/local/var/run/nginx.pid;
    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/var/log/nginx/access.log main;
        port_in_redirect off;
        sendfile        on; 
        keepalive_timeout  65; 
        include /usr/local/etc/nginx/conf.d/*.conf;
    }

修改自定义文件

vim ./conf.d/default.conf

增加一个监听端口

server {
    listen       80;
    server_name  localhost;

    root /Users/username/Sites/; # 该项要修改为你准备存放相关网页的路径

    location / { 
        index index.php;
        autoindex on; 
    }   

    #proxy the php scripts to php-fpm  
    location ~ \.php$ {
        include /usr/local/etc/nginx/fastcgi.conf;
        fastcgi_intercept_errors on; 
        fastcgi_pass   127.0.0.1:9000; 
    }   

}

这个时候还不能访问php站点,因为还没有开启php-fpm。

PHP

PHP在mac下默认安装了,但是不好控制版本,利用brew可以再mac下安装最新版本,甚至是多个版本,我装了php5.5

brew update
    brew tap homebrew/dupes
    brew tap josegonzalez/homebrew-php
    brew install php55 --with-fpm

然后修改php的cli路径和apache使用的php模块。在.bashrc或.zshrc里头加以下内容

vi ~/.bashrc
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"

用刚刚安装的php代替了系统默认cli的php版本。

自启动设置

To reload php55 after an upgrade:
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist

后面会用到mongo和mysql,所以可以直接利用下面命令安装php模块,其他模块也类似

#brew install php55-mysql #这个已经不需要再安装了
brew install php55-mongo

MySQL

mac不自带mysql,这里需要重新安装,方法依然很简单

brew install mysql
    unset TMPDIR
    mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
    sudo chown -R 777 /usr/local/var/mysql/

第一句是安装,后面的是确保正常使用。然后是启动命令

mysql.server start

自启动设置

To reload mysql after an upgrade:
    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

最好给mysql设个密码,方法如下

mysqladmin -u root password 'xxx'

如果想修改mysql的配置,在/usr/local/etc下建立一个my.cnf,例如增加log

[mysqld]
general-log
general_log_file = /usr/local/var/log/mysqld.log

MongoDB

MongoDB可以说是最简单的一个,直接执行

brew install mongodb

启动方法

mongod --fork

相关推荐