Nginx系列(1):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是一個高性能的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

[root@node01 /]# 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++

[root@localhost 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

[root@localhost src]# tar zxvf openssl-fips-2.0.16.tar.gz
省略安裝內容...
[root@localhost src]# cd openssl-fips-2.0.16
[root@localhost 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

 [root@localhost src]# tar zxvf pcre-8.40.tar.gz

省略安裝內容...

[root@localhost src]# cd pcre-8.40

[root@localhost pcre-8.40]# ./configure && make && make install

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

省略安裝內容...


1.6 安裝zlib

[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz

省略安裝內容...

[root@localhost src]# cd zlib-1.2.11

[root@localhost 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

[root@localhost nginx]# tar -zxvf nginx-1.19.0.tar.gz
省略安裝內容...
[root@localhost nginx]# cd nginx-1.19.0
[root@localhost nginx-1.19.0]# ./configure && make && make install
注意:指定自定義安裝目錄,執行 ./configure --prefix=/opt/nginx-opt/nginx && make && make install
省略安裝內容...


2.2 啓動Nginx

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

[root@node01 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啓動成功。


2.3 Nginx基本操作

啓動
[root@localhost ~]# /opt/nginx-opt/nginx/sbin/nginx
停止/重啓
[root@localhost ~]# /opt/nginx-opt/nginx/sbin/nginx -s stop(quit、reload)
命令幫助
[root@localhost ~]#/opt/nginx-opt/nginx/sbin/nginx -h
驗證配置文件
[root@localhost ~]# /opt/nginx-opt/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vim/opt/nginx-opt/nginx/conf/nginx.conf
設置開機啓動
[root@localhost ~]# 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】測試

[root@node01 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            
[root@node01 sbin]# 
[root@node01 sbin]# systemctl daemon-reload       
[root@node01 sbin]# 
[root@node01 sbin]# systemctl start nginx         
[root@node01 sbin]# 
[root@node01 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.
[root@node01 sbin]# 
[root@node01 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/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

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

[root@node01 sbin]# 
[root@node01 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            
[root@node01 sbin]# 
[root@node01 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
[root@node01 sbin]# 


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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章