用了一年的Mac OS X了,以前不熟悉這個系統,用的是系統自帶的PHP 以及DMG包安裝的MySQL,時間長了,慢慢以爲MacBook的速度跟不上了,雖然關機次數很少,可是每次開機,或者喚醒電腦的時候,系統明顯有必定時間的卡頓。特別表如今開機的時候。徹底能夠去泡一個來一桶了。 所以幹掉MD101上的光驅,換上256G的Sandisk SSD, 幹掉原廠的2G * 2,換上8G * 2 。 速度應該是槓槓的了,至少能夠再服役2年吧。
趁着此次加硬盤的機會,就準備完全重作開發環境。如今對Mac也有了必定的瞭解,特意記錄一下本次的開發環境安裝詳情,給本身一個備忘,但願也能夠幫助到剛接觸Mac環境的同窗們。本開發環境,所有基於HomeBrew安裝。
主要軟件版本:PHP5.5.14,Nginx 1.6.0,MySQL5.6.19php
從新安裝系統,在蘋果商店下載好OS X Mavericks安裝文件,而後準備一支16G的USB3.0 U盤。製做OS X Mavericks 全新安裝啓動U盤。
插上U盤,在終端執行:sudo /Applications/Install\ OS\ X\ Mavericks.app/Contents/Resources/createinstallmedia --volume /Volumes/untitled --applicationpath /Applications/Install\ OS\ X\ Mavericks.app --nointeraction
untitled 是你的u盤盤符,根據實際狀況來。html
Erasing Disk: 0%… 10%… 20%… 30%…100%… >Copying installer files to disk… Copy complete. Making disk bootable… Copying boot files… >Copy complete. >Done.
看到上面的信息說明啓動盤製做成功。 安裝起來so easy :)node
安裝完成系統以後, 暫時尚未去遷移文件,因爲本人喜愛攝影,有大量RAW格式的原圖在Aperture 的照片庫中,尼康D800一張RAW文件有40M左右,到時候遷移照片庫和照片流但願不要掉坑裏了。 等開發環境搞定再說了,更新OS X系統到10.9.4 ,而後安裝最新的Xcode, 以及在商店買過的軟件。肯定系統無問題了,進入下一步...mysql
Brew 是 Mac 下面的包管理工具,經過 Github 託管適合 Mac 的編譯配置以及 Patch,能夠方便的安裝開發工具。 Mac 自帶ruby 因此安裝起來很方便,同時它也會自動把git也給你裝上。官方網站: http://brew.sh 。nginx
安裝完成以後,建議執行一下自檢,
brew doctor
若是看到Your system is ready to brew. 那麼你的brew已經能夠開始使用了。laravel
安裝:git
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"
自檢:github
brew doctor
經常使用命令: (全部軟件以PHP5.5爲例子)redis
brew update #更新brew可安裝包,建議每次執行一下 brew search php55 #搜索php5.5 brew tap josegonzalez/php #安裝擴展<gihhub_user/repo> brew tap #查看安裝的擴展列表 brew install php55 #安裝php5.5 brew remove php55 #卸載php5.5 brew upgrade php55 #升級php5.5 brew options php55 #查看php5.5安裝選項 brew info php55 #查看php5.5相關信息 brew home php55 #訪問php5.5官方網站 brew services list #查看系統經過 brew 安裝的服務 brew services cleanup #清除已卸載無用的啓動配置文件 brew services restart php55 #重啓php-fpm
注意:brew services 相關命令最好別常常用了,提示會被移除sql
➜ ~ brew services restart php55 Warning: brew services is unsupported and will be removed soon. You should use launchctl instead. Please feel free volunteer to support it in a tap. Stopping `php55`... (might take a while) ==> Successfully stopped `php55` (label: homebrew.mxcl.php55) ==> Successfully started `php55` (label: homebrew.mxcl.php55)
ohmyzsh & iTerm2兩個神器,在Mac os x下是必定要裝的. 兩組配合起來使用,加上插件。簡直是神同樣的存在。 秒殺梅西,內馬爾啊:) Oh 猛戳到官網
安裝 oh my zsh
curl -L http://install.ohmyz.sh | sh
設置默認shell
查看系統支持的shell列表,Mac 10.9.4 自帶了 zsh 5.0.2,Linux上得安裝。
cat /etc/shells # List of acceptable shells for chpass(1). # Ftpd will not allow users to connect who are not using # one of these shells. /bin/bash /bin/csh /bin/ksh /bin/sh /bin/tcsh /bin/zsh zsh --version zsh 5.0.2 (x86_64-apple-darwin13.0) chsh -s /bin/zsh
雖然Mac自帶了zsh,若是你想要最新版的zsh,那麼你用
brew install zsh
安裝一個最新的吧。/usr/local/bin/zsh --version zsh 5.0.5 (x86_64-apple-darwin13.3.0)
區別也不會很大, 默認的版本已經很新了。
安裝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的優點:
安裝iTerm2:
brew cask install iterm2
上面提過的神器組合,更多使用方法請參考iTerm2官方文檔, 這裏就不詳細說明了。
安裝開發包
brew install wget watch tmux cmake openssl imagemagick graphicsmagick gearman geoip readline autoconf multitail source-highlight autojump zsh-completions sshfs
升級一下系統自帶的vimbrew install ctags macvim --env-std --override-system-vim
安裝經常使用軟件
brew cask install alfred appcleaner firefox google-chrome phpstorm sublime-text sequel-pro sketch mplayerx thunder qq
Alfred 是個很不錯的東西,推薦必須安裝。它默認搜索目錄不包含brew cask安裝的軟件,所以手動將
/opt/homebrew-cask
添加到Alfred的搜索目錄
前面作了這麼多的準備工做,其實 zsh iTerm2 brew 等等這些因爲篇幅以及能力有限,一時半會也講不完,更多選擇,更多歡樂,就在Google search,如今才入正題。come on :)
安裝MySQL
brew install 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
根據終端提示,輸入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 php55
命令來查看安裝php5.5的選項,這裏我用下面的選項安裝:
brew install php55 --with-fpm --with-gmp --with-imap --with-tidy --with-debug --with-mysql --with-libmysql
PHP編譯過程當中若是遇到
configure: error: Cannot find OpenSSL's <evp.h>
錯誤,執行xcode-select --install
從新安裝一下Xcode Command Line Tools 在GitHub HomeBrew上有關於這個討論:
For future reference of anybody looking for Command Line Tools with Xcode 5, open up a Terminal window and type xcode-select --install. A window will appear informing you command line tools are required. Click Install and you should be good to go
等待PHP編譯完成,開始安裝PHP經常使用擴展,擴展安裝過程當中brew會自動安裝依賴包,例如php55-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;
擴展裏面提一下php55-phalcon 和 php55-swoole. 一個是C語言寫的PHP框架,安裝來我的摸索熟悉一下,尚未真正的使用過,大體看了一下文檔,感受很是吊炸天。目前公司的項目是基於Yii2的,也看看這個框架。 另一個swoole是國產的PHP高性能網絡通訊框架,貌似不錯,可能在項目中會考慮用到它。
因爲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 Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
修改php-fpm配置文件,vim /usr/local/etc/php/5.5/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.5/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.5/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 php55 #還能夠用這個命令來啓動php-fpm launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.php55.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/php55/*.plist ~/Library/LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist
安裝php composer
brew install composer #檢查一下狀況 composer --version Composer version 1.0.0-alpha8 2014-01-06 18:39:59
redis memcached這些軟件brew 已經自動依賴安裝上,若是想開機自動啓動,或者查看使用說明
brew info redis
便可。另外,composer的中文文檔:猛戳這裏
安裝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
輸入如下內容:
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"'; 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
建立默認虛擬主機defaultvim /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-sslvim /usr/local/etc/nginx/sites-available/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
接下來你能夠經過下面這些鏈接訪問:
http://localhost/ -> index.html
http://localhost/info -> info.php via phpinfo();
http://localhost/404 -> 404.html
https://localhost/ -> index.html(SSL)
https://localhost/info -> info.php via phpinfo();(SSL)
https://localhost/404 -> 404.html(SSL)
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.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'
#讓快捷命令生效 echo "[[ -f ~/.bash_aliases ]] && . ~/.bash_aliases" >> ~/.bash_profile source ~/.bash_profile #建立站點目錄到主目錄,方便快捷訪問 ln -sfv /var/www ~/htdocs
有時間會進一步整理一下,感受有點亂,第一次在segmentfault上碼字,寫到一半的時候不當心滑一下觸摸板,瀏覽器後退了,雖然有提示不要離開,點擊了不離開以後,Safari白屏。 而後還特意去吐了個槽, 害我白白又浪費好久來寫這個博客。但願能幫助到剛剛接觸Mac的同窗。enjoy Mac‘s development EVN :)
參考資料:Install Nginx, PHP-FPM, MySQL and phpMyAdmin on OS X Mavericks using Homebrew