nginx软件安装部署

2021/5/23 7:27:44

本文主要是介绍nginx软件安装部署,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、下载
两种安装方式:
1、yum安装软件
a 使用官方yum源进行安装 安装的是最新版本 软件目录结构比较标准 (推荐)
b 使用非官方yum源进行安装 安装的不是最新版 目录结构会发生变化
2、编译安装软件
第一个步骤:
wget http://nginx.org/download/nginx-1.16.0.tar.gz
PS: 解决软件的依赖 openssl-devel pcre-devel

[root@ lb01 ~]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
--2021-05-22 14:59:38--  http://nginx.org/download/nginx-1.16.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032345 (1008K) [application/octet-stream]
Saving to: ‘nginx-1.16.0.tar.gz’

100%[===============================================================================>] 1,032,345    352KB/s   in 2.9s   

2021-05-22 14:59:47 (352 KB/s) - ‘nginx-1.16.0.tar.gz’ saved [1032345/1032345]
[root@ lb01 ~]# rpm -qa nginx
nginx-1.16.1-3.el7.x86_64
[root@ lb01 ~]# yum install -y openssl-devel
[root@ lb01 ~]# yum install -y pcre-devel

二、解压相关软件进入其目录

[root@ lb01 ~]# mkdir -p /server/tools
[root@ lb01 ~]# mv nginx-1.16.0.tar.gz /server/tools
[root@ lb01 tools]# tar -xf nginx-1.16.0.tar.gz 
[root@ lb01 tools]# ll
total 1012
drwxr-xr-x. 8 rsync rsync     158 Apr 23  2019 nginx-1.16.0
-rw-r--r--. 1 root  root  1032345 Apr 23  2019 nginx-1.16.0.tar.gz
[root@ lb01 tools]# cd nginx-1.16.0/

第三个步骤:编译安装
编译安装三步曲
a 进行配置操作
./configure (命令)(–prefix= PATH) (–user=USER )
用法:./configure --prefix= (写上参数信息比如:–user=USER )
–prefix=PATH set installation prefix
指定程序安装路径
–user=USER set non-privileged user for worker processes
设置一个虚拟用户管理worker进程(安全)
–group=GROUP set non-privileged group for worker processes
设置一个虚拟用户组管理worker进程(安全)

[root@ lb01 nginx-1.16.0]# ./configure --help

  --help                             print this message

  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                   set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname
***可以看到很多具体参数的用法。***

b 进行软件的编译过程:
make
c 编译安装过程
make install
(接上nginx第二种安装方法)yum官方源安装方法:
第一个历程: 更新nginx官方yum源

[root@ lb01 nginx-1.16.0]# 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

第二个历程: yum安装nginx软件

[root@ lb01 nginx-1.16.0]# yum install -y nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
nginx-stable                                 | 2.9 kB     00:00     
nginx-stable/7/x86_64/primary_db               |  63 kB   00:06     
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-3.el7 will be updated
---> Package nginx.x86_64 1:1.20.0-1.el7.ngx will be an update
--> Finished Dependency Resolution

Dependencies Resolved

====================================================================
 Package   Arch       Version                Repository        Size
====================================================================
Updating:
 nginx     x86_64     1:1.20.0-1.el7.ngx     nginx-stable     790 k

Transaction Summary
====================================================================
Upgrade  1 Package

Total download size: 790 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
warning: /var/cache/yum/x86_64/7/nginx-stable/packages/nginx-1.20.0-1.el7.ngx.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 7bd9bf62: NOKEY
Public key for nginx-1.20.0-1.el7.ngx.x86_64.rpm is not installed
nginx-1.20.0-1.el7.ngx.x86_64.rpm              | 790 kB   00:13     
Retrieving key from https://nginx.org/keys/nginx_signing.key
Importing GPG key 0x7BD9BF62:
 Userid     : "nginx signing key <signing-key@nginx.com>"
 Fingerprint: 573b fd6b 3d8f bc64 1079 a6ab abf5 bd82 7bd9 bf62
 From       : https://nginx.org/keys/nginx_signing.key
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : 1:nginx-1.20.0-1.el7.ngx.x86_64                  1/2 
warning: /etc/nginx/conf.d/default.conf created as /etc/nginx/conf.d/default.conf.rpmnew
warning: /etc/nginx/nginx.conf created as /etc/nginx/nginx.conf.rpmnew
  Cleanup    : 1:nginx-1.16.1-3.el7.x86_64                      2/2 
  Verifying  : 1:nginx-1.20.0-1.el7.ngx.x86_64                  1/2 
  Verifying  : 1:nginx-1.16.1-3.el7.x86_64                      2/2 

Updated:
  nginx.x86_64 1:1.20.0-1.el7.ngx                                   

Complete!

第三个历程: 启动nginx服务,检查服务是否安装正确

[root@ lb01 ~]# systemctl start nginx
[root@ lb01 ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-05-22 17:15:04 CST; 11s ago
     Docs: http://nginx.org/en/docs/
  Process: 4452 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 4453 (nginx)
   CGroup: /system.slice/nginx.service
           ├─4453 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─4454 nginx: worker process

May 22 17:15:04 lb01 systemd[1]: Starting nginx - high performance web server...
May 22 17:15:04 lb01 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
May 22 17:15:04 lb01 systemd[1]: Started nginx - high performance web server.
[root@ lb01 ~]# systemctl enable nginx

测试访问nginx服务
在页面输入nginx的IP地址
在这里插入图片描述

查看软件的目录结构(不切割常年积累会很大,所以为了方便查看必须进行切割处理。)
1、/etc/logrotate.d
实现nginx日志文件定时切割处理
日志切割方法一: 利用脚本实现切割

[root@ lb01 ~]# vim /etc/logrotate.d
	   #!/bin/bash
	   mv /var/log/nginx/access.log  /var/log/nginx	/access_$(date +%F).log
	   systemctl restart nginx

日志切割方法二: 利用专用文件切割程序–logrotate

[root@ lb01 ~]# vim /etc/logrotate.conf
# see "man logrotate" for details
# rotate log files weekly
weekly

# 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	--- 加载包含/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

总结:
01. /etc/nginx 配置文件
02. /var/log/nginx 日志文件
03. /usr/bin/nginx 命令文件
04. /usr/share/nginx/html 站点目录
图片 附件信息 音频 视频等

nginx服务配置文件

[root@ lb01 ~]# vim /etc/nginx/nginx.conf      	 --- 主配置文件
user  nginx;		 --- 定义worker进程管理的用户
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;	  --- 定义错误日志路径信息
pid        /var/run/nginx.pid;				  --- 定义pid文件路径信息


events {
    worker_connections  1024;		          ---一个worker进程可以同时接收1024访问请求
}


http {
    include       /etc/nginx/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;
    #tcp_nopush     on;

    keepalive_timeout  65;		--- 超时时间

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;  --- 加载一个配置文件
 }             

server区域信息

/etc/nginx/conf.d/default.conf  ------ 扩展配置(虚拟主机配置文件)
 [root@ lb01 conf.d]# cat default.conf
server {
    listen       80;			--- 指定监听的端口
    server_name  localhost;		--- 指定网站域名

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/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   /usr/share/nginx/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;
    #}
}

切换到站点目录下

[root@ lb01 conf.d]# cd /usr/share/nginx/html
添加一张图片
[root@ lb01 html]# rz -E
rz waiting to receive.
[root@ lb01 html]# ll
total 264
-rw-r--r--. 1 root root    494 Apr 21 00:09 50x.html
-rw-r--r--. 1 root root 260468 May 22 13:43 http.png
-rw-r--r--. 1 root root    612 Apr 21 00:09 index.html

站点目录下存在了一张图片,我们尝试访问一下
在这里插入图片描述成功!!



这篇关于nginx软件安装部署的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程