学习Nginx(二):版本介绍和安装

​​​​​​​版本

        Nginx官方定义了Mainline、Stable、Legacy三种版本。

1. Mainline version(主线版本)

        该版本包含最新的功能和bug修复,被视为开发版,即正在活跃开发中的版本。其版本号通常为单数,例如1.25.5。这个版本的更新较快,可能会引入新的功能和修复,但也可能存在尚未解决的bug。

2. Stable version(稳定版本)

        最新稳定版适合生产环境使用。其版本号通常为双数,例如1.26。这个版本经过充分测试和验证,bug较少,适合用于承载实际业务。因此,通常建议在生产环境中使用此版本。

3. Legacy versions(历史版本)

        这些版本是之前发布的稳定版,对于需要特定旧版本的兼容性或安全性支持的用户有用。然而,这些版本一般不推荐用于新项目,除非有特殊需求。

源码

1. 只读存储库

2. GitHub库

3. 源URL

4. Linux包

        稳定版本和主线版本的相关Linux软件包。

安装

        Nginx一般可以使用apt/yum/dnf来安装二进制包,若需使用特定的功能模块,则需要使用源码安装。

  • OS:Rocky Linux 9.3 (Blue Onyx)

1. 二进制包安装

1.1. 检查当前系统可安装列表

[root@RockyLinux9 ~]# dnf list nginx
Available Packages
nginx.x86_64                   1:1.20.1-14.el9_2.1                   appstream

1.2. 配置官方仓库

[root@RockyLinux9 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[root@RockyLinux9 ~]# dnf list nginx
Available Packages
nginx.x86_64                    1:1.26.0-1.el9.ngx                nginx-stable

1.3. 安装

[root@RockyLinux9 ~]# dnf install -y nginx

1.4. 服务启动并配置开机自启

[root@RockyLinux9 ~]# systemctl enable nginx --now

1.5. 查看服务状态

[root@RockyLinux9 ~]# systemctl status nginx

1.6. 查看版本及默认编译依赖项

[root@RockyLinux9 ~]# nginx -V
nginx version: nginx/1.26.0
built by gcc 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

1.7. 二进制包安装的默认位置

[root@RockyLinux9 ~]# rpm -ql nginx
/etc/nginx					# 配置文件目录
......
/usr/lib64/nginx/modules				# 模块安装目录
/usr/sbin/nginx			# 二进制程序
/usr/share/nginx/html				# 网站根目录
/var/cache/nginx				# 缓存目录
/var/log/nginx				# 日志目录

1.8. 查看Web页面

  • URL:http://ip(若外部无法访问,请查看防火墙状态)

2. 源码编译安装

2.1. 安装相关编译工具

[root@RockyLinux9 ~]# dnf update
[root@RockyLinux9 ~]# dnf install -y gcc make pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.2. 创建运行用户

[root@RockyLinux9 ~]# useradd -r -s /usr/sbin/nologin nginx

2.3. 下载源码包并解压

[root@RockyLinux9 ~]# curl -O https://nginx.org/download/nginx-1.26.0.tar.gz
[root@RockyLinux9 ~]# tar xf nginx-1.26.0.tar.gz
[root@RockyLinux9 ~]# cd nginx-1.26.0/

2.4. 编译安装

[root@RockyLinux9 nginx-1.26.0]# mkdir /usr/local/nginx
[root@RockyLinux9 nginx-1.26.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@RockyLinux9 nginx-1.26.0]# make && make install

2.5. 修改配置目录用户及属组

[root@RockyLinux9 nginx-1.26.0]# chown -R nginx:nginx /usr/local/nginx/
[root@RockyLinux9 nginx-1.26.0]# ll /usr/local/nginx/
total 4
drwxr-xr-x. 2 nginx nginx 4096 May  9 22:32 conf	# 配置文件目录
drwxr-xr-x. 2 nginx nginx   40 May  9 22:32 html	# 网站根目录
drwxr-xr-x. 2 nginx nginx    6 May  9 22:32 logs	# 日志目录
drwxr-xr-x. 2 nginx nginx   19 May  9 22:32 sbin	# 二进制程序目录

2.6. 创建程序软连接

[root@RockyLinux9 nginx-1.26.0]# ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
'/usr/sbin/nginx' -> '/usr/local/nginx/sbin/nginx'

2.7. 查看版本及编译属性

[root@RockyLinux9 nginx-1.26.0]# nginx -V
nginx version: nginx/1.26.0
built by gcc 11.4.1 20230605 (Red Hat 11.4.1-2) (GCC)
built with OpenSSL 3.0.7 1 Nov 2022
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module

2.8. 启动服务

[root@RockyLinux9 ~]# nginx
[root@RockyLinux9 ~]# ps -ef|grep nginx
root       44079       1  0 22:40 ?        00:00:00 nginx: master process nginx
nginx      44080   44079  0 22:40 ?        00:00:00 nginx: worker process
root       44082   38412  0 22:40 pts/0    00:00:00 grep --color=auto nginx

2.9. 查看Web页面

  • URL:http://ip

2.10. 停止服务

[root@RockyLinux9 ~]# nginx -s stop

2.11. 编写nginx服务文件

[root@RockyLinux9 ~]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000

[Install]
WantedBy=multi-user.target

2.12. 修改配置文件nginx.conf,删除pid注释

[root@RockyLinux9 ~]# vim /usr/local/nginx/conf/nginx.conf
pid        logs/nginx.pid;

# 校验文件
[root@RockyLinux9 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

2.13. 启动服务

# 加载服务脚本
[root@RockyLinux9 ~]# systemctl daemon-reload
# 启动服务
[root@RockyLinux9 ~]# systemctl start nginx
# 查看服务状态
[root@RockyLinux9 ~]# systemctl status nginx

2.14. 导入man手册

# 拷贝文件
[root@RockyLinux9 ~]# cp nginx-1.26.0/man/nginx.8 /usr/share/man/man8/
# 更新man db库
[root@RockyLinux9 ~]# mandb
# 查看nginx确认
[root@RockyLinux9 ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/local/nginx /usr/share/man/man8/nginx.8

来自: 学习Nginx(二):版本介绍和安装

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux技术宅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值