图片 1图片 2

实例整体框架:

shell

图片 3

   
shell是一个命令解释器,实际是一个程序,/bin/bash,linux中所有的命令都由它来解释,有自己的语法

 

    shell脚本

 使用VMware搭建 5台Centos7虚拟机(包括客户端),系统版本:CentOS Linux
release
7.2.1511。实例所安装的nginx版本:1.12.2,mariadb-server版本:5.5.56,php-fpm版本:5.4.16,PHPMyAdmin版本:4.0.10.20。此实例所有虚拟机均已关闭防火墙并设置selinux为Permissive(systemctl
stop firewalld.service,setenforce 0)。

        以.sh结尾

搭建web server:

1、安装php-fpm和mariadb-server并创建web资源存放目录:

[root@webserver Desktop]# yum install -y php php-fpm php-mbstring mariadb-server php-mysql
[root@webserver Desktop]# mkdir /data/html

2、配置php-fpm:

#配置php-fpm
[root@webserver Desktop]# vim /etc/php-fpm.d/www.conf
    listen = 0.0.0.0:9000
    listen.allowed_clients = 10.10.0.11,10.10.0.12
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
    php_value[session.save_handler] = files
    php_value[session.save_path] = /var/lib/php/session
#设置会话session文件属主属组
[root@webserver Desktop]# chown apache:apache /var/lib/php/session
[root@webserver Desktop]# ll -d /var/lib/php/session
    drwxrwx---. 2 apache apache 4096 Aug 20 15:50 /var/lib/php/session/
[root@webserver Desktop]# systemctl start php-fpm.service
[root@webserver Desktop]# ss -tan
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:9000                     *:*                  
LISTEN     0      5      192.168.122.1:53                       *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      128    127.0.0.1:631                      *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      128        ::1:631                     :::*                  
LISTEN     0      100        ::1:25                      :::*

3、创建index.php页面并并下载PHPMyAdmin和WordPress:

[root@webserver Desktop]# cd /data/html
[root@webserver html]# vim index.php
    <h1> 10.10.0.13 server</h1>
    <?php
        phpinfo();
    ?>
[root@webserver html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# ln -sv phpMyAdmin-4.0.10.20-all-languages phpmyadmin
#配置wordpress所用数据库
[root@webserver html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@webserver html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

4、配置mariadb:

[root@webserver html]# vim /etc/my.cnf
    [mysqld]
    skip_name_resolve=ON
    innodb_file_per_table=ON
root@webserver html]# systemctl start mariadb.service
#设置mariadb的安全权限
root@webserver html]# mysql_secure_installation
...
#创建wordpress数据库并授权wpuser操作权限,跟wordpress配置文件保持一致
root@webserver html]# mysql -uroot -p
Enter password: 
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress to 'wpuser'@'%' identified by '12345678';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

        shell语法+linux命令

搭建nginx1:

1、安装Nginx并创建web资源存放目录

[root@nginx1 Desktop]# yum install -y nginx
[root@nginx1 Desktop]# mkdir -pv /data/html

2、创建index.html默认页面并下载PHPMyAdmin和WordPress

[root@nginx1 Desktop]# cd /data/html
[root@nginx1 html]# vim index.html
    <h1>this is 10.10.0.11 nginx</h1>
[root@nginx1 html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages phpmyadmin
#配置wordpress所用数据库
[root@nginx1 html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@nginx1 html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

3、配置虚拟主机并启动nginx:

[root@nginx1 html]# vim /etc/nginx/nginx.conf
#注释nginx默认的主机配置
    ...
    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
    ...
[root@nginx1 html]# vim /etc/nginx/conf.d/vhost.conf   #配置虚拟主机,页面动静分离
  server {
    listen 80;
    server_name www.test.org;
    index index.html;
    location / {
         root /data/html;
    }
    location ~* \.php$ {
      fastcgi_pass 10.10.0.13:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME /data/html/$fastcgi_script_name;
    }
    location ~* ^/(status|ping)$ {
      fastcgi_pass 10.10.0.13:9000;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    }
    }
[root@nginx1 html]# systemctl start nginx.service

    注释:

nginx2配置过程同nginx1.

        单行注释 : #
        多行注释 : :<<c   c

搭建Nginx SLB:

安装nginx并进行负载均衡配置:

[root@SLB Desktop]# yum -y install nginx
[root@SLB Desktop]# vim /etc/nginx/nginx
#在http字段进行以下配置
    http {
        ...
        #定义集群
        upstream webservers {
            server 10.10.0.11:80 max_fails=3;
            server 10.10.0.12:80 max_fails=3;
            server 127.0.0.1:80 backup;
        }
        server {
        ...
        location / {
                proxy_pass http://webservers;   #反代给集群服务器
                proxy_set_header host $http_host;   #设置代理请求报文的host字段为$http_host
                proxy_set_header X-Forward-For $remote_addr;       #为代理请求报文添加X-Forward-For字段以传递真实ip地址$remote_addr
            }
            ...
        }
[root@SLB Desktop]# systemctl start nginx.service

    shell执行方式

客户端进行访问:

1、修改hosts:

[root@client Desktop]# vim /etc/hosts
    ...
    172.16.0.11 www.test.org

2、访问:

图片 4 
 
  图片 5

3、没配置缓存时进行压力测试:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.org (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        nginx/1.12.2
Server Hostname:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   58.734 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700001 bytes
HTML transferred:       18500000 bytes
Requests per second:    1702.59 [#/sec] (mean)
Time per request:       58.734 [ms] (mean)
Time per request:       0.587 [ms] (mean, across all concurrent requests)
Transfer rate:          693.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   8.4      0     295
Processing:     2   57 124.9     31    2962
Waiting:        2   56 124.8     31    2962
Total:          7   58 125.3     33    2962

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     51
  75%     66
  80%     77
  90%    111
  95%    157
  98%    273
  99%    375
 100%   2962 (longest request)

        bash /path/to/script-name  或   /bin/bash
/path/to/script-name    (强烈推荐使用)
        /path/to/script-name   或  ./script-name   
(当前路径下执行脚本)
        source script-name  或  . script-name   
(注意“.“点号后面有空格)

4、在SLB服务器进行缓存配置:

#创建缓存存放目录
[root@SLB Desktop]# mkdir -p /data/nginx/cache
[root@SLB Desktop]# vim /etc/nginx/nginx.conf
#在http字段进行配置
    http {
        ...
        proxy_cache_path /data/nginx/cache levels=1:1 keys_zone=nginxcache:50m max_size=1g;
        ...

        server {
            ...
            proxy_cache nginxcache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 301 302 1h;
            proxy_cache_methods GET HEAD;
            proxy_cache_valid any 1m;
            add_header X-cache '$upstream_cache_status from $host';
            proxy_cache_use_stale http_502;
            ...
        }
[root@SLB Desktop]# systemctl restart nginx.service

5、再次进行压力测试:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking www.test.org (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:        nginx/1.12.2
Server Hostname:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   14.391 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700000 bytes
HTML transferred:       18500000 bytes
Requests per second:    6948.98 [#/sec] (mean)
Time per request:       14.391 [ms] (mean)
Time per request:       0.144 [ms] (mean, across all concurrent requests)
Transfer rate:          2829.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.9      0      48
Processing:     2   14   3.9     13      58
Waiting:        1   13   3.8     13      58
Total:          8   14   3.9     13      67

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     14
  75%     14
  80%     14
  90%     16
  95%     24
  98%     27
  99%     29
 100%     67 (longest request)

 

        前两种执行时,都会开启新的进程执行脚本
        source不会开启新的进程

        使用source时,可以与当前终端共享进程,共享变量(重点)

    变量

        1、name=itcast
        2、name=’itcast $age’  不会解析里面的遍历
        3、name=”itcast $age“ 解析变量后,再拼接成新的字符串

    命令变量

        1、name=`ls`
        2、name=$(ls)

        括号里必须是linux命令,把命令执行的结果记录给变量

    全局变量

        方法一:

        变量名=值

        export 变量

        方法二:(最常用)

        export 变量名=值

        如果是在终端中定义全局变量,作用范围是当前终端及子进程

       
如果想要自己定义的全局变量在跟系统的全局变量一样在所有环境都有效,需要在~/.bashrc或/etc/profile文件中定义:

       
修改~/.bashrc后,直接打开新的终端,定义的全局变量就生效了,此方式只对当前用户有效
       
修改/etc/profile时,需要重启操作系统,定义的全局变量才生效,此方式对所有用户有效
       
上面两种方式在修改文件后,如果想让全局变量在当前终端生效,需要执行命令source
~/.bashrc或source /etc/profile

    查看变量

        标准使用方式:”${变量名}”

    内置

        $0  获取当前执行的shell脚本文件名
        $$  获取执行shell脚本的进程号
        $n 
获取当前执行的shell脚本的第n个参数值,n=1..9,当n为0时表示脚本的文件名,如果n大于9就要用大括号括起来${10}
        $#  获取当前shell命令行中参数的总个数
        $?  获取执行上一个指令的返回值(0为成功,非0为失败)

        $?:上一条命令是执行一个文件,返回的结果是文件中的最后一条命令

    默认值

        1、

         变量a如果有内容,那么就输出a的变量值

        ​ 变量a如果没有内容,那么就输出默认的内容

发表评论

电子邮件地址不会被公开。 必填项已用*标注