常用版本介绍
- 开源版
- https://nginx.org/
- 纯净版,做二次开发难度较大
- 商业版
- https://www.nginx.com/
- 集成了大部分功能,可以直接使
- openresty
- https://openresty.org/cn/
- 通过 Lua 扩展 NGINX 实现的可伸缩的 Web 平台
- Tengine
- https://tengine.taobao.org/
- Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
安装
# 安装必要的库
yum install -y gcc pcre pcre-devel zlib zlib-devel
# 下载并安装
wget https://nginx.org/download/nginx-1.23.4.tar.gz
tar zxf nginx-1.23.4.tar.gz
cd nginx-1.23.4
./configure --prefix=/usr/local/nginx
make && make install
常用命令
cd /usr/local/nginx/sbin
# 查看帮助
./nginx -h
# 启动
./nginx
#快速停止
./nginx -s stop
#优雅关闭,退出前完成已经接受的请求
./nginx -s quit
#重新加载配置
./nginx -s reload
关闭防火墙
如果正常启动后不能正常访问,则可能是防火墙导致的,本地环境可以直接禁用掉防火墙
# 关闭并禁止开机启动
systemctl stop firewalld
systemctl disable firewalld
安装成系统服务
vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - web server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 保存以上内容后,重新加载系统服务
systemctl daemon-reload
# 启动nginx
systemctl start nginx
systemctl status nginx
# 开机启动
systemctl enable nginx.service
目录结构
[root@localhost nginx]# tree conf/ html/ logs/ sbin/
conf/
├── fastcgi.conf
├── fastcgi.conf.default
├── fastcgi_params
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types
├── mime.types.default
├── nginx.conf #主要配置文件,会引用其它配置文件
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params
├── uwsgi_params.default
└── win-utf
html/ #默认静态网站目录
├── 50x.html
└── index.html
logs/
├── access.log #访问日志
├── error.log #错误日志
└── nginx.pid #进程id
sbin/
└── nginx #主进程文件