Linux 环境下安装 Nginx
Nginx 的下载
官方下载地址:http://nginx.org/en/download.html
Nginx 在官方网站目前发布了3种类型的版本,分别为:
Mainline version(开发版),Stable version(稳定版),Legacy versions(早期版本)
在安装的时候建议选择稳定版。
Nginx 的简单介绍
目录结构介绍
- src 目录:存放Nginx的源代码
- man 目录:存放Nginx的帮助文档
- html 目录:存放默认网站文件
- contrib 目录:存放其他机构或组织贡献的文档资料
- conf 目录:存放Nginx服务器的配置文件
- auto 目录:存放大量的脚本文件,和 configure 脚本程序有关
- configure 文件:Nginx 自动安装脚本,用于检查环境,生成编译代码需要的makefile文件
- CHANGES、CHANGES.ru、LICENSE 和 README 都是 Nginx 服务器的相关文档资料
由于Nginx中的功能都是模块化的,而模块又依赖于一些软件包(如pcre库、zlib库和openssl库)才能使用,因此,在安装Nginx前,需要完成Nginx模块依赖的软件安装包安装。
软件包 | 说明 |
---|---|
pcre-devel | 为 Nginx 模块(如 rewrite )提供正则表达式库 |
zlib-devel | 为 Nginx 模块(如 gzip) 提供数据压缩用的函数库 |
openssl-devel | 为 Nginx 模块(如 ssl) 提供密码算法、证书以及 SSL 协议等功能 |
Nginx 编译安装
安装前准备
# 检查相关依赖是否安装
rpm -qa pcre zlib openssl
# 如果没有安装,执行以下命令
yum -y install pcre-devel openssl-devel
# 由于 openssl-devel 依赖于 zlib-devel,在通过yum进行安装时会自动解决依赖,因此上述命令中省略了 zlib-devel。
如果能够直接使用 yum 安装 nginx 相关依赖,接下载执行编译安装1.0,如果无法在线安装相关依赖,可以参考编译安装2.0
编译安装 1.0
# 解压 nginx nginx-1.22.1.tar.gz
tar -zxvf nginx-1.22.1.tar.gz
# 切换到解压后的目录
cd nginx-1.22.1
# 编译
./configure --prefix=/usr/local/nginx --with-http_ssl_module
# 当看到以下页面表示编译成功
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx/nginx"
nginx binary file: "/usr/local/nginx/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/nginx/conf"
nginx configuration file: "/usr/local/nginx/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/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"
# 当编译成功后,执行以下命令
make && make install
在上上述命令中,源码安装的第一步 ./configure 用于对即将安装的软件进行配置,检查当前的环境是否满足安装软件( Nginx )的依赖关系 。其中, --prefix 选项用于设置 Nginx的安装目录,默认值是/usr/local/nginx ,因也可以省略此选项或指定到其他位置,–with-http_ssl_module选项用于设置在Nginx 中允许使用 http_ssl_module 模块的相关功能。
在Nginx 的安装包中还有许多其他模块,目前暂时用不到当用到的时候重新编译 Nginx 并使用 ”–with-“ 选项添加需要的模块即可
编译安装 2.0
如果没有办法在线安装 nginx 所需的依赖,我们可以从网上先下载好所需的依赖包(pcre、zlib 及 openssl)。将依赖包解压,我这里是解压到 nginx 安装包的目录下。
# 切换到 nginx-1.22.1
cd nginx-1.22.1
# 编译
./configure --prefix=/home/afa/nginx/nginx \
--with-http_ssl_module \
--with-pcre=../pcre-8.42 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.0h
# 当看到以下页面表示编译成功
Configuration summary
+ using system PCRE library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/usr/local/nginx/nginx"
nginx binary file: "/usr/local/nginx/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/nginx/conf"
nginx configuration file: "/usr/local/nginx/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/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"
# 当编译成功后,执行以下命令
make && make install