😹 作者: gh-xiaohe
😻 gh-xiaohe的博客
😽 觉得博主文章写的不错的话,希望大家三连(✌关注,✌点赞,✌评论),多多支持一下!!!
常用版本分为四大阵营
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。其特点是占有内存少,并发能力强,事实上Nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用Nginx的网站有:百度、京东、新浪、网易、腾讯、淘宝等。
Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
下载稳定版本即可:http://nginx.org/en/download.html
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KQEPIFWB-1662733434136)(Nginx.assets/image-20220909221315948.png)]](https://1000bd.com/contentImg/2023/11/05/145506323.png)
直接双击nginx.exe,双击后一个黑色的弹窗一闪而过
打开cmd命令窗口,切换到nginx解压目录下,输入命令nginx.exe ,回车即可
直接在浏览器地址栏输入网址 http://localhost:80 回车,出现以下页面说明启动成功!
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-waGgwWLr-1662733434139)(Nginx.assets/image-20220909221635932.png)]](https://1000bd.com/contentImg/2023/11/05/145506316.png)
如果使用cmd命令窗口启动nginx, 关闭cmd窗口是不能结束nginx进程的,可使用两种方法关闭nginx
[root@localhost /]# yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
# 下载wget
[root@localhost /]# yum install wget
[root@localhost /]# wget https://nginx.org/download/nginx-1.16.1.tar.gz # 下载的网络链接地址
[root@localhost /]# tar -zxvf nginx-1.16.1.tar.gz
[root@localhost /]# mkdir -p /usr/local/nginx
[root@localhost /]# cd nginx-1.16.1
[root@localhost /]# ./configure --prefix=/usr/local/nginx
[root@localhost /]# cd nginx-1.16.1
[root@localhost /]# make && make install # 要在nginx目录下
[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]#
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bAVkVSB2-1662732524801)(Nginx.assets/image-20220909220753834.png)]](https://1000bd.com/contentImg/2023/11/05/145506348.png)
[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文件配置的是否有错误。
[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
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx # 在nginx的sbin目录下执行
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -s stop # 在nginx的sbin目录下执行
[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
[root@localhost sbin]# pwd
/usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -s reload # 在nginx的sbin目录下执行
在哪个目录都可以使用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)]](https://1000bd.com/contentImg/2023/11/05/145506408.png)
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