CentOS 安装 Nginx 及配置文件使用

简介: CentOS 安装 Nginx 及配置文件使用

一、简介

二、安装、启动、配置文件目录

  • yum 安装 nginx
$ yum install -y nginx
  • 安装之后,重新访问刚才的 IP。(默认安装完成会自动启动 Nginx,所以可以直接访问,如果无法访问,则手动启动一下 Nginx)
  • nginx 配置文件目录:Nginx 配置文件使用(nginx.conf)
/etc/nginx/nginx.conf
  • nginx 启动命令目录
/usr/sbin/nginx
  • nginx 项目存放根目录(推荐),在这个文件夹内存放线上项目
/home
$ nginx
$ /usr/sbin/nginx
  • systemctl 启动方式 (推荐)
    CentOS 7.x 开始,CentOS 开始使用 systemd 服务来代替 daemon,原来管理系统启动和管理系统服务的相关命令全部由 systemctl 命令来代替。
    优点:比如停电、崩溃或者别的因素导致进程挂了,会自动帮你启起来,当后台挂载的进程数多的时候,就不需要手动去启动一遍,上面原始的那种需要手动去启动,当然还有别的好处。
#启动服务
systemctl start nginx
#停止服务
systemctl stop nginx
#重启服务
systemctl restart nginx
#查看状态
systemctl status nginx
  • 查看启动进程列表中是否已经启动 nginx
$ ps -aux | grep nginx
或:
$ ps -ef | grep nginx
  • 杀死进程,PID 在进程列表中可以找到,一般为每个进程的第二个字段。
$ kill PID
// 强制杀死进程
$ kill -9 PID

三、新建一个配置文件,单独配置一个虚拟机

  • 配置文件目录
/etc/nginx/nginx.conf
  • 打开配置文件,会看到 include /etc/nginx/conf.d/*.conf; 这行,去这个文件夹里面新建 .conf 结尾的配置文件即可,建议每个配置文件对应一个 server

  • 进入配置文件夹,新建一个测试配置文件,并添加一个虚拟机。
$ cd /etc/nginx/conf.d/
  • 新建一个 test.conf
$ touch test.conf
  • 编辑配置文件
$ vim test.conf
  • 配置一个 server,根目录指向 /home/test 目录,目标文件是 index.html
server {
    # 监听端口
    listen 8082;
    # 主机名称
    # server_name www.dzm.com;
    # 根目录
    root /home/test;
    # 匹配协议
    location / {
        index index.html;
    }
    # 代理接口
    # location /api/ {
    #     proxy_pass http://platform-api.yxfengsheng.com/;
    # }
}
  • 保存退出:按 ESC 退出编辑,输入 :wq 保存退出。
  • 进入 /home 根目录,新建 test 项目文件夹,并添加 index.html 项目文件内容
$ cd /home
  • 创建 test 项目文件夹
$ mkdir test
  • 进入 test 项目文件夹
$ cd test/
  • 创建 index.html 项目文件
$ touch index.html
  • 添加网页内容
$ vim index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  DZM CentOS Test
</body>
</html>
  • 保存退出:按 ESC 退出编辑,输入 :wq 保存退出。
  • 重启 或 更新 nginx 配置。
$ systemctl restart nginx
  • 访问 公网IP:8082,刚配置的是 8082 端口。


相关文章
|
3天前
|
安全 关系型数据库 MySQL
CentOS 8 中安装与配置 MySQL
CentOS 8 中安装与配置 MySQL
19 3
|
4天前
|
消息中间件 Linux
Centos安装RabbitMQ
Centos安装RabbitMQ
13 3
|
5天前
|
前端开发 应用服务中间件 nginx
nginx前后端分离、多前端部署配置文件
nginx前后端分离、多前端部署配置文件
10 2
|
4天前
|
Linux Docker 容器
Centos8安装Docker
Centos8安装Docker
21 1
|
5天前
|
Linux 测试技术 开发工具
CentOS Linux 8使用阿里源(安装jdk11、git测试)
CentOS Linux 8使用阿里源(安装jdk11、git测试)
17 1
|
5天前
|
存储 Linux 数据安全/隐私保护
Centos安装
Centos安装
22 2
|
5天前
|
应用服务中间件 nginx
nginx更改配置文件后重启
nginx更改配置文件后重启
12 1
|
3天前
|
应用服务中间件 Linux 程序员
老程序员分享:nginx安装及其配置详细教程
老程序员分享:nginx安装及其配置详细教程
|
3天前
|
应用服务中间件 nginx Docker
Docker安装与管理Nginx
Docker安装与管理Nginx
26 0
|
3天前
|
缓存 Linux Docker
CentOS 7 下安装 Docker 及配置阿里云加速服务
CentOS 7 下安装 Docker 及配置阿里云加速服务
46 0