安装
服务器系统是CentOS 8
使用yum进行安装
类似npm 的包管理器,可以在centOS 上使用.
yum install nginx
nginx -v # 查看版本
升级 Nginx 版本
自行创建高版本的Nginx源,在yum install nginx时才会安装最新版本的Nginx。
yum info nginx # 查看最新版本 目前是1.22.0
vim /etc/yum.repos.d/nginx.repo # nginx.repo 文件
### nginx.repo中添加以下内容 :wq! 保存并退出
[nginx-stable]
name=nginx stable repo
baseurl=https://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=https://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
###
yum 切换源
rename ".repo"".repo.bak" /etc/yum.repos.d/*.repo
wget https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo -O /etc/yum.repos.d/Centos-vault-8.5.2111.repo
wget https://mirrors.aliyun.com/repo/epel-archive-8.repo -O /etc/yum.repos.d/epel-archive-8.repo
sed -i "s/mirrors.cloud.aliyuncs.com/url_tmp/g" /etc/yum.repos.d/Centos-vault-8.5.2111.repo
sed -i "s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g" /etc/yum.repos.d/Centos-vault-8.5.2111.repo
sed -i "s/url_tmp/mirrors.aliyun.com/g" /etc/yum.repos.d/Centos-vault-8.5.2111.repo
sed -i "s/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g" /etc/yum.repos.d/epel-archive-8.repo
yum clean all && yum makecache
操作
常用命令
| 命令 | 功能 | |
|---|---|---|
| nginx | 启动进程 | |
| nginx -t | 验证配置 | |
| nginx -s reload | 重启进程 | |
| nginx -s stop | 杀掉进程 | |
| ps -ef | grep nginx | 查看进程 |
语法
Nginx主配置文件是/etc/nginx/nginx.conf,可用vim /etc/nginx/nginx.conf查看配置。以下是nginx.conf的主体结构。
nginx.conf # 全局配置
├── events # 配置影响:Nginx服务器与用户的网络连接
├── http # 配置功能:代理、缓存、日志等功能
│ ├── upstream # 配置后端地址:负载均衡不可或缺的部分
│ ├── server # 配置虚拟主机:一个http块可包括多个server块
│ ├── server
│ │ ├── location # 一个server块可包括多个location块
│ │ ├── location # location块指令用于匹配URI
│ │ └── ...
│ └── ...
└── ...
防火墙
CentOS的防火墙如果开启的话,需要开放端口给 Nginx, 使用 firewalld 进行控制端口是否开放.
firewalld 使用 systemctl 进行管理.
常用命令:
| 命令 | 功能 |
|---|---|
| systemctl start firewalld | 开启防火墙 |
| systemctl stop firewalld | 关闭防火墙 |
| systemctl status firewalld | 查看防火墙状态 |
| systemctl disable firewalld | 开机禁用防火墙 |
| systemctl enable firewalld | 开机启用防火墙 |
开放端口步骤:
firewall-cmd --version # 查看防火墙版本,确保防火墙已安装
firewall-cmd --state # 查看防火墙状态,确保防火墙已开启
firewall-cmd --zone=public --list-ports # 查看所有打开的端口,若端口已开放则无需继续执行命令
firewall-cmd --zone=public --add-port=9999/tcp --permanent # 开放指定端口 --permanent 表示永久开放
firewall-cmd --reload # 重载防火墙配置
firewall-cmd --zone=public --query-port=9999/tcp # 查看指定端口是否已开放
firewall-cmd --zone=public --remove-port=9999/tcp --permanent # 删除指定端口
在默认情况下,CentOS的防火墙是关闭的,因此无需主动开放指定端口。
配置网站
- 创建项目目录和上传文件
cd / # 进入根目录
mkdir www # 创建 www 文件夹
cd www
mkdir clinet
mkdir serve
cd clinet
mkdir web-tk
# 上传网站到 www/client/web-tk
scp -r /Users/star/Work/web-tk/dist root@IP地址:/www/client/web-tk
- 配置nginx配置 进行解析
cd /etc/nginx/conf.d # 进入目录 创建 web-tk.conf
touch web-tk.conf
vim /etc/nginx/conf.d/web-tk.conf # 添加配置
server {
listen 80;
# server_name yangzw.vip www.yangzw.vip; # 暂时没有域名所以没有填写
location / {
root /www/client/yangzw;
index index.html;
}
}