Centos7 安装Nginx

第一步:安装环境

说明:在安装这些环境之前你可以先查看一下你有没有安装,有则不用再安装

rpm -qa | grep gcc

一. gcc 安装
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install -y gcc-c++

二. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

三. zlib 安装
zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

四. OpenSSL 安装
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

第二步:下载并解压

1.下载nginx   

链接:https://pan.baidu.com/s/1hnfJfsZaw8Ppn70h0blDtw       提取码:735v

2.解压

tar -zxvf nginx-1.18.0.tar.gz -C /usr/local

3.重命名

mv /usr/local/nginx-1.18.0 /usr/local/nginx

第三步:配置

cd /usr/local/nginx   进入到nginx文件夹
./configure           使用默认配置

第四步:编译安装

make && make install

 第五步:启动nginx

cd /usr/local/nginx/sbin/      进入到nginx的sbin目录
./nginx               启动nginx

根据报错信息看到我们没有文件夹及文件,新建文件夹及文件

mkdir /usr/local/nginx/logs     创建文件夹
touch /usr/local/nginx/logs/error.log      创建文件
touch /usr/local/nginx/logs/access.log     创建文件
ls /usr/local/nginx/logs       查看

1. 启动nginx

2.查看nginx进程

ps -ef | grep nginx

 3.停止nginx

./nginx -s quit:   此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:   此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
./nginx -s reload  重启nginx(不推荐此方法,推荐先停止在启动)

4.重新加载配置文件

当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用  ./nginx -s reload   不用先停止 nginx再启动 nginx 即可将配置信息在 nginx 中生效

第六步:开放80端口

  

防火墙基本命令

  1.状态

查看状态: systemctl status firewalld 

启动: systemctl start firewalld

停止: systemctl disable firewalld

禁用: systemctl stop firewalld

  2.添加或删除端口

添加     firewall-cmd --zone=public --add-port=8080/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)

重新载入   firewall-cmd --reload   

查看   firewall-cmd --query-port=8080/tcp     查看8080端口是否开启

删除    firewall-cmd  --remove-port=8080/tcp --permanent      (--permanent永久生效,没有此参数重启后失效)

  6.1.我们先查看一下防火墙是否开启

systemctl status firewalld 

这样是关闭状态,如果出现running则代表防火墙开启

6.2.启动防火墙(已开启跳过此步骤)

systemctl start firewalld

 没有弹出信息则代表命令执行成功,我们可以在查看一下防火墙的状态

启动成功

9.3.查看端口8080是否开启

firewall-cmd --list-ports   查看所有端口,没有则代表没有开放任何一个端口   也可以通过   

firewall-cmd --query-port=8080/tcp  来精确查找8080端口是否开启

 9.4.开启8080端口   firewall-cmd --zone=public --add-port=8080/tcp --permanent

!!!!!  我们这里已经成功执行了命令查看端口还是关闭状态,所以我们要重新启动防火墙    !!!!!

firewall-cmd --reload   

端口成功开启

 

posted @ 2022-04-13 22:05  阿卡ギ  阅读(35)  评论(0编辑  收藏  举报