一 nginx及其應用場景
1.什么是nginx
一款輕量級的Web服務器,反向代理服務器,以及電子郵件代理服務器
主要有三個優(yōu)點:
- 占用內(nèi)存少,并發(fā)能力強
- Nginx為性能優(yōu)化開發(fā),能支持五千個左右的并發(fā)響應
- (Tomcat只有三百到五百)
- Nginx支持熱部署,可以在不間斷服務情況下對軟件進行升級(不要用關閉服務器)
2.應用場景背誦
- 靜態(tài)http服務器,可以獨立提供靜態(tài)http服務
- 虛擬主機:在一臺服務器虛擬出多個網(wǎng)站
- 反向代理:網(wǎng)站訪問量到達一定程度后,單臺服務器已經(jīng)無法完成所有的訪問量,需要用nginx實現(xiàn)反向代理
- 負載均衡:每個服務器承擔一定的資源,不會出現(xiàn)一個服務器承擔過多的服務導致宕機,而另一個服務器閑置的情況下
二 nginx的配置安裝
1.選擇nginx版本
下載nginx, 官方網(wǎng)站:http://nginx.org/en/download.html
我們使用的版本是1.17.8版本。
Nginx在Linux下安裝,只提供了源代碼,所以我們需要進行編譯.
2. 安裝配置環(huán)境
需要安裝gcc的環(huán)境。執(zhí)行命令:
yum install gcc-c++
[root@localhost ~]# yum install gcc-c++
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.njupt.edu.cn
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
updates/7/x86_64/primary_db | 20 MB 00:00:05
正在解決依賴關系
安裝PCRE依賴
nginx的http模塊使用pcre來解析正則表達式,所以需要在linux上安裝pcre庫
[root@localhost ~]# yum install -y pcre pcre-devel
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.njupt.edu.cn
軟件包 pcre-8.32-17.el7.x86_64 已安裝并且是最新版本
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 pcre-devel.x86_64.0.8.32-17.el7 將被 安裝
--> 解決依賴關系完成
nginx使用zlib對http包的內(nèi)容進行g(shù)zip,所以需要在linux上安裝zlib庫。
[root@localhost ~]# yum install -y zlib zlib-devel
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.njupt.edu.cn
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 zlib.x86_64.0.1.2.7-18.el7 將被 升級
---> 軟件包 zlib.x86_64.0.1.2.7-21.el7_9 將被 更新
---> 軟件包 zlib-devel.x86_64.0.1.2.7-21.el7_9 將被 安裝
--> 解決依賴關系完成
OpenSSL 是一個強大的安全套接字層密碼庫,nginx不僅支持http協(xié)議,還支持https,所以需要在linux安裝openssl庫。
[root@localhost ~]# yum install -y openssl openssl-devel
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cqu.edu.cn
* extras: mirrors.cqu.edu.cn
* updates: mirrors.njupt.edu.cn
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 openssl.x86_64.1.1.0.2k-19.el7 將被 升級
---> 軟件包 openssl.x86_64.1.1.0.2k-26.el7_9 將被 更新
--> 正在處理依賴關系 openssl-libs(x86-64) = 1:1.0.2k-26.el7_9,它被軟件包 1:openssl-1.0.2k-26.el7_9.x86_64 需要
---> 軟件包 openssl-devel.x86_64.1.1.0.2k-26.el7_9 將被 安裝
--> 正在處理依賴關系 krb5-devel(x86-64),它被軟件包 1:openssl-devel-1.0.2k-26.el7_9.x86_64 需要
--> 正在檢查事務
---> 軟件包 krb5-devel.x86_64.0.1.15.1-55.el7_9 將被 安裝
--> 正在處理依賴關系 libkadm5(x86-64) = 1.15.1-55.el7_9,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
--> 正在處理依賴關系 krb5-libs(x86-64) = 1.15.1-55.el7_9,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
--> 正在處理依賴關系 libverto-devel,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
--> 正在處理依賴關系 libselinux-devel,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
--> 正在處理依賴關系 libcom_err-devel,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
--> 正在處理依賴關系 keyutils-libs-devel,它被軟件包 krb5-devel-1.15.1-55.el7_9.x86_64 需要
---> 軟件包 openssl-libs.x86_64.1.1.0.2k-19.el7 將被 升級
---> 軟件包 openssl-libs.x86_64.1.1.0.2k-26.el7_9 將被 更新
--> 正在檢查事務
---> 軟件包 keyutils-libs-devel.x86_64.0.1.5.8-3.el7 將被 安裝
---> 軟件包 krb5-libs.x86_64.0.1.15.1-50.el7 將被 升級
---> 軟件包 krb5-libs.x86_64.0.1.15.1-55.el7_9 將被 更新
---> 軟件包 libcom_err-devel.x86_64.0.1.42.9-19.el7 將被 安裝
---> 軟件包 libkadm5.x86_64.0.1.15.1-55.el7_9 將被 安裝
---> 軟件包 libselinux-devel.x86_64.0.2.5-15.el7 將被 安裝
--> 正在處理依賴關系 libsepol-devel(x86-64) >= 2.5-10,它被軟件包 libselinux-devel-2.5-15.el7.x86_64 需要
--> 正在處理依賴關系 pkgconfig(libsepol),它被軟件包 libselinux-devel-2.5-15.el7.x86_64 需要
---> 軟件包 libverto-devel.x86_64.0.0.2.5-4.el7 將被 安裝
--> 正在檢查事務
---> 軟件包 libsepol-devel.x86_64.0.2.5-10.el7 將被 安裝
--> 解決依賴關系完成
把它放到root目錄下
3.安裝nginx
tar -xvf nginx-1.17.8.tar
[root@localhost ~]# ll
總用量 6420
-rw-------. 1 root root 1234 4月 15 20:38 anaconda-ks.cfg
drwxrwxrwx. 2 root root 20 4月 18 17:45 for
-rw-r--r--. 1 root root 6569984 4月 24 20:51 nginx-1.17.8.tar
[root@localhost ~]# tar -xvf nginx-1.17.8.tar
nginx-1.17.8/
nginx-1.17.8/auto/
nginx-1.17.8/conf/
nginx-1.17.8/contrib/
nginx-1.17.8/src/
nginx-1.17.8/configure
nginx-1.17.8/LICENSE
nginx-1.17.8/README
nginx-1.17.8/html/
nginx-1.17.8/man/
nginx-1.17.8/CHANGES.ru
nginx-1.17.8/CHANGES
查看解壓的包
[root@localhost ~]# ll
總用量 6420
-rw-------. 1 root root 1234 4月 15 20:38 anaconda-ks.cfg
drwxrwxrwx. 2 root root 20 4月 18 17:45 for
drwxr-xr-x. 8 1001 1001 158 1月 21 2020 nginx-1.17.8
-rw-r--r--. 1 root root 6569984 4月 24 20:51 nginx-1.17.8.tar
4 進入目錄內(nèi)
[root@localhost ~]# cd nginx-1.17.8
[root@localhost nginx-1.17.8]# ll
總用量 764
drwxr-xr-x. 6 1001 1001 4096 4月 24 20:54 auto
-rw-r--r--. 1 1001 1001 301950 1月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 460775 1月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 4月 24 20:54 conf
-rwxr-xr-x. 1 1001 1001 2502 1月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 4月 24 20:54 contrib
drwxr-xr-x. 2 1001 1001 40 4月 24 20:54 html
-rw-r--r--. 1 1001 1001 1397 1月 21 2020 LICENSE
drwxr-xr-x. 2 1001 1001 21 4月 24 20:54 man
-rw-r--r--. 1 1001 1001 49 1月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 4月 24 20:54 src
執(zhí)行命令 configure,生成mikefile文件
./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
搞定配置
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: "/var/run/nginx/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/temp/nginx/client"
nginx http proxy temporary files: "/var/temp/nginx/proxy"
nginx http fastcgi temporary files: "/var/temp/nginx/fastcgi"
nginx http uwsgi temporary files: "/var/temp/nginx/uwsgi"
nginx http scgi temporary files: "/var/temp/nginx/scgi"
生成了一個makefile文件
[root@localhost nginx-1.17.8]# ll
總用量 768
drwxr-xr-x. 6 1001 1001 4096 4月 24 20:54 auto
-rw-r--r--. 1 1001 1001 301950 1月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 460775 1月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 4月 24 20:54 conf
-rwxr-xr-x. 1 1001 1001 2502 1月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 4月 24 20:54 contrib
drwxr-xr-x. 2 1001 1001 40 4月 24 20:54 html
-rw-r--r--. 1 1001 1001 1397 1月 21 2020 LICENSE
-rw-r--r--. 1 root root 355 4月 24 21:01 Makefile
drwxr-xr-x. 2 1001 1001 21 4月 24 20:54 man
drwxr-xr-x. 3 root root 125 4月 24 21:01 objs
-rw-r--r--. 1 1001 1001 49 1月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 4月 24 20:54 src
make
開始編譯
[root@localhost nginx-1.17.8]# make
make -f objs/Makefile
make[1]: 進入目錄“/root/nginx-1.17.8”
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_log.o \
src/core/ngx_log.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_palloc.o \
src/core/ngx_palloc.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_array.o \
src/core/ngx_array.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_list.o \
src/core/ngx_list.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_hash.o \
src/core/ngx_hash.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_buf.o \
src/core/ngx_buf.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/ngx_queue.o \
src/core/ngx_queue.c
正式安裝
make install
安裝到了usr的local目錄下
cp conf/scgi_params \
'/usr/local/nginx/conf/scgi_params.default'
test -f '/usr/local/nginx/conf/nginx.conf' \
|| cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf'
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/var/run/nginx' \
|| mkdir -p '/var/run/nginx'
test -d '/var/log/nginx' \
|| mkdir -p '/var/log/nginx'
test -d '/usr/local/nginx/html' \
|| cp -R html '/usr/local/nginx'
test -d '/var/log/nginx' \
|| mkdir -p '/var/log/nginx'
make[1]: 離開目錄“/root/nginx-1.17.8”
[root@localhost nginx-1.17.8]#
查看安裝
[root@localhost nginx-1.17.8]# cd /usr/local
[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. 5 root root 42 4月 24 21:04 nginx
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 4月 15 20:34 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
4.啟動nginx
[root@localhost local]# cd nginx/
[root@localhost nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 4月 24 21:04 conf
drwxr-xr-x. 2 root root 40 4月 24 21:04 html
drwxr-xr-x. 2 root root 19 4月 24 21:04 sbin
sbin目錄下放了一些可執(zhí)行文件
執(zhí)行啟動命令
root@localhost nginx]# cd sbin/
[root@localhost sbin]# ll
總用量 3792
-rwxr-xr-x. 1 root root 3881416 4月 24 21:04 nginx
[root@localhost sbin]# ./nginx
查看進程是否啟動了文章來源:http://www.zghlxwxcb.cn/news/detail-424012.html
[root@localhost sbin]# ps aux | grep nginx
root 4737 0.0 0.0 20564 616 ? Ss 21:11 0:00 nginx: master process ./nginx
nobody 4738 0.0 0.0 21000 1316 ? S 21:11 0:00 nginx: worker process
root 4741 0.0 0.0 112824 980 pts/0 R+ 21:13 0:00 grep --color=auto nginx
三 訪問
在瀏覽器輸入自己的虛擬機ip+端口號文章來源地址http://www.zghlxwxcb.cn/news/detail-424012.html
./nginx -s stop 關閉
./nginx -s reload 重啟
到了這里,關于三步配置輕量級服務器nginx的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!