当前位置:首页 > 操作系统 > Linux

Linux Nginx——Yum安装Nginx部署、Nginx编译安装配置、Nginx日志文件

  • Nginx部署-Yum安装

Nginx的官方网站:http://www.nginx.org/

Nginx版本类型
Mainline version: 开发版
Stable version: 稳定版,生产环境上建议版本
Legacy versions: 老版本稳定版

Yum安装nginx

配置Yum源的官网 http://nginx.org/en/linux_packages.html

配置Nginx的Yum源

安装:
首次安装nginx,需要设置Nginx软件包存储库。 之后,可以从存储库安装和更新Nginx。
RHEL/CENTOS

Install the prerequisites 安装先决条件

# sudo yum install yum-utils -y[nginx-stable]name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true[nginx-mainline]name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

yum install nginx -y

这里我们用稳定版本

[root@nginx-server yum.repos.d]#  yum install -y nginx[root@nginx-server yum.repos.d]#  nginx -V    格式化打印[root@nginx-server yum.repos.d]#  nginx -v  查看Nginx版本

关防火墙和Selinux:

[root@nginx-server ~]# setenforce 0[root@nginx-server ~]# systemctl stop firewalld[root@nginx-server ~]# systemctl disable firewalld

启动、设置开机自启

[root@nginx-server ~]# systemctl start nginx [root@nginx-server ~]# systemctl enable nginx

打开浏览器输入IP访问:如10.8.175.1


  • Nginx 编译安装、配置
yum -y install gcc gcc-c++   
安装编译环境

yum install -y pcre pcre-devel   
安装pcre软件包(使nginx支持http rewrite模块)

yum install -y openssl openssl-devel     
安装openssl-devel(使nginx支持ssl)

yum install -y zlib zlib-devel
安装zlib

useradd nginx
创建用户nginxpasswd nginx
设置密码

[root@localhost ~]# wget http://nginx.org/download/nginx-1.19.10.tar.gz[root@localhost ~]# tar xzf nginx-1.19.10.tar.gz -C /usr/local/[root@localhost ~]# cd /usr/local/nginx-1.19.10/[root@localhost nginx-1.19.10]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream[root@localhost nginx-1.19.10]# make && make install下载-解压-配置- 编译安装

Nginx 编译参数

[root@localhost ~]#/usr/local/nginx/sbin/nginx -V查看Nginx安装模块

  • 模块参数具体功能
--with-cc-opt='-g -O2 -fPIE -fstack-protector     
设置额外的参数将被添加到CFLAGS变量。(FreeBSD或者ubuntu使用)

--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' 
 --prefix=/usr/local/nginx                        指向安装目录
--conf-path=/etc/nginx/nginx.conf                指定配置文件
--http-log-path=/var/log/nginx/access.log        指定访问日志
--error-log-path=/var/log/nginx/error.log        指定错误日志
--lock-path=/var/lock/nginx.lock                 指定lock文件
--pid-path=/run/nginx.pid                        指定pid文件
   
--http-client-body-temp-path=/var/lib/nginx/body    
设定http客户端请求临时文件路径

--http-fastcgi-temp-path=/var/lib/nginx/fastcgi     
   设定http fastcgi临时文件路径
   
--http-proxy-temp-path=/var/lib/nginx/proxy         
设定http代理临时文件路径

--http-scgi-temp-path=/var/lib/nginx/scgi           
设定http scgi临时文件路径

--http-uwsgi-temp-path=/var/lib/nginx/uwsgi         
设定http uwsgi临时文件路径
   
--with-debug                         启用debug日志
--with-pcre-jit                      编译PCRE包含“just-in-time compilation”
--with-ipv6                          启用ipv6支持
--with-http_ssl_module               启用ssl支持
--with-http_stub_status_module       获取nginx自上次启动以来的状态
--with-http_realip_module            允许从请求标头更改客户端的IP地址值,默认为关
--with-http_auth_request_module      实现基于一个子请求的结果的客户端授权

--with-http_addition_module          
作为一个输出过滤器,支持不完全缓冲,分部分响应请求

--with-http_dav_module                
增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启

--with-http_geoip_module               
使用预编译的MaxMind数据库解析客户端IP地址,得到变量值

--with-http_gunzip_module              
它为不支持“gzip”编码方法的客户端解压具有“Content-Encoding: gzip”头的响应。

--with-http_gzip_static_module         在线实时压缩输出数据流
--with-http_image_filter_module        传输JPEG/GIF/PNG 图片的一个过滤器)
--with-http_spdy_module                SPDY可以缩短网页的加载时间
--with-http_sub_module                允许用一些其他文本替换nginx响应中的一些文本
--with-http_xslt_module               过滤转换XML请求
--with-mail                           启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module                启用ngx_mail_ssl_module支持启用外部模块支持

修改配置文件、全局参数设置

vim /etc/nginx/nginx.conf

user  nginx;                  #指定用户worker_processes  4;          #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同error_log  logs/error.log;    #指定错误日志worker_rlimit_nofile 10240;   #设置一个nginx进程能打开的最大文件数pid        /var/run/nginx.pid;events {
    worker_connections  1024; #设置一个进程的最大并发连接数}http #服务模块设置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  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
    gzip              on;      #是否开启gzip压缩,将注释去掉开启 
    keepalive_timeout  65;     #设置长连接的超时时间# 虚拟服务器的相关设置server { 
        listen      80;        #设置监听的端口 
        server_name  localhost;        #设置绑定的主机名、域名或ip地址 #        charset koi8-r;        #设置编码字符 
        charset utf-8;
        location / { 
            root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;    #设置默认打开的文档 
        } 
        error_page  500 502 503 504  /50x.html; #设置错误信息返回页面 
        location = /50x.html { 
            root  html;        #这里的绝对位置是/usr/local/nginx/html
        }
    }}

nginx.conf配置文件组成:分别为:全局块、events块、http块三部份。

http模块包含:http全局模块、多个server模块

server块包含:server全局模块、多个location模块。

在统一配置块中嵌套的配置快,各个模块之间不存在次序关系。

  • 检测nginx配置文件是否正确
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t[root@localhost ~]# mkdir -p /tmp/nginx[root@localhost ~]# mkdir /usr/local/nginx/logs

  • 启动nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx

  • 通过 nginx 命令控制 nginx 服务

a、常用命令

nginx -c /path/nginx.conf           # 以特定目录下的配置文件启动nginx:nginx -s reload                     # 修改配置后重新加载生效nginx -s stop                       # 快速停止nginxnginx                               # 快速启动nginxnginx -t                            # 测试当前配置文件是否正确nginx -t -c /path/to/nginx.conf     # 测试特定的nginx配置文件是否正确

b、分配权限

chmod +x /etc/init.d/nginx

c、重新加载系统启动文件

systemctl daemon-reload

d、启动并设置开机自启

systemctl start nginx

  • Nginx 日志文件详解

nginx 日志文件分为 log_format 和 access_log 两部分
 log_format 定义记录的格式,其语法格式为
 log_format 样式名称 样式详情

配置文件中默认有

log_format  main  'remote_addr - remote_user [time_local] "request" '
                  'status body_bytes_sent "$http_referer" '
                  '"http_user_agent" "http_x_forwarded_for"';

Linux Nginx——Yum安装Nginx部署、Nginx编译安装配置、Nginx日志文件 - 文章图片


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐