Centos 7.5安装 Nginx

1. 准备工作

 1.1 查看系统版本

  1.1.1 输入命令

cat /etc/redhat-release

  1.1.2 我的Centos版本

CentOS Linux release 7.5.1804 (Core)

 1.2 安装nginx所需的依赖

  1.2.1 gcc安装

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

yum install -y gcc-c++

  1.2.2 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

  1.2.3 zlib 安装

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

yum install -y zlib zlib-devel

  1.2.4 OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

2. 下载nginx

 2.1 下载地址:

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

 2.2 选择稳定版本

3. 上传

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

 3.2 我的目录

/app/tool/

4. 解压

 4.1 解压到当前目录

tar zxf nginx-1.14.1.tar.gz

 4.2 目录结构

5. 初始化nginx(配置)

 5.1 解压完成后进入解压目录


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

 5.2 使用默认配置(推荐)

  5.2.1 编译

./configure

  5.2.2 完成输出的结果

 5.3 自定义配置

注:将临时文件目录指定为/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. 编译安装

 6.1 执行

make && make install

  6.1.1 输出结果最后一行为

make[1]: Leaving directory `/app/tool/nginx-1.14.1'

 6.2 查找安装路径

  6.2.1 命令

whereis nginx

  6.2.2 输出结果

nginx: /usr/local/nginx

7. 启动与重启

 7.1 启动

  7.1.1 进入sbin目录

cd /usr/local/nginx/sbin

  7.1.2 启动

./nginx

  7.1.3 停止:先查出nginx进程id再使用kill命令强制杀掉进程

./nginx -s stop

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

./nginx -s quit

 7.2 重启

  7.2.1 先停止再启动

./nginx -s quit
./nginx

 7.3 重载配置文件

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

./nginx -s reload

8. 反向代理

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

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

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

server {
        listen       80;
        server_name  wbsxch.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

 9.1 查看nginx版本与编译安装了哪些模块 大写V

./usr/local/nginx/sbin/nginx -V

 9.2 配置nginx 添加http_ssl_module模块

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

 9.3 执行make ,千万不要make install 否则会覆盖现有的nginx

make

 9.4 关闭nginx

./usr/local/nginx/sbin/nginx -s quit

 9.5 复制配置目录/objs/nginx 替换/usr/local/nginx/sbin/nginx

 9.6 查看编译安装的模块

./usr/local/nginx/sbin/nginx -V

9.7 上传证书到nginx的conf目录

 9.8 修改nginx.conf

server {
    listen  80;
    server_name wbsxch.com;
    send_timeout    1800;
    
    rewrite ^(.*) https://wbsxch.com$1 permanent;
    }

    server {
        listen 443;
        server_name wbsxch.com; #填写绑定证书的域名
        index index.html index.htm index.php default.html default.htm default.php;          
        ssl on;
        ssl_certificate /usr/local/nginx/conf/1_wbsxch.com_bundle.crt;
        ssl_certificate_key /usr/local/nginx/conf/2_wbsxch.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 / {
            proxy_pass http://127.0.0.1:8080;
        }
    }

 9.9 验证配置是否正确

./usr/local/nginx/sbin/nginx -t

猜你喜欢

转载自www.cnblogs.com/hsbt2333/p/9921097.html