Centos 7.5安装 Nginx 1.14.1

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/图片名称.后缀

原文地址:https://www.cnblogs.com/hsbt2333/p/9921097.html