Linux安装nginx

本文详细介绍了在Linux环境下下载、安装Nginx稳定版本,处理缺失依赖,编译并配置Nginx,以及创建系统服务脚本以实现开机自启动的过程。
摘要由CSDN通过智能技术生成

下载安装包

下载地址: https://nginx.org/en/download.html

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

image-20221108171720344

上传到Linux并解压

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

tar -zxvf nginx-1.22.0.tar.gz
[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

下载依赖

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

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

演示:

[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、安装gcc库

yum install -y gcc

演示:

[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:
......

2、安装pcre库

yum install -y pcre pcre-devel

演示:

[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.
......

3、安装zlib库

yum install -y zlib zlib-devel

演示:

[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.

4、安装Open SSL

yum install -y openssl openssl-devel

演示:

[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.

再执行./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]# 

编译

make

演示:

[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

安装

make install

演示:

[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 \

启动nginx

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

./nginx  启动

./nginx -s stop  快速停止

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

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

//查看nginx进程
ps -ef|grep nginx

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

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

防火墙

放行端口

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

重启防火墙

firewall-cmd --reload

开启系统服务

创建服务脚本

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

服务内容

[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

重新加载系统服务

systemctl daemon-reload

查看nginx是否在运行

ps -ef | grep nginx

关闭

./nginx -s stop

启动服务

systemctl start nginx.service

开机自启

开机启动服务:

systemctl enable nginx

检查是否开机启动:

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

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

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

以后使用一下命令即可:

启动服务:systemctl start nginx

关闭服务: systemctl stop nginx

重载配置:systemctl reload nginx

重启服务:systemctl restart nginx

开机启动服务:systemctl enable nginx
  • 14
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码上言

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

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

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

打赏作者

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

抵扣说明:

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

余额充值