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
#!/bin/bash
# echo 表示输出
echo "hello world"
#!/bin/bash 头文件
运行该脚本: ./路径/脚本名.sh
https://i-blog.csdnimg.cn/direct/4d1d707a359b48fabfa783ccac167429.png#pic_center
利用 ls -l命令可以查看该文件的权限
https://i-blog.csdnimg.cn/direct/ea30822e994b4ee7ba72f3bd1977e382.png#pic_center
第一个字符:- :表现文件 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 变量 弱语言
#!/bin/bash
name="lay"
echo "姓名:${name}"
echo "姓名:"${name}
1.5 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 是一款表达式盘算工具,利用它能完成表达式的求值操作。
例如,两个数相加(留意利用的是反引号 *`* 而不是单引号 *'*):
#!/bin/bash
a=100
b=20
c=`expr $a / $b`
echo "c==$c"
关系运算符
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
布尔运算符
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语句
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
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
https://i-blog.csdnimg.cn/direct/864946f483874ed2b9105a21770fee68.png#pic_center
2.3 有哪些企业利用nginx
京东 淘宝 12306 新浪等
2.4 安装nginx
nginx可以独立安装在一台服务器,也可以和项目在同一个服务器
1. 安装nginx的依靠插件
yuminstall -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
下载nginx
源码:先编译——>后安装
下载所在:https://nginx.org/en/download.html
https://i-blog.csdnimg.cn/direct/b8ac19e1286246faa95fc2855c7ba631.png#pic_center
创建一个目录作为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目录结构
https://i-blog.csdnimg.cn/direct/9907362981a64487ad8bf41db3effa70.png#pic_center
启动nginx
nginx启动
nginx -s stop关闭
nginx -s reload 重新加载配置文件
https://i-blog.csdnimg.cn/direct/cd893b32caba4ff3bc8754b3001e3759.png#pic_center
防火墙放行80端口
firewall-cmd --add-port=80/tcp --zone=public --premanent
重启防火墙
systemctl restart firewalld
访问nginx80
http://nginx所在的ip:nginx的端口/
https://i-blog.csdnimg.cn/direct/c718477eb6db4ef38416ecfbefa8edf9.png#pic_center
2.5 nginx配置文件修改
切换到/usr/nginx/conf目录下,打开nginx.conf配置文件进行修改
#usernobody;
#工作的线程数
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pid logs/nginx.pid;
events {
# 每个工作对象允许的连接数
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
#log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_loglogs/access.logmain;
sendfile on;
#tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout65;
#模仿配置
server {
listen 81;
server_name localhost;
location /{
root static;
index main.html;
}
}
#gzipon;
server {
listen 80; # 监听的端口号
server_namelocalhost; # 监听的主机名.域名
#charset koi8-r;
#access_loglogs/host.access.logmain;
# 资源/
location / {
root html; #根目录
indexindex.html main.html; # 资源
}
#error_page404 /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_indexindex.php;
# fastcgi_paramSCRIPT_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 {
# denyall;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_namesomenamealiasanother.alias;
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_namelocalhost;
# ssl_certificate cert.pem;
# ssl_certificate_keycert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout5m;
# ssl_ciphersHIGH:!aNULL:!MD5;
# ssl_prefer_server_cipherson;
# location / {
# root html;
# indexindex.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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]