分布式篇 - Nginx安裝與運行
上一篇博文給大家分享了關于正向代理和反向代理的一些基本概念,而反向代理是我們開發程序中經常用到的,比如用反向代理服務器為我們的服務端進行負載均衡、限流等,
- 分布式篇 - 一篇搞懂正向代理和反向代理
Nginx是什么?
Nginx (engine x) 是一款輕量級、高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務,其特點是占有記憶體少,并發能力強,事實上Nginx的并發能力在同型別的服務器中表現較好,
安裝依賴環境
安裝gcc環境
yum install -y gcc-c++
使用-y選項,系統就不會詢問一些問題,像是否允許為該應用添加環境變數(yes or no),系統會默認你對這些問題的回答都是yes,
-y, --assumeyes answer yes for all questions
安裝prce庫
prce庫,用于決議正則運算式,
yum install -y pcre pcre-devel
安裝壓縮和解壓縮依賴
zlib是通用的壓縮庫,提供了一套in-memory壓縮和解壓函式,
yum install -y zlib zlib-devel
安裝openssl
SSL是安全套接層協議,用于HTTP安全傳輸,也就是HTTPS,
yum install -y openssl openssl-devel
下載Nginx
Nginx下載地址:
- Nginx
下載穩定版(Linux系統),
下載好后,使用Xftp將該壓縮檔案放到虛擬機或服務器中,

解壓Nginx
[root@localhost /]# cd /usr/local
[root@localhost local]# ll
總用量 1016
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
-rw-r--r--. 1 root root 1039530 1月 7 16:08 nginx-1.18.0.tar.gz
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[root@localhost local]# tar -zxvf nginx-1.18.0.tar.gz
編譯Nginx
[root@localhost local]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ll
總用量 764
drwxr-xr-x. 6 1001 1001 4096 1月 7 16:24 auto
-rw-r--r--. 1 1001 1001 302863 4月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 4月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 1月 7 16:24 conf
-rwxr-xr-x. 1 1001 1001 2502 4月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 1月 7 16:24 contrib
drwxr-xr-x. 2 1001 1001 40 1月 7 16:24 html
-rw-r--r--. 1 1001 1001 1397 4月 21 2020 LICENSE
drwxr-xr-x. 2 1001 1001 21 1月 7 16:24 man
-rw-r--r--. 1 1001 1001 49 4月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 1月 7 16:24 src
先進行配置,直接使用默認配置,
[root@localhost nginx-1.18.0]# ./configure
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
配置的目的是為了創建Makefile檔案,為后面的編譯階段提供編譯檔案,
[root@localhost nginx-1.18.0]# ll
總用量 768
drwxr-xr-x. 6 1001 1001 4096 1月 7 16:24 auto
-rw-r--r--. 1 1001 1001 302863 4月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 4月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 1月 7 16:24 conf
-rwxr-xr-x. 1 1001 1001 2502 4月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 1月 7 16:24 contrib
drwxr-xr-x. 2 1001 1001 40 1月 7 16:24 html
-rw-r--r--. 1 1001 1001 1397 4月 21 2020 LICENSE
-rw-r--r--. 1 root root 376 1月 7 16:37 Makefile
drwxr-xr-x. 2 1001 1001 21 1月 7 16:24 man
drwxr-xr-x. 3 root root 174 1月 7 16:41 objs
-rw-r--r--. 1 1001 1001 49 4月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 1月 7 16:24 src
編譯
[root@localhost nginx-1.18.0]# make
安裝
[root@localhost nginx-1.18.0]# make install
啟動Nginx
[root@localhost nginx-1.18.0]# cd /usr/local/nginx
[root@localhost nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 1月 7 16:41 conf
drwxr-xr-x. 2 root root 40 1月 7 16:41 html
drwxr-xr-x. 2 root root 6 1月 7 16:41 logs
drwxr-xr-x. 2 root root 19 1月 7 16:41 sbin
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ll
總用量 3764
-rwxr-xr-x. 1 root root 3851608 1月 7 16:41 nginx
./nginx 啟動nginx,
./nginx -t 檢查nginx的組態檔是否符合要求
./nginx -s stop 此方式相當于先查出nginx行程id,再使用kill命令強制殺掉行程,
./nginx -s quit 此方式是待nginx行程處理完任務后,再停止nginx,
./nginx -s reload 重啟nginx,
[root@localhost sbin]# ./nginx
關閉虛擬機防火墻,不然訪問不了nginx服務,
[root@localhost sbin]# systemctl stop firewalld
[root@localhost sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
1月 07 16:02:16 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
1月 07 16:02:17 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
1月 07 16:02:18 localhost.localdomain firewalld[688]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
1月 07 16:50:46 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
1月 07 16:50:47 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
使用虛擬機IP地址訪問nginx服務,

洗掉壓縮檔案,
[root@localhost sbin]# cd /usr/local
[root@localhost local]# ll
總用量 1016
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 11 root root 151 1月 7 16:48 nginx
drwxr-xr-x. 9 1001 1001 186 1月 7 16:37 nginx-1.18.0
-rw-r--r--. 1 root root 1039530 1月 7 16:08 nginx-1.18.0.tar.gz
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[root@localhost local]# rm nginx-1.18.0.tar.gz
rm:是否洗掉普通檔案 "nginx-1.18.0.tar.gz"?y
[root@localhost local]# ll
總用量 0
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 11 root root 151 1月 7 16:48 nginx
drwxr-xr-x. 9 1001 1001 186 1月 7 16:37 nginx-1.18.0
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
之后博主會介紹怎么給nginx添加SSL(基于博主阿里云的服務器),歡迎大家繼續關注,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/246527.html
標籤:其他
上一篇:位元率與波特率的區別
下一篇:云計算復習
