安装Nginx
和前面一样先brew search nginx查找nginx, 看下信息brew info nginx
然后安装brew install nginx
安装信息记录下,用的到:
➜ ~ brew install nginx
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gitless
==> Updated Formulae
fzf gammu
==> Downloading https://homebrew.bintray.com/bottles/nginx-1.10.2.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring nginx-1.10.2.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
Docroot is: /usr/local/var/www
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.
nginx will load all files in /usr/local/etc/nginx/servers/.
To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx
==> Summary
Nginx启动关闭命令:
#测试配置是否有语法错误
nginx -t
#打开 nginx
sudo nginx
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit
好了,来跑下nginx
sudo nginx
到这里为止,我们已经安装完了mysql php nginx, 在安装php的时候添加了--with-mysql, 所以php操作mysql是没有问题的,现在我们就要配置nginx,让它监听php-fpm的进程,这样当用户打开浏览器访问的时候,身为反向代理的nignx就能把东西让php去执行了。
接下来,我们要配置nginx.conf文件,创建一个php-fpm文件(监听php-fpm), 还要约定下将nginx.pid文件,log日志,以及以后我们要配置的站点.conf的路径,我们的路径约定还是按照brew默认的目录来设置,如下:
# nginx.conf,已经被创建好了,我们一会要更改下
/usr/local/etc/nginx/nginx.conf
# php-fpm,这个我们就放在和nginx.conf一样的路径下吧,这个要我们自己创建
/usr/local/etc/nginx/php-fpm
# 日志文件放在/usr/local/var/log/nginx中,默认已经有了access.log和error.log文件了
/usr/local/var/log/nginx/
# nginx.pid文件,放在/usr/local/var/run/下面,和php-fpm.pid放一堆
/usr/local/var/run/
# 以后要配置的站点.conf, 我们就放在/usr/local/etc/nginx/servers/下面,这个servers文件夹本身就存在的
/usr/local/etc/nginx/servers/
# 站点的根目录,也就用brew给我们设置的吧
/usr/local/var/www/
我不知道大家是怎么来定义路径的,我的智商不咋的,记性非常的差,一般我都用brew安装软件,所以用brew默认的路径,我能方便的找到我的文件。
下面我们先来修改nginx.conf, 用vim打开,把下面的信息覆盖nginx.conf, vim /usr/local/etc/nginx/nginx.conf, 如果你不习惯vim, 那就用sublime打开吧subl /usr/local/etc/nginx/nginx.conf
worker_processes 1;
error_log /usr/local/var/log/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/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
port_in_redirect off;
include /usr/local/etc/nginx/servers/*;
}
接下来,将下面的信息放入到php-fpm文件中,vim /usr/local/etc/nginx/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;
}
ok, 下面就能配置站点了,先到/usr/local/var/www目录下建立站点根目录,就叫做default吧,然后在里面建立个info.php,内容就放phpinfo()函数就行.
mkdir /usr/local/var/www/default
vi /usr/local/var/www/default/info.php #输入
最后,去创建站点.conf文件
/usr/local/etc/nginx/servers/default.conf
输入下面的内容
server {
listen 80;
server_name localhost;
root /usr/local/var/www/default;
access_log /usr/local/var/log/nginx/default.access.log main;
location / {
index index.html index.htm index.php;
autoindex on;
include /usr/local/etc/nginx/php-fpm;
}
error_page 404 /404.html;
error_page 403 /403.html;
}
测试下配置文件
➜ servers sudo nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
然后开启php-fpm,已经开启的就不用开了
php-fpm -D
开启nginx服务
sudo nginx # 已经开启的用sudo nginx -s reload 重启下
浏览器打开http://localhost/info.php测试下
别名
上面的php-fpm关闭和重启的命令有点长,我们给它做个别名,在你的个人主目录下先建立一个.aliases文件,然后放入下面的内容
alias fpm.start='php-fpm -D'
alias fpm.stop='kill -INT `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.restart='kill -USR2 `cat /usr/local/var/run/php-fpm.pid`'
alias fpm.status='lsof -Pni4 | grep LISTEN | grep php-fpm'
然后
echo 'source ~/.aliases' >> ~/.zshrc
source ~/.zshrc
安装MNMP (Mac + Nginx + Mysql + Php) 开发环境结束
以上安装出现的问题记录:
- 使用Curl出现的502错误
做有赞接口的时候发现出现了502错误,用下面的测试代码也可以测试:
$curl = curl_init();
// 刚开始抓取了https://github.com,但是页面弹框,后来改用抓取支付宝首页测试
curl_setopt($curl, CURLOPT_URL, 'https://www.alipay.com');
// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 运行cURL,请求网页数据
$data = curl_exec($curl);
// 关闭cURL请求
curl_close($curl);
// 打印出抓取的测试数据
var_dump($data);
产生问题的原因:brew安装curl时默认没有带上--with-openssl
解决方法:
# 先删除curl
brew uninstall curl
# 重新安装curl,带上--with-openssl
brew install curl --with-openssl
然后重启下php-fpm