一. 實(shí)驗(yàn)環(huán)境
本次的實(shí)驗(yàn)環(huán)境見下表:
操作系統(tǒng) | 服務(wù)器IP | hostname |
---|---|---|
centos7.6 | 192.168.1.41 | mufengrow41 |
如何查看相應(yīng)的參數(shù):
-
查看操作系統(tǒng):
[root@mufenggrow ~]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)
-
查看ip
[root@mufenggrow41 ~]# ifconfig |grep inet |awk 'NR==1{print $2}' 192.168.1.56
二. 使用yum安裝nginx
2.1 添加yum源
nginx不在的默認(rèn)的yum源中, 可以使用epel或者官網(wǎng)提供的yum源來安裝。
以下兩種方法,選擇任意一種即可,也就是2.1.1和2.1.2兩個(gè)小節(jié)的內(nèi)容,任選其一:
2.1.1 使用官網(wǎng)提供的源地址(方法一)
1. 找到官網(wǎng)的源
官網(wǎng)提供的源地址:http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2. 使用rpm -ivh 進(jìn)行安裝
[root@mufeng ~]# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.DRyoO4: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準(zhǔn)備中... ################################# [100%]
正在升級/安裝...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
3. 安裝完成之后查看源:
2.1.2 使用epel的方式進(jìn)行安裝(方法二)
1. 先安裝epel
[root@mufeng ~]# sudo yum install yum-utils
2. 安裝完成后,查看安裝的epel包即可
yum install epel-release
2.2 開始安裝nginx
上面的兩個(gè)方法不管選擇哪個(gè),都可以使用yum進(jìn)行安裝:
[root@mufeng ~]# yum install nginx
2.3 啟動(dòng)并進(jìn)行測試
# 查看nginx版本
[root@mufeng ~]# nginx -v
nginx version: nginx/1.22.1
# 設(shè)置開機(jī)自啟動(dòng)
[root@mufeng ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# 啟動(dòng)nginx
[root@mufeng ~]# systemctl start nginx
[root@mufeng ~]#
測試 :
直接訪問IP即可:
2.4 其他的一些用法:
1. 停止服務(wù):
systemctl restart nginx
2. 重新加載nginx
systemctl reload nginx
3. 打開防火墻的80端口:
[root@mufeng ~]# firewall-cmd --zone=public --permanent --add-service=http
success
[root@mufeng ~]# firewall-cmd --reload
success
[root@mufeng ~]#
命令的作用是將 http 服務(wù)添加到 public 區(qū)域的永久規(guī)則中,即允許通過防火墻訪問 http 服務(wù)。
其中,
–zone=public 指定了作用的區(qū)域?yàn)?public
–permanent 表示該規(guī)則將被永久保存
–add-service=http 指定添加的服務(wù)為 http
三. 編譯方式安裝nginx
3.1 下載所需要的包
nginx 包下載地址: http://nginx.org/en/download.html
[root@mufeng ~]# wget http://nginx.org/download/nginx-1.22.1.tar.gz
3.2 創(chuàng)建目錄并解壓nginx包
root@mufeng ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.22.1.tar.gz 公共 模板 視頻 圖片 文檔 下載 音樂 桌面
[root@mufeng ~]# mkdir tools
[root@mufeng ~]# mv nginx-1.22.1.tar.gz tools/
[root@mufeng ~]# cd tools/
[root@mufeng tools]# tar xf nginx-1.22.1.tar.gz
[root@mufeng tools]# ls
nginx-1.22.1 nginx-1.22.1.tar.gz
[root@mufeng tools]# cd nginx-1.22.1/
[root@mufeng nginx-1.22.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@mufeng nginx-1.22.1]#
3.3 安裝編譯需要的包
一般編譯都需要gcc,如果沒有會報(bào)錯(cuò)
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre
root@mufeng nginx-1.22.1]# make && make install
這里指定了 nginx 的安裝路徑為 /usr/local/nginx,同時(shí)啟用了 SSL 和狀態(tài)監(jiān)控模塊。
在編譯 Nginx 時(shí),可以使用 ./configure --help 命令來查看可以使用的編譯選項(xiàng)
3.4 安裝并測試
使用make和make install進(jìn)行安裝
[root@mufeng nginx-1.22.1]# make && make install
開始測試:瀏覽器輸入IP:
3.5 簡化默認(rèn)的啟動(dòng)方式
默認(rèn)的操作方式,比如查看配置文件是否正確:
[root@mufeng nginx-1.22.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
我們使用軟鏈接或者alias的形式來簡化,這里我們使用軟鏈接:
[root@mufeng nginx-1.22.1]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@mufeng nginx-1.22.1]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@mufeng nginx-1.22.1]#
啟動(dòng)方式:
[root@mufeng nginx-1.22.1]# nginx
重新加載方式:
[root@mufeng nginx-1.22.1]# nginx -s reload
關(guān)閉的話只能通過殺死進(jìn)程了:
四. 拓展內(nèi)容
4.1 編譯安裝完nginx的配置文件位置
-
conf: /usr/local/nginx , 存放 nginx 的配置文件,nginx.conf 是 nginx 服務(wù)最核心最主要的配置文件。
-
html:/usr/local/nginx,保存 ningx 服務(wù)器的 web 文件。也可以更改為其他目錄保存 web 文件。
-
logs: /var/logs/nginx,保存 ningx 服務(wù)器的訪問日志、錯(cuò)誤日志等日志。
-
sbin: /usr/local/nginx,保存 nginx 二進(jìn)制啟動(dòng)腳本??梢越邮懿煌瑓?shù)以實(shí)現(xiàn)不同的功能。
如果啟動(dòng)或者重啟的過程中報(bào)錯(cuò),可以查看logs中的日志。
4.2 配置訪問狀態(tài)統(tǒng)計(jì)
我們在編譯的時(shí)候添加了 –with-http_stub_status_module這個(gè)模塊,你可以使用nginx -V查看是否包含這個(gè)模塊。
然后修改配置文件:
[root@mufeng nginx]# vim /usr/local/nginx/conf/nginx.conf
代碼如下:
location / {
root html;
index index.html index.htm;
}
location /status {
stub_status on;
access_log off;
}
查看配置文件是否正確
[root@mufeng nginx]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重啟:
nginx -s reload
測試:
可以看到: 當(dāng)前的活動(dòng)連接數(shù)為3個(gè)
554 表示: 已處理的連接數(shù)為5, 成功的TCP握手次數(shù)為5, 已處理的請求數(shù)為1
我是沐風(fēng)曉月文章來源:http://www.zghlxwxcb.cn/news/detail-851854.html
【Linux】CentOS7操作系統(tǒng)安裝nginx實(shí)戰(zhàn)(多種方法,超詳細(xì))文章來源地址http://www.zghlxwxcb.cn/news/detail-851854.html
到了這里,關(guān)于CentOS系列:【Linux】CentOS7操作系統(tǒng)安裝nginx實(shí)戰(zhàn)(多種方法,超詳細(xì))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!