安裝Homebrew
安裝Homebrew
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
檢查是否存在沖突
brew doctor
對Homebrew更新升級
brew update && brew upgrade
安裝Nginx
安裝nginx
brew install nginx
安裝oh-my-zsh
mac 自帶zsh,在終端中輸入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版本。
輸入brew install zsh,可以安裝最新版的zsh。
輸入 chsh -s /bin/zsh,并重啟終端,可以將系統(tǒng)默認(rèn)的終端shell改為zsh。
然后安裝oh-my-zsh
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
安裝php-fpm
添加源
brew tap homebrew/dupes
brew tap homebrew/php
安裝php-fpm
brew install php56 --whitout-apache --with-imap --with-tidy --with-debug --with-pgsql --with-mysql --with-fpm
將PHP、PHP-FPM加入環(huán)境變量
在終端中
vim .zshrc
找到export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"這一行,在其后面添加
homebrew下載php、php-fpm路徑配置,使其如下所示
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH"
export PATH="$(brew --prefix homebrew/php/php56)/sbin:$PATH"
$(brew --prefix homebrew/php/php56)/bin是使用homebrew下載的php
$(brew --prefix homebrew/php/php56)/sbin是使用homebrew下載的php-fpm
/usr/bin是MAC自帶的php
/usr/sbin是MAC自帶php-fpm
然后使其立即生效
source .zshrc
查看$PATH,最前面的路徑先后后面的路徑
?~echo $PATH
/usr/local/opt/php56/sbin:/usr/local/opt/php56/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
查看php、php-fpm版本
?~php -v
PHP 5.6.19 (cli) (built: Mar 24 2016 20:55:30) (DEBUG)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
?~php-fpm -v
PHP 5.6.19 (fpm-fcgi) (built: Mar 24 2016 20:55:33) (DEBUG)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
?~/usr/bin/php -v
PHP 5.5.31 (cli) (built: Feb 20 2016 20:33:10)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
?~/usr/sbin/php-fpm -v
PHP 5.5.31 (fpm-fcgi) (built: Feb 20 2016 20:33:26)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
從上面可見,配置的環(huán)境變量是正確的,homebrew先于系統(tǒng)自帶的執(zhí)行。
//啟動php-fmp
php-fmp
參考
MAC 設(shè)置環(huán)境變量path的幾種方法
MySQL PHP Nginx Redis Memcache
安裝MySQL
brew install mysql
執(zhí)行完如上命令后,有如下輸出結(jié)果
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
To connect run:
mysql -uroot
To have launchd start mysql at login:
ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
mysql.server start
從上面的一段文字來看,安裝mysql后,用戶名是root,但是沒有密碼。如果設(shè)置密碼的話,執(zhí)行mysql_secure_installation命令。如果不想設(shè)置密碼的話,可以執(zhí)行mysql.server start,然后mysql -uroot,如下圖所示
nhf:~ nanhangfei$mysql.server start
Starting MySQL
. SUCCESS!
nhf:~ nanhangfei$ mysql -uroot
Welcome to the MySQL monitor.Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.11 Homebrew
Copyright (c) 2000, 2016, 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>
如果想要設(shè)置root的密碼的話,執(zhí)行下面的命令,做些相關(guān)的設(shè)置。
mysql_secure_installation
安裝phpmyadmin
brew install phpmyadmin
配置Nginx
首先,為我們的配置文件創(chuàng)建一些文件夾
mkdir -p /usr/local/etc/nginx/logs
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
修改nginx配置文件
vim /usr/local/etc/nginx/nginx.conf
替換其內(nèi)容為
worker_processes? 1;
error_log? /usr/local/etc/nginx/logs/error.log debug;
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/etc/nginx/logs/access.log? main;
sendfile? ? ? ? ? ? on;
keepalive_timeout? 65;
index index.html index.php;
include /usr/local/etc/nginx/sites-enabled/*;
}
然后創(chuàng)建php-fpm配置文件:
vim /usr/local/ect/nginx/conf.d/php-fpm
其內(nèi)容如下,這個文件的意思是nginx中的php文件交由php-fmp解析
location ~ \.php$ {
try_files? ? ? $uri = 404;
fastcgi_pass? 127.0.0.1:9000;
fastcgi_index? index.php;
fastcgi_param? SCRIPT_FILENAME $document_root$fastcgi_script_name;
include? ? ? ? fastcgi_params;
}
搞不清FastCgi與PHP-fpm之間是個什么樣的關(guān)系
創(chuàng)建默認(rèn)虛擬主機(jī)default:
vim /usr/local/etc/nginx/sites-available/default
其內(nèi)容如下所示
server {
listen? ? ? 80;
server_name? localhost;
root? ? ? /var/www/;
access_log? /usr/local/etc/nginx/logs/default.access.log? main;
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;
}
創(chuàng)建ssl默認(rèn)虛擬主機(jī)default-ssl
vim /usr/local/etc/nginx/sites-available/default-ssl
其內(nèi)容如下所示:
server {
listen? ? ? 443;
server_name? localhost;
root? ? ? /var/www/;
access_log? /usr/local/etc/nginx/logs/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;
}
創(chuàng)建phpmyadmin虛擬主機(jī)
vim /usr/local/etc/nginx/sites-available/phpmyadmin
其內(nèi)容如下所示
server {
listen? ? ? 306;
server_name? localhost;
root? ? /usr/local/share/phpmyadmin;
error_log? /usr/local/etc/nginx/logs/phpmyadmin.error.log;
access_log? /usr/local/etc/nginx/logs/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;
}
}
設(shè)置SSL
mkdir-p /usr/local/etc/nginx/ssl
openssl req -new -newkey rsa:4096-days365-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-days365-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
創(chuàng)建虛擬主機(jī)軟連接,開啟虛擬主機(jī)
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
MySQL PHP Nginx Redis Memcache
安裝php-fpm、nginx、mysql、phpmyadmin是參考的這兩篇文章, 但在安裝過程中,會出現(xiàn)幾個問題,但原文章并沒有說明。下面是碰到的問題及解決方案。
啟動php-fmp、nginx、mysql
php-fmp
確認(rèn)php-fpm監(jiān)聽9000端口
?~sudo lsof -Pni4 | grep LISTEN | grep php
Password:
php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
啟動nginx
sudo nginx
重新加載配置|重啟|停止|退出
nginx -s reload|reopen|stop|quit
確認(rèn)nginx在監(jiān)聽80端口
?~sudo lsof -Pni4 | grep LISTEN | grep nginx
或者
index.html復(fù)制到nginx的根目錄/var/www中去
?~curl -IL http://localhost/index.html
HTTP/1.1 200 OK
Server: nginx/1.8.1
Date: Sun, 27 Mar 2016 03:18:30 GMT
Content-Type: text/html
Content-Length: 611
Last-Modified: Thu, 24 Mar 2016 23:16:35 GMT
Connection: keep-alive
ETag: "56f47553-263"
Accept-Ranges: bytes
啟動mysql
mysql.server start
先登錄
mysql -uroot -p
確定mysql端口
mysql> show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port| 3306|
+---------------+-------+?
如果安裝PHP時(shí)出現(xiàn)如下錯誤
checking if the location of ZLIB install directory is defined... no
configure: error: Cannot find libz
解決方式是在終端中執(zhí)行如下命令
xcode-select --install
參考?
configure: error: Cannot find libz
啟動php-fmp時(shí)的問題
?~/usr/local/opt/php56/sbin/php-fpm
[26-Mar-2016 12:15:40] NOTICE: PHP message: PHP Warning:PHP Startup: mcrypt: Unable to initialize module
Module compiled with build ID=API20131226,NTS
PHPcompiled with build ID=API20131226,NTS,debug
These options need to match
in Unknown on line 0
Warning:PHP Startup: mcrypt: Unable to initialize module
Module compiled with build ID=API20131226,NTS
PHPcompiled with build ID=API20131226,NTS,debug
These options need to match
in Unknown on line 0
Unknown(0) : Warning - PHP Startup: mcrypt: Unable to initialize module
Module compiled with build ID=API20131226,NTS
PHPcompiled with build ID=API20131226,NTS,debug
These options need to match
[26-Mar-2016 12:15:40] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[26-Mar-2016 12:15:40] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[26-Mar-2016 12:15:40] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
[26-Mar-2016 12:15:40] ERROR: FPM initialization failed
解決辦法,在終端執(zhí)行如下命令
brew reinstall php56-mcrypt --build-from-source
參考?PHP Warning: PHP Startup: ????????: Unable to initialize module
上一步完成后,又出現(xiàn)如下問題
?~sudo /usr/local/opt/php56/sbin/php-fpm
Password:
[26-Mar-2016 12:40:10] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48)
[26-Mar-2016 12:40:10] ERROR: FPM initialization failed
查看誰占用了9000端口
?~sudo lsof -i:9000
COMMAND PID USERFDTYPEDEVICE SIZE/OFF NODE NAME
php-fpm 707 root7uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)
php-fpm 708 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)
php-fpm 709 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)
php-fpm 710 _www0uIPv4 0x9177969b065b477d0t0TCP localhost:cslistener (LISTEN)
殺掉進(jìn)程id 707、708、709、710
?~sudo kill -9 707
?~sudo kill -9 708
?~sudo kill -9 709
?~sudo kill -9 710
?~sudo /usr/local/opt/php56/sbin/php-fpm
[26-Mar-2016 12:55:29] NOTICE: fpm is running, pid 32706
[26-Mar-2016 12:55:29] NOTICE: ready to handle connections
確認(rèn)php-fpm監(jiān)聽9000端口
?~sudo lsof -Pni4 | grep LISTEN | grep php
Password:
php-fpm32898root8uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32899_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32900_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
php-fpm32901_www0uIPv4 0x9177969b057870850t0TCP 127.0.0.1:9000 (LISTEN)
?~
?~php-fpm -D
[25-Mar-2016 20:23:28] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
[25-Mar-2016 20:23:28] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
[25-Mar-2016 20:23:28] ERROR: FPM initialization failed
?~
解決方法:?
php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf
又出現(xiàn)了如下錯誤
?~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf
[25-Mar-2016 20:24:52] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
[25-Mar-2016 20:24:52] ERROR: failed to post process the configuration
[25-Mar-2016 20:24:52] ERROR: FPM initialization failed
?~
vim /usr/local/etc/php/5.6/php-fpm.conf
然后添加,error_log = /usr/local/var/log/php-fpm.log?保存
?~php-fpm --fpm-config /usr/local/etc/php/5.6/php-fpm.conf
[25-Mar-2016 20:29:54] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root
[25-Mar-2016 20:29:54] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root
[25-Mar-2016 20:29:54] NOTICE: fpm is running, pid 1026
[25-Mar-2016 20:29:54] NOTICE: ready to handle connections
參考
Nginx的網(wǎng)站目錄
配置文件路徑: /usr/local/etc/nginx/nginx.conf
默認(rèn)的網(wǎng)站根目錄: /usr/local/opt//nginx/html
啟動nginx問題
nginx: [error] open() “/usr/local/var/run/nginx.pid” failed
參考http://www.inbeijing.org/archives/1290
?~sudo nginx
nginx: [warn] 1024 worker_connections exceed open file resource limit: 256
因?yàn)閡limit -n的值為256,所以將/usr/local/etc/nginx/nginx.conf配置文件中的worker_connections設(shè)置為256