mac上安装nginx,并修改配置文件

Posted 一目沉浮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mac上安装nginx,并修改配置文件相关的知识,希望对你有一定的参考价值。

 
1. 安装Homebrew
终端输入如下命令
> ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

大概过个几十分钟左右安装完成,其间提示输入密码,输入Mac密码,安装成功后就可以安装nginx

 

2. 安装nginx
终端输入以下命令
> brew install nginx

 

3. 验证是否安装成功
终端输入以下命令
> sudo nginx

 

浏览器输入
> http://localhost:8089

 

页面显示welcome to nginx! 就表示安装成功了。

 

4. 找到nginx的安装目录,一般是安装在usr/local/Cellar/nginx/版本号/bin。你也可以通过命令去查找
```
MacdeMacBook-Pro-3:Desktop zhongbaihua$ cd /usr
MacdeMacBook-Pro-3:usr zhongbaihua$ ls
bin lib local share
include libexec sbin standalone
MacdeMacBook-Pro-3:usr zhongbaihua$ cd local
MacdeMacBook-Pro-3:local zhongbaihua$ ls
Caskroom Homebrew git opt var
Cellar bin include sbin
Frameworks etc lib share
MacdeMacBook-Pro-3:local zhongbaihua$ cd Cellar
MacdeMacBook-Pro-3:Cellar zhongbaihua$ ls
nginx openssl@1.1 pcre
MacdeMacBook-Pro-3:Cellar zhongbaihua$ cd nginx
MacdeMacBook-Pro-3:nginx zhongbaihua$ ls
1.17.6
MacdeMacBook-Pro-3:nginx zhongbaihua$ cd 1.17.6
MacdeMacBook-Pro-3:1.17.6 zhongbaihua$ ls
CHANGES bin
INSTALL_RECEIPT.json homebrew.mxcl.nginx.plist
LICENSE html
README share
MacdeMacBook-Pro-3:1.17.6 zhongbaihua$ cd bin
MacdeMacBook-Pro-3:bin zhongbaihua$ ls
nginx
MacdeMacBook-Pro-3:bin zhongbaihua$ pwd
/usr/local/Cellar/nginx/1.17.6/bin
MacdeMacBook-Pro-3:bin zhongbaihua$

 

---------或者通过以下更加简单的方式查找---------

 

MacdeMacBook-Pro-3:bin zhongbaihua$ which nginx
/usr/local/bin/nginx
MacdeMacBook-Pro-3:bin zhongbaihua$ ls -alF /usr/local/bin/nginx
lrwxr-xr-x 1 zhongbaihua admin 32 12 24 23:03 /usr/local/bin/nginx@ -> ../Cellar/nginx/1.17.6/bin/nginx
MacdeMacBook-Pro-3:bin zhongbaihua$

 

```
5. 修改nginx配置,终端输入以下命令开始nginx配置文件编辑
> vim /usr/local/etc/nginx/nginx.conf

 

```
#user zhongbaihua owner;
worker_processes 1;

 

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#error_log logs/error.log info;

 

#pid logs/nginx.pid;



events {
worker_connections 1024;
}



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 logs/access.log main;

 

sendfile on;
#tcp_nopush on;

 

#keepalive_timeout 0;
keepalive_timeout 65;

 

#gzip on;

 

server {
charset utf-8;
listen 8089;
server_name http_host;
root /Users/zhongbaihua/upload/;
autoindex on;
add_header Cache-Control "no-cache, must-revalidate";
location / {
add_header Access-Control-Allow-Origin *;
}
}

 

server {
listen 8080;
server_name localhost;

 

#charset koi8-r;

 

#access_log logs/host.access.log main;

 

location / {
root html;
index index.html index.htm;
}

 

#error_page 404 /404.html;

 

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the php scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}

 

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

 

# deny access to .htaccess files, if Apache‘s document root
# concurs with nginx‘s one
#
#location ~ /.ht {
# deny all;
#}
}



# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

 

# location / {
# root html;
# index index.html index.htm;
# }
#}



# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

 

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

 

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

 

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

 

# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
include /Users/zhongbaihua/upload/upload.conf;
}

 

```
> 此时你就进入nginx.conf文件的编辑模式,按下键盘i就可以编辑。

 

> 在文件最底下添加include /Users/zhongbaihua/upload/upload.conf;意思是添加一个nginx配置路径,目标文件的内容相当于添加到了nginx.conf内容中。

 

> 文件开头#user zhongbaihua owner; 添加你当前登陆的mac用户名,以及空格 owner。

 

> 修改完毕后,按esc件,输入:wq 后回车,意思是保存并退出。 :q! 回车,是不保存退出

 

6. nginx配置修改了。在你的users/用户名/目录下面新建upload文件夹新建upload.conf文件。

 

> 1.终端 cd /users/用户名。mkdir upload,新建文件夹。1.1 cd upload,进入刚才新建的文件夹。 2. touch upload.conf 新建文件

 

7. 在upload.conf文件中配置nginx

 

> 1. 终端输入 cd upload,进入目标文件件,输入 vim upload.conf。编辑内容

以上是关于mac上安装nginx,并修改配置文件的主要内容,如果未能解决你的问题,请参考以下文章

mac nginx 安装及PHP配置

mac上安装Nginx详细教程

docker上启动nginx,并配置修改nginx的配置文件

nginx安装在Mac上

Mac下安装nginx并配置SSL实现Https的访问

Mac安装PHP运行环境