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

打印 上一主题 下一主题

主题 949|帖子 949|积分 2847

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

  1. #!/bin/bash
  2. # echo 表示输出
  3. 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 变量 弱语言

  1. #!/bin/bash
  2. name="lay"
  3. echo "姓名:${name}"
  4. echo "姓名:"${name}
复制代码
1.5 Shell 通报参数

   执行shell脚本时可以通报参数值
  1. #!/bin/bash
  2. name="lay"
  3. echo "姓名:${name}"
  4. echo "第一个参数值:$1"
  5. echo "第二个参数值:$2"
复制代码
  执行脚本: ./脚本名 通报的参数值 值2
  即使不传值也不会报错,没有下标越界的错误提示
  1.6 Shell数组

数组中可以存放多个值。Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与PHP相似)
与大部门编译语言雷同,数组元素的下标由0开始
Shell数组用 括号来表现,元素用 空格符号分隔开,语法格式如下:
  1. array_name=(value1 value2 .... valuen)
复制代码
关联数组–map聚集
Bash支持关联数组,可以利用任意的字符串、大概整数作为下标来访问数组元素
关联数组利用declare命令来声明,语法格式如下:
  1. declare -A site=(["google"]="www.google.com" ["runoob"]="www.runoob.com" ["taobao"]="www.taobao.com")
复制代码
-A 选项就是用于声明一个关联数组
关联数组的键是唯一的
1.7 Shell 根本运算符

算术运算符
原生Bash不支持简朴的数学运算,但是可以通过其他命令来实现,例如 awk和expr。expr最常用
expr 是一款表达式盘算工具,利用它能完成表达式的求值操作。
例如,两个数相加(留意利用的是反引号 *`* 而不是单引号 *'*):
  1. #!/bin/bash
  2. a=100
  3. b=20
  4. c=`expr $a / $b`
  5. echo "c==$c"
复制代码
关系运算符
  1. a=10
  2. b=20
  3. #
  4. if [ $a -eq $b ]
  5. then
  6.    echo "$a -eq $b : a 等于 b"
  7. else
  8.    echo "$a -eq $b: a 不等于 b"
  9. fi
复制代码
  等于:eq
  不等于:ne
  大于:gt
  大于等于:ge
  小于:lt
  小于等于:le
  布尔运算符
  1. if [ $a -lt 100 -a $b -gt 15 ]
  2. then
  3.    echo "$a 小于 100 且 $b 大于 15 : 返回 true"
  4. else
  5.    echo "$a 小于 100 且 $b 大于 15 : 返回 false"
  6. fi
复制代码
  if
  then
  else
  fi:竣事
  1.8 控制语句

   if语句 for语句
  1. a=10
  2. b=20
  3. if [ $a -eq $b ]
  4. then
  5.    echo "a 等于 b"
  6. elif [ $a -gt $b ]
  7. then
  8.    echo "a 大于 b"
  9. elif [ $a -lt $b ]
  10. then
  11.    echo "a 小于 b"
  12. else
  13.    echo "没有符合的条件"
  14. fi
复制代码
for
  1. for var in item1 item2 ... itemN
  2. do
  3.     command1
  4.     command2
  5.     ...
  6.     commandN
  7. done
复制代码
2. Nginx反向代理服务器

2.1 什么是nginx

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


2.3 有哪些企业利用nginx

  1. 京东 淘宝 12306 新浪等
复制代码
2.4 安装nginx

   nginx可以独立安装在一台服务器,也可以和项目在同一个服务器
  1. 安装nginx的依靠插件
  1. yum  install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
复制代码
下载nginx
   源码:先编译——>后安装
下载所在:https://nginx.org/en/download.html
  

创建一个目录作为nginx的安装路径
  1. mkdir /usr/nginx
复制代码
将下载的nginx压缩包拖拽到/usr/app目录下
解压
  1. tar -zxvf nginx-1.26.1.tar.gz
复制代码
进入解压后的目录
  1. cd nginx-1.26.1
复制代码
指定nginx的安装路径
  1. ./configure --prefix=/usr/nginx
复制代码
编译和安装nginx
  1. make install
复制代码
nginx目录结构

启动nginx
  1. nginx  启动
  2. nginx -s stop  关闭
  3. nginx -s reload 重新加载配置文件
复制代码

防火墙放行80端口
  1. firewall-cmd --add-port=80/tcp --zone=public --premanent
复制代码
重启防火墙
  1. systemctl restart firewalld
复制代码
访问nginx80
  1. http://nginx所在的ip:nginx的端口/
复制代码

2.5 nginx配置文件修改

切换到/usr/nginx/conf目录下,打开nginx.conf配置文件进行修改
  1. #user  nobody;
  2. #工作的线程数
  3. worker_processes  1;
  4. #error_log  logs/error.log;
  5. #error_log  logs/error.log  notice;
  6. #error_log  logs/error.log  info;
  7. #pid        logs/nginx.pid;
  8. events {
  9.     # 每个工作对象允许的连接数
  10.     worker_connections  1024;
  11. }
  12. http {
  13.     include       mime.types;
  14.     default_type  application/octet-stream;
  15.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  16.     #                  '$status $body_bytes_sent "$http_referer" '
  17.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  18.     #access_log  logs/access.log  main;
  19.     sendfile        on;
  20.     #tcp_nopush     on;
  21.     #keepalive_timeout  0;
  22.     keepalive_timeout  65;
  23.    #模仿配置
  24.     server {
  25.        listen 81;
  26.        server_name localhost;
  27.        location /{
  28.            root static;
  29.            index main.html;
  30.          
  31.        }
  32.     }
  33.     #gzip  on;
  34.     server {
  35.         listen       80; # 监听的端口号
  36.         server_name  localhost; # 监听的主机名.域名
  37.         #charset koi8-r;
  38.         #access_log  logs/host.access.log  main;
  39.         # 资源/
  40.         location / {
  41.             root   html; #根目录
  42.             index  index.html main.html; # 资源
  43.         }
  44.         #error_page  404              /404.html;
  45.         # redirect server error pages to the static page /50x.html
  46.         #
  47.         error_page   500 502 503 504  /50x.html;
  48.         location = /50x.html {
  49.             root   html;
  50.         }
  51.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  52.         #
  53.         #location ~ \.php$ {
  54.         #    proxy_pass   http://127.0.0.1;
  55.         #}
  56.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  57.         #
  58.         #location ~ \.php$ {
  59.         #    root           html;
  60.         #    fastcgi_pass   127.0.0.1:9000;
  61.         #    fastcgi_index  index.php;
  62.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  63.         #    include        fastcgi_params;
  64.         #}
  65.         # deny access to .htaccess files, if Apache's document root
  66.         # concurs with nginx's one
  67.         #
  68.         #location ~ /\.ht {
  69.         #    deny  all;
  70.         #}
  71.     }
  72.     # another virtual host using mix of IP-, name-, and port-based configuration
  73.     #
  74.     #server {
  75.     #    listen       8000;
  76.     #    listen       somename:8080;
  77.     #    server_name  somename  alias  another.alias;
  78.     #    location / {
  79.     #        root   html;
  80.     #        index  index.html index.htm;
  81.     #    }
  82.     #}
  83.     # HTTPS server
  84.     #
  85.     #server {
  86.     #    listen       443 ssl;
  87.     #    server_name  localhost;
  88.     #    ssl_certificate      cert.pem;
  89.     #    ssl_certificate_key  cert.key;
  90.     #    ssl_session_cache    shared:SSL:1m;
  91.     #    ssl_session_timeout  5m;
  92.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  93.     #    ssl_prefer_server_ciphers  on;
  94.     #    location / {
  95.     #        root   html;
  96.     #        index  index.html index.htm;
  97.     #    }
  98.     #}
  99. }
复制代码
  1. #模仿配置
  2.     server {
  3.        listen 81;
  4.        server_name localhost;
  5.        location /{
  6.            root static;
  7.            index main.html;
  8.          
  9.        }
  10.     }
复制代码
listen:监听的端标语,需要防火墙放行该端标语
  server_name: 监听的主机名.域名
  location /{} :资源
  location /{
  ​ root static; //根目录,需要在nginx中创建根目录static
  ​ index main.html; //资源,加载该资源
  }
  

  • 重新加载
  1. ../sbin/nginx -s reload
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

温锦文欧普厨电及净水器总代理

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表