Mac下安裝LNMP(Nginx+PHP5.6)環境(轉)

安裝Homebrew

最近工做環境切換到Mac,因此以OS X Yosemite(10.10.1)爲例,記錄一下從零開始安裝Mac下LNMP環境的過程php

確保系統已經安裝xcode,而後使用一行命令安裝依賴管理工具Homebrewhtml

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

以後就可使用node

brew install FORMULA

來安裝所須要的依賴了。mysql

brew(意爲釀酒)的命名頗有意思,所有都使用了釀酒過程當中採用的材料/器具,名詞對應如下的概念:nginx

  • Formula(配方) 程序包定義,本質上是一個rb文件
  • Keg(桶)程序包的安裝路徑
  • Cellar(地窖)全部程序包(桶)的根目錄
  • Tap(水龍頭)程序包的源
  • Bottle (瓶子)編譯打包好的程序包

最終編譯安裝完畢的程序就是一桶釀造好的酒git

更詳細的信息參考Homebrew的官方Cookbookgithub

所以使用Homebrew常見的流程是:web

  1. 增長一個程序源(新增一個水龍頭) brew tap homebrew/php
  2. 更新程序源 brew update
  3. 安裝程序包(按照配方釀酒) brew install git
  4. 查看配置 brew config 能夠看到程序包默認安裝在/usr/local/Cellar下 (酒桶放在地窖內)

安裝PHP5.6(FPM方式)

首先加入Homebrew官方的幾個軟件源redis

brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/php

PHP若是採用默認配置安裝,會編譯mod_php模塊並只運行在Apache環境下,爲了使用Nginx,這裏須要編譯php-fpm而且禁用apache,主要經過參數--without-fpm --without-apache來實現。完整的安裝指令爲sql

brew install php56 \
--without-snmp \
--without-apache \
--with-debug \
--with-fpm \
--with-intl \
--with-homebrew-curl \
--with-homebrew-libxslt \
--with-homebrew-openssl \
--with-imap \
--with-mysql \
--with-tidy

因爲OSX已經自帶了PHP環境,所以須要修改系統路徑,優先運行brew安裝的版本,在~/.bashrc里加入:

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

若是要安裝新的php擴展,能夠直接安裝而不用每次從新編譯php,全部的擴展能夠經過

brew search php56

看到,下面是我本身所須要的擴展,能夠支持Phalcon框架

brew install php56-gearman php56-msgpack php56-memcache php56-memcached php56-mongo  php56-phalcon php56-redis php56-xdebug

PHP-FPM的加載與啓動

安裝完畢後能夠經過如下指令啓動和中止php-fpm

php-fpm -D
killall php-fpm

同時能夠將php-fpm加入開機啓動

ln -sfv /usr/local/opt/php56/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php56.plist

安裝Nginx

brew install nginx

安裝完畢後能夠經過

nginx
nginx -s quit

啓動和關閉,同時也支持重載配置文件等操做

nginx -s reload|reopen|stop|quit

nginx安裝後默認監聽8080端口,能夠訪問http://localhost:8080查看狀態。若是要想監聽80端口須要root權限,運行

sudo chown root:wheel /usr/local/Cellar/nginx/1.6.2/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.2/bin/nginx

並使用root權限啓動

sudo nginx

開機啓動

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Nginx + PHP-FPM配置

Nginx通常都會運行多個域名,所以這裏參考了@fish的方法,按Ubuntu的文件夾結構來存放Nginx的配置文件

mkdir -p /usr/local/var/logs/nginx
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

編輯Nginx全局配置

vim /usr/local/etc/nginx/nginx.conf
worker_processes  1;
error_log   /usr/local/var/logs/nginx/error.log debug;
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" $host $request_time $upstream_response_time $scheme '
        '$cookie_evalogin';

    access_log  /usr/local/var/logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

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

這樣一來首先能夠把一些可複用配置獨立出來放在/usr/local/etc/nginx/conf.d下,好比fastcgi的設置就能夠獨立出來

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_intercept_errors    on;
    include /usr/local/etc/nginx/fastcgi.conf;
}

而後/usr/local/etc/nginx/sites-enabled目錄下能夠一個文件對應一個域名的配置,好比web服務器目錄是/opt/htdocs

vim /usr/local/etc/nginx/sites-enabled/default
server {
    listen       80;
    server_name  localhost;
    root         /opt/htdocs/;

    location / {
        index  index.html index.htm index.php;
        include     /usr/local/etc/nginx/conf.d/php-fpm;
    }
}

此時啓動了php-fpm而且啓動了Nginx後,就能夠經過http://localhost來運行php程序了

安裝MySQL

brew install mysql

能夠經過

mysql.server start
mysql.server stop

來啓動/中止,啓動後默認應爲空密碼,能夠經過mysqladmin設置一個密碼

mysqladmin -uroot password "mypassword"

可是在操做的時候出現了空密碼沒法登入的狀況,最終只能經過mysqld_safe來設置

sudo mysqld_safe --skip-grant-tables
mysql -u root
mysql> UPDATE mysql.user SET Password=PASSWORD('mypassword') WHERE User='root';
mysql> FLUSH PRIVILEGES;

最後將MySQL加入開機啓動

cp /usr/local/Cellar/mysql/5.6.22/homebrew.mxcl.mysql.plist ~/Library/LaunchAgents/

Memcache

brew install memcached

啓動/中止指令

memcached -d
killall memcached

加入開機啓動

cp /usr/local/Cellar/memcached/1.4.20/homebrew.mxcl.memcached.plist ~/Library/LaunchAgents/

Redis

brew install redis

Redis默認配置文件不容許以Deamon方式運行,所以須要先修改配置文件

vim /usr/local/etc/redis.conf

將daemonize修改成yes,而後載入配置文件便可實現後臺進程啓動

redis-server /usr/local/etc/redis.conf

加入開機啓動

cp /usr/local/Cellar/redis/2.8.19/homebrew.mxcl.redis.plist ~/Library/LaunchAgents/

設置別名

最後能夠對全部服務的啓動中止設置別名方便操做

vim ~/.bash_profile

加入

alias nginx.start='launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.stop='launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist'
alias nginx.restart='nginx.stop && nginx.start'
alias php-fpm.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-fpm.restart='php-fpm.stop && php-fpm.start'
alias mysql.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist"
alias mysql.restart='mysql.stop && mysql.start'
alias redis.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.redis.plist"
alias redis.restart='redis.stop && redis.start'
alias memcached.start="launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist"
alias memcached.restart='memcached.stop && memcached.start'

安裝其餘項目支持

brew install composer node

安裝Oh My Zsh

brew install zsh-completions
chsh -s /usr/local/bin/zsh
vim ~/.zshenv

加入內容

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

而後

vim ~/.zshrc

加入內容

fpath=(/usr/local/share/zsh-completions $fpath)
autoload -Uz compinit
compinit -u

最後運行

rm -f ~/.zcompdump; compinit

查看正在使用的shell

dscl localhost -read Local/Default/Users/$USER UserShell

安裝Oh My Zsh

wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | sh

參考

相關文章
相關標籤/搜索