三、脚本编程类(数组练习)
1、写一个脚本:定义一个数组,数组元素为/var/log目录下所有以.log结尾的文件的名字;显示每个文件的行数;
[root@BAIYU_179 log]# cat Arraywc.sh#!/bin/bash#Arraywc=(/var/log/*.log)for i in $(seq 0 $[${#Arraywc[@]}-1]) #do wc -l ${Arraywc[i]} do wc -l ${Arraywc[i]} >> /tmp/log.wc doneawk '{printf "%-32s%s\n",$2,$1}' /tmp/log.wcrm /tmp/log.wc[root@BAIYU_179 log]# bash Arraywc.sh/var/log/anaconda.ifcfg.log 96/var/log/anaconda.log 264/var/log/anaconda.program.log 376/var/log/anaconda.storage.log 1404/var/log/anaconda.yum.log 463/var/log/boot.log 46/var/log/dracut.log 1645/var/log/yum.log 0
2、写一个脚本,生成10个随机数,并按从小到大进行排序;
思路:×××数组a[n],要求把数组a的元素按从小到大排列,
扫描所有元素假如a[k]<a[k-1],就把两者的值互换,最后得到就是从小排到大的咯。
[root@BAIYU_179 ~]# cat Array2.sh#!/bin/bash#for i in $(seq 0 9)do random[$i]=$RANDOMdonefor i in $(seq 0 9)do for ((n=9;n>i;n--)) # for ((n=1;n<=9;n++)) #用这个也可以 do if [[ ${random[n]} -lt ${random[n-1]} ]] then x=${random[n]} random[n]=${random[n-1]} random[n-1]=$x fi donedoneecho ${random[@]}[root@BAIYU_179 ~]# bash Array2.sh3624 7907 7909 13604 16393 21634 23539 24650 26811 30701
3、写一个脚本,能从所有同学中随机挑选一个同学回答问题;
[root@BAIYU_179 ~]# cat Array3.sh#!/bin/bashstudent=(s1 s2 s3 s4 s5 s6 s7 s8 s9)i=$[$RANDOM % ${#student[@]}]echo ${student[i]}[root@BAIYU_179 ~]# bash Array3.shs1[root@BAIYU_179 ~]# bash Array3.shs6[root@BAIYU_179 ~]# bash Array3.shs3
进一步地:可接受一个参数,做为要挑选的同学的个数;
#!/bin/bash#students=(s1 s2 s3 s4 s5 s6 s7 s8 s9)read -t 5 -p "Please input the number of students to answer the quests: " numif [[ $num -le ${#students[@]} ]]then for ((i=0;i
六、高级应用类(中级班选做,高级班必做)
1)一共3台服务器,请合理安排资源分配;
2)通过Nginx的反向代理实现LNMP架构的负载均衡,后端服务内容为wordpress论坛,要求访问任何一台后端web服务器,都能获取一致的最新数据;
3)后端nginx和php分离。
一、分析
node5:前端代理服务器nginx
node6:后端:nginx+nfs(共享wordpress)
node7:后端:php-fpm+mysql
当用访问静态资源时,node6响应,
当访问动态资源时,由node7响应,
这样满足题目要求吗?不知道怎样才能完美的满足题意
二、环境
Server1:192.168.100.175 CentOS6.5-x86_64
Server2:192.168.100.179 CentOS6.5-x86_64
Server3:192.168.100.180 CentOS6.5-x86_64
注意:三台服务器同步时间
三、步骤
1、node5编译安装nginx
1)创建系统用户nginx来运行nginx服务进程
[root@localhost ~]# useradd -r nginx
2)安装依赖包
[root@localhost ~]# yum -y install pcre-devel openssl-devel
3)修改nginx的软件信息
减少***针对某版本漏洞进行***,我们会去更改或隐藏nginx版本号,将http请求响应头里的nginx版本号信息隐藏或更改掉。
[root@localhost ~]# cd src/[root@localhost src]# lsnginx-1.8.0.tar.gz[root@localhost src]# tar xf nginx-1.8.0.tar.gz [root@localhost src]# cd nginx-1.8.0[root@localhost nginx-1.8.0]# lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[root@localhost nginx-1.8.0]# cd src[root@localhost src]# lscore event http mail misc os[root@localhost src]# cd core[root@localhost core]# lsnginx.c ngx_crypt.h ngx_open_file_cache.c ngx_resolver.cnginx.h ngx_cycle.c ngx_open_file_cache.h ngx_resolver.hngx_array.c ngx_cycle.h ngx_output_chain.c ngx_sha1.hngx_array.h ngx_file.c ngx_palloc.c ngx_shmtx.cngx_buf.c ngx_file.h ngx_palloc.h ngx_shmtx.hngx_buf.h ngx_hash.c ngx_parse.c ngx_slab.cngx_conf_file.c ngx_hash.h ngx_parse.h ngx_slab.hngx_conf_file.h ngx_inet.c ngx_proxy_protocol.c ngx_spinlock.cngx_config.h ngx_inet.h ngx_proxy_protocol.h ngx_string.cngx_connection.c ngx_list.c ngx_queue.c ngx_string.hngx_connection.h ngx_list.h ngx_queue.h ngx_syslog.cngx_core.h ngx_log.c ngx_radix_tree.c ngx_syslog.hngx_cpuinfo.c ngx_log.h ngx_radix_tree.h ngx_thread_pool.cngx_crc32.c ngx_md5.c ngx_rbtree.c ngx_thread_pool.hngx_crc32.h ngx_md5.h ngx_rbtree.h ngx_times.cngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.hngx_crypt.c ngx_murmurhash.h ngx_regex.h
[root@localhost core]# vim nginx.h
3)安装nginx
[root@localhost nginx-1.8.0]# ./configure \ --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/tmp/nginx/client/ \ --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-pcre --with-http_realip_module[root@localhost nginx-1.8.0]# make && make install
注意:http_realip_module这个模块允许从请求Headers里更改客户端的IP地址值(例如实时的转发)。
4)启动并测试
启动nginx前先检查是否有错误再启动
[root@localhost nginx-1.8.0]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)nginx: configuration file /etc/nginx/nginx.conf test failed[root@localhost nginx-1.8.0]# mkdir -pv /var/tmp/nginx/clientmkdir: created directory `/var/tmp/nginx'mkdir: created directory `/var/tmp/nginx/client'[root@localhost nginx-1.8.0]# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@localhost nginx-1.8.0]# nginx[root@localhost nginx-1.8.0]# netstat -nlptuActive Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4146/nginx tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1100/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1176/master tcp 0 0 :::22 :::* LISTEN 1100/sshd tcp 0 0 ::1:25 :::* LISTEN 1176/master
请求一个不存在的页面:
可以看到软件信息已经被更改
[root@localhost nginx-1.8.0]# curl -I http://192.168.100.10HTTP/1.1 200 OKServer: lighttpd/1.4.37Date: Thu, 12 Nov 2015 15:38:20 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Thu, 12 Nov 2015 15:24:59 GMTConnection: keep-aliveETag: "5644af4b-264"Accept-Ranges: bytes[root@localhost nginx-1.8.0]#
5)为nginx提供SysV init脚本
[root@localhost nginx-1.8.0]# cd /etc/rc.d/init.d/[root@localhost init.d]# vi nginx[root@localhost init.d]# cat nginx #!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid # Source function library.. /etc/rc.d/init.d/functions # Source networking configuration.. /etc/sysconfig/network # Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx"prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done} start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval} stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval} restart() { configtest || return $? stop sleep 1 start} reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo} force_reload() { restart} configtest() { $nginx -t -c $NGINX_CONF_FILE} rh_status() { status $prog} rh_status_q() { rh_status >/dev/null 2>&1} case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2esac[root@localhost init.d]# lsauditd halt lvm2-lvmetad netconsole ntpd rdisc saslauthd udev-postblk-availability ip6tables lvm2-monitor netfs ntpdate restorecond singlecrond iptables messagebus network oddjobd rsyslog sshdfunctions killall mysqld nginx postfix sandbox svnserve[root@localhost init.d]# chmod +x nginx [root@localhost init.d]# lsauditd halt lvm2-lvmetad netconsole ntpd rdisc saslauthd udev-postblk-availability ip6tables lvm2-monitor netfs ntpdate restorecond singlecrond iptables messagebus network oddjobd rsyslog sshdfunctions killall mysqld nginx postfix sandbox svnserve[root@localhost init.d]# chkconfig --add nginx[root@localhost init.d]# chkconfig --list nginxnginx 0:off 1:off 2:off 3:off 4:off 5:off 6:off[root@localhost init.d]# service nginx statusnginx (pid 4147 4146) is running...[root@localhost init.d]# service nginx configtest nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful[root@localhost init.d]# service nginx restartnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulStopping nginx: [ OK ]Starting nginx: [ OK ]
2、node6安装nginx和nfs
安装nfs
[root@BAIYU_179 ~]# rpm -qa|grep rpcbind[root@BAIYU_179 ~]# rpm -qa|grep nfs[root@BAIYU_179 ~]# yum install rpcbind nfs-util[root@BAIYU_179 ~]# service rpcbind start正在启动 rpcbind:[确定][root@BAIYU_179 ~]# service nfs start[root@BAIYU_179 web]# vi /etc/exports 1 /data/web 192.168.100.0/24(rw)[root@BAIYU_179 web]# exportfs/data/web 192.168.100.0/2
3、node7安装mariadb和php-fpm
安装mariadb
1)创建系统用户mysql和数据存放目录
[root@BAIYU_180 ~]# useradd -r mysql [root@BAIYU_180 ~]# mkdir /data/mydata[root@BAIYU_180 ~]# chown -R mysql.mysql /data/mydata[root@BAIYU_180 ~]# yum install jemalloc #最新版的mariadb依赖此包
2)安装mariadb
[root@BAIYU_180 ~]# lsanaconda-ks.cfg install.log.syslog php-5.6.15.tar.bz2install.log mariadb-10.1.8-linux-x86_64.tar.gz trash.sh[root@BAIYU_180 ~]# tar xf mariadb-10.1.8-linux-x86_64.tar.gz -C /usr/local[root@BAIYU_180 ~]# cd /usr/local[root@BAIYU_180 local]# lsbin games lib libexec sbin srcetc include lib64 mariadb-10.1.8-linux-x86_64 share[root@BAIYU_180 local]# ln -sv mariadb-10.1.8-linux-x86_64/ mysql"mysql" -> "mariadb-10.1.8-linux-x86_64/"[root@BAIYU_180 local]# cd mysql/[root@BAIYU_180 mysql]# chown -R root.mysql .
3)初始化、配置mariadb及服务脚本
[root@BAIYU_180 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mydata[root@BAIYU_180 mysql]# ls /data/mydataaria_log.00000001 ibdata1 ib_logfile1 mysql-bin.000001 mysql-bin.000003 mysql-bin.state testaria_log_control ib_logfile0 mysql mysql-bin.000002 mysql-bin.index performance_schema[root@BAIYU_180 mysql]# sp support-files/binary-configure my-large.cnf mysql-log-rotate wsrep_notifymagic my-medium.cnf mysql.server my-huge.cnf my-small.cnf policy/ my-innodb-heavy-4G.cnf mysqld_multi.server wsrep.cnf [root@BAIYU_180 mysql]# cp support-files/my-large.cnf /etc/my.cnf cp:是否覆盖"/etc/my.cnf"? y[root@BAIYU_180 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld[root@BAIYU_180 mysql]# chkconfig --add mysqld[root@BAIYU_180 mysql]# chkconfig --list mysqldmysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@BAIYU_180 mysql]# vi /etc/my.cnf在mysqld段添加如下2行:datadir=/data/mydatainnodb_file_per_table = ON
4)启动并测试
[root@BAIYU_180 mysql]# vi /etc/profile.d/mysql.sh 1 PATH=/usr/local/mysql/bin:$PATH[root@BAIYU_180 mysql]# mysqlWelcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 4Server version: 10.1.8-MariaDB-log MariaDB ServerCopyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || test |+--------------------+4 rows in set (0.00 sec)
安装php
1)安装依赖包
[root@BAIYU_180 ~]# yum install libxml2-devel bzip2-devel libmcrypt-devel freetype curl-devel -y
2)安装php
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl # make && make install
3)配置php,php-fpm
[root@BAIYU_180 php-5.6.15]# cp php.ini-production /etc/php.ini [root@BAIYU_180 php-5.6.15]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm[root@BAIYU_180 php-5.6.15]# ls /etc/rc.d/init.d/php-fpm/etc/rc.d/init.d/php-fpm[root@BAIYU_180 php-5.6.15]# chmod +x /etc/rc.d/init.d/php-fpm [root@BAIYU_180 php-5.6.15]# ls /etc/rc.d/init.d/php-fpm -l -rwxr-xr-x 1 root root 2354 11月 17 21:17 /etc/rc.d/init.d/php-fpm[root@BAIYU_180 php-5.6.15]# chkconfig --add php-fpm[root@BAIYU_180 php-5.6.15]# chkconfig --list php-fpmphp-fpm 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭[root@BAIYU_180 php-5.6.15]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf# vim /usr/local/php/etc/php-fpm.conf#配置fpm的相关选项为你所需要的值,并启用pid,status,ping文件(如下最后一行):pm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers = 8listen = 0.0.0.0:9000pm.status_path = /statusping.path = /ping pid = /usr/local/php/var/run/php-fpm.pid #要和服务脚本中的路径一致
4)启动并测试
[root@BAIYU_180 php-5.6.15]# service php-fpm start Starting php-fpm done[root@BAIYU_180 php-5.6.15]# netstat -nlptu Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 24236/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1338/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1172/master tcp 0 0 0.0.0.0:50817 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN 22918/php-fpm tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 24908/mysqld udp 0 0 0.0.0.0:111 0.0.0.0:* 24236/rpcbind udp 0 0 0.0.0.0:663 0.0.0.0:* 24236/rpcbind
4、整合nginx和php5
1)修改/etc/nginx/nginx.conf启用如下几行:
65 location ~ \.php$ { 66 root /data/web; 67 fastcgi_pass 192.168.100.180:9000; 68 fastcgi_index index.php; 69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 70 include fastcgi_params; 71 }
添加index.php主页格式
43 location / { 44 root /data/web; proxy_pass http://192.168.100.179; 45 index index.php index.html index.htm; 46 }
2)编辑/etc/nginx/fastcgi_params,将其内容更改为如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;fastcgi_param SERVER_SOFTWARE nginx;fastcgi_param QUERY_STRING $query_string;fastcgi_param REQUEST_METHOD $request_method;fastcgi_param CONTENT_TYPE $content_type;fastcgi_param CONTENT_LENGTH $content_length;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param SCRIPT_NAME $fastcgi_script_name;fastcgi_param REQUEST_URI $request_uri;fastcgi_param DOCUMENT_URI $document_uri;fastcgi_param DOCUMENT_ROOT $document_root;fastcgi_param SERVER_PROTOCOL $server_protocol;fastcgi_param REMOTE_ADDR $remote_addr;fastcgi_param REMOTE_PORT $remote_port;fastcgi_param SERVER_ADDR $server_addr;fastcgi_param SERVER_PORT $server_port;fastcgi_param SERVER_NAME $server_name;
3)在node7上授权用户wpuser访问
[root@BAIYU_180 ~]# mysqlWelcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 5Server version: 10.1.8-MariaDB-log MariaDB ServerCopyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> CREATE DATABASE wpdb;Query OK, 1 row affected (0.04 sec)MariaDB [(none)]> GRANT ALL PRIVILEGES ON wpdb.* TO 'wpuser'@'192.168.%.%' IDENTIFIED BY 'wppass';Query OK, 0 rows affected (0.07 sec)MariaDB [(none)]> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.03 sec)
在node6安装wordpass:
[root@BAIYU_180 ~]# lsanaconda-ks.cfg install.log.syslog wordpress-4.3.1-zh_CN.tar.gzinstall.log trash.sh[root@BAIYU_180 ~]# tar xf wordpress-4.3.1-zh_CN.tar.gz [root@BAIYU_180 ~]# lsanaconda-ks.cfg install.log.syslog wordpressinstall.log trash.sh wordpress-4.3.1-zh_CN.tar.gz[root@BAIYU_180 ~]# mv wordpress/* /data/web[root@BAIYU_180 ~]# cd /data/web[root@BAIYU_180 web]# lsindex.php wp-blog-header.php wp-includes wp-settings.phplicense.txt wp-comments-post.php wp-links-opml.php wp-signup.phpreadme.html wp-config-sample.php wp-load.php wp-trackback.phpwp-activate.php wp-content wp-login.php xmlrpc.phpwp-admin wp-cron.php wp-mail.php[root@BAIYU_180 we]# cp wp-config-sample.php wp-config.php #创建配置文件本文件包含以下配置选项: 21 // ** MySQL 设置 - 具体信息来自您正在使用的主机 ** // 22 /** WordPress数据库的名称 */ 23 define('DB_NAME', 'wpdb'); 24 25 /** MySQL数据库用户名 */ 26 define('DB_USER', 'wpuser'); 27 28 /** MySQL数据库密码 */ 29 define('DB_PASSWORD', 'wppass'); 30 31 /** MySQL主机 */ 32 define('DB_HOST', '192.168.100.180'); 33 34 /** 创建数据表时默认的文字编码 */ 35 define('DB_CHARSET', 'utf8'); 36 37 /** 数据库整理类型。如不确定请勿更改 */ 38 define('DB_COLLATE', ''); # 更改对应的设置即可
node5,node7都挂载/data/web目录
好吧,只实现了分布式部署lnmp
后面的内容都没跟上,没时间看前面的,等有时间再补上其它的题目吧