Linux安装Nginx

Linux安装Nginx

𝓓𝓸𝓷 Lv6

一、简介

Nginx(“engine x”)是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
在高连接并发的情况下,Nginx是Apache服务器不错的替代品

  • Nginx特点

1.高性能,高并发
2.轻量且高扩展性
3.高可靠性
4.支持热部署,nginx可以在运行期间,更新迭代,代码部署
5.大多数公司都在用nginx
6.nginx使用的是epool网络模型

  • Nginx术语

Web服务器又称为WWW服务器或网站服务器,主要用于提供网上信息浏览服务,Nginx就是一款高性能的Web服务器软件

二、下载

https://nginx.org/en/download.html

image-20230206163701082

下载Nginx稳定版本:

  • 蓝色方框为Linux版本
  • 红色方框为Windows版本

三、安装依赖环境

1
2
3
4
5
6
7
8
# c编译器
yum -y install gcc gcc-c++ autoconf automake make
# 解析正则的pcre库
yum install -y pcre pcre-devel
# 添加对gzip的支持
yum install -y zlib zlib-devel
# SSL
yum -y install make gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

四、关闭防火墙

1
2
[root@server01 ~]# systemctl stop firewalld 
[root@server01 ~]# systemctl disable firewalld

五、关闭selinx

1
2
[root@server01 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux
[root@server01 ~]# setenforce 0

六、安装Nginx

安装三种方法: yum源安装,源码安装

(1) yum源安装方法

这里强烈建议安装stream模块,stream是用来处理tcp转发的。不然后面想用没有会很麻烦!!! 如果真的用不到stream可以不输入–with-stream

1.1 配置yum源
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
26
27
28
29
30
31
32
33
34
35

[root@web01 ~]# vim /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

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true



Centos8配置阿里Yum源:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
或者
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

cd /etc/yum.repos.d/

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*

yum clean all && yum makecache

1.2 yum清空缓存
1
2
[root@server01 ~]# yum clean all
[root@server01 ~]# yum makecache
1.3 安装
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
26
[root@server01 ~]# yum install nginx
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.ustc.edu.cn
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.22.1-1.el7.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================
Package Arch Version Repository Size
===========================================================================================
Installing:
nginx x86_64 1:1.22.1-1.el7.ngx nginx-stable 797 k

Transaction Summary
===========================================================================================
Install 1 Package

Total download size: 797 k
Installed size: 2.8 M
Is this ok [y/d/N]: y
1.4 检查安装
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
[root@server01 ~]# nginx -v
nginx version: nginx/1.22.1

[root@server01 ~]# nginx -V
nginx version: nginx/1.22.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
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-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'


[root@server01 ~]# yum list installed |grep -i nginx
nginx.x86_64 1:1.22.1-1.el7.ngx @nginx-stable


[root@server01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.1
/usr/share/doc/nginx-1.22.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx


[root@server01 ~]# rpm -qi nginx
Name : nginx
Epoch : 1
Version : 1.22.1
Release : 1.el7.ngx
Architecture: x86_64
Install Date: Tue 07 Feb 2023 03:14:31 PM CST
Group : System Environment/Daemons
Size : 2917123
License : 2-clause BSD-like license
Signature : RSA/SHA256, Wed 19 Oct 2022 06:58:05 PM CST, Key ID abf5bd827bd9bf62
Source RPM : nginx-1.22.1-1.el7.ngx.src.rpm
Build Date : Wed 19 Oct 2022 06:48:30 PM CST
Build Host : ip-10-1-17-124.eu-central-1.compute.internal
Relocations : (not relocatable)
Vendor : NGINX Packaging <nginx-packaging@f5.com>
URL : https://nginx.org/
Summary : High performance web server
Description :
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server.

1.5 查看nginx配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@server01 ~]# rpm -qc nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params

[root@server01 ~]# which nginx
/usr/sbin/nginx

[root@server01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@server01 ~]# grep pid /etc/nginx/nginx.conf
pid /var/run/nginx.pid;
1.6 启动nginx
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
26
27
28
29
30
31
32
[root@server01 ~]# systemctl start nginx

[root@server01 ~]# ps -ef|grep nginx
root 4587 1 0 15:24 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 4588 4587 0 15:24 ? 00:00:00 nginx: worker process
nginx 4589 4587 0 15:24 ? 00:00:00 nginx: worker process
root 4597 4160 0 15:24 pts/2 00:00:00 grep --color=auto nginx

[root@server01 ~]# netstat -lntp |grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5359/nginx: master
tcp6 0 0 :::80 :::* LISTEN 5359/nginx: master

[root@server01 ~]# lsof -i:80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 5359 root 6u IPv4 62307 0t0 TCP *:http (LISTEN)
nginx 5359 root 7u IPv6 62308 0t0 TCP *:http (LISTEN)
nginx 5360 nginx 6u IPv4 62307 0t0 TCP *:http (LISTEN)
nginx 5360 nginx 7u IPv6 62308 0t0 TCP *:http (LISTEN)
nginx 5361 nginx 6u IPv4 62307 0t0 TCP *:http (LISTEN)
nginx 5361 nginx 7u IPv6 62308 0t0 TCP *:http (LISTEN)

[root@server01 ~]# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 09 Feb 2023 04:51:55 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"
Accept-Ranges: bytes

1.7 配置nginx自动启动
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[root@server01 ~]# vim /usr/lib/systemd/system/nginx.service 

[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/tmp/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
ExecQuit=/usr/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target



Nginx官方自带systemd配置:
[root@server01 ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)"
ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)"

[Install]
WantedBy=multi-user.target


参数说明:
[Unit]
#Description指定当前服务的简单描述。
Description=nginx网站代理服务
#After表示当前服务在network.target之后启动,可以指定多个服务,以空格隔开。
After=network.target

[Service]
#ExecStart将以fork()方式启动,此时父进程将会退出,子进程将成为主进程。
Type=forking# ExecStart指定启动进程时执行的命令。
# ExecReload指定当该服务被要求重新载入配置时所执行的命令。另外,还有一个特殊的环境变量 $MAINPID 可用于表示主进程的PID,例如可以这样使用:/bin/kill -HUP $MAINPID。强烈建议将 ExecReload= 设为一个能够确保重新加载配置文件的操作同步完成的命令行。
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit#web服务设置私有/tmp或/var/tmp目录
PrivateTmp=true

[Install]
# “WantedBy=multi-user.target”表示当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
WantedBy=multi-user.target



[root@server01 ~]# systemctl is-enabled nginx
disabled
[root@server01 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.



systemctl命令:
# systemctl daemon-reload

# systemctl start nginx
# systemctl restart nginx
# systemctl stop nginx
# systemctl status nginx.service

# systemctl enable nginx
# systemctl disable nginx.service

# systemctl list-units --type=service
1.8 访问nginx主页
1
http://10.11.68.115

image-20230210161527913

(2) Epel源安装方法

EPEL(Extra Packages for Enterprise Linux), 是由Fedora Special Interest Group维护的Enterprise Linux(RHEL、CentOS)中经常用到的包。官方主页:https://fedoraproject.org/wiki/EPEL

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
26
27
1.备份epel源(如有配置其他epel源)
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup

2 epel源下载
下载地址:https://mirrors.aliyun.com/epel/
epel过期源(epel-archive):https://developer.aliyun.com/mirror/epel-archive

下载新repo 到/etc/yum.repos.d:
[root@server01 ~]# wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

或者直接安装:
[root@server01 ~]# yum -y install epel-release

[root@server01 ~]# ll /etc/yum.repos.d/ep*
-rw-r--r--. 1 root root 951 Oct 3 2017 /etc/yum.repos.d/epel.repo
-rw-r--r--. 1 root root 1050 Oct 3 2017 /etc/yum.repos.d/epel-testing.repo

3.yum清空缓存
[root@server01 ~]# yum clean all && yum makecache

4.安装
root@server01 ~]# yum install nginx

5, 启动nginx
[root@server01 ~]# systemctl start nginx

(3) 源码编译安装
3.1 下载
1
[root@server01 ~]# wget https://nginx.org/download/nginx-1.22.1.tar.gz
3.2 解压
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@server01 ~]# tar xvf nginx-1.22.1.tar.gz 
[root@server01 ~]# cd nginx-1.22.1
[root@server01 nginx-1.22.1]# ll
total 804
drwxr-xr-x. 6 1001 1001 4096 Feb 10 10:36 auto
-rw-r--r--. 1 1001 1001 317399 Oct 19 16:02 CHANGES
-rw-r--r--. 1 1001 1001 485035 Oct 19 16:02 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Feb 10 10:36 conf
-rwxr-xr-x. 1 1001 1001 2590 Oct 19 16:02 configure
drwxr-xr-x. 4 1001 1001 72 Feb 10 10:36 contrib
drwxr-xr-x. 2 1001 1001 40 Feb 10 10:36 html
-rw-r--r--. 1 1001 1001 1397 Oct 19 16:02 LICENSE
drwxr-xr-x. 2 1001 1001 21 Feb 10 10:36 man
-rw-r--r--. 1 1001 1001 49 Oct 19 16:02 README
drwxr-xr-x. 9 1001 1001 91 Feb 10 10:36 src

3.3 查看帮助
1
[root@server01 nginx-1.22.1]# ./configure --help
3.4 安装依赖包
1
yum -y install make gcc pcre  pcre-devel zlib  zlib-devel openssl openssl-devel
3.5 安装
1
[root@server01 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --with-stream  --with-http_stub_status_module --with-http_ssl_module && make && make install 
3.6 设置环境PATH变量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@server01 ~]# vim /etc/profile
PATH=$PATH:/usr/local/nginx/sbin

[root@server01 ~]# source /etc/profile

[root@server01 ~]# nginx -v
nginx version: nginx/1.22.1

[root@server01 ~]# 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

如果不想设置环境变量可以创建软链接:
[root@server01 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx
[root@server01 ~]# ll /usr/local/sbin/
total 0
lrwxrwxrwx. 1 root root 27 Feb 10 16:20 nginx -> /usr/local/nginx/sbin/nginx

/usr/bin和/usr/sbin放置常用可执行程序,sbin表示需要管理员权限,因此推荐将nginx放入/usr/local/sbin目录
3.7 启动nginx
1
2
3
4
5
6
7
8
[root@server01 ~]# which nginx
/usr/local/nginx/sbin/nginx

[root@server01 ~]# /usr/local/nginx/sbin/nginx
或者
[root@server01 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
或者
[root@server01 ~]# nginx
3.8 停止nginx
  • 立即停止

Nginx程序允许传递选项-s表示发送信号到主进程,如果后面跟上stop表示停止服务

1
[root@server01 ~]# nginx -s stop
  • 从容停止

Nginx提供从容停止方式quit,是在完成当前工作任务后再停止

1
[root@server01 ~]# nginx -s quit
  • 强制停止
1
2
3
4
5
[root@server01 ~]# pkill nginx

[root@server01 ~]# kill nginx主进程PID

[root@server01 ~]# killall nginx
3.9 手工配置启动脚本

https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
Should work on RHEL, Fedora, CentOS. Tested on CentOS 5.
Save this file as /etc/init.d/nginx:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/subsys/nginx

make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}

start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}

stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}

restart() {
configtest || return $?
stop
sleep 1
start
}

reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $prog -HUP
retval=$?
echo
}

force_reload() {
restart
}

configtest() {
$nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
status $prog
}

rh_status_q() {
rh_status >/dev/null 2>&1
}

case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
(4) 注意事项
1
2
3
4
5
进入sbin文件夹中 执行./nginx启动nginx,至此nginx的安装就结束了,浏览器输入IP:端口号查看是否可以进入nginx主界面,进入则成功。

注意nginx默认端口号是80.需要提前去阿里云安全组中开放端口。同时把本机的防火墙关闭。

如果想要修改端口号可以去conf下的nginx.conf中修改,修改完成后去sbin文件夹中执行./nginx -s reload重启nginx

七、卸载nginx

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
1.停止nginx进程
[root@server01 ~]# systemctl stop nginx

[root@server01 ~]# which nginx
[root@server01 ~]# /usr/sbin/nginx -s stop

2.卸载nginx安装包
[root@server01 yum.repos.d]# rpm -qa nginx
[root@server01 yum.repos.d]# yum remove nginx-1.22.1-1.el7.ngx.x86_64

3.删除nginx相关文件
[root@server01 ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.22.1
/usr/share/doc/nginx-1.22.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx


[root@server01 ~]# systemctl list-units --type=service|grep nginx
● nginx.service loaded failed failed nginx - high performance web server

[root@server01 ~]# systemctl disable nginx.service

[root@server01 ~]# rm -rf /etc/nginx
[root@server01 ~]# rm -rf /usr/lib/systemd/system/nginx*
[root@server01 ~]# rm -rf /usr/lib64/nginx*
[root@server01 ~]# rm -rf /usr/libexec/initscripts/legacy-actions/nginx*
[root@server01 ~]# rm -rf /usr/sbin/nginx*
[root@server01 ~]# rm -rf /usr/share/doc/nginx*
[root@server01 ~]# rm -rf /usr/share/nginx*
[root@server01 ~]# rm -rf /var/cache/nginx
[root@server01 ~]# rm -rf /var/log/nginx
[root@server01 ~]# rm -rf /usr/share/man/man8/nginx.8.gz

八、Nginx配置文件

Nginx服务器安装完成后,默认安装时自带的配置文件全部存储在conf目录下,并且为了备份还原,每个配置文件都提供了一个以.default结尾的备份文件,其中nginx.conf为Nginx默认的主配置文件。

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1.配置文件结构
main
events {...}
http{
server{
location {...}
}
}

Nginx的配置文件主要由main、events、http、server和location 5个块组成。

| 块 | 描述 |
| -------- | ------------------------------------------------------------ |
| main | 主要控制Nginx子进程所属的用户和用户组、派生子进程数、错误日志位置与级别、pid位置、子进程优先级、进程对应CPU、进程能够打开的文件描述符数目等 |
| events | 控制Nginx处理连接的方式 |
| http | Nginx处理http请求的主要配置块,大多数配置都在这里面进行 |
| server | Nginx中主机的配置块,可用于配置多个虚拟主机 |
| location | server中对应目录级别的控制块,可以有多个 |

[root@server01 conf]# grep -v "#" nginx.conf|grep -v '^$'
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}


worker_processes表示参数,1表示指令,当一个参数含有多个指令时需要使用{},且每个指令以;结尾
配置文件中引入的文件路径可以是相对路径也可以是绝对路径

2.默认指令说明
| 指令 | 说明 |
| ------------------ | -------------------------------------------------------- |
| worker_processes | 配置Nginx的工作进程数,一般设为CPU总核数或者总核数的两倍 |
| worker_connections | 配置Nginx允许单个进程并发连接的最大请求数 |
| include | 用 于引入配置文件 |
| default_type | 设置默认文件类型 |
| sendfile | 默认值为on,表示开启高效文件传输模式 |
| keepalive_timeout | 设置长连接超时时间(单位: 秒) |
| listen | 监听端口,默认监听80端口 |
| server_name | 设置主机域名 |
| root | 设置主机站点根目录地址 |
| index | 指定默认索引文件 |
| error_page | 自定义错误页面 |
  • Title: Linux安装Nginx
  • Author: 𝓓𝓸𝓷
  • Created at : 2024-06-05 11:23:09
  • Updated at : 2024-07-20 05:15:41
  • Link: https://www.zhangdong.me/nginx-installation.html
  • License: This work is licensed under CC BY-NC-SA 4.0.
评论