安装 Nginx 服务器
# 安装 Nginx 服务
1 更新索引 :sudo apt update
2 安装 nginx :sudo apt install nginx
3 查看版本:nginx -v
4 管理 nginx
启动 nginx:sudo systemctl start nginx
停止 nginx:sudo systemctl stop nginx
重启 nginx:sudo systemctl restart nginx
设置 nginx 开机自启:sudo systemctl enable nginx
查看 nginx 状态:sudo systemctl status nginx
5 测试 nginx 是否成功:sudo nginx -t 如果测试成功,你将看到类似"syntax is okay"和"test is successful"的消息
6 查看 nginx 日志:sudo journalctl -u nginx
7 查看 nginx 所在目录
- sudo find / -name nginx
- 使用 locate 命令查看
- sudo apt install plocate
- locate nginx
8 ftp上传文件到html目录下
- 查看所有者和组: ls -ld /usr/share/nginx/html
- 修改所有者: sudo chown -R ubuntu:ubuntu /usr/share/nginx/html
- 修改权限: sudo chmod -R 755 /usr/share/nginx/html
- 修改组: sudo chgrp ubuntu /user/share/nginx/html 这句话将组修改为用户 ubuntu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
安装成功并正确启动 Nginx 服务后如下所示:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Sat 2025-01-25 16:56:08 CST; 1h 2min ago
Docs: man:nginx(8)
Main PID: 41227 (nginx)
Tasks: 3 (limit: 1969)
Memory: 2.4M (peak: 2.9M)
CPU: 19ms
CGroup: /system.slice/nginx.service
├─41227 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
├─41228 "nginx: worker process"
└─41229 "nginx: worker process"
Jan 25 16:56:08 VM-8-2-ubuntu systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Jan 25 16:56:08 VM-8-2-ubuntu systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 配置 Nginx 服务
1 用 vim 打开文件: sudo vim /etc/nginx/nginx.conf
2 配置如下:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
# Server块:定义一个虚拟主机(网站)
server {
listen 80; # 监听端口
server_name www.ai789.site ai789.site; # 域名
root /var/www/html; # 网站根目录
# Location块:定义URI路径的匹配规则
location / {
index index.html; # 默认首页
}
# 反向代理配置示例
# location /api/ {
# proxy_pass http://backend_server;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# }
}
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
3 修改用户组和权限
- 修改所有者: sudo chown -R www-data:www-data /var/www/html
- 修改权限: sudo chmod -R 755 /var/www/html
- 修改组: sudo chgrp www-data /var/www/html 这里的 www-data 是一个linux操作系统 id 允许 nginx 访问 html资源。
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
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
提示
快捷操作:i 插入, p 粘贴 ,3dd剪切光标处3行
重新加载服务 :sudo systemctl reload nginx
listen 80:指定 Nginx 监听80端口(HTTP默认端口)。
server_name your_domain.com: 指定服务器响应的域名。如果你没有域名,也可以使用IP地址。
location / { ... } 块定义了对于根目录(/)的请求如何处理。
root /path/to/your/dist: 指定了包含index.html文件的根目录。你需要将/path/to/your/dist替换为实际的路径。
index index.html: 指定当请求为目录时,默认返回index.html文件。
# 卸载 Nginx 服务
1 停止服务:sudo systemctl stop nginx
2 卸载 Nginx 包:sudo apt remove nginx nginx-common
3 删除未使用的依赖包:sudo apt autoremove
4 删除所有 Nginx 配置文件(可选):sudo rm -rf /etc/nginx
5 删除 Nginx 日志文件(可选):sudo rm -rf /var/log/nginx
6 检查 Nginx 是否完全卸载:which nginx 返回空说明全部卸载完毕
1
2
3
4
5
6
2
3
4
5
6
# 彻底清除 Nginx 及相关文件
1 确保 Nginx 服务已经停止:sudo systemctl stop nginx
2 卸载 Nginx 及其配置文件:sudo apt purge nginx nginx-common nginx-full
3 删除 Nginx 相关的文件和目录:
# 删除 Nginx 配置目录
sudo rm -rf /etc/nginx
# 删除 Nginx 日志文件
sudo rm -rf /var/log/nginx
# 删除 Nginx 网页内容目录(如果有的话)
sudo rm -rf /usr/share/nginx
# 删除 Nginx 网站内容目录(如果有的话)
sudo rm -rf /var/www/html
# 删除 Nginx 的服务文件等
sudo rm -rf /lib/systemd/system/nginx.service
sudo rm -rf /lib/systemd/system/multi-user.target.wants/nginx.service
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 修改var/www/html组和权限
sudo chown -R ubuntu:ubuntu /var/www/html sudo chmod -R 755 /var/www/html sudo chgrp ubuntu /var/www/html
编辑 (opens new window)
上次更新: 2025/03/22, 13:47:44
← 常用配置 安装 Docker 容器→