下载安装Brew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew
brew又叫Homebrew,是Mac中的一款软件包管理工具,通过brew可以很方便的在Mac中安装软件或者是卸载软件.
一般Mac电脑会默认安装有brew.
常用指令如下:
- brew 搜索软件
brew search nginx
- brew 安装软件
brew install nginx
- brew 卸载软件
brew uninstall nginx
- brew 升级
sudo brew update
- 查看安装信息(经常用到, 比如查看安装目录等)
sudo brew info nginx
- 查看已经安装的软件
brew list
brew安装nginx
- 安装nginx
可以用brew很方便地安装nginx.sudo brew install nginx
- 启动nginx服务
sudo brew services start nginx
利用http://localhost:8080
进行访问, 如果出现如下界面,说明启动成功
nginx
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
使用nginx搭建https服务器
步骤如下:
- 搭建一个nginx服务器;
- 使用openssl生成服务器私钥,客户端私钥,服务器公钥,客户端公钥;
- 生成CA根证书,为服务器证书和客户端证书(可选)提供签名服务;
- 生成服务器证书和客户端证书,并请求CA的签名;
- 在操作系统上信任我们伪造的服务器证书,这里没有为客户端生成证书;
- 随便写一个web服务,提供一个url返回一段静态报文,让nginx为它做一下代理,然后启动这个web服务器;
- 测试。
****生成服务端私钥****: $ openssl genrsa -out server.key 2048 ****生成服务端公钥****: $ openssl rsa -in server.key -pubout -out server.pem ****生成客户端私钥****: $ openssl genrsa -out client.key 2048 ****生成客户端公钥****: $ openssl rsa -in client.key -pubout -out client.pem ****生成CA证书****: $ openssl genrsa -out ca.key 2048 $ openssl req -new -key ca.key -out ca.csr $ openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt 生成客户端服务端证书 服务端: $ openssl req -new -key server.key -out server.csr $ openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt 客户端: $ openssl req -new -key client.key -out client.csr 向CA申请签名: $ openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt
证书和私钥生成完毕后,开始配置nginx(http部分从略);
#HTTPS server server { listen 443 ssl; server_name localhost; ssl_certificate ../ssl/certs/server.crt; #放置服务器证书的目录 ssl_certificate_key ../ssl/private/server.key; #放置服务器私钥的目录 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://127.0.0.1:8887; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }