1 二进制安装

Ubuntu等系统: sudo apt-get install nginx centos系统: sudo yum install nginx

2 编译安装

2.1 环境准备

准备好make、wget、g++等软件。 yum install -y gcc-c++ make wget

2.2 下载软件

下载如下编译需要用的源代码。 下载openssl主要用于ssl模块加密,支持htps: wget https://www.openssl.org/source/openssl-1.0.2s.tar.gz 下载pcre来实现对地址重定向,地址重写功能和localtion指令以及正则表达式的支持: wget https://ftp.exim.org/pub/pcre/pcre-8.43.tar.gz 下载zlib gzip压缩模块: wget https://zlib.net/fossils/zlib-1.2.11.tar.gz 下载nginx源码: wget "https://nginx.org/download/nginx-1.18.0.tar.gz" --user-agent="Mozilla/5.0 (X11;U;Linux i686;en-US;rv:1.9.0.3) Geco/2008092416 Firefox/3.0.3"

2.3 解压文件

ls *.tar.gz | xargs -n1 tar xzvf

2.4 编译安装

编译选项:

参数

描述

--prefix=

Nginx安装目录,以及有其他配置脚本选项的路径设置的所有相对路径的基本位置。默认值/usr/local/nginx

--sbin-path=<PATH

Nginx二进制执行文件的名称,默认值:/sbin/nginx

--conf-path=

Nginx配置文件的名称。但是,您可以通过在nginx命令行上使用选项指定其他文件来始终在启动时覆盖此值。默认值: conf / nginx.conf-c

--pid-path=

nginx.pid文件的名称,用于存储nginx主进程的进程ID 。安装后,可以使用Nginx配置文件中的pid指令更改文件名的路径。默认值: /logs/nginx.pid

--error-log-path=

error,warn和诊断数据的日志文件的名称。安装后,可以使用Nginx配置文件中的error_log指令更改文件名。默认值: /logs/error.log

--http-log-path=

HTTP服务器请求的主日志文件的名称。安装后,始终可以使用Nginx配置文件中的access_log指令更改文件名。默认值: /logs/access.log

--user=

Nginx运行进程的拥有者。安装后,可以使用Nginx配置文件中的user指令更改名称。默认:nobody

--group=name

nginx运行进程的拥有者用户组。安装后,可以使用NGINX配置文件中的user指令更改名称。默认值:--user选项设置的值

--with-pcre=

PCRE库源代码的路径,这是位置指令和Rewrite模块中正则表达式支持所必需的

--with-pcre-jit

使用“即时编译”支持(pcre_jit指令)构建PCRE库

--with-zlib=

zlib库的源代码路径,Gzip模块需要该路径

--with-http_ssl_modul

启用HTTPS支持

--with-http_v2_module

开启 HTTP/2请求支持

2.4.1 编译配置

./configure \
   --with-openssl=../openssl-1.0.2s \
   --with-pcre=../pcre-8.43 \
   --with-zlib=../zlib-1.2.11 \
   --with-http_ssl_module \
   --with-http_v2_module

2.4.2 检查

输出下面内容,表示正常

Configuration summary
  + using PCRE library: ../pcre-8.43
  + using OpenSSL library: ../openssl-1.0.2s
  + using zlib library: ../zlib-1.2.11

  nginx path prefix: "/home/admin/nginx"
  nginx binary file: "/home/admin/nginx/sbin/nginx"
  nginx modules path: "/home/admin/nginx/modules"
  nginx configuration prefix: "/home/admin/nginx/conf"
  nginx configuration file: "/home/admin/nginx/conf/nginx.conf"
  nginx pid file: "/home/admin/nginx/logs/nginx.pid"
  nginx error log file: "/home/admin/nginx/logs/error.log"
  nginx http access log file: "/home/admin/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

2.4.3 编译

make 如果报错,请确定gcc-c++ make软件是否全局安装。

2.4.4 安装

make install

2.5 安装完成

默认安装目录/usr/local/nginx

2.6 配置nginx.conf

路径位于/usr/local/nginx/conf/nginx.conf:

#指定线程的用户 需创建个非root权限的用户
user  www;
worker_processes  1;

# 指定用户线程
error_log   /home/www/wwwlogs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# 指定pid文件生成路径 和systemd配置文件的位置对应
pid        /var/run/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" '
    #                  '$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;
        }

        #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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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

    # 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;
    #    }
    #}
# 指定多站点配置文件的路径
include /home/wwwconfig/*.conf;
}

2.7 配置systemd

创建配置文件: vim /usr/lib/systemd/system/nginx.service

增加内容如下:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重载systemd systemctl daemon-reload

2.8 启动nginx

注意如果监听的端口低于1024需要root用户启动 systemctl start nginx