Nginx 学习

Nginx 学习,第1张

Nginx 学习(一).Linux 安装Nginx & 高可用Nginx 1. 简介

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。 [12]

其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布

2. 主流版本
  • Nginx 开源版本 : http://nginx.org/ 原始版本

  • Nginx Plus 商业版本 : https://www.nginx.com/ F5 产商收购nginx后添加了功能

  • Openresty : https://openresty.org/cn/ OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项

  • Tengine : http://tengine.taobao.org/ Tengine是由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性

3. 安装

http://nginx.org/ 下载 我这里是最新版本 nginx 1.21.6

3.1 configure

校验Linux环境 缺啥安装啥

[root@localhost nginx-1.21.6]# ./configure --prefix=/package/nginx (带安装目录)
checking for OS
 + Linux 3.10.0-862.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found
3.2 安装缺少的依赖
[root@localhost nginx-1.21.6]# yum install -y gcc

##再次校验依赖 出现的问题还有一下几个 
[root@localhost nginx-1.21.6]# ./configure --prefix=/package/nginx
.......
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
###解决  该依赖是解决正则表达式
[root@localhost nginx-1.21.6]# yum install -y pcre pcre-devel
##################################

[root@localhost nginx-1.21.6]# ./configure --prefix=/package/nginx  
.......
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
###解决  该依赖是解决 HTTP 请求包中的内容做gzip 的压缩 
[root@localhost nginx-1.21.6]# yum install -y zlib zlib-devel

##### 可选项目  如果需要https 的话还需要安装 openssl
[root@localhost nginx-1.21.6]# yum install -y openssl  openssl-devel
3.3 编译&安装
[root@localhost nginx-1.21.6]# make

[root@localhost nginx-1.21.6]# make install
4. 启动
##开启nginx
[root@localhost sbin]# ./nginx  

###停止nginx
[root@localhost sbin]# ./nginx -s  stop

## 优雅退出 已接收的工作完成
[root@localhost sbin]# ./nginx -s quit 

###重新加载配置文件
[root@localhost sbin]# ./nginx  -s reload  

注意:外部访问的话最好开启外部80端口的访问

firewall-cmd --zone=public --add-port=80/tcp --permanent
5. 安装成系统服务
##  文件目录
vim /usr/lib/systemd/system/nginx.service

## 重新加载配置文件、
systemctl daemon-reload

注:–prefix=/package/nginx (带安装目录) 就是下面文件的前缀 ,如果前面不是这个,下面文件就要做相对应的替换;

[Unit] Description=nginx - web server 
After=network.target remote-fs.target nss-lookup.target 

[Service] 
Type=forking 
PIDFile=/package/nginx/logs/nginx.pid 
ExecStartPre=/package/nginx/sbin/nginx -t -c /package/nginx/conf/nginx.conf 
ExecStart=/package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf 
ExecReload=/package/nginx/sbin/nginx -s reload 
ExecStop=/package/nginx/sbin/nginx -s stop 
ExecQuit=/package/nginx/sbin/nginx -s quit 
PrivateTmp=true 

[Install] 
WantedBy=multi-user.target

重新加载系统服务

[root@localhost system]# systemctl daemon-reload

##启动 并查看状态
[root@localhost system]# systemctl start nginx
[root@localhost system]# systemctl status  nginx
● nginx.service - nginx - web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 三 2022-04-27 15:16:41 CST; 5s ago
  Process: 14911 ExecStart=/package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
  Process: 14909 ExecStartPre=/package/nginx/sbin/nginx -t -c /package/nginx/conf/nginx.conf (code=exited, status=0/SUCCESS)
 Main PID: 14913 (nginx)
   CGroup: /system.slice/nginx.service
           ├─14913 nginx: master process /package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf
           └─14914 nginx: worker process

6. 高可用安装

KeepAlived + Nginx 配置HA 高可用

详细安装看 Nginx 学习(三).Nginx 实践 最后一节

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/872495.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存