Blog
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)

HiuZing

🍑
首页
文档
收藏
关于
  • 在线转换时间戳 (opens new window)
  • 在线压缩图片 (opens new window)
  • Float-Double转二进制 (opens new window)
  • 文件转Hex字符串 (opens new window)
  • Nginx

    • Nginx介绍
    • Nginx安装
      • Nginx安装
      • Nginx常用命令
        • 案例:
    • Nginx配置文件
    • Nginx反向代理
    • Nginx拉取git项目
    • Nginx添加虚拟主机配置文件
    • Nginx项目自动更新
    • Vue-histroy模式跳转路由404问题
    • GoAccess
    • Nginx负载均衡
  • Docker

  • Server

  • Linux

  • Windows

  • 运维
  • Nginx
HiuZing
2022-12-06
目录

Nginx安装

# Nginx安装

  1. 下载Nginx包 nginx: download (opens new window)

  2. 在Ubuntu中,切换root用户

    # 切换至root用户
    sudo su root
    
    1
    2
  3. 安装依赖包

    # nginx 编译时依赖 gcc 环境
    apt-get install gcc
    apt-get install libpcre3 libpcre3-dev
    # 安装zlib,nginx 使用 zlib 对 http 包内容进行gzip压缩
    apt-get install zlib1g zlib1g-dev
    
    # Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代
    # apt-get install openssl openssl-dev
    
    # 安装 openssl,用于通讯加密
    sudo apt-get install openssl 
    sudo apt-get install libssl-dev
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
  4. 安装Nginx

    cd /usr/local
    # 创建nginx文件夹
    mkdir nginx
    # 打开nginx文件夹
    cd nginx
    # 下载nginx压缩包
    wget http://nginx.org/download/nginx-1.13.7.tar.gz
    # 解压nginx
    tar -xvf nginx-1.13.7.tar.gz 
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  5. 编译Nginx

    # 进入nginx-1.13.7目录
    /usr/local/nginx/nginx-1.13.7
    # 执行命令 检查平台安装环境
    ./configure
    # 执行make命令 进行源码编译
    make
    # 执行make install命令 安装nginx
    make install
    
    1
    2
    3
    4
    5
    6
    7
    8
  6. 启动Nginx(不配置文件到这步可以输入网址测试)

    # 查看nginx配置(默认通过完整路径执行命令)
    cd /usr/local/nginx/sbin -t
    # 启动nginx
    ./nginx
    # 网页输入ip地址,访问成功,到此,nginx安装完毕
    
    
    # 简单路径(需要配置,以下是配置方式)
    nginx -t
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
  7. 制作 nginx 软连接

  8. 进入 usr/bin 目录

    cd /usr/bin
    
    1
  9. 制作软连接

    # ln -s 路径 定义软连接的名字
    ln -s /usr/local/nginx/sbin/nginx nginx
    
    1
    2
  10. 制作配置文件

  11. 首先进入到 nginx 的默认配置文件中

    vim /usr/local/nginx/conf/nginx.conf
    
    1
  12. 在最底部增加配置项(按下 i 进入 输入模式)

    # 慕课
    include /nginx/*.conf;
    # 自研
    include /usr/local/nginx/conf/conf.d/*.conf;
    
    1
    2
    3
    4
  13. 按下 esc 键,通过 :wq! 保存并退出

  14. 创建新的配置文件 / 创建一个共有文件夹

    # 慕课
    touch /nginx/nginx.conf
    # 自研(创建一个共有文件夹)
    mkdir conf.d/
    
    1
    2
    3
    4
  15. 进入到 /root/nginx/nginx.conf 文件 / 进入/usr/local/nginx/conf/conf.d$文件夹创建配置配置文件

    # 慕课
    vim /nginx/nginx.conf
    
    # 自研 进入 conf.d文件夹创建配置配置文件
    cd conf.d/
    touch xiaojing-dev.nipx.cn.conf
    
    1
    2
    3
    4
    5
    6

    image-20230322134229162

  16. 写入如下配置

    # 慕课
    # imooc-admin
    server {
        # 端口
        listen       80;
        # 域名
        server_name  localhost;
        # 资源地址
        root   /nginx/dist/;
        # 目录浏览
        autoindex on;
        # 缓存处理
        add_header Cache-Control "no-cache, must-revalidate";
        # 请求配置
        location / {
            # 跨域
            add_header Access-Control-Allow-Origin *;
            # 返回 index.html
            try_files $uri $uri/ /index.html;
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
  17. 通过 :wq! 保存退出

  18. 在 root/nginx 中创建 dist 文件夹,资源地址

    # 慕课
    mkdir /nginx/dist
    
    1
    2
  19. 在 nginx/dist 中写入 index.html 进行测试

  20. 通过 nginx -s reload 重启服务

  21. 在 浏览器中通过,IP 测试访问

# Nginx常用命令

  1. Nginx命令

    # 查看nginx的版本号2
    nginx -v
    
    # 启动nginx
    ./nginx
    
    # 关闭nginx
    ./nginx -s stop
    # Quit 是一个优雅的关闭方式,Nginx在退出前完成已经接受的请求处理
    nginx -s quit
    
    # 重新加载nginx配置
    ./nginx -s reload
    
    # 验证nginx配置文件
    nginx -t //输出nginx.conf syntax is ok即表示nginx的配置文件正确
    
    # 配置 指定通过当前路径来表示 Nginx启动
    nginx -c /usr/local/nginx/conf/nginx.conf 
    
    # 查看nginx进程
    ps -ef | grep nginx
    
    # 输出标准格式的linux进程命令
    ps -ef
    
    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
  2. Nginx文件位置

    • /usr/sbin/nginx:主程序
    • /etc/nginx:存放配置文件
    • /usr/share/nginx:存放静态文件
    • /var/log/nginx:存放日志
  3. 重启Nginx配置

    # 进入nginx启动目录
    cd /usr/local/nginx/sbin
    
    # 检查nginx配置文件是否正确
    nginx -t
    
    #重新加载配置文件
    ./nginx -s reload
    
    1
    2
    3
    4
    5
    6
    7
    8

# 案例:

无法在 Windows 上停止 nginx 服务器。我已经尝试过: nginx -s stop 、 taskkill /if nginx.exe 和通过任务管理器结束进程,但它仍在运行!

解决方案:

使用 @taskkill /f /im nginx.exe 完成此任务。

上次更新: 2024/08/14, 04:14:33
Nginx介绍
Nginx配置文件

← Nginx介绍 Nginx配置文件→

最近更新
01
React Native 使用SVG
08-13
02
Docker基础命令
08-04
03
算数逻辑单元
07-30
更多文章>
Theme by Vdoing | Copyright © 2021-2024 WeiXiaojing | 友情链接
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式