Linux——Shell脚本和Nginx反向代理服务器

1. Linux中的shell脚本【了解】

1.1 什么是shell

Shell是一个用C语言编写的程序,它是用户使用Linux的桥梁

Shell 既是一种命令语言,有是一种程序设计语言

Shell是指一种应用程序,这个应用程序提供了一个界面,用户通过这个界面访问操作系统内核的服务

Ken Thompson 的 sh 是第一种 Unix Shell ,Windows Explorer 是一个典型的图形界面Shell
shell就是用户与linux交换的一款语言

1.2 后缀

.sh linux系统脚本的后缀

.bat windows系统脚本的后缀

1.3 编写第一个shell

shell 复制代码
#!/bin/bash
# echo 表示输出
echo "hello world"

#!/bin/bash 头文件
运行该脚本: ./路径/脚本名.sh

使用 ls -l命令可以查看该文件的权限

第一个字符:- :表示文件 d:表示目录 l:快捷方式

后面的字符三个为一组

rw-:这三个字符,表示当前创建文件的用户具有的权限

r--:这三个字符,表示当前创建该文件的用户所在的组成员具有的权限

r--:这三个字符,表示其他用户具有的权限

r:read 读 数字4

w:write 写 数字2

x:execute 执行 数字1
修改权限:

chmod u+rwx g+rwx o+rwx 文件名【增加权限】

chmod u-rwx g-rwx o-rwx 文件名【减权限】

我们也可以通过数字修改权限:chmod 735 文件

1.4 变量 弱语言

shell 复制代码
#!/bin/bash
name="lay"
echo "姓名:${name}"
echo "姓名:"${name}

1.5 Shell 传递参数

执行shell脚本时可以传递参数值

shell 复制代码
#!/bin/bash
name="lay"
echo "姓名:${name}"
echo "第一个参数值:$1"
echo "第二个参数值:$2"

执行脚本: ./脚本名 传递的参数值 值2

即使不传值也不会报错,没有下标越界的错误提示

1.6 Shell数组

数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与PHP相似)

与大部分编译语言类似,数组元素的下标由0开始

Shell数组用 括号来表示,元素用 空格符号分隔开,语法格式如下:

array_name=(value1 value2 .... valuen)

关联数组--map集合

Bash支持关联数组,可以使用任意的字符串、或者整数作为下标来访问数组元素

关联数组使用declare命令来声明,语法格式如下:

declare -A site=(["google"]="www.google.com" ["runoob"]="www.runoob.com" ["taobao"]="www.taobao.com")

-A 选项就是用于声明一个关联数组

关联数组的键是唯一的

1.7 Shell 基本运算符

算术运算符

原生Bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk和expr。expr最常用

expr 是一款表达式计算工具,使用它能完成表达式的求值操作。

例如,两个数相加(注意使用的是反引号 *`* 而不是单引号 *'*):

shell 复制代码
#!/bin/bash
a=100
b=20
c=`expr $a / $b`
echo "c==$c"

关系运算符

shell 复制代码
a=10
b=20
#
if [ $a -eq $b ]
then
   echo "$a -eq $b : a 等于 b"
else
   echo "$a -eq $b: a 不等于 b"
fi

等于:eq

不等于:ne

大于:gt

大于等于:ge

小于:lt

小于等于:le

布尔运算符

shell 复制代码
if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a 小于 100 且 $b 大于 15 : 返回 true"
else
   echo "$a 小于 100 且 $b 大于 15 : 返回 false"
fi

if

then

else

fi:结束

1.8 控制语句

if语句 for语句

shell 复制代码
a=10
b=20
if [ $a -eq $b ]
then
   echo "a 等于 b"
elif [ $a -gt $b ]
then
   echo "a 大于 b"
elif [ $a -lt $b ]
then
   echo "a 小于 b"
else
   echo "没有符合的条件"
fi

for

shell 复制代码
for var in item1 item2 ... itemN
do
    command1
    command2
    ...
    commandN
done

2. Nginx反向代理服务器

2.1 什么是nginx

Nginx 是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3 )代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx 的并发能力在同类型的网页服务器中表现较好。并发能力: 50,000 C语言编写的

2.2 为什么使用nginx

2.3 有哪些企业使用nginx

京东 淘宝 12306 新浪等

2.4 安装nginx

nginx可以独立安装在一台服务器,也可以和项目在同一个服务器

1. 安装nginx的依赖插件

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

下载nginx

源码:先编译------>后安装

下载地址:https://nginx.org/en/download.html

创建一个目录作为nginx的安装路径

mkdir /usr/nginx

将下载的nginx压缩包拖拽到/usr/app目录下

解压

tar -zxvf nginx-1.26.1.tar.gz

进入解压后的目录

cd nginx-1.26.1

指定nginx的安装路径

 ./configure --prefix=/usr/nginx

编译和安装nginx

make install

nginx目录结构

启动nginx

nginx  启动
nginx -s stop  关闭
nginx -s reload 重新加载配置文件

防火墙放行80端口

 firewall-cmd --add-port=80/tcp --zone=public --premanent

重启防火墙

 systemctl restart firewalld

访问nginx80

http://nginx所在的ip:nginx的端口/

2.5 nginx配置文件修改

切换到/usr/nginx/conf目录下,打开nginx.conf配置文件进行修改

c++ 复制代码
#user  nobody; 
#工作的线程数
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    # 每个工作对象允许的连接数
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
   #模仿配置
    server {
       listen 81;
       server_name localhost;
       location /{
           root static;
           index main.html;
          
       }
    }

    #gzip  on;
    server {
        listen       80; # 监听的端口号
        server_name  localhost; # 监听的主机名.域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        # 资源/ 
        location / {
            root   html; #根目录
            index  index.html main.html; # 资源
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}
#模仿配置
    server {
       listen 81;
       server_name localhost;
       location /{
           root static;
           index main.html;
          
       }
    }

listen:监听的端口号,需要防火墙放行该端口号

server_name: 监听的主机名.域名

location /{} :资源

location /{

​ root static; //根目录,需要在nginx中创建根目录static

​ index main.html; //资源,加载该资源

}

  • 重新加载

    ../sbin/nginx -s reload

相关推荐
Hqst_Kevin10 分钟前
Hqst 品牌 H81801D 千兆 DIP 网络变压器在光猫收发器机顶盒中的应用
运维·服务器·网络·5g·网络安全·信息与通信·信号处理
DREAM依旧19 分钟前
《深入了解 Linux 操作系统》
linux
阿赭ochre44 分钟前
Linux环境变量&&进程地址空间
linux·服务器
honey ball44 分钟前
仪表放大器AD620
运维·单片机·嵌入式硬件·物联网·学习
Iceberg_wWzZ44 分钟前
数据结构(Day14)
linux·c语言·数据结构·算法
秋已杰爱1 小时前
进程间关系与进程守护
运维·服务器
可儿·四系桜1 小时前
如何在多台Linux虚拟机上安装和配置Zookeeper集群
linux·服务器·zookeeper
Flying_Fish_roe1 小时前
linux-软件包管理-包管理工具(Debian 系)
linux·运维·debian
BLEACH-heiqiyihu1 小时前
红帽9中nginx-源码编译php
运维·nginx·php
大广-全栈开发2 小时前
centos 7 安装gitlab
linux·git·centos