主机配置
mac os 10.11 mysql:5.7.9 nginx:1.8.0 PHP:5.6.16
homebrew 的安装
终端中输入 ruby -e "$(curl -fsSL https://raw.githubusercontent.com/ Homebrew/install/master/install)"
1
2
3
|
终端中输入
ruby
-
e
"$(curl -fsSL https://raw.githubusercontent.com/ Homebrew/install/master/install)"
|
安装php-fpm
//因为已经安装homebrew 所以可以直接使用homebrew安装php-fpm brew tap homebrew/dupes brew tap homebrew/php brew install --without-apache --with-fpm --with-mysql php56
1
2
3
4
5
|
//因为已经安装homebrew 所以可以直接使用homebrew安装php-fpm
brew
tap
homebrew
/
dupes
brew
tap
homebrew
/
php
brew
install
--
without
-
apache
--
with
-
fpm
--
with
-
mysql
php56
|
安装完成 现在我们将php-fpm 添加入环境变量中 方便我们通过终端直接进行启动
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.bash_profile #If you use bsh echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc #If you use ZSH
1
2
3
4
5
|
echo
'export PATH="/usr/local/sbin:$PATH"'
>>
~
/
.
bash_profile
#If you use bsh
echo
'export PATH="/usr/local/sbin:$PATH"'
>>
~
/
.
zshrc
#If you use ZSH
|
创建文件夹 并启动服务
mkdir -p ~/Library/LaunchAgents ln -sfv /usr/local/opt/php56/homebrew.mxcl.php56.plist ~/ Library/LaunchAgents/ launchctl load -w ~/Library/LaunchAgents/ homebrew.mxcl.php56.plist
1
2
3
4
|
mkdir
-
p
~
/
Library
/
LaunchAgents
ln
-
sfv
/
usr
/
local
/
opt
/
php56
/
homebrew
.
mxcl
.
php56
.
plist
~
/
Library
/
LaunchAgents
/
launchctl
load
-
w
~
/
Library
/
LaunchAgents
/
homebrew
.
mxcl
.
php56
.
plist
|
如果没有报出什么bug的话 在终端中键入
lsof -Pni4 | grep LISTEN | grep php
1
2
|
lsof
-
Pni4
|
grep
LISTEN
|
grep
php
|
应该会有下图的显示
php-fpm 69659 frdmn 6u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69660 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69661 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) php-fpm 69662 frdmn 0u IPv4 0x8d8ebe505a1ae01 0t0 TCP 127.0.0.1:9000 (LISTEN) `
1
2
3
4
5
|
php
-
fpm
69659
frdmn
6u
IPv4
0x8d8ebe505a1ae01
0t0
TCP
127.0.0.1
:
9000
(
LISTEN
)
php
-
fpm
69660
frdmn
0u
IPv4
0x8d8ebe505a1ae01
0t0
TCP
127.0.0.1
:
9000
(
LISTEN
)
php
-
fpm
69661
frdmn
0u
IPv4
0x8d8ebe505a1ae01
0t0
TCP
127.0.0.1
:
9000
(
LISTEN
)
php
-
fpm
69662
frdmn
0u
IPv4
0x8d8ebe505a1ae01
0t0
TCP
127.0.0.1
:
9000
(
LISTEN
)
`
|
mysql 安装
brew install mysql ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
1
2
3
|
brew
install
mysql
ln
-
sfv
/
usr
/
local
/
opt
/
mysql
/
*
.
plist
~
/
Library
/
LaunchAgents
|
添加mysql到环境变量中
launchctl load ~/Library/LaunchAgents/ homebrew.mxcl.mysql.plist
1
2
|
launchctl
load
~
/
Library
/
LaunchAgents
/
homebrew
.
mxcl
.
mysql
.
plist
|
进入mysql的初始化环节 去确认一些mysql的缺省选项
mysql_secure_installation Enter current password for root (enter for none) ##回车 我们并不需要 > Change the root password? [Y/n] 如不愿意使用root密码缺省mysql的password 输入n 并键入自己想使用的password > Remove anonymous users? [Y/n] Yes. 匿名用户我们并不需要 > Disallow root login remotely? [Y/n] Yes. 我们仅需要127.0.1 > Remove test database and access to it? [Y/n] Yes. 无需保留冗余的测试文件 > Reload privilege tables now? [Y/n] Yes.重新加载数据库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
mysql_secure_installation
Enter
current
password
for
root
(
enter
for
none
)
##回车 我们并不需要
>
Change
the
root
password
?
[
Y
/
n
]
如不愿意使用
root密码缺省
mysql的
password
输入
n
并键入自己想使用的
password
>
Remove
anonymous
users
?
[
Y
/
n
]
Yes
.
匿名用户我们并不需要
>
Disallow
root
login
remotely
?
[
Y
/
n
]
Yes
.
我们仅需要
127.0.1
>
Remove
test
database
and
access
to
it
?
[
Y
/
n
]
Yes
.
无需保留冗余的测试文件
>
Reload
privilege
tables
now
?
[
Y
/
n
]
Yes
.重新加载数据库
|
测试mysql
mysql -uroot -p
1
2
|
mysql
-
uroot
-
p
|
输入权限密码 这是你应该能看见
Type \'help;\' or \'h\' for help. Type \'c\' to clear the current input statement. mysql> 退出mysql 输入q
1
2
3
4
5
6
|
Type
\'
help
;
\'
or
\'
h
\'
for
help
.
Type
\'
c
\'
to
clear
the
current
input
statement
.
mysql
>
退出
mysql
输入
q
|
php-fpm安装及配置
curl -L https://gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm
1
2
|
curl
-
L
https
:
//gist.github.com/frdmn/7853158/raw/php-fpm -o /usr/local/etc/nginx/conf.d/php-fpm
|
注意墙内用户最好翻墙 不然timeout是你的噩梦
创建默认虚拟主机
curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl curl -L https://gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
1
2
3
4
|
curl
-
L
https
:
//gist.github.com/frdmn/7853158/raw/sites-available_default -o /usr/local/etc/nginx/sites-available/default
curl
-
L
https
:
//gist.github.com/frdmn/7853158/raw/sites-available_default-ssl -o /usr/local/etc/nginx/sites-available/default-ssl
curl
-
L
https
:
//gist.github.com/frdmn/7853158/raw/sites-available_phpmyadmin -o /usr/local/etc/nginx/sites-available/phpmyadmin
|
使用git 来克隆实例主机
git clone http://git.frd.mn/frdmn/nginx-virtual-host.git /var/www rm -rf /var/www/.git
1
2
3
|
git
clone
http
:
//git.frd.mn/frdmn/nginx-virtual-host.git /var/www
rm
-
rf
/
var
/
www
/
.
git
|
然后删除/var/www/.get文件夹 防止你的文件被git上传
nginx的安装
使用homebrew来安装nginx
brew install nginx
1
2
|
brew
install
nginx
|
我们必须确保80端口是开启的,因为nginx是基于80端口的
sudo cp -v /usr/local/opt/nginx/*.plist /Library/LaunchDaemons/ sudo chown root:wheel /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1
2
3
|
sudo
cp
-
v
/
usr
/
local
/
opt
/
nginx
/
*
.
plist
/
Library
/
LaunchDaemons
/
sudo
chown
root
:
wheel
/
Library
/
LaunchDaemons
/
homebrew
.
mxcl
.
nginx
.
plist
|
第一次开始nginx
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1
2
|
sudo
launchctl
load
/
Library
/
LaunchDaemons
/
homebrew
.
mxcl
.
nginx
.
plist
|
默认的配置设置是将监听8080端口而非http默认的80端口
curl -IL http://127.0.0.1:8080
1
2
|
curl
-
IL
http
:
//127.0.0.1:8080
|
终端中应该有如下显示
HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Mon, 19 Oct 2014 19:07:47 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 19 Oct 2014 19:01:32 GMT Connection: keep-alive ETag: \"5444dea7-264\" Accept-Ranges: bytes`
1
2
3
4
5
6
7
8
9
10
|
HTTP
/
1.1
200
OK
Server
:
nginx
/
1.6.2
Date
:
Mon
,
19
Oct
2014
19
:
07
:
47
GMT
Content
-
Type
:
text
/
html
Content
-
Length
:
612
Last
-
Modified
:
Mon
,
19
Oct
2014
19
:
01
:
32
GMT
Connection
:
keep
-
alive
ETag
:
\"
5444dea7
-
264
\"
Accept
-
Ranges
:
bytes
`
|
我们停止nginx 对其进行进一步配置
sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1
2
|
sudo
launchctl
unload
/
Library
/
LaunchDaemons
/
homebrew
.
mxcl
.
nginx
.
plist
|
安装ssl
创建文件夹来存放ssl的证书和私钥
mkdir -p /usr/local/etc/nginx/ssl
1
2
|
mkdir
-
p
/
usr
/
local
/
etc
/
nginx
/
ssl
|
生成4096比特 的RSA秘钥
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
1
2
3
|
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
1
2
3
4
|
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
sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
1
2
|
sudo
launchctl
load
/
Library
/
LaunchDaemons
/
homebrew
.
mxcl
.
nginx
.
plist
|
然后你可以 打开下面的这些网站,来确认自己的nginx是否正确安装
http://localhost → \"Nginx works\" page http://localhost/info → phpinfo() http://localhost/nope → \" Not Found\" page https://localhost:443 → \"Nginx works\" page (SSL) https://localhost:443/info → phpinfo() (SSL) https://localhost:443/nope → \"Not Found\" page (SSL) https://localhost:306 → phpMyAdmin (SSL)
1
2
3
4
5
6
7
8
|
http
:
//localhost → \"Nginx works\" page
http
:
//localhost/info → phpinfo()
http
:
//localhost/nope → \" Not Found\" page
https
:
//localhost:443 → \"Nginx works\" page (SSL)
https
:
//localhost:443/info → phpinfo() (SSL)
https
:
//localhost:443/nope → \"Not Found\" page (SSL)
https
:
//localhost:306 → phpMyAdmin (SSL)
|
对服务的控制
因为种种原因 我们可能要重新启动来进行 重命名
curl -L https://gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases cat /tmp/.bash_aliases >> ~/.bash_aliases // If you use Bash echo \"source ~/.bash_aliases\" >> ~/.bash_profile // If you use ZSH echo \"source ~/.bash_aliases\" >> ~/.zshrc
1
2
3
4
5
6
7
|
curl
-
L
https
:
//gist.github.com/frdmn/7853158/raw/bash_aliases -o /tmp/.bash_aliases
cat
/
tmp
/
.
bash_aliases
>>
~
/
.
bash_aliases
// If you use Bash
echo
\"
source
~
/
.
bash_aliases
\"
>>
~
/
.
bash_profile
// If you use ZSH
echo
\"
source
~
/
.
bash_aliases
\"
>>
~
/
.
zshrc
|
现在我们可以输入以下命令更新我们的变量 让我们的设置生效
source ~/.bash_profile //or source ~/.zshrc`
1
2
3
4
|
source
~
/
.
bash_profile
//or
source
~
/
.
zshrc
`
|
现在你可以使用更加简短的命名来优雅的开关服务 而不必打一长串的指令 路径 那很傻
nginx.start nginx.stop nginx.restart
1
2
3
4
|
nginx
.
start
nginx
.
stop
nginx
.
restart
|
要快速地浏览的最新错误或访问日志
nginx.logs.access nginx.logs.default.access nginx.logs.phpmyadmin.access nginx.logs.default-ssl.access nginx.logs.error nginx.logs.phpmyadmin.error
1
2
3
4
5
6
7
|
nginx
.
logs
.
access
nginx
.
logs
.
default
.
access
nginx
.
logs
.
phpmyadmin
.
access
nginx
.
logs
.
default
-
ssl
.
access
nginx
.
logs
.
error
nginx
.
logs
.
phpmyadmin
.
error
|
或者检查配置文件
sudo nginx -t
1
2
|
sudo
nginx
-
t
|
这篇文章的原文是:https://blog.frd.mn/install-nginx-php-fpm-mysql-and-phpmyadmin-on-os-x-mavericks-using-homebrew/
有人反馈该文章顺序有问题。推荐看英文原文。
补充一下:原文的 Control the services 一节的”or if you use ZSH:” 的命令应该是”echo “source ~/.bash_aliases” >> ~/.zshrc && . ~/.zshrc” 原文在”~/.zshrc”之前少了个点。
安装ssl 的 第三小节第2行命令: ln -sfv /usr/local/etc/nginx/sites-available/default-ssl / usr/local/etc/nginx/sites-enabled/default-ssl 第二个路径的 usr 前多了两个空格, 请改下