nginx - Mac下安装与运行

1525 查看

前言

由于用Nodejs配置前端开发环境产生了一些瓶颈,而且容易产生一些问题,所以之后前端开发就直接用nginx做了。这样就能线上和本地的环境相近了一些,减轻了一些部署的难度。因为是第一次用,所以遇到了一些小坑,在此记下来。

安装

在Mac安装的时候比较简单

brew install nginx

在安装好后,按以往的经验直接sudo nginx,结果报错了。

dyld: Library not loaded: /usr/local/lib/libpcre.1.dylib
  Referenced from: /usr/local/bin/nginx
  Reason: image not found

Google之,运行brew doctor。再次显示错误。

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:

    mackup
    pcre

按照提示去运行brew link mackupbrew link pcre
这样就算是安装好了。

操作

根据查到的资料,要做一个配置文件。

events {
  worker_connections 1024;
}

http {
  server {
    listen       9000;
    server_name  localhost;

    location / {
      root   /Users/limi/code/hypclass-webpage/run/;#网站的跟路径
      index  index.html index.htm;
    }
  }
}

这是最精简的配置了,不过好像nginx配置网站的地址只支持绝对路径。
然后看了一下官方的指南把启动命令改了,貌似正确的是这样。

sudo nginx -c /Users/limi/code/website/nginx.conf

到此浏览器输入http://localhost:9000/,正常显示了。

最后要把nginx关掉,再次查看文档查命令。

nginx -s stop
nginx -s quit

貌似都可以关闭,至于有什么区别,日后在看。