Nginx安装与使用

参考网站:linux nginx安装以及配置

一、Nginx Linux 版本的基本使用

下载地址:Nginx官方网址

1565086188620

其中,mainline 表示主线(最新)版本;stable 表示稳定版本

或者

在 Linux 中使用 wget 命令下载:

1
wget -c https://nginx.org/download/nginx-1.16.0.tar.gz

1565086440569

二 安装步骤

2.1 解压安装包

1
tar -zxvf nginx-1.16.0.tar.gztar -zxvf nginx-1.16.0.tar.gz

2.2 进入安装目录

1564470825825

2.3 Nginx 配置

2.3.1 使用默认配置(推荐)

1
./configure./configure

这时一般会出现错误,提示我们添加 gcc 环境

1564471268289

2.3.1.1 安装 gcc 环境

1
yum install gcc-c++yum install gcc-c++

注:若 Linux 系统版本是 aliyun,一般已经安装了 gcc 环境;而 centos7 等系统大都需要用户安装

2.3.1.2 继续使用默认配置

1
2
3
4
5
./configure
# 安装到指定路径
./configure --prefix=/usr/local/nginx./configure
# 安装到指定路径
./configure --prefix=/usr/local/nginx

这里提示我们需要安装 PCRE 依赖库:

1564473284232

2.3.1.3 安装 PCRE 依赖库

1
yum install -y pcre pcre-develyum install -y pcre pcre-devel

PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库*。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。*

2.3.1.4 安装 zlib 依赖库

1
yum install -y zlib zlib-develyum install -y zlib zlib-devel

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

2.3.1.5 安装 OpenSSL 安全套接字层密码库

1
yum install -y openssl openssl-develyum install -y openssl openssl-devel

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 Centos 安装 OpenSSL 库。

2.3.1.6 最后再次执行默认配置

1
./configure

1564473993088

成功!

同时,./configure命令会创建一个 Makefile 文件,为 C 语言提供编译需要

1564474485304

1.2.3.2 自定义配置(不推荐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注:将临时文件目录指定为:/var/temp/nginx,需要在 /var 下创建 temp 及 nginx 目录

1.2.3.3 编译并安装 Nginx

1
2
3
4
5
make install
# 或者
make && make installmake install
# 或者
make && make install

make :编译,它从 Makefile 中读取指令,然后编译。

make install :安装,它从 Makefile 中读取指令,安装到指定的位置。

1.2.3.4 查看安装路径

1
whereis nginx

1564474774892

1.2.3.5 安装目录文件分析

1565089768083

  • conf:配置文件
  • html:网页文件
  • logs:日志文件
  • sbin:主要二进制程序

2.4 启动、停止 Nginx

进入 Nginx 安装目录(并不是 Nginx 的解压目录),执行命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
./sbin/nginx # 启动
./nginx # 启动
./nginx -s stop # 停止(此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程)
./nginx -s quit # 停止(此方式停止步骤是待nginx进程处理任务完毕进行停止)
./nginx -s reload # 在提供服务的时候,重新刷新配置文件
kill -INT 5531 # 其中5531就是nginx的主进程号,这种方法叫做nginx信号控制
kill -HUP 5531 # 软重启
# 其实在nginx启动后,会在logs下有一个nginx.pid文件用于记录主进程号
kill -INT 'cat logs/nginx.pid'
kill -HUP 'cat logs/nginx.pid'./sbin/nginx # 启动
./nginx # 启动
./nginx -s stop # 停止(此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程)
./nginx -s quit # 停止(此方式停止步骤是待nginx进程处理任务完毕进行停止)
./nginx -s reload # 在提供服务的时候,重新刷新配置文件
kill -INT 5531 # 其中5531就是nginx的主进程号,这种方法叫做nginx信号控制
kill -HUP 5531 # 软重启
# 其实在nginx启动后,会在logs下有一个nginx.pid文件用于记录主进程号
kill -INT 'cat logs/nginx.pid'
kill -HUP 'cat logs/nginx.pid'

2.4.1 Nginx 信号控制

信号 信号说明
TERM, INT Quick shutdown
QUIT Graceful shutdown 优雅地关闭线程,即等请求结束后再关闭
KILL Halts a stubborn process 停止一个顽固的过程
HUP Configuration reload 配置重新加载 Start the new worker processes with a new configuration 使用新配置启动新的工作进程 Gracefully shutdown the old worker processes 优雅地关闭旧的工作进程,即工作中的暂时不变,先变工作中的进程 该命令也称为:软重启
USR1 Reopen the log files 重新打开日志文件 重读日志,在日志按月/日分割时有用 例如我需要每天一个日志文件,但nginx认的并不是文件名,而是内存节点 就算重命名或新建名称一致的文件,还是一直往旧日志文件上写,这时就用USER1
USR2 Upgrade Executable on the fly 动态升级可执行文件(平滑地升级)
WINCH Gracefully shutdown the worker processes 正常关闭工作进程(配合USER2来进行升级)

2.4.2 查看 Nginx 配置是否有误

1
./sbin/nginx -t./sbin/nginx -t

2.4.3 查看 Nginx 是否启动成功

1
2
ps aux|grep nginx
#这里能够看到,nginx分为两个进程:主进程-子进程,子进程负责工作,主进程负责管控子进程

1564475996668

或者

看能不能访问 Nginx 欢迎首页

1564479236790

同时,也可以在外部局域网内使用浏览器进行访问欢迎页面

1564479385795

PS:如果 curl 访问成功了,但在外部访问失败,则应该是 Linux 防火墙对端口进行了拦截

2.5 修改 Nginx 端口号

Nginx 的默认端口号为:80

2.5.1 进入配置文件文件夹

1
cd /usr/local/nginx/confcd /usr/local/nginx/conf

注:这里是usr下的安装路径,不是我们解压的安装包路径

2.5.2 备份配置文件(可略)

1
cp nginx.conf nginx.conf.backcp nginx.conf nginx.conf.back

2.5.3 编辑 nginx.conf 配置文件

1
vi nginx.conf

1564475593864

将端口号修改为 81(自定);Esc + :wq(保存并退出)

记住需要重新启动(推荐)

1
2
3
./nginx -s quit
./nginx./nginx -s quit
./nginx

或者

重新刷新配置

1
./nginx -s reload./nginx -s reload

2.6 设置 Nginx 开机自启动

1
vi /etc/rc.localvi /etc/rc.local

2.6.1 添加语句

1
/usr/local/nginx/sbin/nginx

1564476563993

2.6.2 设置权限

1
chmod 755 /etc/rc.local





  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2024 Liangxj
  • 访问人数: | 浏览次数: