prepar to install

准备工作

安装必要的Brew,Oh My Zsh等辅助工具

Brew

brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件,和linux系统中的yum非常相似,只需要一个命令, 非常方便。官方网站:http://brew.sh

安装:

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

国内的网络环境极有可能导致安装速度过于缓慢,有时候会让你感觉安装处于假死的状态... 不必担心,耐心等待即可,切不可心烦意乱的关闭终端窗口。

检测:

brew doctor
        

返回“Your system is ready to brew.”就说明你已经成功安装了brew。

常用命令:

brew update                        #更新brew可安装包,建议每次执行一下
brew search php70                  #搜索php7.0
brew tap josegonzalez/php          #安装扩展<gihhub_user/repo>   
brew tap                           #查看安装的扩展列表
brew install php70                 #安装php7.0
brew remove  php70                 #卸载php7.0
brew upgrade php70                 #升级php7.0
brew options php70                 #查看php7.0安装选项
brew info    php70                 #查看php7.0相关信息
brew home    php70                 #访问php7.0官方网站
        

安装Oh My Zsh

目前常用的 Linux 系统和 OS X 系统的默认 Shell 都是 bash,但是真正强大的 Shell 是深藏不露的 zsh, 这货绝对是马车中的跑车,跑车中的飞行车,史称『终极 Shell』,但是由于配置过于复杂,所以初期无人问津,很多人跑过来看看zsh的配置指南,什么都不说转身就走了。直到有一天,国外有个穷极无聊的程序员开发出了一个能够让你快速上手的zsh项目,叫做「oh my zsh」,Github:https://github.com/robbyrussell/oh-my-zsh。这玩意就像「X天叫你学会 PHP」系列,可以让你神功速成,而且是真的。

自动安装

sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
        

安装 Oh My Zsh

homebrew-cask

安装cask:

brew tap phinze/homebrew-cask && brew install brew-cask 
        
cask常用命令:

brew cask search        #列出所有可以被安装的软件
brew cask search php    #查找所有和php相关的应用
brew cask list          #列出所有通过cask安装的软件
brew cask info phpstorm #查看 phpstorm 的信息
brew cask uninstall qq  #卸载 QQ
        

这里谈谈cask对比Mac App Store的优势:

  • 对常用软件支持更全面(特别是开发者),cask里面会给你一些惊喜;
  • 软件更新速度快,一般都是最新版本 Store上很久很久才会更新版本;
  • 命令安装感觉比打开Store方便,另外Store在国内的速度也是XXOO。

安装开发常用的包&软件


brew install wget watch tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf multitail source-highlight autojump zsh-completions sshfs
        
install environment

开始搭建PHP开发环境

安装MySQL PHP Nginx Redis Memcache

前面做了这么多的准备工作,其实 zsh iTerm2 brew 等等这些由于篇幅以及能力有限,一时半会也讲不完,更多选择,更多欢乐,就在Google search,现在才入正题。come on :)

安装MySQL


brew install mysql
        

磨刀不误砍柴工,一行命令即可下载最新版本的MySQL,是不是很方便呢~ 不过MySQL的安装包很大,在这里需要等待一段时间,期间Brew会自动寻找合适的下载源。例如在下面这个示例图中就尝试了两次。

安装MySQL

MySQL开机启动

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
        
初始化MySQL

/usr/local/opt/mysql/bin/mysql_secure_installation
        

注意:如果你输入了上面的命令后提示 Error: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 时,请确保你的MySQL处于开启状态,或者直接运行 brew services start mysql 即可解决。

根据终端提示,输入root密码,然后依次确认一些安全选项。具体信息可以参考外国友人的这篇文章


#查看一下MySQL运行情况
➜  ~  ps aux | grep mysql
calvin           1695   0.0  0.5  2719864  90908   ??  S     1:38上午   0:00.31 /usr/local/Cellar/mysql/5.6.19/bin/mysqld --basedir=/usr/local/Cellar/mysql/5.6.19 --datadir=/usr/local/var/mysql --plugin-dir=/usr/local/Cellar/mysql/5.6.19/lib/plugin --bind-address=127.0.0.1 --log-error=/usr/local/var/mysql/CalvinsMacBook-Pro.local.err --pid-file=/usr/local/var/mysql/CalvinsMacBook-Pro.local.pid --socket=/tmp/mysql.sock --port=3306
calvin           1323   0.0  0.0  2444628   1020   ??  S     1:38上午   0:00.04 /bin/sh /usr/local/opt/mysql/bin/mysqld_safe --bind-address=127.0.0.1 --datadir=/usr/local/var/mysql

#测试连接MySQL
mysql -uroot -p
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 23
Server version: 5.6.19-log Homebrew

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
        

安装phpmyadmin


brew install phpmyadmin
        

安装PHP

添加brew的PHP扩展库:


brew update
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
        

可以使用brew options php56命令来查看安装php5.6的选项,这里我用下面的选项安装:


brew install php56 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql
        

等待PHP编译完成,开始安装PHP常用扩展,扩展安装过程中brew会自动安装依赖包,例如 php56-pdo-pgsql 会自动装上 postgresql ,这里我安装以下PHP扩展:


brew install php55-apcu\
 php55-gearman\
 php55-geoip\
 php55-gmagick\
 php55-imagick\
 php55-intl\
 php55-mcrypt\
 php55-memcache\
 php55-memcached\
 php55-mongo\
 php55-opcache\
 php55-pdo-pgsql\
 php55-phalcon\
 php55-redis\
 php55-sphinx\
 php55-swoole\
 php55-uuid\
 php55-xdebug;
        

由于Mac自带了php和php-fpm,因此需要添加系统环境变量PATH来替代自带PHP版本。


echo 'export PATH="$(brew --prefix php55)/bin:$PATH"' >> ~/.bash_profile  #for php
echo 'export PATH="$(brew --prefix php55)/sbin:$PATH"' >> ~/.bash_profile  #for php-fpm
echo 'export PATH="/usr/local/bin:/usr/local/sbib:$PATH"' >> ~/.bash_profile #for other brew install soft
source ~/.bash_profile
        

测试一下效果:


#brew安装的php 他在/usr/local/opt/php55/bin/php
php -v    
PHP 5.5.14 (cli) (built: Jul 16 2014 15:43:06) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans 

#Mac自带的PHP
/usr/bin/php -v   
PHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

#brew安装的php-fpm 他在/usr/local/opt/php55/sbin/php-fpm
php-fpm -v
PHP 5.5.14 (fpm-fcgi) (built: Jul 16 2014 15:43:12) (DEBUG)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

#Mac自带的php-fpm
/usr/sbin/php-fpm -v
PHP 5.4.24 (fpm-fcgi) (built: Jan 19 2014 21:32:57)
Copyright (c) 1997-2013 The PHP Group
        

修改php-fpm配置文件,vim /usr/local/etc/php/5.6/php-fpm.conf ,找到pid相关大概在25行,去掉注释 pid = run/php-fpm.pid, 那么php-fpm的pid文件就会自动产生在 /usr/local/var/run/php-fpm.pid,下面要安装的Nginx pid文件也放在这里。


#测试php-fpm配置
php-fpm -t
php-fpm -c /usr/local/etc/php/5.6/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -t

#启动php-fpm
php-fpm -D
php-fpm -c /usr/local/etc/php/5.6/php.ini -y /usr/local/etc/php/5.5/php-fpm.conf -D

#关闭php-fpm
kill -INT `cat /usr/local/var/run/php-fpm.pid`

#重启php-fpm
kill -USR2 `cat /usr/local/var/run/php-fpm.pid`

#也可以用上文提到的brew命令来重启php-fpm,不过他官方不推荐用这个命令了
brew services restart php56

#还可以用这个命令来启动php-fpm
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php56.plist
        

启动php-fpm之后,确保它正常运行监听9000端口:


lsof -Pni4 | grep LISTEN | grep php
php-fpm   30907 calvin    9u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30917 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30918 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
php-fpm   30919 calvin    0u  IPv4 0xf11f9e8e8033a2a7      0t0  TCP 127.0.0.1:9000 (LISTEN)
#正常情况,会看到上面这些进程
        

PHP-FPM开机启动:


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

安装Nginx


brew install nginx --with-http_geoip_module
        

Nginx启动关闭命令:


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

#打开 nginx
sudo nginx

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

#也可以使用Mac的launchctl来启动|停止
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
        

Nginx开机启动


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

Nginx监听80端口需要root权限执行,因此:


sudo chown root:wheel /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
sudo chmod u+s /usr/local/Cellar/nginx/1.6.0_1/bin/nginx
        
配置nginx.conf

创建需要用到的目录:


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

vim /usr/local/etc/nginx/nginx.conf 输入以下内容:


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

    sendfile        on;
    keepalive_timeout  65;
    port_in_redirect off;

    include /usr/local/etc/nginx/sites-enabled/*;
}
        
设置nginx php-fpm配置文件

vim /usr/local/etc/nginx/conf.d/php-fpm
#proxy the php scripts to 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;
}
        
nginx虚拟主机准备工作

#创建 info.php index.html 404.html 403.html文件到 /var/www 下面
vi /var/www/info.php
vi /var/www/index.html
vi /var/www/403.html
vi /var/www/404.html
        
创建默认虚拟主机default

vim /usr/local/etc/nginx/sites-available/default 输入:


server {
    listen       80;
    server_name  localhost;
    root         /var/www/;

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

    location / {
        index  index.html index.htm index.php;
        autoindex   on;
        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;
}
        
创建ssl默认虚拟主机default-ssl

server {
    listen       443;
    server_name  localhost;
    root       /var/www/;

    access_log  /usr/local/var/logs/nginx/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;
}
        
创建phpmyadmin虚拟主机

vim /usr/local/etc/nginx/sites-available/phpmyadmin #输入以下配置
server {
    listen       306;
    server_name  localhost;
    root    /usr/local/share/phpmyadmin;

    error_log   /usr/local/var/logs/nginx/phpmyadmin.error.log;
    access_log  /usr/local/var/logs/nginx/phpmyadmin.access.log main;

    ssl                  on;
    ssl_certificate      ssl/phpmyadmin.crt;
    ssl_certificate_key  ssl/phpmyadmin.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

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

mkdir -p /usr/local/etc/nginx/ssl
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=phpmyadmin" -keyout /usr/local/etc/nginx/ssl/phpmyadmin.key -out /usr/local/etc/nginx/ssl/phpmyadmin.crt
        
创建虚拟主机软连接,开启虚拟主机

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
ln -sfv /usr/local/etc/nginx/sites-available/phpmyadmin /usr/local/etc/nginx/sites-enabled/phpmyadmin
        
启动|停止Nginx

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

接下来你可以通过下面这些连接访问:

  1. http://localhost/ -> index.html
  2. http://localhost/info -> info.php via phpinfo();
  3. http://localhost/404 -> 404.html
  4. https://localhost/ -> index.html(SSL)
  5. https://localhost/info -> info.php via phpinfo();(SSL)
  6. https://localhost/404 -> 404.html(SSL)
  7. https://localhost:306 -> phpmyadmin(SSL)

设置快捷服务控制命令

为了后面管理方便,将命令 alias 下,vim ~/.bash_aliases 输入一下内容:


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.php56.plist"
alias php-fpm.stop="launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.php56.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'
        

#让快捷命令生效
echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile     
source ~/.bash_profile
#创建站点目录到主目录,方便快捷访问
ln -sfv /var/www ~/htdocs
        

请登录

WOWPHP 账号登录 GitHub 账号登录

还没有账号?现在去注册一个~