Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >linux主要发行版安装nginx教程

linux主要发行版安装nginx教程

作者头像
ianzhi
发布于 2019-07-31 03:48:25
发布于 2019-07-31 03:48:25
2.5K0
举报
文章被收录于专栏:LNMP开发那些事LNMP开发那些事

本文主要讲各主要linux发行版安装nginx的方法,以及通用的编译安装方法。 关于nginx的配置,将放在以后的文章中。 文章参考nginx官方网站安装教程,在翻译的基础上,添加个人的总结,以及一些常见问题的处理。

一、使用包管理器安装

RedHat,CentOS发行版,yum包管理器安装nginx

  1. 新建文件 /etc/yum.repos.d/nginx.repo,并写入以下内容:

nginx name=nginx repo baseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/ gpgcheck=0 enabled=1

  1. 根据你使用的操作系统情况,将内容中的“OS” 替换为 “rhel” or “centos”,根据使用的系统版本,将 “OSRELEASE” 替换为 “6” or “7”。

Debian/Ubuntu发行版,apt包管理器安装nginx

下载nginx软件仓库的密钥文件

点此下载密钥文件

将密钥添加到系统中

sudo apt-key add nginx_signing.key

  1. 将仓库信息写入到/etc/apt/sources.lish文件

deb http://nginx.org/packages/OS/CODENAME nginx deb-src http://nginx.org/packages/OS/ CODENAME nginx

根据使用的发行版信息,将OS修改为ubuntu或者debian

查询文件下面的附录一,将命令中的CODENAME,替换为你使用的系统版本的codename。

  1. 安装nginx

sudo apt update sudo apt install nginx

二、编译安装nginx

示例二

示例一

下载nginx源代码

进入下面的地址,选择自己想要安装的版本即可,服务器请安装stable版本。

下载地址nginx官方网站

解压归档文件

  • 在下载文件的保存位置,右键,选择“在终端打开”,运行

tar -xvf nginx-1.15.5.tar.gz

解压归档文件

  1. 切换到解压出来的文件夹中

cd ./nginx-1.15.5

  1. configure configure的作用主要是检查当前环境是否满足安装条件,并对即将安装的软件进行配置。 这里可以直接运行

./configure

但是,我们可能不只需要nginx的基础功能,或者需要对安装进行一些配置。这时,我们需要添加一些编译参数。具体的编译参数可以通过:

./configure --help

来查看,具体各配置项目的意义,请查看附录二。

一般可以这样配置:

./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre

configure过程中可能会遇到的一些问题请查看附录三。

成功示例图

  1. 安装软件

make && make install

只要上一步的configure通过,这里一般不会有问题。

  1. 运行nginx

/usr/local/nginx/sbin/nginx

运行成功无返回,如果运行失败报错,请查看文章底部。

三、附录

附录一 nginx二进制包支持的发行版

RHEL和CentOS

Version Supported Platforms

6.x x86_64, i386

7.4+ x86_64, ppc64le

Debian

Version Codename Supported Platforms

8.x jessie x86_64, i386

9.x stretch x86_64, i386

Ubuntu

Version Codename Supported Platforms

14.04 trusty x86_64, i386, aarch64/arm64

16.04 xenial x86_64, i386, ppc64el, aarch64/arm64

17.10 artful x86_64, i386

18.04 bionic x86_64

附录二 nginx编译参数含义

 --help 显示帮助信息

 --prefix=PATH 设置安装路径

 --sbin-path=PATH 启动服务可执行文件路径

 --modules-path=PATH 模块路径

 --conf-path=PATH 设置配置文件的路径

 --error-log-path=PATH 错误日志的路径

 --pid-path=PATH 进程号文件保存路径

 --lock-path=PATH 锁文件保存路径

 --user=USER 设置用来启动nginx进程的用户名

 --group=GROUP 设置用来启动nginx进程的用户所属的用户组

 --build=NAME 设置构建名称

 --builddir=DIR 设置构建目录

 --with-select_module 安装指定模块

 --without-select_module 禁用指定模块

 --with-poll_module 安装连接池模块

 --without-poll_module禁用连接池模块

 --with-threads 安装线程池模块

 --with-file-aio 安装AIO模块,允许nginx异步操作文件

 --with-http_ssl_module 安装ssl模块

 --with-http_v2_module 安装http v2模块

 --with-http_realip_module 将客户端地址更改为在指定的头字段中发送的地址

 --with-http_addition_module 附加模块

 --with-http_xslt_module 

 --with-http_xslt_module=dynamic 可以构建使用一个或多个XSLT样式表转换XML响应

 --with-http_image_filter_module

 --with-http_image_filter_module=dynamic 用于转换JPEG,GIF,PNG和WebP的图像的格式

 --with-http_geoip_module

 --with-http_geoip_module=dynamic 创建基于与MaxMind GeoIP二进制文件相配的客户端IP地址的ngx_http_geoip_module变量

 --with-http_sub_module 允许用一些其他文本替换nginx响应中的一些文本

 --with-http_dav_module 增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法

 --with-http_flv_module 寻求内存使用基于时间的偏移量文件

 --with-http_mp4_module 

 --with-http_gunzip_module enable ngx_http_gunzip_module

 --with-http_gzip_static_module enable ngx_http_gzip_static_module

 --with-http_auth_request_module enable ngx_http_auth_request_module

 --with-http_random_index_module 随机选择该目录下的一个文件显示出来

 --with-http_secure_link_module 防盗链模块

 --with-http_degradation_module 在内存不足的情况下返回204或444码

 --with-http_slice_module CDN系统中,向父层回源时,如果文件过大,使用slice可以让用户快速得到响应

 --with-http_stub_status_module 查看Nginx的一些状态信息

 --without-http_charset_module 取消安装charset模块,该模块添加特定字符集设置到客户端相应的 “Content-Type”域中

 --without-http_gzip_module 禁用gzip模块,该模块用来减轻服务器的带宽问题

 --without-http_ssi_module 禁用ssi模块,该模块可以使静态网页实现像动态网页一样 include 的功能

 --without-http_userid_module 禁用userid模块,该模块可以标示客户端设置合适的cookies

 --without-http_access_module 禁用access模块,该模块可以实现基于IP的访问控制

 --without-http_auth_basic_module 禁用auth_basic模块,该模块可以实现基础http认证

 --without-http_mirror_module 把真实请求引流到测试环境

 --without-http_autoindex_module 禁用autoindex模块,该模块允许列出目录内容

 --without-http_geo_module 禁用geo模块,该模块可以实现限速白名单的作用

 --without-http_map_module 禁用模块,该模块可以创建变量,这些变量的值与另外的变量值相关联

 --without-http_split_clients_module 禁用split_clients模块,该模块可以实现负载均衡

 --without-http_referer_module 禁用referer模块,该模块可根据header中的referer信息屏蔽某些请求对网站或应用的访问

 --without-http_rewrite_module 禁用rewrite模块,该模块可以实现url重写

 --without-http_proxy_module 禁用proxy模块,该模块可以实现代理

 --without-http_fastcgi_module 禁用fastcgi模块,该模块允许将请求传递给另一台服务器。

 --without-http_uwsgi_module 禁用uwsgi模块,该模块允许将请求传递到uwsgi服务器

 --without-http_scgi_module 禁用scgi模块,该模块允许nginx同Scgi协同工作

 --without-http_grpc_module 禁用grpc模块,该模块可以代理gRPC TCP连接

 --without-http_memcached_module 禁用memcached模块,该模块允许nginx直接操作memcached缓存

 --without-http_limit_conn_module 禁用limit_conn模块,该模块用来限制连接数

 --without-http_limit_req_module 禁用limit_req模块,该模块用来限制请求数

 --without-http_empty_gif_module 禁用empty_gif模块,该模块可以返回一个保存在内存中的透明像素的GIF图片

 --without-http_browser_module 禁用browser模块,该模块创建变量,它们的值取决于 请求头中“User-Agent”的值

 --without-http_upstream_hash_module 禁用upstream_hash模块,该模块可以用来定义后端负载集群

 --without-http_upstream_ip_hash_module 禁用upstream_ip_hash模块,该模块可以根据请求所属的客户端IP计算得到一个数值,然后把请求发往该数值对应的后端

 --without-http_upstream_least_conn_module 禁用upstream_least_conn

 --without-http_upstream_random_module 禁用upstream_random模块

 --without-http_upstream_keepalive_module 禁用upstream_keepalive模块

 --without-http_upstream_zone_module 禁用upstream_zone模块

 --with-http_perl_module 开启perl模块

 --with-http_perl_module=dynamic 允许动态perl模块

 --with-perl_modules_path=PATH 指定perl模块目录位置

 --with-perl=PATH 指定perl可执行文件位置

 --http-log-path=PATH 指定日志文件路径

 --http-client-body-temp-path=PATH 指定客户端请求体临时文件路径

 --http-proxy-temp-path=PATH 指定代理临时文件路径

 --http-fastcgi-temp-path=PATH 指定fastcgi临时文件路径

 --http-uwsgi-temp-path=PATH 指定uwsgi临时文件路径

 --http-scgi-temp-path=PATH 指定http_scgi临时文件路径

 --without-http 禁用http服务

 --without-http-cache 禁用http缓存

邮件模块

 --with-mail 开启POP3/IMAP4/SMTP模块

 --with-mail=dynamic 开启动态POP3/IMAP4/SMTP模块

 --with-mail_ssl_module 开始加密mail模块

 --without-mail_pop3_module 禁用pop3模块 

 --without-mail_imap_module 禁用imap模块 

 --without-mail_smtp_module 禁用smtp模块

stream模块

 --with-stream 开启TCP/UDP策略模块

 --with-stream=dynamic 开启动态TCP/UDP策略

 --with-stream_ssl_module 为代理流服务器提供必要的SSL/TLS协议支持

以下为负载均衡各种算法的支持, 作用与http基本相同

 --with-stream_realip_module 

 --with-stream_geoip_module

 --with-stream_geoip_module=dynamic

 --with-stream_ssl_preread_module

 --without-stream_limit_conn_module

 --without-stream_access_module

 --without-stream_geo_module

 --without-stream_map_module

 --without-stream_split_clients_module

 --without-stream_return_module

 --without-stream_upstream_hash_module

 --without-stream_upstream_least_conn_module

 --without-stream_upstream_random_module

 --without-stream_upstream_zone_module

 --with-google_perftools_module 开启google_perftools模块,可以优化Nginx和MySQL的内存管

 --with-cpp_test_module 添加cpp测试模块

 --add-module=PATH 允许扩展模块

 --add-dynamic-module=PATH 允许加载动态模块的路径

 --with-compat 动态加载模块

 --with-cc=PATH 指定C编译器的备用位置

 --with-cpp=PATH 指定C预处理器的备用位置

 --with-cc-opt=OPTIONS 要传递到C编译器命令行的其他选项

 --with-ld-opt=OPTIONS 要传递到C链接器命令行的其他选项

 --with-cpu-opt=CPU 指定cpu体系结构,有效值:pentium, pentiumpro, pentium3, pentium4,athlon, opteron, sparc32, sparc64, ppc64

 --without-pcre 禁用pcre库

 --with-pcre 强制使用pcre库

 --with-pcre=DIR pcre源文件路径

 --with-pcre-opt=OPTIONS 设置pcre安装配置

 --with-pcre-jit 构建带有JIT编译器的PCRE

 --with-zlib=DIR 制定使用的zlib库路径

 --with-zlib-opt=OPTIONS 为zlib安装制定附加配置

 --with-zlib-asm=CPU 为zlib库指定汇编器源,有效的值:pentium, pentiumpro

 --with-libatomic 使用系统默认libatomic库

 --with-libatomic=DIR 制定使用的libatomic库路径

 --with-openssl=DIR 指定使用的openssl库路径

 --with-openssl-opt=OPTIONS 设置openssl构建选项

 --with-debug 开启debug日志

附录三 nginx编译过程中可能遇到的问题及解决方案

  • ./configure: error: C compiler cc is not found. 这是没有安装c的编译器,执行

yum install gcc

安装gcc,中间交互一路y即可。

  • ./configure: error: the HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 缺少pcre库,执行

64位 yum install pcre-devel.x86_64 # 32位 yum install pcre-devel.i686

来安装pcre库

  • ./configure: error: SSL modules require the OpenSSL library. You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using --with-openssl=<path> option. 缺少openssl库,执行

64位 yum install openssl.x86_64 # 32位 yum install openssl.i686

  • nginx: emerg getpwnam("nginx") failed 你指定了运行用户,但是还没有在系统中创建该用户,执行

useradd nginx

  • nginx: emerg mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory) 未创建该目录,执行

mkdir -p /var/tmp/nginx/client/

如果大家还遇到了其他的问题,请在文章后面留言,持续更新,以帮助大家解决安装上的问题。

文章作者ianzhi,原文地址:https://cloud.tencent.com/developer/article/1476790

文章版权归作者所有,转载请保留此声明。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-10-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Centos下安装FastDFS
FastDFS运行需要一些依赖,在课前资料提供的虚拟中已经安装好了这些依赖,如果大家想要从头学习,可以按下面方式安装:
用户4396583
2024/07/25
1680
Nginx编译安装参数详解
./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
非著名运维
2022/06/22
1.6K0
Nginx编译安装参数详解
Nginx总结(一)如何安装Nginx【详细教程】
以前写过一些Nginx的文章,但都是用到什么说什么,没有一个完整系统的总结。趁最近有时间,打算将Nginx相关的内容重新整理一下。nginx系列文章地址如下:https://www.cnblogs.com/zhangweizhong/category/1529997.html
章为忠学架构
2020/01/17
12.9K0
Nginx总结(一)如何安装Nginx【详细教程】
nginx基础5
Tip: 以下是可配置的选项,不加参数会按默认特性配置 [root@h102 nginx-1.9.5]# ./configure --help --help print this message --prefix=PATH set installation prefix --sbin-path=PATH set nginx binary pathname
franket
2022/05/12
1820
Nginx 模块(1)
Nginx (engine x) 可以作为 HTTP 和反向代理服务器,也可以作为邮件代理和普通的 TCP/UDP 代理服务器
franket
2021/10/18
5050
Linux下安装和使用Nginx
Nginx (“engine x”) 是一个高性能的 HTTP和反向代理服务器,也是一个 IMAP/POP3/SMTP 服务器。 正向代理:
共饮一杯无
2022/11/24
1.2K0
Linux下安装和使用Nginx
Nginx(一)安装及启停
    我发现很多博客排版杂乱,表达不清,读者看了往往云里雾里。我此前的博客也是如此,我自己很不满意。今起,每一篇博客都会用心写,此前的博客我也会尽力修改。至少要做到排版整洁,让自己满意,让别人能看懂。
用户1432189
2018/09/05
8860
Nginx(一)安装及启停
Nginx服务实践
注意下载页面最好选择稳定版:http://nginx.org/en/download.html
陈雷雷
2019/07/10
8490
重识Nginx - 02 手把手教你编译适合自己的nginx 1.22.0
文章目录 编译Nginx 下载nginx 1.22.0 step 2 解压 编译 (这里我们只指定 prefix,其他默认 ) make 编译 make install 安装 Configure的命令参数 编译Nginx 下载nginx 1.22.0 https://nginx.org/en/download.html 复制链接地址 ,下载 1.22.0 [root@VM-0-7-centos ng]# wget https://nginx.org/download/nginx-1.22.0.
小小工匠
2022/09/27
8260
重识Nginx - 02 手把手教你编译适合自己的nginx 1.22.0
nginx动态添加模块
有时候我们在环境部署nginx时,由于环境初期较简单,随着后期业务发展,需要的功能越来越多时,可能我们最开始编译安装的nginx已经无法满足我们现在的需求了,比如说,我们想使用nginx的缓存功能,想使用nginx的连接限制模块等,这时我们就需要在不覆盖之前已经编译好的nginx来动态添加所需的模块了。
dogfei
2020/07/31
2.1K0
2.Nginx进阶学习之最佳配置实践指南
描述:在企业线上生产环境中推荐进行Nginx编译安装,可以按照业务侧重点进行相应 Nginx 编译参数配置,所以编译参数不是功能加的越多越好,应该尽可能少编译模块不用的最好不要加入,本小结将以最新的Nginx版本以及依赖版本进行编译演示。
全栈工程师修炼指南
2022/09/29
2K0
2.Nginx进阶学习之最佳配置实践指南
Nginx 安装
Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。
用户5760343
2022/05/23
2920
Nginx 安装
LNMP之Nginx
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/PO
用户1173509
2018/03/28
1.1K0
LNMP之Nginx
OpenNJet如何做到让用户永远在线
最近看到了国内开源的一个名为OpenNJet的项目,有一个响亮的口号:“下一代云原生应用引擎”。
手撕代码八百里
2024/05/24
1180
OpenNJet如何做到让用户永远在线
Nginx入门详解文档
Nginx入门详解文档 1 文章内容 掌握nginx+tomcat反向代理的使用方法。 掌握nginx作为负载均衡器的使用方法。 掌握nginx实现web缓存方法。 2 nginx介绍 2.1 什么是nginx Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。 3 nginx安装 3.1 下载 进入http://nginx.org/en/download.html
Java帮帮
2018/03/15
1.4K0
Nginx入门详解文档
爬虫、代理和Nginx
做过爬虫的人应该都知道,抓的网站和数据多了,如果爬虫抓取速度过快,免不了触发网站的防爬机制,几乎用的同一招就是封IP。解决方案有2个:
后端技术探索
2018/10/18
2.2K0
爬虫、代理和Nginx
Nginx环境准备与安装
进入到 Nginx 解压包目录/usr/local/Nginx-1.20.1 目录中,查看 Nginx 的目录。
会洗碗的CV工程师
2024/06/09
1680
Nginx环境准备与安装
Nginx实用模块
Nginx 是一个很强大的高性能Web和反向代理应用。原先一直停留在apt install nginx 的阶段,直到开始深入了解其模块等,才发现:nginx厉害!!nginx除了基础功能以外还有很多有趣且实用的模块。因为nginx内置了许多变量(http://nginx.org/en/docs/varindex.html 部分是模块实现)、逻辑运算、指令,组合起来,就可以实现强大的功能。
yumusb
2020/04/22
9690
Nginx教程
Nginx是一款高性能的http 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu、内存等资源消耗却非常低,运行非常稳定。
红目香薰
2022/11/29
5570
Nginx教程
Nginx编译安装nginx-upsync-module模块以实现动态负载
【转载请注明出处】:https://cloud.tencent.com/developer/article/1627571
后端老鸟
2020/05/13
1.2K0
Nginx编译安装nginx-upsync-module模块以实现动态负载
相关推荐
Centos下安装FastDFS
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档