YiMo.一茉 YiMo.一茉
首页
导航站
  • Java基础知识
  • Java进阶
  • 设计模式
  • Spring
  • SpringMVC
  • SpringBoot
  • SpringCloud
  • Redis
  • MySQL

    • MySQL-基础
    • MySQL-进阶
  • HTML
  • CSS
  • Vue2
  • Vue3
  • uni-app
  • 博客实战教程
  • 图书馆实战教程
  • 网关
  • 数据缓存
  • 搜索
  • 注册中心
  • 消息队列
  • RPC框架
  • Nginx
  • Docker
  • Gitlab
  • Kafka
  • Linux
  • Linux环境搭建

    • 导航
    • 安装JDK
    • 安装MySQL
    • 安装Nginx
    • 安装Tomcat
    • 部署SpringBoot项目
    • 安装node.js
    • 部署Vue项目
    • 安装Redis
  • 每日一更
  • Java基础
  • 前端基础
  • 友链
  • 学习分享
  • 生活
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

YiMO.一茉

拥抱生活,追寻真实的快乐
首页
导航站
  • Java基础知识
  • Java进阶
  • 设计模式
  • Spring
  • SpringMVC
  • SpringBoot
  • SpringCloud
  • Redis
  • MySQL

    • MySQL-基础
    • MySQL-进阶
  • HTML
  • CSS
  • Vue2
  • Vue3
  • uni-app
  • 博客实战教程
  • 图书馆实战教程
  • 网关
  • 数据缓存
  • 搜索
  • 注册中心
  • 消息队列
  • RPC框架
  • Nginx
  • Docker
  • Gitlab
  • Kafka
  • Linux
  • Linux环境搭建

    • 导航
    • 安装JDK
    • 安装MySQL
    • 安装Nginx
    • 安装Tomcat
    • 部署SpringBoot项目
    • 安装node.js
    • 部署Vue项目
    • 安装Redis
  • 每日一更
  • Java基础
  • 前端基础
  • 友链
  • 学习分享
  • 生活
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Nginx

  • Docker

  • Gitlab

  • Kafka

  • Linux

  • Linux环境搭建

    • 导航
    • 安装JDK
    • 安装MySQL
    • 安装Nginx
      • 1、下载安装包
      • 2、上传到Linux并解压
      • 3、下载依赖
        • 3.1、安装gcc库
        • 3.2、安装pcre库
        • 3.3、安装zlib库
        • 3.4、安装Open SSL
      • 4、编译
      • 5、安装
      • 6、启动nginx
      • 7、防火墙
      • 8、开启系统服务
      • 9、开机自启
    • 安装Tomcat
    • 部署SpringBoot项目
    • 安装node.js
    • 部署Vue项目
    • 安装Redis
  • 运维
  • Linux环境搭建
YiMO.一茉
2022-11-04
目录

安装Nginx

# 安装Nginx

# 1、下载安装包

下载地址: https://nginx.org/en/download.html (opens new window)

选择稳定版本:Stable version中的 nginx-1.22.0 (opens new window)(版本以后还会变,根据当前版本下载即可)

image-20221108171720344

# 2、上传到Linux并解压

上传到/usr/local目录下,进入到该目录下,解压压缩包

tar -zxvf nginx-1.22.0.tar.gz
1
[root@Captian local]# tar -zxvf nginx-1.22.0.tar.gz
nginx-1.22.0/
nginx-1.22.0/auto/
nginx-1.22.0/conf/
nginx-1.22.0/contrib/
nginx-1.22.0/src/
nginx-1.22.0/configure
1
2
3
4
5
6
7

# 3、下载依赖

进入到解压文件根目录,执行命令行,检查安装环境

./configure  --prefix=/usr/local/nginx
1

演示:

[root@Captian nginx-1.22.0]# ./configure  --prefix=/usr/local/nginx
checking for OS
 + Linux 4.18.0-348.7.1.el8_5.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

[root@Captian nginx-1.22.0]# 
1
2
3
4
5
6
7
8

# 3.1、安装gcc库

yum install -y gcc
1

演示:

[root@Captian /]# yum install -y gcc
Last metadata expiration check: 2:28:53 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Dependencies resolved.
=============================================================================================================================================================
 Package                                  Architecture                    Version                                   Repository                          Size
=============================================================================================================================================================
Installing:
......
1
2
3
4
5
6
7
8

# 3.2、安装pcre库

yum install -y pcre pcre-devel
1

演示:

[root@Captian /]# yum install -y pcre pcre-devel
Last metadata expiration check: 2:35:09 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package pcre-8.42-4.el8.x86_64 is already installed.
Dependencies resolved.
......
1
2
3
4
5

# 3.3、安装zlib库

yum install -y zlib zlib-devel
1

演示:

[root@Captian /]# yum install -y zlib zlib-devel
Last metadata expiration check: 2:48:44 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package zlib-1.2.11-13.el8.x86_64 is already installed.
Dependencies resolved.
1
2
3
4

# 3.4、安装Open SSL

yum install -y openssl openssl-devel
1

演示:

[root@Captian /]# yum install -y openssl openssl-devel
Last metadata expiration check: 2:49:43 ago on Fri 12 Aug 2022 02:23:13 PM CST.
Package openssl-1:1.1.1k-5.el8_5.x86_64 is already installed.
Dependencies resolved.
1
2
3
4

再执行./configure --prefix=/usr/local/nginx

将页面拉到最后几十行,如下所示即OK

Configuration summary
  + using system PCRE2 library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@Captian nginx-1.22.0]# 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 4、编译

make
1

演示:

[root@Captian nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory '/usr/local/nginx-1.22.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
	src/core/nginx.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_log.o \
	src/core/ngx_log.c
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/ngx_palloc.o \
	src/core/ngx_palloc.c
1
2
3
4
5
6
7
8
9
10
11
12

# 5、安装

make install
1

演示:

[root@Captian nginx-1.22.0]# make
make -f objs/Makefile
make[1]: Entering directory '/usr/local/nginx-1.22.0'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g  -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
	-o objs/src/core/nginx.o \
1
2
3
4
5

# 6、启动nginx

进入安装好的目录/usr/local/nginx/sbin

./nginx  启动

./nginx -s stop  快速停止

./nginx -s quit 优雅关闭,在退出前完成已经接受的连接请求

./nginx -s reload 重新加载配置

//查看nginx进程
ps -ef|grep nginx
1
2
3
4
5
6
7
8
9
10

查看虚拟机IP地址ip addr,打开浏览器访问ip

若不能访问,请关掉防火墙

# 7、防火墙

放行端口

firewall-cmd --zone=public --add-port=80/tcp --permanent
1

重启防火墙

firewall-cmd --reload
1

# 8、开启系统服务

创建服务脚本

vim /usr/lib/systemd/system/nginx.service
1

服务内容

[Unit]
#必须加守护神不然会报错
Description=nginx -web server
# 指定启动nginx之前需要其他的其他服务,如network.target等
After=network.target remote-fs.target nss-lookup.target

[Service]
# Type为服务的类型,仅启动一个主进程的服务为simple,需要启动若干子进程的服务为forking
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 设置执行systemctl start nginx后需要启动的具体命令。
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 设置执行systemctl reload nginx后需要执行的具体命令。
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 设置执行systemctl stop nginx后需要执行的具体命令。
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
# 设置在什么模式下被安装,设置开机启动的时候需要有这个。
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

重新加载系统服务

systemctl daemon-reload
1

查看nginx是否在运行

ps -ef | grep nginx
1

关闭

./nginx -s stop
1

启动服务

systemctl start nginx.service
1

# 9、开机自启

开机启动服务:

systemctl enable nginx
1

检查是否开机启动:

systemctl is-enabled nginx
1
[root@Captian ~]# systemctl is-enabled nginx
enabled

1
2
3

会发现命令有做一个软连接,这个就是为了开机启动:

Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

以后使用一下命令即可:

启动服务:systemctl start nginx

关闭服务: systemctl restart nginx

重载配置:systemctl reload nginx

重启服务:systemctl restart nginx

开机启动服务:systemctl enable nginx
1
2
3
4
5
6
7
8
9
上次更新: 2023/03/05, 20:36:52

← 安装MySQL 安装Tomcat→

最近更新
01
项目实战部署
03-16
02
Maven插件
03-16
03
软件安装
03-16
更多文章>
Theme by Vdoing | Copyright © 2022-2023 YiMO.一茉 | 苏ICP备2022035828 | MIT License | 一茉 All rights reserved.
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×