三步配置轻量级服务器nginx

一 nginx及其应用场景

1.什么是nginx

一款轻量级的Web服务器,反向代理服务器,以及电子邮件代理服务器
主要有三个优点:

  • 占用内存少,并发能力强
  • Nginx为性能优化开发,能支持五千个左右的并发响应
  • (Tomcat只有三百到五百)
  • Nginx支持热部署,可以在不间断服务情况下对软件进行升级(不要用关闭服务器)

2.应用场景背诵

  • 静态http服务器,可以独立提供静态http服务
  • 虚拟主机:在一台服务器虚拟出多个网站
  • 反向代理:网站访问量到达一定程度后,单台服务器已经无法完成所有的访问量,需要用nginx实现反向代理
  • 负载均衡:每个服务器承担一定的资源,不会出现一个服务器承担过多的服务导致宕机,而另一个服务器闲置的情况下

二 nginx的配置安装

1.选择nginx版本

下载nginx, 官方网站:http://nginx.org/en/download.html
我们使用的版本是1.17.8版本。
Nginx在Linux下安装,只提供了源代码,所以我们需要进行编译.

2. 安装配置环境

需要安装gcc的环境。执行命令:

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包的内容进行gzip,所以需要在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协议,还支持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 415 20:38 anaconda-ks.cfg
drwxrwxrwx. 2 root root      20 418 17:45 for
-rw-r--r--. 1 root root 6569984 424 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 415 20:38 anaconda-ks.cfg
drwxrwxrwx. 2 root root      20 418 17:45 for
drwxr-xr-x. 8 1001 1001     158 121 2020 nginx-1.17.8
-rw-r--r--. 1 root root 6569984 424 20:51 nginx-1.17.8.tar

4 进入目录内

[root@localhost ~]# cd nginx-1.17.8
[root@localhost nginx-1.17.8]# ll
总用量 764
drwxr-xr-x. 6 1001 1001   4096 424 20:54 auto
-rw-r--r--. 1 1001 1001 301950 121 2020 CHANGES
-rw-r--r--. 1 1001 1001 460775 121 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 424 20:54 conf
-rwxr-xr-x. 1 1001 1001   2502 121 2020 configure
drwxr-xr-x. 4 1001 1001     72 424 20:54 contrib
drwxr-xr-x. 2 1001 1001     40 424 20:54 html
-rw-r--r--. 1 1001 1001   1397 121 2020 LICENSE
drwxr-xr-x. 2 1001 1001     21 424 20:54 man
-rw-r--r--. 1 1001 1001     49 121 2020 README
drwxr-xr-x. 9 1001 1001     91 424 20:54 src

执行命令 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 424 20:54 auto
-rw-r--r--. 1 1001 1001 301950 121 2020 CHANGES
-rw-r--r--. 1 1001 1001 460775 121 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001    168 424 20:54 conf
-rwxr-xr-x. 1 1001 1001   2502 121 2020 configure
drwxr-xr-x. 4 1001 1001     72 424 20:54 contrib
drwxr-xr-x. 2 1001 1001     40 424 20:54 html
-rw-r--r--. 1 1001 1001   1397 121 2020 LICENSE
-rw-r--r--. 1 root root    355 424 21:01 Makefile
drwxr-xr-x. 2 1001 1001     21 424 20:54 man
drwxr-xr-x. 3 root root    125 424 21:01 objs
-rw-r--r--. 1 1001 1001     49 121 2020 README
drwxr-xr-x. 9 1001 1001     91 424 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 411 2018 bin
drwxr-xr-x. 2 root root  6 411 2018 etc
drwxr-xr-x. 2 root root  6 411 2018 games
drwxr-xr-x. 2 root root  6 411 2018 include
drwxr-xr-x. 2 root root  6 411 2018 lib
drwxr-xr-x. 2 root root  6 411 2018 lib64
drwxr-xr-x. 2 root root  6 411 2018 libexec
drwxr-xr-x. 5 root root 42 424 21:04 nginx
drwxr-xr-x. 2 root root  6 411 2018 sbin
drwxr-xr-x. 5 root root 49 415 20:34 share
drwxr-xr-x. 2 root root  6 411 2018 src

4.启动nginx

[root@localhost local]# cd nginx/
[root@localhost nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 424 21:04 conf
drwxr-xr-x. 2 root root   40 424 21:04 html
drwxr-xr-x. 2 root root   19 424 21:04 sbin

sbin目录下放了一些可执行文件
执行启动命令

root@localhost nginx]# cd sbin/
[root@localhost sbin]# ll
总用量 3792
-rwxr-xr-x. 1 root root 3881416 424 21:04 nginx
[root@localhost sbin]# ./nginx

查看进程是否启动了

[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+端口号
在这里插入图片描述

./nginx -s stop 关闭
./nginx -s reload 重启
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值