目录
概要
本文主要介绍了安装nginx的两种方式如:yum安装和源码安装方式。
1 YUM部署nginx
访问nginx官网,nginx news
#安装yum工具包集合
sudo yum install yum-utils
#创建nginx官方源文件
vi /etc/yum.repos.d/nginx.repo
#文件内容如下:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
#检查是否配置成功,也可以看自己将要安装的nginx版本
yum list | grep nginx
#安装nginx
yum install -y nginx
#开启nginx
systemctl start nginx
#设置为开机自启
systemctl enable nginx
#查看自己安装的版本
nginx -v
#关闭防火墙
systemctl stop firewalld
#设置防火墙开机不自启
systemctl disable firewalld
2 源码包部署nginx
2.1 安装nginx环境准备
1、装编译环境,安装 gcc环境,nginx 编译时依赖 gcc 环境
yum -y install gcc gcc-c++
2、安装pcre软件包让nginx支持重写功能(使nginx支持http rewrite模块)
yum install -y pcre pcre-devel
3、安装 openssl-devel(ssl是安全套接字层密码库,主要用于通信加密)
yum install -y openssl openssl-devel
4、安装 zlib,zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩
yum install -y zlib zlib-devel
5、创建用户 nginx,为了防止权限过于混乱导致安全问题
useradd nginx
passwd nginx
6、下载nginx
#如果下载不了,可能是链接失效,请在官网找到源码安装,复制链接并使用wget 下载
wget https://nginx.org/download/nginx-1.24.0.tar.gz
2.2 编译安装 nginx
1、解压编译安装
#解压nginx源码包到指定目录
tar -vzxf nginx-1.24.0 -C /usr/local
#进入到nginx-1.24.0目录内
cd /usr/local/nginx-1.24.0
#查看目录内容
ls
#进行配置
./configure \
#配置内容如下:
--group=nginx \
--user=nginx \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/tmp/nginx/client_body \
--http-proxy-temp-path=/tmp/nginx/proxy \
--http-fastcgi-temp-path=/tmp/nginx/fastcgi \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-pcre
#进行编译和编译安装
make &&make install
# 查看 nginx版本 v小写
nginx -v
#查看nginx模块 V大写
nginx -V
# 模块参数介绍
# 设置额外的参数将被添加到CFLAGS变量(FreeBSD或者ubuntu使用)
--with-cc-opt='-g -O2 -fPIE -fstack-protector'
--param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2'
--with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now'
--prefix=/usr/share/nginx # 指向安装目录
--conf-path=/etc/nginx/nginx.conf # 指定配置文件
--http-log-path=/var/log/nginx/access.log # 指定访问日志
--error-log-path=/var/log/nginx/error.log # 指定错误日志
--lock-path=/var/lock/nginx.lock # 指定lock文件
--pid-path=/run/nginx.pid # 指定pid文件
--http-client-body-temp-path=/var/lib/nginx/body # 设定http客户端请求临时文件路径
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi # 设定http fastcgi临时文件路径
--http-proxy-temp-path=/var/lib/nginx/proxy # 设定http代理临时文件路径
--http-scgi-temp-path=/var/lib/nginx/scgi # 设定http scgi临时文件路径
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi # 设定http uwsgi临时文件路径
--with-debug # 启用debug日志
--with-pcre-jit # 编译PCRE包含“just-in-timecompilation”
--with-ipv6 # 启用ipv6支持
--with-http_ssl_module # 启用ssl支持
--with-http_stub_status_module # 获取nginx自上次启动以来的状态
--with-http_realip_module # 允许从请求标头更改客户端的IP地址值,默认为关# 实现基于一个子请求的结果的客户端授权。如果该子请求返回的2xx响应代码,所述接入是允许的。如果它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其他响应代码被认为是一个错误。
--with-http_auth_request_module
--with-http_addition_module # 作为一个输出过滤器,支持不完全缓冲,分部分
响应请求
# 增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法 默认关闭,需编译开启
--with-http_dav_module
--with-http_geoip_module # 使用预编译的MaxMind数据库解析客户端IP地
址,得到变量值
--with-http_gunzip_module # 它为不支持“gzip”编码方法的客户端解压具
有“Content-Encoding: gzip”头的响应。
--with-http_gzip_static_module # 在线实时压缩输出数据流
--with-http_image_filter_module # 传输JPEG/GIF/PNG 图片的一个过滤器)(默
认为不启用。gd库要用到)
--with-http_spdy_module # SPDY可以缩短网页的加载时间
--with-http_sub_module # 允许用一些其他文本替换nginx响应中的一些文
本
--with-http_xslt_module # 过滤转换XML请求
--with-mail # 启用POP3/IMAP4/SMTP代理模块支持
--with-mail_ssl_module # 启用ngx_mail_ssl_module支持启用外部模块
支持配置nginx参数
2.3 编辑配置文件并运行nginx
1、编辑配置文件
#编辑配置文件
vim /etc/nginx/nginx.conf
#进入命令文件
usr/local/nginx/sbin/
#检测修改后的配置文件是否正确
nginx -t
#运行nginx服务
nginx
2、nginx配置文件介绍
# 全局参数设置worker_processes 1 ; # 设置 nginx 启动进程的数量,一般设置成与逻辑 cpu 数量相同error_log logs/error.log; # 指定错误日志worker_rlimit_nofile 102400 ; # 设置一个 nginx 进程能打开的最大文件数pid /var/run/nginx.pid;events { # 事件配置worker_connections 10240 ; # 设置一个进程的最大并发连接数use epoll; # 事件驱动类型}# http 服务相关设置http {log_format main 'remote_addr - remote_user [time_local] "request" ''status body_bytes_sent "$http_referer" ''"http_user_agent" "http_x_forwarded_for"' ;access_log /var/log/nginx/access.log main; # 设置访问日志的位置和格式sendfile on; # 用于开启文件高效传输模式,一般设置为on,若nginx用 来进行磁盘IO负载应用时,可以设置为off,降低系统负载tcp_nopush on; # 减少网络报文段数量,当有数据时,先别着急发送, 确保 数据 包已经装满数据, 避免了网络拥塞tcp_nodelay on; # 提高 I/O 性能,确保数据尽快发送 , 提高可数据传输效率gzip on; # 是否开启 gzip 压缩keepalive_timeout 65 ; # 设置长连接的超时时间,请求完成之后还要保持连接多久,不是请求时间多久,目的是保持长连接,减少创建连接过程给系 统带来的性能损耗,类似于线程池,数据库连接池types_hash_max_size 2048 ; # 影响散列表的冲突率。 types_hash_max_size 越大,就会消耗更多的内存,但散列 key 的冲突率会降低,检索速度就更快。types_hash_max_size 越小,消耗的内存就越小,但散列 key 的冲突率可能上升include /etc/nginx/mime.types; # 关联 mime 类型,关联资源的媒体类型( 不同的媒体类型的打开方式 )default_type application/octet-stream; # 根据文件的后缀来匹配相应的 MIME类型,并写入 Response header ,导致浏览器播放文件而不是下载# 虚拟服务器的相关设置server {listen 80 ; # 设置监听的端口server_name localhost; # 设置绑定的主机名、域名或 ip 地址charset koi8-r; # 设置编码字符location / {root /var/www/nginx; # 设置服务器默认网站的根目录位置index index.html index.htm; # 设置默认打开的文档}error_page 500 502 503 504 /50x.html; # 设置错误信息返回页面location = /50x.html {root html; # 这里的绝对位置是 /var/www/nginx/html}}}