日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

Nginx安裝與常見命令

作者:Charge8 更新時間: 2023-09-12 編程語言

一、Nginx簡介

官方文檔:https://www.nginx.com/
Nginx中文文檔:https://nginx.cn/doc/index.html

Nginx由俄羅斯人(Igor Sysoev)編寫的輕量級Web服務(wù)器,它的發(fā)音為 [?end??nks] 。

Nginx 不僅是一款高性能的 HTTP服務(wù)器和反向代理服務(wù)器,同時也支持 IMAP/POP3/SMTP代理服務(wù)。

Nginx應(yīng)用場景:

  • 1)http 服務(wù)器。Nginx 是一個 http 服務(wù)可以獨立提供 http 服務(wù)??梢宰鼍W(wǎng)頁靜態(tài)服務(wù)器。
  • 2)虛擬主機??梢詫崿F(xiàn)在一臺服務(wù)器虛擬出多個網(wǎng)站。例如個人網(wǎng)站使用的虛擬主機。
  • 3)反向代理,負載均衡。當(dāng)網(wǎng)站的訪問量達到一定程度后,單臺服務(wù)器不能滿足用戶的請求時,需要用多臺服務(wù)器集群可以使用 nginx 做反向代理。并且多臺服務(wù)器可以平均分擔(dān)負載,不會因為某臺服務(wù)器負載高宕機而某臺服務(wù)器閑置的情況。
  • 4)正向代理
  • 5)動靜分離
  • 6)負載均衡
  • 7)集群高可用。

二、Nginx安裝

Nginx的安裝方式有很多種,一般我們使用的是tar.gz安裝 Nginx。這里使用 nginx安裝。

1、安裝前環(huán)境庫安裝

第一步:安裝gcc庫。

安裝 nginx 需要先將官網(wǎng)下載的源碼進行編譯,編譯依賴 gcc 環(huán)境,如果沒有 gcc環(huán)境,需要安裝 gcc。

## 檢查是否安裝命令
[root@centos7 ~]# rpm -q gcc-c++
未安裝軟件包 gcc-c++ 
## 安裝命令
[root@centos7 ~]# yum -y install gcc-c++
已加載插件:fastestmirror
.......
[root@centos7 ~]# rpm -q gcc-c++
gcc-c++-4.8.5-36.el7.x86_64

第二步:安裝pcre庫。

PCRE(Perl Compatible Regular Expressions)是一個 Perl 庫,包括 perl 兼容的正則表達式庫。

nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫。

## 檢查是否安裝命令
[root@centos7 ~]# rpm -q pcre pcre-devel
pcre-8.32-17.el7.x86_64
未安裝軟件包 pcre-devel
## 安裝命令
[root@centos7 ~]# yum install -y pcre pcre-devel
已加載插件:fastestmirror 
.......
[root@centos7 ~]# rpm -q pcre pcre-devel
pcre-8.32-17.el7.x86_64
pcre-devel-8.32-17.el7.x86_64

注意:pcre-devel 是使用 pcre 開發(fā)的一個二次開發(fā)庫。nginx 也需要此庫。

第三步:安裝zlib庫。

zlib庫提供了很多種壓縮和解壓縮的方式。

nginx使用 zlib對 http包的內(nèi)容進行 gzip,所以需要在 linux 上安裝 zlib 庫。

## 安裝命令
[root@centos7 ~]# yum install -y zlib zlib-devel

第四步:安裝openssl庫。

(1)OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協(xié)議,并提供豐富的應(yīng)用程序供測試或其它目的使用。

(2)nginx 不僅支持 http 協(xié)議,還支持 https(即在 ssl 協(xié)議上傳輸 http),所以需要在linux 安裝 openssl 庫。

## 安裝命令
[root@centos7 ~]# yum install -y openssl openssl-devel

注意:上面四步其實可以合成一步安裝,命令為:

[root@centos7 ~]# yum install -y gcc gcc-c++ make libtool wget pcre pcre-devel zlib zlib-devel openssl openssl-devel

2、Nginx安裝包解壓并安裝

我們可以使用文件上傳工具,將 Nginx安裝包文件上傳到 Linux中。Nginx下載安裝包地址:http://nginx.org/download/nginx-1.19.4.tar.gz

或者使用命令在線下載安裝包:wget http://nginx.org/download/nginx-1.19.4.tar.gz
如果 wget命令未找到,需要安裝wget命令:yum -y install wget

下面是把 Nginx 安裝到 /usr/local/nginx 目錄下。依次執(zhí)行下面命令即可。

# 1.下載安裝包,并解壓
[root@centos7 ~]# cd /usr/local/src
wget http://nginx.org/download/nginx-1.19.4.tar.gz
tar -zxvf nginx-1.19.4.tar.gz
# 2.創(chuàng)建臨時文件的存放目錄
cd /var
mkdir temp
cd temp
mkdir nginx
# 3.回到Nginx解壓后的根目錄下
cd /usr/local/src/nginx-1.19.4
# 4.進入到該目錄之后可以看到configure這個可執(zhí)行文件,配置安裝參數(shù)。
[root@centos7 nginx-1.19.4]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
# 5.參數(shù)設(shè)置如下所示,直接復(fù)制粘貼執(zhí)行即可。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
# 6.之后我們回到解壓后的Nginx的根目錄下,使用gcc編譯安裝
cd /usr/local/src/nginx-1.19.4
make && make install
# 以上命令執(zhí)行完以后,就會自動在/usr/local/nginx下創(chuàng)建一個nginx目錄,這個位置就是Nginx的安裝目錄。

注意:

  • 安裝 nginx之前先要將安裝環(huán)境配置完成,不然安裝nginx會安裝失敗。
  • 上邊將臨時文件目錄指定為/var/temp/nginx,需要在/var 下創(chuàng)建 temp以及nginx目錄;
  • nginx安裝成功之后,默認安裝在/usr/local/nginx目錄下。

安裝成功后 /usr/local/nginx 目錄下如下:

[root@centos7 nginx-1.19.4]# cd /usr/local/nginx
[root@centos7 nginx]# ls
conf  html  sbin

3、啟動Nginx

nginx 安裝成功,啟動 nginx,即可訪問虛擬機上的 nginx。

Nginx 默認的是偵聽 80 端口。所以確保系統(tǒng)的 80 端口沒被其他程序占用。

查看80端口:netstat -ano|grep 80

1)啟動 Nginx命令:

# 首先進入 /usr/local/nginx/sbin/目錄下
[root@centos7 nginx]# cd /usr/local/nginx/sbin
[root@centos7 sbin]# ll
總用量 3816
-rwxr-xr-x 1 root root 3903720 911 00:06 nginx
# 然后執(zhí)行./nginx 命令進行啟動Nginx
[root@centos7 sbin]# ./nginx
# 查看是否啟動
[root@centos7 sbin]# ps aux|grep nginx
root      12600  0.0  0.0  20568   616 ?        Ss   00:10   0:00 nginx: master process ./nginx
nobody    12601  0.0  0.0  21004  1328 ?        S    00:10   0:00 nginx: worker process
root      12603  0.0  0.0 112724   988 pts/0    S+   00:11   0:00 grep --color=auto nginx

2)測試

打開瀏覽器訪問此機器的 IP,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 Nginx 已經(jīng)安裝并運行成功。

在這里插入圖片描述

注意:測試前需要將防火墻關(guān)閉,如果是云服務(wù)器需要在安全組的配置中將80端口方行,不然80端口的訪問會被阻攔。

三、Nginx常用命令

1、查看版本號

[root@centos7 ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.19.4

2、啟動 nginx

首先 cd /usr/local/nginx/sbin/進入到nginx的安裝目錄下,

然后執(zhí)行./nginx 命令進行啟動Nginx。

啟動之后使用命令:ps aux|grep nginx 查看是否啟動。

# 首先進入 /usr/local/nginx/sbin/目錄下
[root@centos7 nginx]# cd /usr/local/nginx/sbin
[root@centos7 sbin]# ll
總用量 3816
-rwxr-xr-x 1 root root 3903720 911 00:06 nginx
# 然后執(zhí)行./nginx 命令進行啟動Nginx
[root@centos7 sbin]# ./nginx
# 查看是否啟動
[root@centos7 sbin]# ps aux|grep nginx
root      12600  0.0  0.0  20568   616 ?        Ss   00:10   0:00 nginx: master process ./nginx
nobody    12601  0.0  0.0  21004  1328 ?        S    00:10   0:00 nginx: worker process
root      12603  0.0  0.0 112724   988 pts/0    S+   00:11   0:00 grep --color=auto nginx

注意:執(zhí)行./nginx 啟動 nginx ,如果不指定-c ,nginx在啟動時默認加載 conf/nginx.conf 文件,此文件的地址也可以在編譯安裝 nginx 時指定./configure 的參數(shù)(–conf-path= 指向配置文件(nginx.conf)

如果指定 -c加載的 nginx 配置文件,命令如下:./nginx -c /usr/local/nginx/conf/nginx.conf

3、停止 nginx

方式 1,快速停止:

首先進入cd /usr/local/nginx/sbin該目錄下,

然后利用命令:./nginx -s stop 進行關(guān)閉。

[root@centos7 ~]# cd /usr/local/nginx/sbin
[root@centos7 sbin]# ./nginx -s stop

此方式相當(dāng)于先查出 nginx 進程 id 再使用 kill 命令強制殺掉進程。

方式 2,完整停止(推薦使用):

首先進入cd /usr/local/nginx/sbin該目錄下

然后利用命令:./nginx -s quit 進行關(guān)閉。

[root@centos7 sbin]# cd /usr/local/nginx/sbin
[root@centos7 sbin]# ./nginx -s quit

此方式停止步驟是待 nginx 進程處理任務(wù)完畢進行停止。

4、重啟 nginx

方式 1,先停止再啟動(推薦使用):

對 nginx 進行重啟相當(dāng)于先停止 nginx 再啟動 nginx,即先執(zhí)行停止命令再執(zhí)行啟動命令。命令如下:

# 先執(zhí)行停止命令再執(zhí)行啟動命令
[root@centos7 sbin]# ./nginx -s quit
[root@centos7 sbin]# ./nginx
[root@centos7 sbin]#

方式 2,重新加載配置文件:

當(dāng) nginx 的配置文件 nginx.conf 修改后,要想讓配置生效需要重啟 nginx,使用-s reload

不用先停止 nginx 再啟動 nginx 即可將配置信息在 nginx 中生效,命令如下:

[root@centos7 sbin]# ./nginx -s reload
[root@centos7 sbin]#

– 求知若饑,虛心若愚。

原文鏈接:https://blog.csdn.net/qq_42402854/article/details/132818351

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新