linux高阶-Nginx服务(二)-安装与配置

Nginx安装

centos安装nginx

//配置epel源
yum -y install epel-release

//下载安装nginx
yum -y install nginx

//查看版本
nginx -v

//查看版本以及配置选项
nginx -V

ubuntu安装nginx

//配置apt源
vim /etc/apt/sources.list

//源文件内容为
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

//更新apt源
apt updata

//查看nginx版本列表
apt-cache madison nginx

//下载安装nginx
apt -y install nginx

//查看版本
nginx -v

//查看版本以及配置选项
nginx -V

查看帮助

//查看帮助
nginx -h

nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit #显⽰版本和编译参数
-t : test configuration and exit #测试配置⽂件是否异常
-T : test configuration, dump it and exit #测试并打印
-q : suppress non-error messages during configuration testing #静默模式
-s signal : send signal to a master process: stop, quit, reopen, reload #发送信号
-p prefix : set prefix path (default: /usr/share/nginx/) #指定Nginx ⽬录
-c filename : set configuration file (default: /etc/nginx/nginx.conf) #配置⽂件路径
-g directives : set global directives out of configuration file #设置全局指令

目录结构

//查看配置文件
rpm -ql nginx

//主要配置文件
--------------------------------------------
/etc/nginx/fastcgi.conf   # fastcgi配置文件
/etc/nginx/nginx.conf   # nginx主配置文件
/usr/sbin/nginx      # nginx主程序
/usr/share/nginx/html/    # 网页主目录
/var/log/nginx  #nginx日志目录
/etc/nginx/conf.d/*.conf #子配置文件
/etc/nginx/uwsgi_params #uwsgi协议配置文件
/etc/nginx/scgi_params #scgi协议配置文件
/etc/nginx/mime.types #支持的mime类型
------------------------------

编译安装

配置基础编译环境

yum -y install vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate \
gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel \
systemd-devel nettools iotop bc zip unzip zlib-devel \
bash-completion nfs-utils automake libxml2 libxml2-devel \
libxslt libxslt-devel perl perl-ExtUtils-Embed
  • gcc为GNU Compiler Collection的缩写,可以编译C和C++源代码等,它是GNU开发的C和C++以及其他很多种语⾔的编译器(最早的时候只能编译C,后来很快进化成⼀个编译多种语⾔的集合,如Fortran、Pascal、Objective-C、Java、Ada、 Go等。)
  • gcc 在编译C++源代码的阶段,只能编译 C++ 源⽂件,⽽不能⾃动和 C++ 程序使⽤的库链接(编译过程分为编译、链接两个阶段,注意不要和可执⾏⽂件这个概念搞混,相对可执⾏⽂件来说有三个重要的概念:编译 (compile)、链接(link)、加载(load)。源程序⽂件被编译成⽬标⽂件,多个⽬标⽂件连同库被链接成⼀个最终的可执⾏⽂件,可执⾏⽂件被加载到内存中运⾏)。因此,通常使⽤ g++ 命令来完成 C++ 程序的编译和连接,该程序会⾃动调⽤ gcc 实现编译。
  • gcc-c++也能编译C源代码,只不过把会把它当成C++源代码,后缀为.c的,gcc把它当作是C程序,⽽g++当作是c++程序;后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的。
  • automake是⼀个从Makefile.am⽂件⾃动⽣成Makefile.in的⼯具。为了⽣成Makefile.in,automake还需⽤到perl,由于automake创建的发布完全遵循GNU标准,所以在创建中不需要perl。libtool是⼀款⽅便⽣成各种程序库的⼯具。
  • pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的Rewrite模块和HTTP 核⼼模块会使⽤到PCRE正则表达式语法。
  • zlip zlib-devel:nginx启⽤压缩功能的时候,需要此模块的⽀持。
  • openssl openssl-devel:开启SSL的时候需要此模块的⽀持。
//进入指定目录
cd /usr/local/src/

//下载源码包
wget https://nginx.org/download/nginx-1.16.1.tar.gz

//解包
tar xf nginx-1.16.1.tar.gz

//进入目录,准备编译
cd nginx-1.16.1/

#编译是为了检查系统环境是否符合编译安装的要求。
#⽐如是否有gcc编译⼯具,是否⽀持编译参数当中的模块。
#并根据开启的参数等⽣成Makefile⽂件为下⼀步做准备:

//开始编译
------------------------------
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
------------------------------------

//根据Makefile文件生成相应的模块
make

//创建目录,并将生成的模块和文件复制到相应的目录
make install

//创建服务所需系统用户
useradd nginx -s /sbin/nologin -u 2000

//授权创建的用户访问服务目录
chown -R nginx.nginx /apps/nginx 

//自定义启动脚本
vim /usr/lib/systemd/system/nginx.service

-------------------------------------------
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid   #注意这里的路径,必须和配置文件里的PID路径一致
ExecStartPre=/usr/bin/rm -f /var/run/nginx.pid  #这里也是
ExecStartPre=/usr/sbin/nginx -t   #主程序的路径
ExecStart=/usr/sbin/nginx   #主程序的路径
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
------------------------------------------------
  • 备注:nginx完成编译安装后,有四个主要的目录
    • /apps/nginx/conf:该⽬录中保存了nginx所有的配置⽂件,其中nginx.conf是nginx服务器的最核⼼最主要的配置⽂件,其他的.conf则是⽤来配置nginx相关的功能的,例如fastcgi功能使⽤的是fastcgi.conf和fastcgi_params两个⽂件,配置⽂件⼀般都有个样板配置⽂件,是⽂件名.default结尾,使⽤的使⽤将其复制为并将default去掉即可。
    • /apps/nginx/html:该⽬录中保存了nginx服务器的web⽂件,但是可以更改为其他⽬录保存web⽂件,另外还有⼀个50x的web⽂件是默认的错误⻚⾯提⽰⻚⾯。
    • /apps/nginx/logs:该⽬录⽤来保存nginx服务器的访问⽇志错误⽇志等⽇志,logs⽬录可以放在其他路径,⽐如/var/logs/nginx⾥⾯。
    • /apps/nginx/sbin:该⽬录⽤来保存nginx⼆进制启动脚本,可以接受不同的参数以实现不同的功能。

配置Nginx

Nginx的配置⽂件的组成部分: 主配置⽂件:nginx.conf,⼦配置⽂件 include conf.d/*.conf

fastcgi, uwsgi,scgi等协议相关的配置⽂件
mime.types:⽀持的mime类型,MIME(Multipurpose Internet Mail Extensions)多⽤途互联⽹邮件扩展类
型,MIME消息能包含⽂本、图像、⾳频、视频以及其他应⽤程序专⽤的数据,是设定某种扩展名的⽂件⽤⼀种应⽤程序来
打开的⽅式类型,当该扩展名⽂件被访问的时候,浏览器会⾃动使⽤指定应⽤程序来打开。多⽤于指定⼀些客⼾端⾃定义
的⽂件名,以及⼀些媒体⽂件打开⽅式。
Nginx主配置⽂件的配置指令⽅式:
directive value [value2 ...];
注意:
(1) 指令必须以分号结尾
(2) ⽀持使⽤配置变量
内建变量:由Nginx模块引⼊,可直接引⽤
⾃定义变量:由⽤⼾使⽤set命令定义
set variable_name value;
引⽤变量:$variable_name

MIME 参考⽂档:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Basics_of_HTTP/MIME_Types

main block  # 主配置段,即全局配置段,对http,mail都有效

# 事件驱动相关的配置
event {
    ...
}  

# http/https 协议相关配置段
http {
    ...
}   

# mail 协议相关配置段
mail {
    ...
}   

# stream 服务器相关配置段
stream {
    ...
}   

Nginx默认配置文件

[root@s2 ~]# grep -v "#" /apps/nginx/conf/nginx.conf | grep  -v "^$"
# 全局配置端,对全局⽣效,主要设置nginx的启动⽤⼾/组,启动的⼯作进程数量,⼯作模式,Nginx的PID路径,⽇志路径等。
user  nginx nginx;
worker_processes  1;   # 启动⼯作进程数数量
events { # events设置快,主要影响nginx服务器与⽤⼾的⽹络连接,⽐如是否允许同时接受多个⽹络连接,使⽤哪种事件驱动模型处理请求,每个⼯作进程可以同时⽀持的最⼤连接数,是否开启对多⼯作进程下的⽹络连接进⾏序列化等。
worker_connections  1024;   # 设置单个nginx⼯作进程可以接受的最⼤并发,作为web服务器的时候最⼤并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections *worker_processes)/2
}
http { # http块是Nginx服务器配置中的重要部分,缓存、代理和⽇志格式定义等绝⼤多数功能和第三⽅模块都可以在这设置,http块可以包含多个server块,⽽⼀个server块中⼜可以包含多个location块,server块可以配置⽂件引⼊、MIME-Type定义、⽇志⾃定义、是否启⽤sendfile、连接超时时间和单个链接的请求上限等。
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on; # 作为web服务器的时候打开sendfile加快静态⽂件传输,指定是否使⽤sendfile系统调⽤来传输⽂件,sendfile系统调⽤在两个⽂件描述符之间直接传递数据(完全在内核中操作),从⽽避免了数据在内核缓冲区和⽤⼾缓冲区之间的拷⻉,操作效率很⾼,被称之为零拷⻉,硬盘 >> kernel buffer (快速拷⻉到kernelsocket buffer) >>协议栈。
    keepalive_timeout  65;  # ⻓连接超时时间,单位是秒
    server { # 设置⼀个虚拟机主机,可以包含⾃⼰的全局快,同时也可以包含多个location模块。⽐如本虚拟机监听的端⼝、本虚拟机的名称和IP配置,多个server 可以使⽤⼀个端⼝,⽐如都使⽤80端⼝提供web服务
        listen       80;  # 配置server监听的端⼝
        server_name  localhost;  # 本server的名称,当访问此名称的时候nginx会调⽤当前serevr内部的配置进程匹配。
        location / { # location其实是server的⼀个指令,为nginx服务器提供⽐较多⽽且灵活的指令,都是在location中提现的,主要是基于nginx接受到的请求字符串,对⽤⼾请求的UIL进⾏匹配,并对特定的指令进⾏处理,包括地址重定向、数据缓存和应答控制等功能都是在这部分实现,另外很多第三⽅模块的配置也是在location 块中配置。
            root   html; # 相当于默认⻚⾯的⽬录名称,默认是相对路径,可以使⽤绝对路径配置。
            index  index.html index.htm; # 默认的⻚⾯⽂件名称
        }
        error_page   500 502 503 504  /50x.html; # 错误⻚⾯的⽂件名称
        location = /50x.html { # location处理对应的不同错误码的⻚⾯定义到/50x.html,这个跟对应其
server中定义的⽬录下。
            root   html; # 定义默认⻚⾯所在的⽬录
        }
    }

# 和邮件相关的配置
# mail {
#                 ...
#         }          mail 协议相关配置段

# tcp代理配置,1.9版本以上⽀持
# stream {
#                 ...
#         }       stream 服务器相关配置段

# 导⼊其他路径的配置⽂件
# include /apps/nginx/conf.d/*.conf
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值