注意事项
- 服务器中 删除文件、修改nginx.conf 文件需要 root 权限
- 进入服务器需要再服务器的防火墙中设置允许该网络进入
1、 安装nginx
yum install nginx
2、判断nginx 是否安装成功
wget http://127.0.0.1
输出类似以下信息表示成功
--2023-06-20 09:50:18-- http://127.0.0.1/
Connecting to 127.0.0.1:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 840 [text/html]
Saving to: ‘index.html.2’
100%[=============================================================================================>] 840 --.-K/s in 0s
2023-06-20 09:50:18 (199 MB/s) - ‘index.html.2’ saved [840/840]
3、启动nginx
systemctl start nginx
4、配置nginx (默认目录:/etc/nginx/nginx.conf)
vi /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name 域名或ip地址;
location / {
root /usr/share/nginx/easydo/dist; ## .html 文件存放的位置
index index.html;
try_files $uri $uri/ /index.html;
}
location ^~ /rest/ {
proxy_pass 转发网址;
}
location ^~ /open-apis/ {
proxy_pass 转发网址;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 8080;
listen [::]:8080;
server_name 域名或IP地址;
location / {
root /usr/share/nginx/easydoz-next/dist;
index index.html;
try_files $uri $uri/ /index.html;
}
location ^~ /rest/ {
proxy_pass 转发网址;
}
location ^~ /open-apis/ {
proxy_pass 转发网址;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
5、重启 nginx
systemctl restart nginx
6、重新配置启动的nginx文件
systemctl reload nginx
7、判断nginx 是否启动成功
#方法一 启动nginx
systemctl start nginx
##如果nginx已经启动,这条命令不会有任何输出
#方法二 查看进程
ps -ef | grep nginx
##如果nginx正在运行,会输出至少一行包含”nginx“字符的记录,例如输出一下结果
##root 11635 1812 0 10:21 pts/0 00:00:00 grep --color=auto nginx
##root 22692 1 0 Jun20 ? 00:00:00 nginx: master process /usr/sbin/nginx
##nginx 23497 22692 0 Jun20 ? 00:00:02 nginx: worker process
##nginx 23498 22692 0 Jun20 ? 00:00:02 nginx: worker process
#方法三 查看nginx是否有监听端口
lsof -i:80
##如果正在监听,会输出至少一条包含nginx记录,例如以下输出
##COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
##nginx 22692 root 6u IPv4 1843650 0t0 TCP *:http (LISTEN)
##nginx 22692 root 7u IPv6 1843651 0t0 TCP *:http (LISTEN)
##nginx 23497 nginx 6u IPv4 1843650 0t0 TCP *:http (LISTEN)
##nginx 23497 nginx 7u IPv6 1843651 0t0 TCP *:http (LISTEN)
##nginx 23497 nginx 10u IPv4 15698152 0t0 TCP VM-28-17-centos:http->183.136.182.141:25673 (ESTABLISHED)
##nginx 23497 nginx 12u IPv4 15692488 0t0 TCP VM-28-17-centos:http->218.17.135.100:jt400 (ESTABLISHED)
##nginx 23498 nginx 5u IPv4 15697246 0t0 TCP VM-28-17-centos:http->183.136.182.141:25674 (ESTABLISHED)
##nginx 23498 nginx 6u IPv4 1843650 0t0 TCP *:http (LISTEN)
##nginx 23498 nginx 7u IPv6 1843651 0t0 TCP *:http (LISTEN)
##nginx 23498 nginx 12u IPv4 15691576 0t0 TCP VM-28-17-centos:http->218.17.135.100:6129 (ESTABLISHED)
8、创建静态文件存放位置
# 注意:创建的目录路径要和 nginx 中server配置的一样
mkdir /usr/share/nginx/easydo
## 创建 easydo 文件夹
9、上传静态 dist.zip 文件
#在本地电脑中打开控制台,进入dist.zip文件存放的目录,建议放在桌面
wb.wangdanyang02@HIH-L-10293 ~ % cd desktop ## 进入桌面
wb.wangdanyang02@HIH-L-10293 desktop % scp dist.zip root@[服务器ip地址]:/usr/share/nginx/easydo
## 命令含义:将 dist.zip 文件上传到服务器中的 /usr/share/nginx/easydo 目录中
10、解压文件
###首次解压dist.zip文件
##进入服务器中,root 权限
##进入 /usr/share/nginx/easydo 目录
[root@VM-28-17-centos /]# cd /usr/share/nginx/easydo
##查看 easydo文件中是否存在dist.zip 文件,
[root@VM-28-17-centos easydo]# ll
##创建 dist 目录
mkdir dist
##解压 dist.zip 文件
unzip dist.zip
##解压后的文件会自动存放在 dist 文件夹中
11、更新包
##更新包时,dist 文件夹中已经存在html文件,此时需要先删除dist文件夹中的所有文件,在解压dist.zip文件
rm -fr *
##上传新的 dist.zip 文件到服务器时,需要先将服务其中的 dist.zip文件删除,在上传新的 dist.zip 文件,
##进入 easydo目录中
rm dist.zip
13、其他命令
## 1、查看当前内核版本
uname -r
## 2、查看当前系统版本
cat/etc/redhat-release
## 3、查看nginx安装目录
rpm -ql nginx
## 4、查看目录下的所有文件
ls
## 5、查看目录下所有文件详情
ll
14、进入远程服务器(密码)
## 打开电脑控制台 输入命令
ssh root@[服务器IP地址]
#回车 ,会提示输入密码
### 注意此时输入的密码是不会有任何显示的
#输入正确密码后回车,就可以登录服务器了
15、重命名文件操作
##需要重命名文件时,可以使用 cp 拷贝源文件,在删除源文件
cp a.txt b.txt
##命令说明,拷贝a.txt文件,命名为b.txt文件
##拷贝后再删除 a.txt文件
rm a.txt