关于nginx:Nginx-1142-移植指南openEuler-2003-LTS-SP1

1次阅读

共计 9091 个字符,预计需要花费 23 分钟才能阅读完成。

Nginx 1.14.2 移植指南(openEuler 20.03 LTS SP1)

介绍

简要介绍

Nginx 是一款轻量级的 Web 服务器 / 反向代理服务器及电子邮件(IMAP/POP3)代理服务器,其特点是占有内存少,并发能力强,反对 FastCGI、SSL、Virtual Host、URL Rewrite、gzip 等性能,并且反对很多第三方的模块扩大。

开发语言:C

一句话形容:Web 服务器 / 反向代理服务器及电子邮件(IMAP/POP3)代理服务器

倡议的版本

倡议应用版本为“Nginx 1.14.2”。
阐明:
本文档实用于 Nginx 1.14.2 版本,其余版本的 Nginx 移植步骤也可参考本文档。

环境要求

硬件要求

硬件要求如表 1 所示。
表 1 硬件要求

我的项目 阐明
服务器 TaiShan 200 服务器(型号 2280)
CPU 鲲鹏 920 5250 处理器
磁盘分区 对磁盘分区无要求

操作系统要求

操作系统要求如表 2 所示。
表 2 操作系统要求

我的项目 版本 版本查看命令
openEuler 20.03 LTS SP1 cat /etc/openEuler-release
Kernel 4.19.90 uname -r

配置编译环境

配置 Yum 源

阐明:
如果组网环境处于外网受限状况下,服务器 yum 命令无奈通过外界获取依赖包时,可参考本节内容进行本地源配置。

  1. 将操作系统镜像文件 openEuler-20.03-LTS-everything-aarch64-dvd.iso 文件拷贝到每台服务器的“/root”目录下。
  2. 镜像文件挂载。
    a. 将“/root”目录下的 openEuler 操作系统对应 iso 文件挂载到“/mnt”目录下。
    mount /root/openEuler-20.03-LTS-SP1-everything-aarch64-dvd.iso /mnt
    阐明:
    该操作单次失效,重启后生效。若须要配置开机启动主动挂载镜像(可选),可参考上面步骤。

    1. 关上 fstab 文件。
      vi /etc/fstab
    2. 编辑 fstab 文件,在文件开端增加如下信息:
      /root/openEuler-20.03-LTS-SP1-everything-aarch64-dvd.iso /mnt iso9660 loop 0 0
    3. 保留并退出 fstab 文件。
  3. 增加本地源文件。
    a. 进入“/etc/yum.repos.d”目录。
    cd /etc/yum.repos.d
    阐明:
    此时,倡议将此目录下的 *.repo 文件移到任意其余备份目录下。
    b. 创立 local.repo 文件。

    1. 关上 local.repo 文件。
      vi local.repo
    2. 编辑 local.repo 文件,在 local.repo 文件中增加如下内容:

      [local]
      name=local.repo
      baseurl=file:///mnt
      enabled=1
      gpgcheck=0

      阐明:
      其中,baseurl 中 file 门路为镜像挂载门路,与镜像文件挂载中的目录“/mnt”对应。

    3. 保留并退出 local.repo 文件。
    4. 失效本地源。

      yum clean all
      yum makecache
      yum list

装置依赖包

下载并装置依赖包

yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel pcre2-devel perl-devel perl-ExtUtils-Embed openssl openssl-devel

获取源码

本文应用源码编译装置,因而须要获取到 Nginx。

  1. 下载 Nginx 源码
    cd /home
    wget https://nginx.org/download/nginx-1.14.2.tar.gz --no-check-certificate

阐明:
也能够通过本地浏览器下载源码之后上传到服务器 ”/home” 目录下。
源码地址:https://nginx.org/download/ng…

留神:
若及其须要配置代理才能够拜访外网,请参考上面操作配置网络代理。

  1. 关上 profile 文件
    vi /etc/profile
  2. 增加如下代码后,保留并退出文件。
    其中,代理服务用户名、代理服务器明码、代理服务器 IP 和代理服务端口须要依据以后环境配置

    export http_proxy="http:// 代理服务器名: 代理服务器明码 @代理服务器 IP:代理服务器端口"
    export http_proxy=$http_proxy
    export no_proxy=127.0.0.1,.huawei.com,localhost,local,.local
  3. 应用代理效。
    source /etc/profile
  4. 查看环境变量中的代理信息。
    env
  5. 验证代理是否配置胜利。
    curl www.baidu.com
    能够失常解析百度即为配置胜利。

编译和装置

  1. 解压 Nginx 安装包。
    tar -xvf nginx-1.14.2.tar.gz
  2. 进入 ”nginx-1.14.2″ 目录。
    cd /home/nginx-1.14.2/
  3. 配置 Nginx。
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module
    阐明:
  4. –prefix=PATH: 用来制订 Nginx 的装置目录,默认装置目录为 ”/usr/local/nginx”。
  5. 不须要配置 with-http_stub_status_module 模块,该统计模块会影响 Nginx 性能。
  6. 编译并装置 Nginx
    make -j96 && make -j96 install
    阐明:
    -j96: 充分利用 CPU 多核优势,放慢编译装置速度。
    CPU 的核数能够通过 lscpu 查看。
    5. 查看装置目录。
    ls /usr/local/nginx

运行和验证

生成证书

  1. 进入 ”/usr/local/nginx” 目录,在该目录下生成密钥 key。
    cd /usr/local/nginx
    openssl genrsa -des3 -out server_2048.key 2048
    会有两次要求输出明码,输出同一个即可,此时会生成 server_2048.key 文件。

    [[email protected] nginx]# openssl genrsa -des3 -out server_2048.key 2048
    Generating RSA private key, 2048 bit long modulus (2 primes)
    ..................................................................................+++++
    ................+++++
    e is 65537 (0x010001)
    Enter pass phrase for server_2048.key:
    Verifying - Enter pass phrase for server_2048.key:

    阐明:
    可通过如下命令实现免明码应用此文件:
    openssl rsa -in server_2048.key -out -server_2048.key

    [[email protected] nginx]# openssl rsa -in server_2048.key -out -server_2048.key
    Enter pass phrase for server_2048.key
    writing RSA key
  2. 创立服务器证书的申请文件。

    openssl req -new -key server_2048.key -out server_2048.csr
[[email protected] nginx]# openssl req -new -key server_2048.key -out server_2048.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

输出 1 中设置的明码,其中 Country Name 选项输出 CN,其余选项能够不填。

  1. 重写密钥 key。
    openssl rsa -in server_2048.key -out server_2048.key

    [[email protected] nginx]# openssl rsa -in server_2048.key -out server_2048.key
    writing RSA key
  2. 生成证书。
    openssl x509 -req -days 365 -in server_2048.csr -signkey server_2048.key -out server_2048.crt

    [[email protected] nginx]# openssl x509 -req -days 365 -in server_2048.csr -signkey server_2048.key -out server_2048.crt
    Signature ok
    subject=C = CN, ST = Some-State, O = Internet Widgits Pty Ltd
    Getting Private key

    输出 1 中设置的明码。若曾经设置免明码应用该文件,则无需输出明码。

配置性能

配置 Nginx 的 HTTPS 性能

  1. 关上 nginx.conf 配置文件。
    vi /usr/local/nginx/conf/nginx.conf
  2. 批改 nginx.conf 配置文件以下三处配置后,保留并退出(Esc+ :wq)。

    • 定义 Nginx 运行的用户权限 user 为 root。
    • 批改 listen 监测端口,能够应用默认端口,本文批改为 20000。
    • 指定 ssl_certificate 和 ssl_certificate_key 文件。
原文默认内容:
#user  nobody;
...
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
批改后内容:
user  root;
 ...
     HTTPS server
    
    server {
        listen       20000 ssl;
        server_name  localhost;

        ssl_certificate      /usr/local/nginx/server_2048.crt;
        ssl_certificate_key  /usr/local/nginx/server_2048.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

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

配置 Nginx 的 HTTP 性能

  1. 关上 nginx.conf 配置文件。
    vi /usr/local/nginx/conf/nginx.conf
  2. 批改 nginx.conf 配置文件以下三处配置后,保留并退出(Esc+ :wq)。

    • 定义 Nginx 运行的用户权限 user 为 root。
    • 批改 listen 监测端口,能够应用默认端口,本文批改为 10000。
原文件默认内容:
user  root;
...
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '
    #                  '$status $body_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

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
   }
}
批改后内容:
user  root;
...
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '
    #                  '$status $body_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

    server {
        listen       10000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

运行 Nginx

  1. 启动 Nginx(两种形式)。

    • 通过 Service 服务启动(应用该办法须要先将 Nginx 退出 Service 服务再执行启动命令)。

      1. 批改“/etc/init.d/nginx”文件。
        a. 删除原文件 nginx。
        rm -rf /etc/init.d/nginx
        b. 新建 nginx 文件。

         ```vi /etc/init.d/nginx```
         c. 增加如下内容后,保留并退 
          #!/bin/bash
          # chkconfig: 2345 10 90
          # description: nginx
          case "$1" in
         'start')
           /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           echo "$0_start";
          ;;
        'stop')
         /usr/local/nginx/sbin/nginx -s quit
         echo "$0_stop";
         ;;
          esac
      2. 批改“/etc/init.d/nginx”文件权限。
        chmod 777 /etc/init.d/nginx

        1. 将 Nginx 退出 chkconfig 治理列表。

        chkconfig --add /etc/init.d/nginx

        1. 设置 Nginx 开机主动启动。

        chkconfig nginx on

        1. 启动 Nginx。

        service nginx start

        • 通过脚本命令启动。

        /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  2. 查看 Nginx 的过程。
    ps -ef | grep nginx

    [[email protected] nginx]# ps -ef | grep nginx
    root        9463       1  0 18:22 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    root        9464    9463  0 18:22 ?        00:00:00 nginx: worker process
    root        9466    1352  0 18:23 ttyAMA0  00:00:00 grep --color=auto nginx

    阐明:
    敞开 Nginx 命令如下(3 种形式,可选)。业务运行中不要执行该命令。

    • 通过 Service 服务敞开。
      service nginx stop
    • 通过脚本命令敞开。
      /usr/local/nginx/sbin/nginx -s quit
    • 应用完结过程命令。

      pkill nginx

[[email protected] nginx]# pkill nginx
[[email protected] nginx]# ps -ef | grep nginx
root        9469    1352  0 18:27 ttyAMA0  00:00:00 grep --color=auto nginx

验证 Nginx

  1. 查看 Nginx 的监测端口(10000 是 HTTP 监测端口,20000 是 HTTPS 监测端口)。
    netstat -anp | grep 10000
    netstat -anp | grep 20000
    netstat -anpt
[[email protected] nginx]# netstat -anp | grep 10000
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      9535/nginx: master  
[[email protected] nginx]# netstat -anp | grep 20000
tcp        0      0 0.0.0.0:20000           0.0.0.0:*               LISTEN      9535/nginx: master  
[[email protected] nginx]# netstat -anpt
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:10000           0.0.0.0:*               LISTEN      9535/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      775/sshd: /usr/sbin 
  1. 查看 Nginx 的 HTML 文件所在目录。
    ll -h /usr/local/nginx/html/

    [[email protected] nginx]# ll -h /usr/local/nginx/html/
    total 8.0K
    -rw-r--r--. 1 root root 537 Mar 20 16:46 50x.html
    -rw-r--r--. 1 root root 612 Mar 20 16:46 index.html
  2. 验证 HTTPS 性能。
    通过 curl 本地拜访 Nginx 的 HTML 页面。
    curl -k https://127.0.0.1:20000/index.html
[[email protected] nginx]# curl -k  https://127.0.0.1:20000/index.html



Welcome to nginx!

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }



<h1>Welcome to nginx!</h1>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online ation and support please refer to
<a href="#" class="white">nginx.org</a>.

Commercial support is available at
<a href="#" class="white">nginx.com</a>.

<em>Thank you for using nginx.</em>

  1. 验证 HTTP 性能。
    通过 curl 本地拜访 Nginx 的 HTML 页面。
    curl http://127.0.0.1:10000/index.html
[[email protected] nginx]# curl http://127.0.0.1:10000/index.html



Welcome to nginx!

    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }



<h1>Welcome to nginx!</h1>
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.

For online ation and support please refer to
<a href="#" class="white">nginx.org</a>.

Commercial support is available at
<a href="#" class="white">nginx.com</a>.

<em>Thank you for using nginx.</em>

卸载 Nginx

  1. 编译装置只是生成对应的文件,不波及卸载,间接删除对应的装置目录即可。
    rm -rf /usr/local/nginx
正文完
 0