Nginx介绍 安装

😹 作者: gh-xiaohe
😻 gh-xiaohe的博客
😽 觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!!

🚏 Nginx介绍 安装

🚀 Nginx版本区别

常用版本分为四大阵营

🚄 Nginx概念

🚬 Nginx介绍

    Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上Nginx的并发能力在同类型的网页服务器表现较好,中国大陆使用Nginx的网站有:百度、京东、新浪、网易、腾讯、淘宝等

    Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

🚬 Nginx的特点

  • 稳定性能强。7*24 小时不间断运行
  • Nginx提供了非常丰富的配置实例
  • 占用内存小并发能力强 50,000个并发 tomcat 1500

🚒 Nginx安装

🚬 Window安装

🚭 1、下载nginx

下载稳定版本即可:http://nginx.org/en/download.html

🚭 2、解压

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KQEPIFWB-1662733434136)(Nginx.assets/image-20220909221315948.png)]

🚭 3、启动
  • 直接双击nginx.exe,双击后一个黑色的弹窗一闪而过

  • 打开cmd命令窗口,切换到nginx解压目录下,输入命令nginx.exe ,回车即可

🚭 4、检查是否启动成功

直接在浏览器地址栏输入网址 http://localhost:80 回车,出现以下页面说明启动成功!

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-waGgwWLr-1662733434139)(Nginx.assets/image-20220909221635932.png)]

🚭 5、关闭nginx

    如果使用cmd命令窗口启动nginx, 关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx

  • 输入nginx命令 nginx -s stop(快速停止nginx) 或 nginx -s quit(完整有序的停止nginx)
  • 使用taskkill taskkill /f /t /im nginx.exe
    • taskkill是用来终止进程的,
    • /f是强制终止
    • /t终止指定的进程和任何由此启动的子进程。
    • /im示指定的进程名称

🚬 Linux安装

🚭 1、安装依赖包
[root@localhost /]# yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
🚭 2、下载Nginx安装包wget
# 下载wget
[root@localhost /]# yum install wget
[root@localhost /]# wget https://nginx.org/download/nginx-1.16.1.tar.gz # 下载的网络链接地址
🚭 3、解压
[root@localhost /]# tar -zxvf nginx-1.16.1.tar.gz
🚭 4、安装到指定目录
[root@localhost /]# mkdir -p /usr/local/nginx
[root@localhost /]# cd nginx-1.16.1
[root@localhost /]# ./configure --prefix=/usr/local/nginx
🚭 5、编译 安装
[root@localhost /]# cd nginx-1.16.1
[root@localhost /]# make && make install # 要在nginx目录下
🚭 6、树形展示结构
[root@localhost /]# yum install tree 安装树形结构
[root@localhost nginx]# tree
.
├── client_body_temp
├── conf
│   ├── fastcgi.conf
│   ├── fastcgi.conf.default
│   ├── fastcgi_params
│   ├── fastcgi_params.default
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── mime.types.default
│   ├── nginx.conf
│   ├── nginx.conf.default
│   ├── scgi_params
│   ├── scgi_params.default
│   ├── uwsgi_params
│   ├── uwsgi_params.default
│   └── win-utf
├── fastcgi_temp
├── html
│   ├── 50x.html
│   └── index.html
├── logs
│   ├── access.log
│   └── error.log
├── proxy_temp
├── sbin
│   └── nginx
├── scgi_temp
└── uwsgi_temp

9 directories, 20 files
[root@localhost nginx]# 

🚬 Nginx的目录结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bAVkVSB2-1662732524801)(Nginx.assets/image-20220909220753834.png)]

🚤 Nginx命令

🚬 查看Nginx版本命令:

[root@localhost nginx]# cd sbin/
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -v # 在nginx的sbin目录下执行
nginx version: nginx/1.22.0

🚬 检查一下nginx.conf文件配置的是否有错误

在启动Nginx服务之前,可以先检查一下conf/nginx.conf文件配置的是否有错误。

[root@localhost nginx]# cd sbin/
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t # 在nginx的sbin目录下执行
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

🚬 启动Nginx服务命令:

[root@localhost sbin]# pwd
/usr/local/nginx/sbin   
[root@localhost sbin]# ./nginx # 在nginx的sbin目录下执行

🚬 停止Nginx服务命令:

[root@localhost sbin]# pwd
/usr/local/nginx/sbin   
[root@localhost sbin]# ./nginx -s stop # 在nginx的sbin目录下执行

🚬 查看Nginx进程:

[root@localhost sbin]# ps -ef|grep nginx
# 两个进程 master 和 worker 第三个进程是执行ps -ef|grep nginx的进程可以忽略
# 默认两个进程 worker进程可以有多个 修改配置文件 nginx.conf
root      33011      1  0 18:37 ?        00:00:00 nginx: master process ./nginx
nobody    33012  33011  0 18:37 ?        00:00:00 nginx: worker process 
root      33063  27845  0 18:38 pts/1    00:00:00 grep --color=auto nginx

🚬 当修改Nginx配置文件后,重新加载配置文件:

[root@localhost sbin]# pwd
/usr/local/nginx/sbin   
[root@localhost sbin]# ./nginx -s reload # 在nginx的sbin目录下执行

🚗 配置Nginx环境变量(重)

在哪个目录都可以使用nginx命令

[root@localhost sbin]# vim /etc/profile

PATH=/usr/local/nginx/sbin:$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
[root@localhost /]# source /etc/profile  # 重新加载

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HYIoYP61-1662732524802)(Nginx.assets/image-20220909101251547-166273174758911.png)]

🚲 将Nginx变成系统服务(重)

🚬 创建服务脚本

vim /usr/lib/systemd/system/nginx.service

🚬 脚本内容

[Unit]
Description=nginx - web server
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=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

🚬 重新加载系统服务

[root@localhost /]# systemctl daemon-reload

🚬 启动服务

[root@localhost /]# systemctl start nginx.service

🚬 查看服务

[root@localhost /]# systemctl status nginx.service

🚬 查看进程

[root@localhost /]# ps -ef | grep nginx

🚬 开机启动

[root@localhost /]# systemctl enable nginx.service 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gh-xiaohe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值