通過yum源進行nginx的安裝
阿新 • • 發佈:2021-01-30
文章目錄
第1章 通過yum進行nginx安裝
1.1 作業系統環境介紹
#### 作業系統的版本
[[email protected] ~]# cat /etc/redhat-release;uname -r;uname -m
CentOS Linux release 7.9.2009 (Core)
3.10.0-1160.el7.x86_64
x86_64
#### selinux和firewalld防火牆
[[email protected] ~]# getenforce
Disabled
[[email protected] ~]# firewall-cmd --state
not running
#### 相關網絡卡的IP地址
[[email protected] ~]# ifconfig ens33|awk -F "[ :]+" 'NR==2{print $3}'
10.0.0.21
[[email protected] ~]# ifconfig ens34|awk -F "[ :]+" 'NR==2{print $3}'
172.16.1.21
1.2 如何配置nginx的源
Centos的base源沒有nginx,epel源是有nginx的(但可選版本不多)
#### base源下的驗證
[[email protected] ~]#
[[email protected] ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]#
[[email protected] ~]# yum list nginx --show-duplicates
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Error: No matching Packages to list
#### epel源下的驗證
[[email protected] ~]#
[[email protected] ~]# yum install epel-release -y
[[email protected] ~]#
[[email protected] ~]# yum list nginx --show-duplicates
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.bfsu.edu.cn
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
nginx.x86_64 1:1.16.1-3.el7 epel
nginx的儲存庫如何配置
http://nginx.org/en/linux_packages.html 上有關於Centos/Rhel品牌的Linux何更新儲存庫,如下所示。
[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
[nginx-stable]表示穩定版,[nginx-mainline]表示主線版(跟著釋出走的,就不是穩定版了),我們不要[nginx-mainline]及其後面的內容。最終的內容如下所示:
[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
1.3 yum安裝nginx走起
安裝yum-utils工具
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
yum install -y yum-utils
rpm -qa yum-utils
which yum-config-manager
配置nginx的源
#### 建立/etc/yum.repos.d/nginx.repo檔案並新增內容
cat >>/etc/yum.repos.d/nginx.repo<<EOF
[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
EOF
#### 檢視/etc/yum.repos.d/nginx.repo檔案內容
[[email protected] ~]#
[[email protected] ~]# cat /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
安裝指定nginx版本
#### 檢視有哪些nginx版本可以安裝
[[email protected] ~]#
[[email protected] ~]# yum list nginx --show-duplicates
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
nginx.x86_64 1:1.8.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.8.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.2-1.el7.ngx nginx-stable
nginx.x86_64 1:1.10.3-1.el7.ngx nginx-stable
nginx.x86_64 1:1.12.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.12.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.12.2-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.14.0-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.14.1-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.14.2-1.el7_4.ngx nginx-stable
nginx.x86_64 1:1.16.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.16.1-1.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-1.el7.ngx nginx-stable
nginx.x86_64 1:1.18.0-2.el7.ngx nginx-stable
#### 安裝nginx的指定版本,若不指定,那麼就是安裝最新的版本
[[email protected] ~]#
[[email protected] ~]# chattr -i /etc/passwd /etc/shadow /etc/group /etc/gshadow
[[email protected] ~]#
[[email protected] ~]# yum install -y nginx-1.16.0
[[email protected] ~]#
[[email protected] ~]# rpm -qa nginx
nginx-1.16.0-1.el7.ngx.x86_64
[[email protected] ~]#
[[email protected] ~]# which nginx
/usr/sbin/nginx
#### 啟動nignx並加入開機自啟動
[[email protected] ~]#
[[email protected] ~]# systemctl start nginx
[[email protected] ~]#
[[email protected] ~]# 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 Fri 2021-01-29 11:55:09 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 2633 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2634 (nginx)
CGroup: /system.slice/nginx.service
├─2634 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2635 nginx: worker process
Jan 29 11:55:09 node21 systemd[1]: Starting nginx - high performance web server...
Jan 29 11:55:09 node21 systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Jan 29 11:55:09 node21 systemd[1]: Started nginx - high performance web server.
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 2634 root 6u IPv4 25817 0t0 TCP *:http (LISTEN)
nginx 2635 nginx 6u IPv4 25817 0t0 TCP *:http (LISTEN)
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
訪問一下nginx服務
[[email protected] ~]# curl -I localhost:80
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Fri, 29 Jan 2021 03:56:52 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 14:36:26 GMT
Connection: keep-alive
ETag: "5cbf22ea-264"
Accept-Ranges: bytes
[[email protected] ~]#
[[email protected] ~]# curl -I 10.0.0.21:80
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Fri, 29 Jan 2021 03:57:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 14:36:26 GMT
Connection: keep-alive
ETag: "5cbf22ea-264"
Accept-Ranges: bytes
[[email protected] ~]# curl -I 172.16.1.21:80
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Fri, 29 Jan 2021 03:57:08 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 14:36:26 GMT
Connection: keep-alive
ETag: "5cbf22ea-264"
Accept-Ranges: bytes