CentOS 7 离线安装nginx流程记录

1.下载

下载nginx离线安装包(官网下载,最新稳定版)

http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.25.5-1.el7.ngx.x86_64.rpm

2.上传

上传nginx-1.25.5-1.el7.ngx.x86_64.rpm安装包到服务器

3. 安装nginx

1
2
# 执行安装命令
yum localinstall nginx-1.25.5-1.el7.ngx.x86_64.rpm --disablerepo=*

4.提示缺少依赖

提示缺少依赖:libpcre2-8.so.0()(64bit)

下载依赖

网址:http://rpmfind.net/linux/rpm2html/search.php?query=libpcre2-8.so.0%28%29%2864bit%29&submit=Search+...&system=centos&arch=

下载pcre2-10.23-2.el7.x86_64.rpm

5.上传依赖

上传依赖pcre2-10.23-2.el7.x86_64.rpm到服务器

6.安装依赖

1
yum localinstall pcre2-10.23-2.el7.x86_64.rpm --disablerepo=*

7.再次安装nginx

依赖安装完成后,再次安装nginx

1
yum localinstall nginx-1.25.5-1.el7.ngx.x86_64.rpm  --disablerepo=*

安装完成

8.开启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
# 启动nginx
systemctl start nginx
# nginx开机自启
systemctl enable nginx

#查看nginx状态
systemctl status nginx

# 输出如下内容即启动成功
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2024-06-14 09:32:32 CST; 18s ago
Docs: http://nginx.org/en/docs/
Main PID: 8941 (nginx)
CGroup: /system.slice/nginx.service
├─8941 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─8942 nginx: worker process
├─8943 nginx: worker process
├─8944 nginx: worker process
├─8945 nginx: worker process
├─8946 nginx: worker process
├─8947 nginx: worker process
├─8948 nginx: worker process
└─8949 nginx: worker process

Jun 14 09:32:32 0001 systemd[1]: Starting nginx - high performance web server...
Jun 14 09:32:32 0001 systemd[1]: Started nginx - high performance web server.

nginx已经正常运行!

9.配置反向代理

配置文件位置:/etc/nginx/conf.d/default.conf

编辑配置文件,替换里面的内容如下:

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
# 处理WebSocket连接
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

server {
listen 80;
server_name localhost;

# 配置反向代理
location ^~ /
{
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;

add_header X-Cache $upstream_cache_status;

# 设置 Nginx 缓存
set $static_fileG0ZQTXEv 0;
if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" ) {
set $static_fileG0ZQTXEv 1;
expires 1m;
}
if ( $static_fileG0ZQTXEv = 0 ) {
add_header Cache-Control no-cache;
}
}

#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.env|\.svn|\.project|LICENSE|README.md)
{
return 404;
}

access_log /var/log/nginx/web.access.log;
error_log /var/log/nginx/web.error.log;
}

检查配置文件是不是正常

1
2
3
4
5
6
# 检查配置文件
nginx -t

# 输出下面内容证明配置文件没有错误
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

检查正常后,重载配置生效

1
2
# 重新加载nginx配置
nginx -s reload

CentOS 7 离线安装nginx流程记录
http://www.yangchao.me/posts/115/
作者
小不点
发布于
2024年6月14日
许可协议