云服务器:阿里云CentOS7.3 64

Nginx的安装有两种方式,一种通过yum安装,一种通过下载Redis源代码编译安装。

一、yum安装Nginx

1、添加官方rpm源,在进行安装

[root@sihan ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
Retrieving http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
warning: /var/tmp/rpm-tmp.myR0Nv: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@sihan ~]# yum install -y nginx
Loaded plugins: fastestmirror
nginx                                                                                                            | 2.9 kB  00:00:00     
nginx/x86_64/primary_db                                                                                          |  42 kB  00:00:01     
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.14.2-1.el7_4.ngx will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
 Package                     Arch                         Version                                     Repository                   Size
========================================================================================================================================
Installing:
 nginx                       x86_64                       1:1.14.2-1.el7_4.ngx                        nginx                       754 k

Transaction Summary
========================================================================================================================================
Install  1 Package

Total download size: 754 k
Installed size: 2.6 M
Downloading packages:
nginx-1.14.2-1.el7_4.ngx.x86_64.rpm                                                                              | 754 kB  00:00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : 1:nginx-1.14.2-1.el7_4.ngx.x86_64                                                                                    1/1 
----------------------------------------------------------------------

Thanks for using nginx!

Please find the official documentation for nginx here:
* http://nginx.org/en/docs/

Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html

Commercial subscriptions for nginx are available on:
* http://nginx.com/products/

----------------------------------------------------------------------
  Verifying  : 1:nginx-1.14.2-1.el7_4.ngx.x86_64                                                                                    1/1 

Installed:
  nginx.x86_64 1:1.14.2-1.el7_4.ngx                                                                                                     

Complete!
[root@sihan ~]# 

2、了解yum安装的nginx路径

一般来说我们只需要关注两个东西即可,一个是nginx.conf文件,一个是静态资源存放路径

[root@sihan ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
[root@sihan ~]# ls /usr/share/nginx/
html
[root@sihan ~]# ls /etc/nginx/
conf.d  fastcgi_params  koi-utf  koi-win  mime.types  modules  nginx.conf  scgi_params  uwsgi_params  win-utf

3、启动nginx

[root@sihan ~]# service nginx start
Redirecting to /bin/systemctl start  nginx.service
nginx欢迎页

4、配置nginx.conf转将请求转发到tomcat首页

[root@sihan ~]# vi /etc/nginx/nginx.conf

添加upstream模块,并在根目录设置proxy_pass。设置好保存之后需要重启nginx。

http {
    upstream localhost {
        server 127.0.0.1:8080;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://localhost;
            root   html;
            index  index.html index.htm;
        }
}

效果如图所示

5、配置https

  • 1、首先你得拥有一个自己的域名并解析到服务器上,这是前提条件。
  • 2、这边我们选择阿里云提供的免费证书服务,这个只能指向一个域名。
    找到ssl证书

    点击购买证书

    选择免费证书

    购买成功后进入证书控制台,进行申请(证书为免费版,只能写一个域名,而且不能使用通配符)

    在域名解析中添加解析值(如果已经存在该记录类型的主机记录的解析,需要先删除,待验证完毕后再添加),验证成功后即可将解析删除。

    进入已签发页面,下载nginx版本的证书

    查看帮助文档,记录下https的配置

server {
 listen 443;
 server_name localhost;
 ssl on;
 root html;
 index index.html index.htm;
 ssl_certificate   cert/www.sihan1151.cn.pem;
 ssl_certificate_key   cert/www.sihan1151.cn.key;
 ssl_session_timeout 5m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 location / {
     root html;
     index index.html index.htm;
 }
}

在Nginx的安装目录(/etc/nginx)下创建cert目录,并且将下载的全部文件拷贝到cert目录中。并修改nginx.conf的配置文件。这边我们做兼容http处理,自动将http请求转发至https。配置如下:(注意:yum安装的html目录在/usr/share/nginx/下,所以这里的root需要修改为:root /usr/share/nginx/html;)

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  'remote_addr -remote_user [time_local] "request" '
    #                  'statusbody_bytes_sent "http_referer" '
    #                  '"http_user_agent" "http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream localhost {
        server 127.0.0.1:8080;
    }
       server {
        listen       80;
        server_name  www.sihan1151.cn;
        rewrite ^/(.*) https://server_name$request_uri? permanent;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}

    }

    # HTTPS server
    #
    server {
         listen 443;
         server_name www.sihan1151.cn;
         ssl on;
         root html;
         index index.html index.htm;
         ssl_certificate   cert/www.sihan1151.cn.pem;
         ssl_certificate_key  cert/www.sihan1151.cn.key;
         ssl_session_timeout 5m;
         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
         ssl_prefer_server_ciphers on;
         location / {
             root /usr/share/nginx/html;
             index index.html index.htm;
         }
         
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }

}

至此,https算完成,可输入http请求测试效果:

6、nginx配置多域名转发

这个其实很简单,在http节点下配置多个server,每个server_name指向不同域名即可实现。如果项目做了前后端分离,还可以配置location下的root指向不同的文件夹。

    server {
        listen       80;
        server_name  a.sihan1151.cn;
        location / {
            root   html;
            index  index-a.html;
        }
    }
    server {
        listen       80;
        server_name  b.sihan1151.cn;
        location / {
            root   html;
            index  index-b.html;
        }
    }

二、下载Nginx源代码编译方式安装

1、安装依赖

  • 1、编译源码依赖 gcc 环境。
  • 2、PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。
  • 3、zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
  • 4、OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
[root@sihan ~]# yum install -y gcc-c++
[root@sihan ~]# yum install -y pcre pcre-devel
[root@sihan ~]# yum install -y zlib zlib-devel
[root@sihan ~]# yum install -y openssl openssl-devel

检查是否安装某依赖

[root@sihan ~]# rpm -qa|grep -i openssl
openssl-libs-1.0.2k-16.el7.x86_64
openssl-1.0.2k-16.el7.x86_64

2、查看并下载、解压Nginx

Nginx版本查看:http://nginx.org/download/

[root@sihan ~]# wget http://nginx.org/download/nginx-1.9.15.tar.gz
--2019-02-01 14:53:43--  http://nginx.org/download/nginx-1.9.15.tar.gz
Resolving nginx.org (nginx.org)... 95.211.80.227, 62.210.92.35, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|95.211.80.227|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 908984 (888K) [application/octet-stream]
Saving to: ‘nginx-1.9.15.tar.gz’

100%[==============================================================================================>] 908,984      373KB/s   in 2.4s   

2019-02-01 14:53:46 (373 KB/s) - ‘nginx-1.9.15.tar.gz’ saved [908984/908984]

[root@sihan ~]# tar -zxvf nginx-1.9.15.tar.gz -C /usr/local/
...

3、编译安装

进入解压目录编译安装,安装的默认目录是 /usr/local/bin/,需要root权限执行命令。可以使用 PREFIX=/XXX 参数指定安装目录,那有这个目录权限的普通用户也可以执行安装命令

[root@sihan ~]# cd /usr/local/nginx-1.9.15/
[root@sihan nginx-1.9.15]# ./configure
[root@sihan nginx-1.9.15]# make
[root@sihan nginx-1.9.15]# mkdir /usr/local/nginx/
[root@sihan nginx-1.9.15]# make PREFIX=/usr/local/nginx install

4、启动nginx

[root@sihan bin]# /usr/local/nginx/sbin/nginx

将nginx注册服务启动等可参考redis环境配置,这里不再做说明。

三、yum安装的nginx的卸载

如果是通过第二种方式安装的,直接删除以下文件夹即可。

[root@sihan init.d]# cd /usr/local/
[root@sihan local]# ls
aegis  bin  etc  games  include  lib  lib64  libexec  nginx  nginx-1.9.15  sbin  share  src  tomcat-base  tomcat-production
[root@sihan redis-5.0.3]# rm -rf /usr/local/nginx 
[root@sihan redis-5.0.3]# rm -rf /usr/local/nginx-1.9.15

下面展示的是yum安装的卸载

1、查看安装的nginx

[root@sihan ~]# yum list installed | grep nginx
nginx.x86_64                         1:1.14.2-1.el7_4.ngx            @nginx     
nginx-release-centos.noarch          7-0.el7.ngx                     installed  

2、卸载nginx

[root@sihan ~]# yum remove nginx
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.14.2-1.el7_4.ngx will be erased
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
 Package                     Arch                         Version                                    Repository                    Size
========================================================================================================================================
Removing:
 nginx                       x86_64                       1:1.14.2-1.el7_4.ngx                       @nginx                       2.6 M

Transaction Summary
========================================================================================================================================
Remove  1 Package

Installed size: 2.6 M
Is this ok [y/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Erasing    : 1:nginx-1.14.2-1.el7_4.ngx.x86_64                                                                                    1/1 
  Verifying  : 1:nginx-1.14.2-1.el7_4.ngx.x86_64                                                                                    1/1 

Removed:
  nginx.x86_64 1:1.14.2-1.el7_4.ngx                                                                                                     

Complete!

3、验证

[root@sihan ~]# nginx -v
-bash: /usr/sbin/nginx: No such file or directory

文章来源于互联网,如有雷同请联系站长删除:CentOS服务器安装配置nginx及https环境、多域名转发

发表评论