暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

01.Nginx安装

运维之旅 2021-02-24
248
  • 将会更新一系列的Nginx教程

目录

  • Nginx简介
  • Nginx安装
    • yum安装
    • 二进制安装

1.简介

1.1.nginx是什么?

Nginx 则是免费的、开源的、高性能的HTTP和反向代理服务器、邮件代理服务器、以及TCP/UDP代理服务器  

1.2.为什么大家都使用nginx?

- 作为web服务器:Nginx处理静态文件、索引文件以及自动索引效率非常高
- 作为代理服务器:Nginx可以实现快速、高效的反向代理,提升网站性能
- 作为负载均衡服务器:支持简单的容错和利用算法进行负载均衡
- 在性能方面:Nginx专门为性能优化而开发的,在实现上非常注重效率。它采用epoll模型,可以支持更多的并发连接,并在大并发时占用很低的内存资源
- 在高可用方面:Nginx支持热部署,启动速度迅速,因此在不间断服务的情况下,对软件版本或者配置升级,及时运行数月也无需重新启动,几乎可以做到7*24小时不及按段运行

2.安装

2.1.yum安装

  • 需要提前配置好epel源
# yum install epel-release -y
# yum install -y nginx
# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.16.1
/usr/share/doc/nginx-1.16.1/CHANGES
/usr/share/doc/nginx-1.16.1/README
/usr/share/doc/nginx-1.16.1/README.dynamic
/usr/share/doc/nginx-1.16.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.16.1
/usr/share/licenses/nginx-1.16.1/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/en-US
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/img
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx

#
 which nginx
/usr/sbin/nginx

2.1.1.检查安装

# rpm -qa nginx
nginx-1.16.1-3.el7.x86_64

#
 查看nginx安装包的信息
# rpm -qi nginx
Name        : nginx
Epoch       : 1
Version     : 1.16.1
Release     : 3.el7
Architecture: x86_64
Install Date: Tue 09 Feb 2021 11:17:50 AM CST
Group       : Unspecified
Size        : 1694049
License     : BSD
Signature   : RSA/SHA256, Sun 01 Nov 2020 10:58:48 AM CST, Key ID 6a2faea2352c64e5
Source RPM  : nginx-1.16.1-3.el7.src.rpm
Build Date  : Sun 01 Nov 2020 10:02:58 AM CST
Build Host  : buildhw-x86-09.iad2.fedoraproject.org
Relocations : (not relocatable)
Packager    : Fedora Project
Vendor      : Fedora Project
URL         : http://nginx.org/
Bug URL     : https://bugz.fedoraproject.org/nginx
Summary     : A high performance web server and reverse proxy server
Description :
Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
IMAP protocols, with a strong focus on high concurrency, performance and low
memory usage.

2.1.2.查看帮助

# nginx -h
nginx version: nginx/1.16.1
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit #显示版本和编译参数
  -t            : test configuration and exit #测试配置文件是否异常
  -T            : test configuration, dump it and exit #测试并打印
  -q            : suppress non-error messages during configuration testing  #静默模式
  -s signal     : send signal to a master process: stop, quit, reopen, reload #发送信号
  -p prefix     : set prefix path (default: /usr/share/nginx/)  #指定Nginx目录
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)  #指定配置文件路径
  -g directives : set global directives out of configuration file  #设置全局指令

2.1.3.验证Nginx

# 检查配置文件是否ok
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

#
 查看nginx版本
# nginx -v
nginx version: nginx/1.16.1

#
 查看nginx版本,编译参数
# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1c FIPS  28 May 2019 (running with OpenSSL 1.1.1g FIPS  21 Apr 2020)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

2.1.4.Nginx启动脚本

# more /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2.1.5.配置Nginx

  • 默认配置文件:/etc/nginx/nginx.conf,,默认配置如下
# egrep -v "#|^$" /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

2.1.6.管理Nginx

# 启动
nginx

#
 quit是一个优雅的关闭方式,Nginx在退出前完成已经接受的连接请求
nginx -s quit

#
 stop是快速关闭,不管有没有正在处理的请求。
nginx -s stop

#
 热加载配置,不影响服务正常运行
nginx -s reload

--------另外我们还可以用systemctl管理-------------
# 服务管理
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl status nginx

2.1.7.访问Nginx

http://ip

2.2.编译安装

【说在前面-这是centos6环境下的笔记-centos7需要更改一下启动脚本】

1.为什么不用yum安装?
因为内网环境,利用rpm包安装,解决依赖比较麻烦。

2.编译参数怎么确定?
找个外网环境yum安装nginx,然后nginx -V获取的编译参数,然后编译的时候把这些参数编译进去。

2.2.1.安装依赖

# yum install -y pcre pcre-devel gcc-c++  zlib zlib-devel openssl openssl-devel

  • pcre pcre-devel:在Nginx编译需要 PCRE(Perl Compatible Regular Expression),因为Nginx 的 Rewrite模块和HTTP 核心模块会使用到PCRE正则表达式语法
  • zlip zlib-devel:nginx启用压缩功能的时候,需要此模块的支持
  • openssl openssl-devel:开启SSL的时候需要此模块的支持

2.2.2.创建用户

# useradd -s /sbin/nologin -M nginx

2.2.3.编译

# 将软件包下载到/soft目录下
cd /soft  && tar zxvf nginx-1.16.1.tar.gz

#
 编译
cd nginx-1.16.1

#
 编译是为了检查系统环境是否符合编译安装的要求,比如是否有gcc编译工具,是否支持编译参数当中的模块,并根据开启的参数等生成Makefile文件为下一步做准备
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --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 --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio  --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

#
 编译步骤,根据Makefile文件生成相应的模块
make  

#
 创建目录,并将生成的模块和文件复制到相应的目录
make install

2.2.4.检查配置文件

[root@nginx01 nginx-1.16.1]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

报错解决:
[root@nginx02 nginx-1.16.1]# mkdir /var/cache/nginx/client_temp -pv
mkdir: created directory `/var/cache/nginx'
mkdir: created directory `/var/cache/nginx/client_temp'

2.2.5.拷贝yum自带的启动脚本过来或者看官网的

# centos7的,找一下上面yum安装的脚本就好了,这里不再记录

https://www.nginx.com/resources/wiki/start/topics/examples/initscripts/
因为我的编译参数,包括安装路径,配置文件路径,用户都是用yum的参数,所以脚本是直接拿来就能用,无需更改

#
 添加执行权限
chmod +x /etc/init.d/nginx

2.2.6.添加到服务管理列表,并让其开机自动启动

chkconfig --add nginx
chkconfig nginx on

#
 查看添加情况
chkconfig --list nginx

2.2.7.启动|停止|加载|查看状态

# service nginx start|stop|reload|status

2.2.8.如果不想用service管理

nginx           #启动
nginx -s reload #平滑加载
nginx -s stop   #快速停止或关闭
nginx -s quit   #正常停止或关闭

2.2.9.nginx一些命令

nginx -t   检查语法
nginx -v   查看版本
nginx -V   查看编译参数


文章转载自运维之旅,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论