1. 准备工作

  1. 查看系统版本

    输入命令

    cat /etc/redhat-release

    我的Centos版本

    CentOS Linux release 7.5.1804 (Core)

  2. 安装nginx所需的依赖

    • gcc安装

      安装 nginx 需要先将官网下载的源码进行编译,而编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

      yum install -y gcc-c++

    • PCRE pcre-devel 安装

      PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

      yum install -y pcre pcre-devel

    • zlib 安装

      zlib 库提供了很多种压缩和解压缩的方式 nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库

      yum install -y zlib zlib-devel

    • OpenSSL 安装

      OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。

      nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

      yum install -y openssl openssl-devel

2. 下载nginx

  1. 下载地址:

    https://nginx.org/en/download.html

    选择稳定版本

3. 上传

  1. 使用xshell连接linux系统 再使用xftp上传文件到指定目录

    我的目录

    /app/tool/

4. 解压

  1. 解压到当前目录

    tar zxf nginx-1.14.1.tar.gz

    目录结构

5. 初始化nginx(配置)

  1. 解压完成后进入解压目录

    cd /app/tool/nginx-1.14.1

  2. 配置

    其实在 nginx-1.14.0 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。

    • 默认配置(推荐)

      编译

      ./configure

      完成输出的结果

    • 自定义配置

      注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

      ./configure \
      --prefix=/usr/local/nginx \
      --conf-path=/usr/local/nginx/conf/nginx.conf \
      --pid-path=/usr/local/nginx/conf/nginx.pid \
      --lock-path=/var/lock/nginx.lock \
      --error-log-path=/var/log/nginx/error.log \
      --http-log-path=/var/log/nginx/access.log \
      --with-http_gzip_static_module \
      --http-client-body-temp-path=/var/temp/nginx/client \
      --http-proxy-temp-path=/var/temp/nginx/proxy \
      --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
      --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
      --http-scgi-temp-path=/var/temp/nginx/scgi

6. 编译安装

  1. 执行

    make && make install

  2. 输出结果最后一行:

    make[1]: Leaving directory `/app/tool/nginx-1.14.1'
  3. 查找安装路径

    whereis nginx

    输出结果:

    nginx: /usr/local/nginx

7. 启动与重启

  1. 启动

    • 进入sbin目录

      cd /usr/local/nginx/sbin

    • 启动

      ./nginx

  2. 停止

    • 先查出nginx进程id再使用kill命令强制杀掉进程

      ./nginx -s stop

    • 停止:等待nginx进程处理任务完毕进行停止

      ./nginx -s quit

  3. 重启

    先停止再启动

    ./nginx -s quit



    ./nginx

  4. 重载配置文件

    当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:

    ./nginx -s reload

  5. 开放80端口

    firewall-cmd --zone=public --add-port=80/tcp --permanent

    firewall-cmd --reload

8. 反向代理

  1. 编辑配置文件

    修改/usr/local/nginx/conf下的 nginx.conf文件

    vim /usr/local/nginx/conf/nginx.conf

  2. 修改配置文件

    在nginx.conf的http{}里面添加

    server {
    listen 80;
    server_name example.com;
    location / {
    proxy_pass http://127.0.0.1:8080;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root html;
    }
    }

9. 配置https

  1. 检查模块

    进入/usr/local/nginx/sbin/目录下 查看nginx版本与编译安装了哪些模块

    cd /usr/local/nginx/sbin/

    ./nginx -V

  2. 添加http_ssl_module模块

    进入nginx解压目录下配置nginx

    cd /app/tool/nginx-1.14.1/

    ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

  3. 编译

    千万不要make install 否则会覆盖现有的nginx

    make

  4. 关闭nginx

    进入/usr/local/nginx/sbin/目录下

    ./nginx -s quit

  5. 复制配置目录

    将/app/tool/nginx-1.14.1/objs/nginx 替换/usr/local/nginx/sbin/nginx

    复制前注意先将之前的nginx文件备份

    mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

    复制 如果提示是否覆盖 键入y即可

    cp /app/tool/nginx-1.14.1/objs/nginx /usr/local/nginx/sbin/

  6. 查看编译安装的模块

    进入/usr/local/nginx/sbin/目录下

    cd /usr/local/nginx/sbin/

    ./nginx -V

  7. 上传证书

    我上传的目录为

    /usr/local/nginx/conf

    我使用的是腾讯云的免费证书

    1_域名_bundle.crt2_域名.key 上传到 /usr/local/nginx/conf/目录下

  8. 修改nginx.conf

    vim /usr/local/nginx/conf/nginx.conf

    http{}末尾添加以下内容

    注:example.com应换成自己的域名

        server {
    listen 80;
    server_name example.com;
    send_timeout 1800; rewrite ^(.*) https://example.com$1 permanent;
    } server {
    listen 443;
    server_name example.com; #填写绑定证书的域名
    index index.html index.htm index.php default.html default.htm default.php;
    ssl on;
    ssl_certificate /usr/local/nginx/conf/1_example.com_bundle.crt;
    ssl_certificate_key /usr/local/nginx/conf/2_example.com.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    #按照这个套件配置
    ssl_prefer_server_ciphers on;
    location / {
    root html;
    index index.html index.htm;
    }
    }
  9. 验证配置是否正确

    进入/usr/local/nginx/sbin/目录下

    cd /usr/local/nginx/sbin/

    ./nginx -t

    如下提示表示成功

    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  10. 开启443端口

firewall-cmd --zone=public --add-port=443/tcp --permanent

firewall-cmd --reload

  1. 启动nginx

./nginx



现在就可以使用https访问了



10. 配置图片服务器

  1. 修改/usr/local/nginx/conf/nginx.conf

    vim conf/nginx.conf

    修改的位置为server{}里面,配置了https的需要在443端口server下添加如下内容,如果没有配置https则在默认80端口下添加即可

  2. 内容

        location /images/ {
    root /app/home/;
    autoindex on;
    }
  3. 修改图片目录的权限

    我的目录为/app/home/

    chmod 777 -R /app/home/

  4. 存放图片需要存入设置目录的images目录下

    我的图片路径为

    /app/home/images/

  5. 重载nginx配置文件

    cd /usr/local/nginx/sbin/



    ./sbin -s reload

  6. 访问

    现在就可以访问图片了,访问地址为

    域名(iP:端口)/images/图片名称.后缀

Centos 7.5安装 Nginx 1.14.1的更多相关文章

  1. CentOS 7.4安装Nginx 1.14.0

    一.安装所需环境   1.gcc 安装         yum install gcc-c++    

  2. 编译安装 nginx -1.14.2

    编译安装 nginx -1.14.2 1 ) 下载nginx-1.14.2 源码包: wget http://nginx.org/download/nginx-1.14.2.tar.gz 2 ) 编译 ...

  3. 在CentOS 7中安装nginx服务器

    简要地介绍一下,如何在CentOS 7中安装nginx服务器  下载对应当前系统版本的nginx包(package) # wget  http://nginx.org/packages/centos/ ...

  4. Python之——CentOS 6.5安装Python2.7.14

    Python之——CentOS 6.5安装Python2.7.14   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/l1028386804/art ...

  5. Linux系统运维笔记(四),CentOS 6.4安装Nginx

    Linux系统运维笔记(四),CentOS 6.4安装Nginx 1,安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool op ...

  6. 如何在 CentOS 7 上安装 Nginx

    本文首发:开发指南:如何在 CentOS 7 上安装 Nginx Nginx 读作 engine x, 是一个免费的.开源的.高性能的 HTTP 和反向代理服务,主要负责负载一些访问量比较大的站点. ...

  7. CentOS 7 源代码安装Nginx

    本篇简要介绍CentOS 7 源代码安装Nginx. Preface # yum install epel-release -y # yum group install "Developme ...

  8. Centos 7下安装nginx,使用yum install nginx,提示没有可用的软件包

    Centos 7下安装nginx,使用yum install nginx,提示没有可用的软件包. 18 (flaskApi) [root@67 flaskDemo]# yum -y install n ...

  9. centos 下yum 安装nginx

    centos 下yum 安装nginx 1. 直接yum install nginx不行,要先处理下源: rpm -ivh http://nginx.org/packages/centos/6/noa ...

随机推荐

  1. python基础---集合类型(Sets)

    集合类型(Sets) 集合对象是不同的(不可重复)hashable对象的无序集合.常见用法包括:成员关系测试.移除序列中的重复.以及科学计算,例如交集.并集.差分和对称差分.通俗点来说,集合是一个无序 ...

  2. mybatis-generator1.3.6的使用

    下载地址: http://blog.mybatis.org/2017/12/mybatis-generator-version-136-released.html 参考了 http://blog.cs ...

  3. 洛谷P1510 精卫填海

    //01背包 求背包内物品价值超过某一定值时的最小体积 #include<bits/stdc++.h> using namespace std; ; ; int n,v_tot,w_tot ...

  4. xml schema介绍

    https://www.runoob.com/schema/schema-tutorial.html

  5. oracle函数 sysdate

    [功能]:返回当前日期. [参数]:没有参数,没有括号 [返回]:日期 [示例]select sysdate  hz from dual; 返回:2008-11-5

  6. BERT可视化工具bertviz体验

    BERT可视化工具体验:bertviz是用于BERT模型注意力层的可视化页面. 1,bertviz的github地址:https://github.com/jessevig/bertviz 2,将be ...

  7. ipykernel_launcher.py: error: unrecognized arguments: -f /Users/apple/Library/Jupyter/runtime/kernel

    当在jupyter下使用parser.parse_args()出错则改换为parser.parse_known_args()[0]其效用是差不多的,至于为什么出现错误,我也不知道…

  8. POJ2186 Popular Cows 题解 强连通分量入门题

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  9. logback 打印mybatis sql mybatis 日志打印sql语句和返回结果

    logback 打印sql语句: 在logback日志文件中开启debug模式 <logger name="com.ibatis" level="DEBUG&quo ...

  10. 【原生JS】图片预加载之有序预加载

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...