分布式篇 - Nginx安装与运行
发布日期:2021-06-30 16:04:50 浏览次数:3 分类:技术文章

本文共 6820 字,大约阅读时间需要 22 分钟。

分布式篇 - 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下载地址:

下载稳定版(Linux系统)。在这里插入图片描述

下载好后,使用Xftp将该压缩文件放到虚拟机或服务器中。
在这里插入图片描述

解压Nginx

[root@localhost /]# cd /usr/local[root@localhost local]# ll总用量 1016drwxr-xr-x. 2 root root       6 4月  11 2018 bindrwxr-xr-x. 2 root root       6 4月  11 2018 etcdrwxr-xr-x. 2 root root       6 4月  11 2018 gamesdrwxr-xr-x. 2 root root       6 4月  11 2018 includedrwxr-xr-x. 2 root root       6 4月  11 2018 libdrwxr-xr-x. 2 root root       6 4月  11 2018 lib64drwxr-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.gzdrwxr-xr-x. 2 root root       6 4月  11 2018 sbindrwxr-xr-x. 5 root root      49 1月   7 15:44 sharedrwxr-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总用量 764drwxr-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.rudrwxr-xr-x. 2 1001 1001    168 1月   7 16:24 conf-rwxr-xr-x. 1 1001 1001   2502 4月  21 2020 configuredrwxr-xr-x. 4 1001 1001     72 1月   7 16:24 contribdrwxr-xr-x. 2 1001 1001     40 1月   7 16:24 html-rw-r--r--. 1 1001 1001   1397 4月  21 2020 LICENSEdrwxr-xr-x. 2 1001 1001     21 1月   7 16:24 man-rw-r--r--. 1 1001 1001     49 4月  21 2020 READMEdrwxr-xr-x. 9 1001 1001     91 1月   7 16:24 src

先进行配置,直接使用默认配置。

[root@localhost nginx-1.18.0]# ./configurechecking for OS + Linux 3.10.0-1160.el7.x86_64 x86_64checking 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总用量 768drwxr-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.rudrwxr-xr-x. 2 1001 1001    168 1月   7 16:24 conf-rwxr-xr-x. 1 1001 1001   2502 4月  21 2020 configuredrwxr-xr-x. 4 1001 1001     72 1月   7 16:24 contribdrwxr-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 Makefiledrwxr-xr-x. 2 1001 1001     21 1月   7 16:24 mandrwxr-xr-x. 3 root root    174 1月   7 16:41 objs-rw-r--r--. 1 1001 1001     49 4月  21 2020 READMEdrwxr-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总用量 4drwxr-xr-x. 2 root root 4096 1月   7 16:41 confdrwxr-xr-x. 2 root root   40 1月   7 16:41 htmldrwxr-xr-x. 2 root root    6 1月   7 16:41 logsdrwxr-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总用量 1016drwxr-xr-x.  2 root root       6 4月  11 2018 bindrwxr-xr-x.  2 root root       6 4月  11 2018 etcdrwxr-xr-x.  2 root root       6 4月  11 2018 gamesdrwxr-xr-x.  2 root root       6 4月  11 2018 includedrwxr-xr-x.  2 root root       6 4月  11 2018 libdrwxr-xr-x.  2 root root       6 4月  11 2018 lib64drwxr-xr-x.  2 root root       6 4月  11 2018 libexecdrwxr-xr-x. 11 root root     151 1月   7 16:48 nginxdrwxr-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.gzdrwxr-xr-x.  2 root root       6 4月  11 2018 sbindrwxr-xr-x.  5 root root      49 1月   7 15:44 sharedrwxr-xr-x.  2 root root       6 4月  11 2018 src[root@localhost local]# rm nginx-1.18.0.tar.gzrm:是否删除普通文件 "nginx-1.18.0.tar.gz"?y[root@localhost local]# ll总用量 0drwxr-xr-x.  2 root root   6 4月  11 2018 bindrwxr-xr-x.  2 root root   6 4月  11 2018 etcdrwxr-xr-x.  2 root root   6 4月  11 2018 gamesdrwxr-xr-x.  2 root root   6 4月  11 2018 includedrwxr-xr-x.  2 root root   6 4月  11 2018 libdrwxr-xr-x.  2 root root   6 4月  11 2018 lib64drwxr-xr-x.  2 root root   6 4月  11 2018 libexecdrwxr-xr-x. 11 root root 151 1月   7 16:48 nginxdrwxr-xr-x.  9 1001 1001 186 1月   7 16:37 nginx-1.18.0drwxr-xr-x.  2 root root   6 4月  11 2018 sbindrwxr-xr-x.  5 root root  49 1月   7 15:44 sharedrwxr-xr-x.  2 root root   6 4月  11 2018 src

之后博主会介绍怎么给nginx添加SSL(基于博主阿里云的服务器),欢迎大家继续关注。

转载地址:https://kaven.blog.csdn.net/article/details/112300026 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:分布式篇 - Nginx添加SSL实现HTTPS访问
下一篇:分布式篇 - 一篇搞懂正向代理和反向代理

发表评论

最新留言

不错!
[***.144.177.141]2024年04月11日 23时56分32秒