nginx的安装应用--01--(inux运维15)

本文介绍了Nginx的特点及如何在CentOS 7上通过YUM安装并配置Nginx。包括设置官方YUM源、启动服务、关键文件目录说明及配置文件详解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. nginx的特点

1.支持高并发,消耗内存资源少
2.具有多种功能
3.多平台部署
4.nginx实现网络通讯使用异步网络io模型(epoll模型)

2. nginx的安装(yum的安装方式centos7)

2.1 官网寻找稳定版本

在这里插入图片描述

2.2 配置官方的yum源

官方的yum源:

http://nginx.org/en/linux_packages.html#RHEL-CentOS
在这里插入图片描述
根据上边的进行配置,

vim /etc/yum.repos.d/nginx.repo

写入:

[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

2.3 安装

yum install -y nginx

2.4 启动,开机自启

systemctl start nginx
ssytemctl enable nginx

查看是否启动

systemctl status nginx

3. nginx的一些重要的文件目录

3.1 /etc/logrotate.d

实现nginx的日志文件的切割。
方法一:脚本

#!bin/bash
mv /var/log/nginx/acess.log /var/log/nginx/acess_$(date+ %F).log

方法二:利用专用文件
vim /etc/logrotate.dconf

[root@web02 18:57:46 /etc]# cat logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly

# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
	minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}
# rotate log files weekly
       weekly                     --- 定义默认日志切割的周期
       
       # keep 4 weeks worth of backlogs
       rotate 4                   --- 定义只保留几个切割后的文件
       
       # create new (empty) log files after rotating old ones
       create                     --- 创建出一个相同的源文件
       
       # use date as a suffix of the rotated file
       dateext                    --- 定义角标(扩展名称信息)
       
       # uncomment this if you want your log files compressed
       #compress                  --- 是否对切割后的文件进行压缩处理
       
       # RPM packages drop log rotation information into this directory
       include /etc/logrotate.d   --- 加载包含/etc/logrotate.d/目录中文件配置
       
       # no packages own wtmp and btmp -- we'll rotate them here
       /var/log/wtmp {            --- 单独对某个文件进行切割配置
           monthly
           create 0664 root utmp
       	   minsize 1M             --- 最小大小为1M,小于1M不进行切割              
           rotate 1
       }
       
       /var/log/btmp {
           missingok
           monthly
           create 0600 root utmp
           rotate 1
       }

3.2 其他

/etc/nginx配置文件
/var/log/nginx日志文件
/usr/bin/nginx命令文件
/usr/share/nginx/html站点目录

3.3 nginx服务配置文件

/etc/nginx/nginx.conf

grep -v  "^$" nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

参数解释:

第一个部分: 配置文件主区域配置
	user  www;               	 --- 定义worker进程管理的用户
	补充: nginx的进程
	master process:  主进程		---管理服务是否能够正常运行   boss
	worker process:  工作进程	---处理用户的访问请求         员工  
    worker_processes  2;        ---定义有几个worker进程  == CPU核数 / 核数的2倍
    error_log  /var/log/nginx/error.log warn;   --- 定义错误日志路径信息
    pid        /var/run/nginx.pid;              --- 定义pid文件路径信息
	
	第二个部分: 配置文件事件区域
    events {                    
        worker_connections  1024;   --- 一个worker进程可以同时接收1024访问请求
    }
	
	第三个部分: 配置http区域
    http {
        include       /etc/nginx/mime.types;      --- 加载一个配置文件
        default_type  application/octet-stream;   --- 指定默认识别文件类型
        log_format  oldboy  '$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  oldboy;
		                  --- 指定日志路径          
        sendfile        on;   ???
        #tcp_nopush     on;   ???
        keepalive_timeout  65;   --- 超时时间
        #gzip  on;
        include /etc/nginx/conf.d/*.conf;        --- 加载一个配置文件
    }


	/etc/nginx/nginx.d/default  --- 扩展配置(虚拟主机配置文件)
	第四个部分: server区域信息(配置一个网站 www/bbs/blog -- 一个虚拟主机)
	server {
        listen       8080;                --- 指定监听的端口
        server_name  www.oldboy.com;      --- 指定网站域名                     
        root   /usr/share/nginx/html;     --- 定义站点目录的位置
        index  index.html index.htm;      --- 定义首页文件
        error_page   500 502 503 504  /50x.html;   --- 优雅显示页面信息
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }

先把nginx.conf进行备份,再去除多余的部分

cp nginx.conf{,.bak}

egrep -v  "^$|#" nginx.conf.bak > nginx.conf

这里我们创建一个虚拟用户www,进行nginx进程的的管理

useradd www -s /sbin/nologin -M

然后把nginx.conf中的user修改,重启nginx

[root@web02 19:09:53 /etc/nginx]# systemctl restart nginx
[root@web02 19:10:00 /etc/nginx]# ps -ef | grep nginx
root      8175     1  0 19:09 ?        00:00:00 nginx: master process /usr/sbin/nginx
www       8177  8175  0 19:09 ?        00:00:00 nginx: worker process
root      8183  7984  0 19:10 pts/0    00:00:00 grep --color=auto nginx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长安有故里y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值