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
卸載完畢!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM