Nginx 环境搭建和基本配置
更新时间:2018-12-11 | 阅读量(1,117)
#1-Nginx基本配置
## 1.Nginx 环境搭建
###1.Mac下搭建 Nginx
####1.brew 简介
brew又叫Homebrew,是Mac中的一款软件包管理工具,通过brew可以很方便的在Mac中安装软件或者是卸载软件。一般Mac电脑会默认安装有brew, 常用指令如下:
```
- brew 搜索软件
brew search nginx
- brew 安装软件
brew install nginx
- brew 卸载软件
brew uninstall nginx
- brew 升级
sudo brew update
- 查看安装信息(经常用到, 比如查看nginx安装,配置文件,虚拟主机等目录)
sudo brew info nginx
- 查看已经安装的软件
brew list
```
####2.brew安装nginx
安装nginx
可以用brew很方便地安装nginx.
`sudo brew install nginx`
#####1.启动nginx服务
```
sudo brew services start nginx
```
浏览器访问:http://localhost:8080, 如果出现nginx界面,说明启动成功.
```
1)查看nginx版本
nginx -v
2)查看nginx进程是否启动
lsof -i:80 或者 ps -ef | grep nginx
3)杀死进程
kill -QUIT 进程PID
4)查看端口号是否被占用
netstat -an | grep 8090
```
#####2.关闭nginx服务
`sudo brew services stop nginx`
另外几个比较方便的指令
#####3.重新加载nginx
`nginx -s reload`
#####4.停止nginx
`nginx -s stop`
####3.Nginx 安装路径
1.执行 `which nginx 或 brew info nginx` 查看 Nginx 路径信息
```
- 这个是nginx配环境变量
/usr/local/bin/nginx
- 查看nginx安装的路径
/usr/local/Cellar/nginx/1.15.1
```
2.查看 Nginx 配置文件路径
```
- 查看nginx的配置文件, 默认的端口是8080
/usr/local/etc/nginx/nginx.conf
```
3.默认的网站目录
```
/usr/local/var/www
```
### 2.Window下搭建Nginx
https://blog.csdn.net/kingscoming/article/details/79042874
https://www.imooc.com/article/15667
### 3.Linux下搭建Nginx
https://segmentfault.com/a/1190000007116797
http://www.runoob.com/linux/nginx-install-setup.html
## 2.Nginx 基本配置
http://nginx.org/en/docs/
1.基本配置
http://nginx.org/en/docs/http/ngx_http_core_module.html
```js
# 修改配置文件需要重启服务器
user liujun liu; # 1.Nginx worker 进程运行的用户及用户组
worker_processes 1; # 2.Nginx worker 进程个数
events {
worker_connections 1024; #3.每个 worker 的最大连接数
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65; # 4.keepalive 超时时间
# 5.创建虚拟主机(主机名称为:localhost)
server {
listen 8090;
server_name localhost;
location / { # 6.location的值为 / , 代表匹配所有请求
root html; # 7.项目发布的目录, 例如:/usr/local/var/www/webapps/jd
index index.html index.htm; # 8.指定首页索引文件的名称
}
}
}
# 注意该配置文件的末尾不能有多余的空行
```
如下图:

浏览器访问: http://127.0.0.1:8090/
**2.修改虚拟主机的名称**
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建虚拟主机
server {
listen 8090;
# 2.主机名称为:www.liujun.com 或者 localhost
server_name www.liujun.com localhost; # 3.本地测试要在host添加 www.liujun.com
location / {
root html;
index index.html index.htm;
}
}
}
```
浏览器访问: http://127.0.0.1:8090/ 或者 http://localhost:8090/ 或者 http://www.liujun.com:8090/
> mac 电脑 hosts 文件在 /etc/hosts
**3.修改发布的目录**
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建虚拟主机(主机名称为:localhost)
server {
listen 8090;
server_name localhost;
location / {
root /usr/local/var/www/webapps/blog; # 2.修改发布项目的目录
index index.html index.htm;
}
}
}
```
浏览器访问: http://127.0.0.1:8090/ 或者 http://localhost:8090/