天天看點

Nginx系列(1):Nginx安裝及配置詳解前言1、準備依賴環境2、Nginx安裝配置

目錄

前言

1、準備依賴環境

1.1 首先檢視Linux 的系統版本号

1.2 下載下傳nginx、openssl、zlib、pcre

1.3 安裝c++編譯環境

1.4 安裝openssl

1.5 安裝PCRE

1.6 安裝zlib

1.7 鏡像檔案下載下傳

2、Nginx安裝配置

2.1 安裝Nginx

2.2 啟動Nginx

2.3 Nginx基本操作

2.4 使用systemctl控制Nginx

Nginx系列(1):Nginx安裝及配置詳解前言1、準備依賴環境2、Nginx安裝配置

前言

Nginx是一個高性能的HTTP和反向代理WEB伺服器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫為俄羅斯通路量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0釋出于2004年10月4日。

其将源代碼以類BSD許可證的形式釋出,因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4釋出。

Nginx是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,在BSD-like 協定下發行。其特點是占有記憶體少,并發能力強,事實上nginx的并發能力在同類型的網頁伺服器中表現較好,中國大陸使用nginx網站使用者有:百度、京東、新浪、網易、騰訊、淘寶等。

關于負載均衡及反向代理的具體了解和使用,後邊将逐漸展開總結,這裡暫不贅述。下面直接進入安裝配置步驟。

1、準備依賴環境

安裝Nginx時,發現系統沒有裝各種依賴庫,基本上是gcc、pre-devel、openssl-devel、zlib-devel,Linux又是内網環境,并不能用yum install來進行安裝,這裡就提供一種離線安裝方式。

1.1 首先檢視Linux 的系統版本号

cat /etc/redhat-release

[[email protected] /]# cat /etc/redhat-release

CentOS Linux release 7.6.1810 (Core)

1.2 下載下傳nginx、openssl、zlib、pcre

自定義安裝包路徑:# mkdir/opt/xxx-opt/xxx

依賴包下載下傳指令:

# wget http://nginx.org/download/nginx-1.19.0.tar.gz

# wget http://www.openssl.org/source/openssl-fips-2.0.16.tar.gz

# wget http://zlib.net/zlib-1.2.11.tar.gz

# wget http://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz

# wget http://mirror.koddos.net/gcc/releases/gcc-10.1.0.tar.gz

1.3 安裝c++編譯環境

【方式一】yum方式安裝,如果有聯網環境,或者配置好的yum源倉庫,可以直接執行指令:

yum install gcc-c++

[[email protected] src]# yum install gcc-c++

省略安裝内容...

期間會有确認提示輸入y回車

Is this ok [y/N]:y

省略安裝内容...

【方式二】rpm包離線安裝

解壓鏡像CentOS-7-x86_64-DVD-1810.iso,進入到CentOS-7-x86_64-DVD-1810\Packages目錄,這下面存儲了很多rpm包。找到下面列出的rpm包,上傳到CentOS機器任意位置。

binutils-2.27-34.base.el7.x86_64.rpm

cpp-4.8.5-36.el7.x86_64.rpm

gcc-4.8.5-16.el7.x86_64.rpm

gcc-c++-4.8.5-36.el7.x86_64.rpm

glibc-2.17-260.el7.x86_64.rpm

glibc-common-2.17-260.el7.x86_64.rpm

glibc-devel-2.17-260.el7.x86_64.rpm

glibc-headers-2.17-260.el7.x86_64.rpm

kernel-headers-3.10.0-957.el7.x86_64.rpm

libgcc-4.8.5-36.el7.x86_64.rpm

libgomp-4.8.5-36.el7.x86_64.rpm

libmpc-1.0.1-3.el7.x86_64.rpm

libstdc++-devel-4.8.5-36.el7.x86_64.rpm

mpfr-3.1.1-4.el7.x86_64.rpm

zlib-1.2.7-18.el7.x86_64.rpm

執行rpm包安裝:rpm -Uvh ./*.rpm --nodeps --force

接下來,我們來逐漸安裝Nginx的相關依賴。

1.4 安裝openssl

[[email protected] src]# tar zxvf openssl-fips-2.0.16.tar.gz

省略安裝内容...

[[email protected] src]# cd openssl-fips-2.0.16

[[email protected] openssl-fips-2.0.16]# ./config &&make && make install

注意:指定自定義安裝目錄,執行 ./config --prefix=/opt/openssl-opt/ openssl &&make && make install

省略安裝内容...

注意:

在安裝openssl時需要安裝Perl5,否則報一下錯誤。Operating system: x86_64-whatever-linux2 You need Perl 5.

安裝Perl 5的執行步驟為:

#需要安裝 perl-5https://www.cpan.org/src/README.html

# wget https://www.cpan.org/src/5.0/perl-5.28.0.tar.gz

# tar -xzf perl-5.28.0.tar.gz

# cd perl-5.28.0

# ./Configure -des -Dprefix=$HOME/localperl &&  Make &&  make install

注意:

在執行Configure 指令時,如果出現cc: command not found 錯誤,先安裝gcc--c++即可。

1.5 安裝PCRE

 [[email protected] src]# tar zxvf pcre-8.40.tar.gz

省略安裝内容...

[[email protected] src]# cd pcre-8.40

[[email protected] pcre-8.40]# ./configure && make && make install

注意:指定自定義安裝目錄,執行 ./configure --prefix=/opt/pcre-opt/ pcre && make && make install

省略安裝内容...

1.6 安裝zlib

[[email protected] src]# tar zxvf zlib-1.2.11.tar.gz

省略安裝内容...

[[email protected] src]# cd zlib-1.2.11

[[email protected] zlib-1.2.11]# ./configure && make && make install

注意:指定自定義安裝目錄,執行 ./configure --prefix=/opt/zlib-opt/zlib && make && make install

省略安裝内容...

OK,經過上邊步驟,我們已經完成Ngix依賴元件的安裝。

1.7 鏡像檔案下載下傳

CentOS 7.6-1810鏡像ISO下載下傳:http://vault.centos.org/7.6.1810/isos/x86_64/

CentOS 7.2-1511鏡像ISO下載下傳:http://vault.centos.org/7.2.1511/isos/x86_64/

Nginx下載下傳:http://nginx.org/download/

2、Nginx安裝配置

Nginx安裝包下載下傳:# wget http://nginx.org/download/nginx-1.19.0.tar.gz

2.1 安裝Nginx

自定義安裝目錄的路徑:# mkdir/opt/nginx-opt/nginx

#解壓nginx-1.19.0.tar.gz

[[email protected] nginx]# tar -zxvf nginx-1.19.0.tar.gz

省略安裝内容...

[[email protected] nginx]# cd nginx-1.19.0

[[email protected] nginx-1.19.0]# ./configure && make && make install

注意:指定自定義安裝目錄,執行 ./configure --prefix=/opt/nginx-opt/nginx && make && make install

省略安裝内容...

2.2 啟動Nginx

先找一下nginx安裝到什麼位置上了。

[[email protected] sbin]# whereis nginx

nginx: /usr/sbin/nginx

這裡我們執行的自定義安裝,路徑好像不是我們指定的哇。進入看下:

lrwxrwxrwx  1 root root          31 Jun 23 05:48 nginx -> /opt/nginx-opt/nginx/sbin/nginx

這個軟連接配接指向了我們自定義安裝路徑。

建立軟連接配接:ln -s /opt/nginx-opt/nginx/sbin/nginx /sbin/ && nginx && nginx -t

打開浏覽器輸入localhost或者http://192.168.0.118:80/會看到下圖,說明nginx啟動成功。

Nginx系列(1):Nginx安裝及配置詳解前言1、準備依賴環境2、Nginx安裝配置

2.3 Nginx基本操作

啟動

[[email protected] ~]# /opt/nginx-opt/nginx/sbin/nginx

停止/重新開機

[[email protected] ~]# /opt/nginx-opt/nginx/sbin/nginx -s stop(quit、reload)

指令幫助

[[email protected] ~]#/opt/nginx-opt/nginx/sbin/nginx -h

驗證配置檔案

[[email protected] ~]# /opt/nginx-opt/nginx/sbin/nginx -t

配置檔案

[[email protected] ~]# vim/opt/nginx-opt/nginx/conf/nginx.conf

設定開機啟動

[[email protected] ~]# systemctl enable nginx

2.4 使用systemctl控制Nginx

Systemctl是一個系統管理守護程序、工具和庫的集合,用于取代System V、service和chkconfig指令,初始程序主要負責控制systemd系統和服務管理器。

通過Systemctl –help可以看到該指令主要分為:查詢或發送控制指令給systemd服務,管理單元服務的指令,服務檔案的相關指令,任務、環境、快照相關指令,systemd服務的配置重載,系統開機關機相關的指令。 

【1】建立nginx.service

vim /usr/lib/systemd/system/nginx.service

【2】編輯nginx服務啟動檔案

[Unit]

Description=nginx -high performance web server

After=network.target remote-fs.target nss-lookup.target

[Service]

Type=forking

WorkingDirectory=/opt/nginx-opt/nginx

ExecStartPre=/opt/nginx-opt/nginx/sbin/nginx -t -c \

/opt/nginx-opt/nginx/conf/nginx.conf

ExecStart=/opt/nginx-opt/nginx/sbin/nginx -c \

/opt/nginx-opt/nginx/conf/nginx.conf

ExecReload=/opt/nginx-opt/nginx/sbin/nginx -s reload

ExecStop=/opt/nginx-opt/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

【3】使檔案生效

Systemctl daemon-reload

【4】啟動nginx

systemctl start nginx

【5】關閉nginx

systemctl stop nginx

【6】重新開機nginx

systemctl restart nginx

【7】開機啟動

systemctl enable nginx

【8】測試

[[email protected] sbin]# netstat -ntlp                 

Active Internet connections (only servers)

Proto     Recv-Q      Send-Q Local Address         Foreign Address         State        PID/Program name    

tcp           0                0 0.0.0.0:22                           0.0.0.0:*                  LISTEN       916/sshd            

tcp6         0                0 :::22                                    :::*                           LISTEN       916/sshd            

[[email protected] sbin]# 

[[email protected] sbin]# systemctl daemon-reload       

[[email protected] sbin]# 

[[email protected] sbin]# systemctl start nginx         

[[email protected] sbin]# 

[[email protected] sbin]# systemctl status nginx.service

● nginx.service - nginx -high performance web server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)

   Active: active (running) since Sat 2020-06-27 06:08:53 CST; 8s ago

  Process: 1633 ExecStart=/opt/nginx-opt/nginx/sbin/nginx -c /opt/nginx-opt/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)

  Process: 1631 ExecStartPre=/opt/nginx-opt/nginx/sbin/nginx -t -c /opt/nginx-opt/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)

 Main PID: 1635 (nginx)

    Tasks: 2

   Memory: 956.0K

   CGroup: /system.slice/nginx.service

           ├─1635 nginx: master process /opt/nginx-opt/nginx/sbin/nginx -c /opt/nginx-opt/nginx/conf/nginx.conf

           └─1637 nginx: worker process

Jun 27 06:08:53 node01 systemd[1]: Starting nginx -high performance web server...

Jun 27 06:08:53 node01 nginx[1631]: nginx: the configuration file /opt/nginx-opt/nginx/conf/nginx.conf syntax is ok

Jun 27 06:08:53 node01 nginx[1631]: nginx: configuration file /opt/nginx-opt/nginx/conf/nginx.conf test is successful

Jun 27 06:08:53 node01 systemd[1]: Started nginx -high performance web server.

[[email protected] sbin]# 

[[email protected] sbin]# curl http://localhost/

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

    body {

        width: 35em;

        margin: 0 auto;

        font-family: Tahoma, Verdana, Arial, sans-serif;

    }

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/" target="_blank" rel="external nofollow" >nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/" target="_blank" rel="external nofollow" >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

[[email protected] sbin]# 

[[email protected] sbin]# netstat -ntlp         

Active Internet connections (only servers)

Proto Recv-Q      Send-Q Local Address           Foreign Address         State        PID/Program name    

tcp        0                   0 0.0.0.0:80                         0.0.0.0:*                 LISTEN       1635/nginx: master  

tcp        0                   0 0.0.0.0:22                         0.0.0.0:*                 LISTEN       916/sshd            

tcp6       0                  0 :::22                                  :::*                         LISTEN       916/sshd            

[[email protected] sbin]# 

[[email protected] sbin]# ps -aux | grep nginx

root       1635  0.0  0.0  20564   624 ?        Ss   06:08   0:00 nginx: master process /opt/nginx-opt/nginx/sbin/nginx -c /opt/nginx-opt/nginx/conf/nginx.conf

nobody     1637  0.0  0.1  21000  1572 ?        S    06:08   0:00 nginx: worker process

root       1732  0.0  0.0 112812   972 pts/0    R+   06:16   0:00 grep --color=auto nginx

[[email protected] sbin]# 

願你就像早晨八九點鐘的太陽,活力十足,永遠年輕。

繼續閱讀