linux之nginx1.21环境安装和配置

## 1. nginx 1.21

1.1. 安装

官网上,下载 nginx-1.21.1

image-20210720200312465

image-20210720200602063

上传到 linux 中,在 /usr/local 下

image-20210720200830810

进入到 linux 中,到 /usr/local 下
# 切换到 /usr/local 下
[root@localhost ~]#  cd /usr/local

# 解压 nginx-1.21.0.tar.gz,别重命名为 nginx
[root@localhost local]# tar -zxvf nginx-1.21.0.tar.gz

# 安装 nginx 环境依赖支持
[root@localhost local]# yum -y install pcre-devel openssl openssl-devel

# 进入到 nginx-1.21.0 里面
[root@localhost local]# cd nginx-1.21.0
[root@localhost nginx-1.21.0]# ./configure

# 执行安装命令
[root@localhost nginx-1.21.0]# make install
执行完 make install 成功后,会在 nginx-1.21.0 的同级目录下,生成 nginx 就是最终程序

image-20210721192530393

配置环境变量
# 配置 nginx 环境变量
[root@localhost local]# vim /etc/profile

image-20210721192906586

刷新环境配置
# 刷新配置
[root@localhost local]# source /etc/profile
开启 nginx 服务
# 因为配置了环境变量,在哪里开启,关闭,重启都可以
# 开启服务
[root@localhost local]# nginx

# 停止服务
[root@localhost local]# nginx -s stop

# 重启服务
[root@localhost local]# nginx -s reload

image-20210721194430927

配置反射代理
# 配置反射代理
[root@localhost local]# vim /usr/local/nginx/conf/nginx.conf
配置内容
# 配置信息 主机ip地址:端口号
proxy_pass http://192.168.190.128:8080;

# 配置完后,一定要重启 nginx 服务
[root@localhost local]# nginx -s reload

image-20210721193314285

最终效果

image-20210721194947819

拒绝访问
# 二选一
# 第一种解决方案,简单高效,关闭防火墙
[root@localhost local]# systemctl stop firewalld.service

# 第二种解决方案,开放对应程序的端口号
# 查看程序 master 的 pid
[root@localhost local]# ps aux | grep nginx
root      22891  0.0  0.0  20712  1356 ?        Ss   03:55   0:00 nginx: master process nginx
nobody    25369  0.0  0.0  23220  1744 ?        S    04:01   0:00 nginx: worker process
root      43247  0.0  0.0 112808   964 pts/1    S+   04:50   0:00 grep --color=auto nginx

# 通过上面,可以看到程序的 master 的 pid 是 22891
# 通过 pid 查看占用端口
[root@localhost local]# netstat -anp | grep 22891
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      22891/nginx: master
unix  3      [ ]         STREAM     CONNECTED     68105    22891/nginx: master
unix  3      [ ]         STREAM     CONNECTED     68104    22891/nginx: master

# 找到 tcp 里面包含的端口,通过上面信息可以看到是 80,那就打开80 端口访问
[root@localhost local]# firewall-cmd --add-port=80/tcp --permanent

# 最后再重载生效刚才的端口设置
[root@localhost local]# firewall-cmd --reload
安装完毕!

1.2. 卸载

# 查看是否安装 nginx
[root@localhost local]# rpm -qa | grep nginx
[root@localhost local]# find / -name nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx


# 关闭 nginx 服务
[root@localhost local]# nginx -s stop

# 删除或卸载
[root@localhost local]#  rm -rf /usr/local/nginx
卸载完毕!
posted @ 2021-07-21 20:08  陈可儿  阅读(4281)  评论(0编辑  收藏  举报