一、Nginx-Web快速入门
1、Nginx基本简述
Nginx是一个开源项目且高性能、可靠的Http Web服务、代理服务。
1、开源:可直接获取源代码
2、高性能:支持海量并发
3、可靠:服务稳定
2、我们为什么选择Nginx服务
(1).Nginx非常轻量
1、功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
2、代码模块化(易读、便于二次开发,对于开发人员非常友好)
(2).互联网公司都选择Nginx
1、Nginx技术成熟,具备的功能是企业最常用而且最需要的。
2、适合当前主流架构趋势,微服务、云架构、中间层。
3、同一技术栈,降低维护成本,降低技术更新成本。
(3).Nginx采用Epool网络模型,Apache采用Select模型
Select:当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
Epool:当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。
3、Nginx应用场景
二、Nginx常见两种安装方式
1、yum安装(使用官方源)
Nginx yum官方源
#安装先决条件:
[root@Server-1 ~]# sudo yum install yum-utils
#要设置 yum 存储库,请创建名为 /etc/yum.repos.d/nginx.repo 以下内容的文件:
[root@Server-1 ~]# vim /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 包的存储库。如果您想使用主线 nginx 包,请运行以下命令:
[root@Server-1 ~]#sudo yum-config-manager --enable nginx-mainline
要安装 nginx,请运行以下命令:
[root@Server-1 ~]#sudo yum install nginx
当提示接受 GPG 密钥时,验证指纹是否匹配 573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62,如果匹配,则接受它。
#查看版本
[root@Server-1 ~]# nginx -v
nginx version: nginx/1.20.2

2、源码编译安装
Nginx tar包 官方源
[root@Server-1 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@Server-1 ~]# tar xf nginx-1.14.2.tar.gz
[root@Server-1 ~]# cd nginx-1.14.2/
[root@Server-1 nginx-1.14.2]# ./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-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@Server-1 nginx-1.14.2]# make && make install
如果在公司,有人叫你根据现有的Nginx,搭建一个Nginx的测试环境。在公司现有Nginx的服务器上,使用nginx -V查看配置
[root@Server-1 ~]# nginx -V
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --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-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@Server-1 ~]#
然后在测试环境使用源码安装 ,抄之前的 configure arguments 即可。
checking for openat(), fstatat() … found checking for getaddrinfo() … found checking for PCRE library … found checking for PCRE JIT support … found checking for OpenSSL library … found checking for zlib library … found creating objs/Makefile
Configuration summary + using threads + using system PCRE library + using system OpenSSL library + using system zlib library
nginx path prefix: “/etc/nginx” nginx binary file: “/usr/sbin/nginx” nginx modules path: “/usr/lib64/nginx/modules” nginx configuration prefix: “/etc/nginx” nginx configuration file: “/etc/nginx/nginx.conf” nginx pid file: “/var/run/nginx.pid” nginx error log file: “/var/log/nginx/error.log” nginx http access log file: “/var/log/nginx/access.log” nginx http client request body temporary files: “/var/cache/nginx/client_temp” nginx http proxy temporary files: “/var/cache/nginx/proxy_temp” nginx http fastcgi temporary files: “/var/cache/nginx/fastcgi_temp” nginx http uwsgi temporary files: “/var/cache/nginx/uwsgi_temp” nginx http scgi temporary files: “/var/cache/nginx/scgi_temp”
./configure出现以上内容,即check没问题
[root@Server-1 nginx-1.14.2]# make && make install
使用echo $? 返回0则没出错
[root@Server-1 nginx-1.14.2]# echo $?
0
[root@Server-1 nginx-1.14.2]#
三、Nginx-目录结构
[root@Server-1]# tree /usr/local/nginx/
/usr/local/nginx/
├── client_body_temp
├── conf #存放一系列配置文件的目录
│ ├── fastcgi.conf #fastcgi程序相关配置文件
│ ├── fastcgi.conf.default #fastcgi程序相关配置文件备份
│ ├── fastcgi_params #fastcgi程序参数文件
│ ├── fastcgi_params.default #fastcgi程序参数文件备份
│ ├── koi-utf #编码映射文件
│ ├── koi-win #编码映射文件
│ ├── mime.types #媒体类型控制文件
│ ├── mime.types.default #媒体类型控制文件备份
│ ├── nginx.conf #主配置文件
│ ├── nginx.conf.default #主配置文件备份
│ ├── scgi_params #scgi程序相关配置文件
│ ├── scgi_params.default #scgi程序相关配置文件备份
│ ├── uwsgi_params #uwsgi程序相关配置文件
│ ├── uwsgi_params.default #uwsgi程序相关配置文件备份
│ └── win-utf #编码映射文件
├── fastcgi_temp #存放fastcgi程序临时文件
├── html #存放网页文档
│ ├── 50x.html #错误页码显示网页文件
│ └── index.html #网页的首页文件
├── logs #存放nginx的日志文件
│ ├── access.log #默认访问日志
│ ├── error.log #错误日志
│ └── nginx.pid #nginx pid文件
├── proxy_temp #代理相关临时文件
├── sbin #存放启动程序
│ └── nginx #nginx启动程序
├── scgi_temp #存放scgi程序临时文件
└── uwsgi_temp #存放uwsgi程序临时文件
9 directories, 21 files
四、Nginx-启动、停止方式
#如有http 先关闭之前已启动的httpd服务
[root@Server-1 ~]# systemctl stop httpd
[root@Server-1 ~]# systemctl disable httpd
然后启动Nginx服务
[root@Server-1 ~]# systemctl enable nginx
[root@Server-1 ~]# systemctl start nginx
启动方式有两种:不能混合着用
nginx 启动
nginx -s stop 停止
nginx -s reload |restart 重载服务
systemctl start nginx
systemctl restart nginx
systemctl stop nginx
启动之后访问nginx

原文始发于微信公众号(运维库):01-Nginx安装
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/64337.html