Nginx简介 & 安装
- 1、Nginx简介
- 1)Nginx 特性
- 2)Tengine简介
- 2、Nginx安装
- 1)RPM包安装
- ①在线RPM包安装
- ②yum源安装
- 2)源码安装
1、Nginx简介
Nginx 是一款轻量级的网页服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器。此软件在 BSD-like 协议下发行,可以在UNIX、GNU/Linux、BSD、Mac OS X、Solaris,以及Microsoft Windows等操作系统中运行。
Nginx 具有高并发(特別是静态资源)、 占用系统资源少等特性。而在功能应用方面,Nginx 不但是一个优秀的 Web 服务软件,同时还具有反向代理、负载均衡以及缓存服务功能。
Nginx 官网:http://nginx.org/
1)Nginx 特性
基本特性:
□ 可针对静态资源进行高速、高并发访问以及缓存;
□ 可使用反向代理加速,并且可进行数据缓存;
□ 具有简单负载均衡、节点健康检查和容错功能;
□ 支持远程 FastCGI 服务的缓存加速;
□ 支持 FastCGI 、Uwsgi 、SCGI 、 Memcached Servers 的加速和缓存;
□ 支持 SSL 、TLS 、SNI;
□ 具有模块化的架构:过滤器包括 gzip 压缩 、ranges 支持、chunked 响应 、 XSLT、SSI 及图像缩放等功能,另外在 SSI 过滤器中,一个包含多个 SSI 的页面,如果经由FastCGI 或反向代理处理,可被并行处理;其他 WWW 服务特性:
□ 支持基于名字、端口及 IP 的多虚拟主机站点;
□ 支持 Keep-alive 和 pipelined 连接;
□ 可进行简单 、方便、灵活的配置和管理;
□ 支持修改 Nginx 配置,并且在代码上线时,可平滑重启,不中断业务访问;
□ 可自定义访问日志格式,临时缓冲写日志操作,快速日志轮询及通过 rsyslog 处理日志;
□ 可利用信号控制 Nginx 进程;
□ 支持 3xx-5xx HTTP 状态码重定向;
□ 支持 rewrite 模块,支持 URI 重写及正则表达式E配;
□ 支持基于客户端 IP 地址和 HTTP 基本认证的访问控制;
□ 支持 PUT 、 DELETE 、 MKCOL 、 COPY 及 MOVE 等较特殊的 HTTP 请求方法;
□ 支持 FLV 流和 MP4 流技术产品应用;
□ 支持 HTTP 响应速率限制;
□ 支持同一 IP 地址的并发连接或请求数限制;
□ 支持邮件服务代理;
2)Tengine简介
Tengine 是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。
从2011年12月开始,Tengine成为一个开源项目,Tengine团队在积极地开发和维护着它。Tengine团队的核心成员来自于淘宝、搜狗等互联网企业。Tengine是社区合作的成果,我们欢迎大家参与其中,贡献自己的力量。
Tengine官网:http://tengine.taobao.org/
2、Nginx安装
Nginx获取:
RPM包获取:http://nginx.org/packages/
源码包获取:http://nginx.org/download/
1)RPM包安装
①在线RPM包安装
安装Nginx:
[root@www ~]# yum install http://nginx.org/packages/centos/7Server/x86_64/RPMS/nginx-1.20.1-1.el7.ngx.x86_64.rpm -y
启动Nginx并检测Nginx是否开启:
[root@www ~]# systemctl start nginx
查看Nginx状态:
[root@www ~]# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2022-06-11 17:08:50 CST; 3s ago
Docs: http://nginx.org/en/docs/
Process: 18057 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 18058 (nginx)
CGroup: /system.slice/nginx.service
├─18058 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
├─18059 nginx: worker process
├─18060 nginx: worker process
├─18061 nginx: worker process
└─18062 nginx: worker process
Jun 11 17:08:50 www.a.com systemd[1]: Starting nginx - high performance web server...
Jun 11 17:08:50 www.a.com systemd[1]: Started nginx - high performance web server.
查看Nginx端口是否开启(80端口):
[root@www ~]# netstat -lnupt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 18058/nginx: master
访问默认站点成功
②yum源安装
如果要使用 yum 安装 Nginx, 先需要配置 epel yum源
阿里云:http://mirrors.aliyun.com/repo/Centos-7.repo
网易:http://mirrors.163.com/.help/CentOS7-Base-163.repo
安装yum源包:
[root@localhost ~]# wget -c http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# wget -c http://mirrors.163.com/.help/CentOS7-Base-163.repo
安装epel源:
[root@localhost ~]# yum install epel-release -y
检测yum源是否可用(是否包含nginx包):
[root@localhost ~]# yum list | grep nginx
https://mirrors.neusoft.edu.cn/epel/7/x86_64/repodata/cc77c42bfbb31c206e9670f5940f3261bea10c83879c30b60eba88ed90dac4c9-updateinfo.xml.bz2: [Errno 14] HTTPS Error 404 - Not Found
Trying other mirror.
To address this issue please refer to the below wiki article
https://wiki.centos.org/yum-errors
If above article doesn't help to resolve this issue please use https://bugs.centos.org/.
collectd-nginx.x86_64 5.8.1-1.el7 epel
munin-nginx.noarch 2.0.66-1.el7 epel
nginx.x86_64 1:1.20.1-9.el7 epel
nginx-all-modules.noarch 1:1.20.1-9.el7 epel
nginx-filesystem.noarch 1:1.20.1-9.el7 epel
nginx-mod-devel.x86_64 1:1.20.1-9.el7 epel
nginx-mod-http-image-filter.x86_64 1:1.20.1-9.el7 epel
nginx-mod-http-perl.x86_64 1:1.20.1-9.el7 epel
nginx-mod-http-xslt-filter.x86_64 1:1.20.1-9.el7 epel
nginx-mod-mail.x86_64 1:1.20.1-9.el7 epel
nginx-mod-stream.x86_64 1:1.20.1-9.el7 epel
owncloud-nginx.noarch 9.1.5-1.el7 epel
pagure-web-nginx.noarch 5.13.3-2.el7 epel
pcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates
python2-certbot-nginx.noarch 1.11.0-1.el7 epel
sympa-nginx.x86_64 6.2.68-1.el7 epel
安装Nginx:
[root@localhost ~]# yum install nginx -y
启动Nginx并检测Nginx是否开启:
[root@localhost ~]# systemctl start nginx
查看Nginx状态:
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2022-06-11 16:44:35 CST; 4s ago
Process: 1471 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 1469 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 1466 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 1473 (nginx)
CGroup: /system.slice/nginx.service
├─1473 nginx: master process /usr/sbin/nginx
├─1474 nginx: worker process
├─1475 nginx: worker process
├─1476 nginx: worker process
└─1477 nginx: worker process
Jun 11 16:44:35 localhost.localdomain systemd[1]: Starting The nginx HTTP and reverse .....
Jun 11 16:44:35 localhost.localdomain nginx[1469]: nginx: the configuration file /etc/n...k
Jun 11 16:44:35 localhost.localdomain nginx[1469]: nginx: configuration file /etc/nginx...l
Jun 11 16:44:35 localhost.localdomain systemd[1]: Started The nginx HTTP and reverse p...r.
Hint: Some lines were ellipsized, use -l to show in full.
查看Nginx端口是否开启(80端口):
[root@localhost ~]# netstat -lnupt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1473/nginx: master
tcp6 0 0 :::80 :::* LISTEN 1473/nginx: master
--配置文件 /etc/nginx/nginx.conf
[root@localhost ~]# ll /etc/nginx/nginx.conf
-rw-r--r-- 1 root root 2336 Oct 19 2021 /etc/nginx/nginx.conf
--站点主目录 /usr/share/nginx/html/
[root@localhost ~]# ll -d /usr/share/nginx/html/
drwxr-xr-x 3 root root 136 Jun 11 16:44 /usr/share/nginx/html/
--查看版本 /usr/sbin/nginx -v
[root@localhost ~]# /usr/sbin/nginx -v
nginx version: nginx/1.20.1
--配置文件语法检查 /usr/sbin/nginx -t
[root@localhost ~]# /usr/sbin/nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
--服务启动停止 systemctl {start|stop|restart|reload|status} nginx
注意:若需要gzip和rewrite的正则,需要zlib,zlib-devel,pcre已经安装好。
访问默认站点成功
注:由于安装途径不一样,因此默认站点不一样
2)源码安装
[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.20.1.tar.gz -C /usr/local/src
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# ll
total 0
drwxr-xr-x 8 1001 1001 158 May 25 2021 nginx-1.20.1
[root@localhost src]# cd nginx-1.20.1/
[root@localhost nginx-1.20.1]# ll
total 788
drwxr-xr-x 6 1001 1001 326 Jun 11 17:17 auto
-rw-r--r-- 1 1001 1001 311503 May 25 2021 CHANGES
-rw-r--r-- 1 1001 1001 475396 May 25 2021 CHANGES.ru
drwxr-xr-x 2 1001 1001 168 Jun 11 17:17 conf
-rwxr-xr-x 1 1001 1001 2590 May 25 2021 configure
drwxr-xr-x 4 1001 1001 72 Jun 11 17:17 contrib
drwxr-xr-x 2 1001 1001 40 Jun 11 17:17 html
-rw-r--r-- 1 1001 1001 1397 May 25 2021 LICENSE
drwxr-xr-x 2 1001 1001 21 Jun 11 17:17 man
-rw-r--r-- 1 1001 1001 49 May 25 2021 README
drwxr-xr-x 9 1001 1001 91 Jun 11 17:17 src
[root@localhost nginx-1.20.1]# groupadd -g 996 -r nginx
[root@localhost nginx-1.20.1]# useradd -g 996 -u 998 -c "Nginx web server" -d /var/lib/nginx -s /sbin/nologin nginx
[root@localhost nginx-1.20.1]# id nginx
uid=998(nginx) gid=996(nginx) groups=996(nginx)
[root@localhost nginx-1.20.1]# grep nginx /etc/group
nginx:x:996:
[root@localhost nginx-1.20.1]# grep nginx /etc/passwd
nginx:x:998:996:Nginx web server:/var/lib/nginx:/sbin/nologin
安装编译环境:
[root@localhost nginx-1.20.1]# yum install gcc gcc-c++ make -y
查看安装Nginx的参数:
#后面会做具体解释
[root@localhost nginx-1.20.1]# ./configure --help
--help print this message
--prefix=PATH set installation prefix
--sbin-path=PATH set nginx binary pathname
--modules-path=PATH set modules path
--conf-path=PATH set nginx.conf pathname
--error-log-path=PATH set error log pathname
--pid-path=PATH set nginx.pid pathname
--lock-path=PATH set nginx.lock pathname
--user=USER set non-privileged user for
worker processes
--group=GROUP set non-privileged group for
worker processes
--build=NAME set build name
--builddir=DIR set build directory
--with-select_module enable select module
--without-select_module disable select module
--with-poll_module enable poll module
--without-poll_module disable poll module
--with-threads enable thread pool support
--with-file-aio enable file AIO support
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_v2_module enable ngx_http_v2_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module
--with-http_xslt_module=dynamic enable dynamic ngx_http_xslt_module
--with-http_image_filter_module enable ngx_http_image_filter_module
--with-http_image_filter_module=dynamic
enable dynamic ngx_http_image_filter_module
--with-http_geoip_module enable ngx_http_geoip_module
--with-http_geoip_module=dynamic enable dynamic ngx_http_geoip_module
--with-http_sub_module enable ngx_http_sub_module
--with-http_dav_module enable ngx_http_dav_module
--with-http_flv_module enable ngx_http_flv_module
--with-http_mp4_module enable ngx_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 enable ngx_http_random_index_module
--with-http_secure_link_module enable ngx_http_secure_link_module
--with-http_degradation_module enable ngx_http_degradation_module
--with-http_slice_module enable ngx_http_slice_module
--with-http_stub_status_module enable ngx_http_stub_status_module
--without-http_charset_module disable ngx_http_charset_module
--without-http_gzip_module disable ngx_http_gzip_module
--without-http_ssi_module disable ngx_http_ssi_module
--without-http_userid_module disable ngx_http_userid_module
--without-http_access_module disable ngx_http_access_module
--without-http_auth_basic_module disable ngx_http_auth_basic_module
--without-http_mirror_module disable ngx_http_mirror_module
--without-http_autoindex_module disable ngx_http_autoindex_module
--without-http_geo_module disable ngx_http_geo_module
--without-http_map_module disable ngx_http_map_module
--without-http_split_clients_module disable ngx_http_split_clients_module
--without-http_referer_module disable ngx_http_referer_module
--without-http_rewrite_module disable ngx_http_rewrite_module
--without-http_proxy_module disable ngx_http_proxy_module
--without-http_fastcgi_module disable ngx_http_fastcgi_module
--without-http_uwsgi_module disable ngx_http_uwsgi_module
--without-http_scgi_module disable ngx_http_scgi_module
--without-http_grpc_module disable ngx_http_grpc_module
--without-http_memcached_module disable ngx_http_memcached_module
--without-http_limit_conn_module disable ngx_http_limit_conn_module
--without-http_limit_req_module disable ngx_http_limit_req_module
--without-http_empty_gif_module disable ngx_http_empty_gif_module
--without-http_browser_module disable ngx_http_browser_module
--without-http_upstream_hash_module
disable ngx_http_upstream_hash_module
--without-http_upstream_ip_hash_module
disable ngx_http_upstream_ip_hash_module
--without-http_upstream_least_conn_module
disable ngx_http_upstream_least_conn_module
--without-http_upstream_random_module
disable ngx_http_upstream_random_module
--without-http_upstream_keepalive_module
disable ngx_http_upstream_keepalive_module
--without-http_upstream_zone_module
disable ngx_http_upstream_zone_module
--with-http_perl_module enable ngx_http_perl_module
--with-http_perl_module=dynamic enable dynamic ngx_http_perl_module
--with-perl_modules_path=PATH set Perl modules path
--with-perl=PATH set perl binary pathname
--http-log-path=PATH set http access log pathname
--http-client-body-temp-path=PATH set path to store
http client request body temporary files
--http-proxy-temp-path=PATH set path to store
http proxy temporary files
--http-fastcgi-temp-path=PATH set path to store
http fastcgi temporary files
--http-uwsgi-temp-path=PATH set path to store
http uwsgi temporary files
--http-scgi-temp-path=PATH set path to store
http scgi temporary files
--without-http disable HTTP server
--without-http-cache disable HTTP cache
--with-mail enable POP3/IMAP4/SMTP proxy module
--with-mail=dynamic enable dynamic POP3/IMAP4/SMTP proxy module
--with-mail_ssl_module enable ngx_mail_ssl_module
--without-mail_pop3_module disable ngx_mail_pop3_module
--without-mail_imap_module disable ngx_mail_imap_module
--without-mail_smtp_module disable ngx_mail_smtp_module
--with-stream enable TCP/UDP proxy module
--with-stream=dynamic enable dynamic TCP/UDP proxy module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_realip_module enable ngx_stream_realip_module
--with-stream_geoip_module enable ngx_stream_geoip_module
--with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--without-stream_limit_conn_module disable ngx_stream_limit_conn_module
--without-stream_access_module disable ngx_stream_access_module
--without-stream_geo_module disable ngx_stream_geo_module
--without-stream_map_module disable ngx_stream_map_module
--without-stream_split_clients_module
disable ngx_stream_split_clients_module
--without-stream_return_module disable ngx_stream_return_module
--without-stream_set_module disable ngx_stream_set_module
--without-stream_upstream_hash_module
disable ngx_stream_upstream_hash_module
--without-stream_upstream_least_conn_module
disable ngx_stream_upstream_least_conn_module
--without-stream_upstream_random_module
disable ngx_stream_upstream_random_module
--without-stream_upstream_zone_module
disable ngx_stream_upstream_zone_module
--with-google_perftools_module enable ngx_google_perftools_module
--with-cpp_test_module enable ngx_cpp_test_module
--add-module=PATH enable external module
--add-dynamic-module=PATH enable dynamic external module
--with-compat dynamic modules compatibility
--with-cc=PATH set C compiler pathname
--with-cpp=PATH set C preprocessor pathname
--with-cc-opt=OPTIONS set additional C compiler options
--with-ld-opt=OPTIONS set additional linker options
--with-cpu-opt=CPU build for the specified CPU, valid values:
pentium, pentiumpro, pentium3, pentium4,
athlon, opteron, sparc32, sparc64, ppc64
--without-pcre disable PCRE library usage
--with-pcre force PCRE library usage
--with-pcre=DIR set path to PCRE library sources
--with-pcre-opt=OPTIONS set additional build options for PCRE
--with-pcre-jit build PCRE with JIT compilation support
--with-zlib=DIR set path to zlib library sources
--with-zlib-opt=OPTIONS set additional build options for zlib
--with-zlib-asm=CPU use zlib assembler sources optimized
for the specified CPU, valid values:
pentium, pentiumpro
--with-libatomic force libatomic_ops library usage
--with-libatomic=DIR set path to libatomic_ops library sources
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
--with-debug enable debug logging
安装Nginx:
安装 Nginx 前先检查并安装 Nginx 基础依赖包 pcre-devel 、openssl-devel:
1.检查是否安装基础依赖包 pcre-devel 、openssl-devel:
[root@localhost nginx-1.20.1]# rpm -qa | egrep 'pcre-devel | openssl-devel'
2.安装 Nginx 所需的 pcre 库:
[root@localhost nginx-1.20.1]# yum install pcre-devel -y
3.安装 openssl-devel 包:
[root@localhost nginx-1.20.1]# yum install openssl-devel -y
4.正式安装Nginx:
[root@localhost nginx-1.20.1]# ./configure \
> --user=nginx \
> --group=nginx \
> --prefix=/usr/local/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
pcre:pcre 的全称为 perl compatible regular expressions,中文译为 “ perl 兼容正则表达式”,官方站点为 http://www.pcre.org/
,安装 pere 库是为了使 Nginx 支持具备 URI 重写功能的 rewrite 模块,如果不安装 pere 库,则 Nginx 无法使用 rewrite 模块功能,Nginx的 rewrite 模块功能几乎是企业应用必须的。
openssl:Nginx 在使用 HTTPS 服务的时候要用到此模块,如果不安装 openssl 相关包,安装Nginx 的过程中会报错
编译:
[root@localhost nginx-1.20.1]# make
[root@localhost nginx-1.20.1]# make install
编写启动停止脚本:
[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
创建新的脚本需要重载脚本:
[root@localhost ~]# systemctl daemon-reload
启动Nginx并检测Nginx是否开启:
[root@localhost ~]# systemctl start nginx.service
查看Nginx状态:
[root@localhost ~]# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2022-06-11 17:47:54 CST; 4s ago
Docs: http://nginx.org/en/docs/
Process: 5387 ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Process: 5384 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 5388 (nginx)
CGroup: /system.slice/nginx.service
├─5388 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/...
└─5389 nginx: worker process
Jun 11 17:47:54 localhost.localdomain systemd[1]: Starting nginx - high performance we.....
Jun 11 17:47:54 localhost.localdomain nginx[5384]: nginx: the configuration file /usr/l...k
Jun 11 17:47:54 localhost.localdomain nginx[5384]: nginx: configuration file /usr/local...l
Jun 11 17:47:54 localhost.localdomain systemd[1]: Started nginx - high performance web...r.
Hint: Some lines were ellipsized, use -l to show in full.
查看Nginx端口是否开启(80端口):
[root@localhost ~]# netstat -lnupt | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5388/nginx: master
访问默认站点成功
注:由于安装途径不一样,因此默认站点不一样