最近接了一个老旧的项目要进行功能迁移,上去一看是大好几年前的jquery项目,本地项目需要配置nginx才能进行启动查看,上次配置这玩意儿还是刚毕业的时候在window电脑上配置的,现在早忘了咋弄了,这次借此机会记录下mac电脑的nginx的配置。
一、安装 Homebrew
mac电脑的同学对这个应该很熟悉了,他就是mac的大管家,管理你软件包的一款工具。基本上日常开发用到的工具,通过这个都能进行安装,常见的包管理器 nvm之类的。
首先第一步开手机热点进行操作。。。
1、首先需要安装一下
这个我不知道是不是后续mac自带这个了,我记忆住是我电脑买回来是直接就有这个,只是版本比较低,需要自己去更新下。
可以使用 brew -v 进行版本的查看,没有的话就需要安装一下了,安装操作如下:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2、更新
安装成功之后,需要更新下版本 :brew update
好了,大多数人开始各种报错或者下载缓慢:原因也很简单,国内无法连接,下载不下来东西
3、解决办法:替换地址
替换 brew.git 仓库地址
$ cd "$(brew --repo)"
$ git remote set-url origin https://mirrors.aliyun.com/homebrew/brew.git
还原:
$ cd "$(brew --repo)"
$ git remote set-url origin https://github.com/Homebrew/brew.git
替换 homebrew-core.git 仓库地址:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://mirrors.aliyun.com/homebrew/homebrew-core.git
还原:
$ cd "$(brew --repo)/Library/Taps/homebrew/homebrew-core"
$ git remote set-url origin https://github.com/Homebrew/homebrew-core.git
替换 homebrew-bottles 访问地址:
1、这个步骤跟 macOS 系统使用的 shell 版本有关系,先查看 shell 版本
$ echo $SHELL
会输出 /bin/zsh 或 /bin/bash
2、如果输出 /bin/zsh,访问地址换成这个:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
$ source ~/.zshrc
还原:
$ vi ~/.zshrc
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
$ source ~/.zshrc
3、如果输出 /bin/bash,访问地址换成这个:
$ echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.bash_profile
$ source ~/.bash_profile
还原:
$ vi ~/.bash_profile
# 然后,删除 HOMEBREW_BOTTLE_DOMAIN 这一行配置
$ source ~/.bash_profile
配置到这里就结束了,可以再次执行
$ brew update
网上一般就是截止到这儿然后让你去更新代码,但是很大情况下你会发现还是不行,会卡在一个 core 的下载上不动。
4、删除homebrew-core
(1)第一种方式
$ cd /usr/local/Homebrew/Library/Taps/homebrew
$ rm -rf homebrew-core
$ brew upgrade
再次跟新:>brew upgrade(注意命令),一般会持续十五到二十分钟左右吧,耐心点儿等待
(2)第二种方式
还有一种是直接git克隆去安装:(这个我没使用,不知道正确性)
// 进入 Taps 文件夹
$ cd /usr/local/Homebrew/Library/Taps
// 新建 homebrew 文件并进入,如果有这个文件可以直接进入
$ mkdir homebrew && cd homebrew
或
$ cd homebrew
// Git 下载
$ git clone git://mirrors.ustc.edu.cn/homebrew-core.gi
成功之后,执行安装命令,中途需要回车确认:
$ /bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
复制代码
最后看到 ==> Installation successful! 就说明安装成功了。
最后,整体跟新下:brew update,然后把你的配置操作进行还原
二、安装和配置nginx
1、安装和找到nginx
安装更新完homebrew后,就一个命令就完事儿了
$ brew install nginx
查看版本号,确认是否安装
$ nginx -v
打开nginx的配置文件地址
$ open /usr/local/etc/nginx/
2、配置nginx
打开nginx.conf文件,对其中的一些东西进行配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#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 {
listen 8006; // 配置端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /Users/xxx/xxx/xxx; // 代理项目文件夹
index index.html xxx.html xxx.html; // 代理的项目根地址
}
location /xxx { // 接口的代理地址
// 配置主要参考自己公司项目
proxy_pass nnn/xxx; // 代理地址(看两个xxx是一样的)
proxy_redirect off;
proxy_set_header nnn;
proxy_set_header nnn;
client_max_body_size nnn;
}
#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/*;
}
需要配置的地方我都加注释了,注意查收
3、启动
来吧,收获成果的时候
启动:brew services start nginx
关闭:brew services stop nginx
打开地址查看
补充:可以先不配置自己的东西,启动下nginx会有个欢迎界面;确定自己安装成功之后再进行配置自己的东西