nginx详细安装教程

一、简介

1-1 什么是 nginx

Nginx 是高性能的 HTTP 和反向代理的web服务器,处理高并发能力是十分强大的。
Nginx 可以作为静态页面的 web 服务器,同时还支持 CGI 协议的动态语言,比如 perl、php 等。但是不支持 java。Java 程序只能通过与 tomcat 配合完成。Nginx 专为性能优化而开发

二、安装教程

2-1 关闭防火墙

systemctl status firewalld.service(查看防火墙状态)
systemctl stop firewalld.service (关闭防火墙)
systemctl start firewalld.service (开启防火墙)
systemctl disable firewalld.service (禁止防火墙自启动)
systemctl enable firewalld.service (防火墙随系统开启启动)

2-2 安装依赖环境

一次性安装4个插件

yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

一次安装如果有问题,可以试一下分开安装(上面命令执行成功了就就无需执行以下命令了)

yum install gcc-c++

# pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
yum install -y pcre pcre-devel

# zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
yum install -y zlib zlib-devel
 
# penssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔,所以需要安装
yum install -y openssl openssl-devel

2-3 安装 nginx

上传到目录 /usr/nginx里面

或者联网直接下载:

wget -c https://nginx.org/download/nginx-1.15.10.tar.gz

解压文件

tar -zxvf nginx-1.15.10.tar.gz

使用默认配置

cd nginx-1.15.10
[root@hostM1 nginx-1.15.10]# ./configure

2-4 编译和安装

make   # 编译
make install # 安装

查看安装路径

whereis nginx

2-5 nginx使用

cd /usr/local/nginx

有四个目录
conf 配置目录
html 默认的代理目录
logs 日志目录
sbin 二进制文件目录(这个目录中往往放的是软件的启动命令)

2-6 启动

cd   /usr/local/nginx/sbin/
./nginx

直接验证Nginx启动成功及关闭Nginx的命令

ps -ef |grep nginx #显示nginx进程
./nginx -s stop    #关闭nginx

在这里插入图片描述

关闭及动态加载配置文件的命令如下

./nginx -s stop            # 关闭
./nginx -s reload         # 动态加载配置文件
./nginx -s quit            #  此方式停止步骤是待nginx进程处理任务完毕进行停止。

2-7 访问nginx

使用IP进行访问:192.168.67.34
在这里插入图片描述
此时nginx就安装成功了

2-8 设置Nginx开机自启

添加nginx.service文件

vim /lib/systemd/system/nginx.service

文件内容

在复制的时候按下a如下面显示的才行要不然[Unit]复制不上
在这里插入图片描述

[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
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
PrivateTmp=true
[Install]
WantedBy=multi-user.target

设置开机启动

配置之后需要使文件生效
systemctl daemon-reload
退出并保存文件,执行systemctl enable nginx.service使nginx开机启动
#开机自启动
systemctl enable nginx.service
#停止开机自启动
systemctl disable nginx.service
#查询当前状态
systemctl status nginx.service
#启动服务
systemctl start nginx.service
#重新启动服务
systemctl restart nginx.service
#停止服务
systemctl stop nginx.service
#重新加载配置
systemctl reload nginx.service
  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员小王꧔ꦿ

感谢打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值