apache,squid,shell脚本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了apache,squid,shell脚本相关的知识,希望对你有一定的参考价值。
网页重写
把所有80端口的请求重定向由https来处理
[[email protected] conf.d]# vim music.conf
<Virtualhost *:80>
ServerName music.westos.com
RewriteEngine on
RewriteRule ^(/.*)$ https://%{HTTP_HOST}$1 [redirect=301]
</Virtualhost>
<Directory "/var/www/virtual/music/html">
Require all granted
</Directory>
<Virtualhost *:443>
ServerName music.westos.com
DocumentRoot /var/www/virtual/music/html
Customlog logs/news-443.log combined
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/www.westos.com.crt
SSLCertificateKeyFile /etc/pki/tls/private/www.westos.com.key
</Virtualhost>
测试成功,浏览器输入http://music.westos.com 自动跳转https://music.westos.com
apache支持的语言测试,可安装http-manual参考测试方法,可下载中文版手册
php语言测试
[[email protected] conf.d]# yum install php -y
[[email protected] conf.d]# cd /var/www/html/
[[email protected] html]# ls
file1.html index.html
[[email protected] html]# rm -fr file1.html
[[email protected] html]# vim index.php
<?php
phpinfo ();
?>
[[email protected] html]# systemctl restart httpd.service
输入www.westos.com进行测试
cgi脚本测试
[[email protected] html]# mkdir cgi
[[email protected] html]# ls
cgi index.php
[[email protected] html]# cd cgi/ ##编辑如下
[[email protected] cgi]# vim index.cgi
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print `date`;
[[email protected] cgi]# perl index.cgi ###测试脚本是否成功
[[email protected] cgi]# chmod +x index.cgi
[[email protected] conf.d]# vim default.conf #编辑如下
<Virtualhost _default_:80>
DocumentRoot /var/www/html
Customlog logs/default.log combined
</Virtualhost>
<Directory "/var/www/html/cgi">
Options +ExecCGI
AddHandler cgi-script .cgi
</Directory>
浏览器输入172.25.254.16/cgi/inde 测试
显示时间,刷新网页时间随之刷新
##搭建论坛
[[email protected] conf.d]# systemctl start mariadb ###启动数据库服务,并查看状态
[[email protected] conf.d]# systemctl status mariadb.service
[[email protected] conf.d]# netstat -antlpe | grep mysql ###查看数据库开放端口,27端口已关闭无需设定,若端口打开状态 。vim /etc/my.cnf 添加skip-networking=1
(也可用软件XAMPP(Apache+MySQL+PHP+PERL)代替以上环境,进行论坛的下一步搭建
在网络上下载discuz
(Crossday Discuz! Board 简称 Discuz!是一套通用的社区论坛软件系统,用户可以在不需要任何编程的基础上,通过简单的设置和安装,在互联网上搭建起具备完善功能、很强负载能力和可高度定制的论坛服务
[[email protected] discuz3.2]# cp -p upload discuz-3.2 -r
[[email protected] discuz3.2]# ls
discuz-3.2 Discuz_X3.2_SC_GBK.zip readme upload utility
[[email protected] discuz3.2]# mv discuz-3.2 /var/www/html/
浏览器输入http://172.25.254.16/discuz-3.2/install/进行安装
结果如下,出现乱码
可能是下载版本不对。此版本为简体中文GBK,改换简体中文UTF8重复以上步骤尝试
问题解决
[[email protected] html]# chmod 777 discuz-3.2
[[email protected] html]# chmod 777 discuz-3.2 -R
安装成功,结果如下
#############squid代理服务器
[[email protected] ~]# yum install squid -y
[[email protected] ~]# vim /etc/squid/squid.conf
56 http_access allow all
62 cache_dir ufs /var/spool/squid 100 16 256
[[email protected] ~]# cd /var/spool/
[[email protected] spool]# ls
abrt-upload at cups mail postfix squid
anacron cron lpd plymouth rhsm up2date
[[email protected] spool]# cd squid/
[[email protected] squid]# ls
[[email protected] squid]# systemctl start squid
[[email protected] squid]# ls ###启动squid服务后自动生成目录
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F swap.state
[[email protected] squid]# netstat -antlpe | grep squid
tcp6 0 0 :::3128 :::* LISTEN 0 74443 8240/(squid-1)
[[email protected] ~]# firefox & ###打开浏览器,设置代理服务器ip
(Edit--Preferences--Advanced--Network--Settings..Manual proxy configuration
输入squid服务器ip,及开放端口3128 。如下
用虚拟机连接网络成功
[[email protected] ~]# ping www.baidu.com
ping: unknown host www.baidu.com
(ping不通,但是可以连接网络
###反向代理
[[email protected] ~]# yum remove httpd -y
[[email protected] ~]# rpm -ql httpd
package httpd is not installed
[[email protected] ~]# yum install squid -y
[[email protected] ~]# cat /var/www/html/index.html
This page shows 172.25.254.116‘s information
[[email protected] ~]# systemctl restart httpd.service ##创建父级服务器环境
[[email protected] ~]# vim /etc/squid/squid.conf ##配置反向代理服务器ip
56 http_access allow all
57
58 # Squid normally listens to port 3128
59 http_port 80 vhost vport
60 cache_peer 172.25.254.116 parent 80 0 no-query
61 # Uncomment and adjust the following to add a disk cache directory.
62 cache_dir ufs /var/spool/squid 100 16 256
[[email protected] Desktop]# systemctl restart squid.service
在foundation37主机(第客户端主机)中输入使用反向代理服务的主机ip,获得父级服务器共享内容
##轮询
[[email protected] Desktop]# vim /etc/squid/squid.conf ##
60 cache_peer 172.25.254.116 parent 80 0 no-query originserver round-robin name=web1
在下行编辑,加入另一父级服务器ip及名称即可
每次刷新,跳转不同的ip,以减缓服务器压力
########shell脚本
[[email protected] ~]# a=date
[[email protected] ~]# echo a
a
[[email protected] ~]# echo $a
date
[[email protected] ~]# a=`date`
[[email protected] ~]# echo $a
Wed Dec 14 22:47:41 EST 2016
[[email protected] ~]# b=2
[[email protected] ~]# echo $bc
[[email protected] ~]# echo ${b}c
2c
[[email protected] ~]# c=3
[[email protected] ~]# echo ${bc}
[[email protected] ~]# echo ${b,c}
2
[[email protected] ~]# echo ${b}${c}
23
[[email protected] ~]# bc=5
[[email protected] ~]# echo ${bc}
5
[[email protected] ~]# echo ‘‘‘‘
[[email protected] ~]# echo "‘‘"
‘‘
[[email protected] ~]# i=1
[[email protected] ~]# echo $[i++]
1
[[email protected] ~]# echo $[i++]
2
[[email protected] ~]# echo $[i++]
3
[[email protected] ~]# echo $[1+2]
3
[[email protected] ~]# echo $[1%2]
1
[[email protected] ~]# echo $[1*2]
2
[[email protected] ~]# echo $[3/2]
1
[[email protected] ~]# let a=3+3
[[email protected] ~]# echo $a
6
[[email protected] ~]# ((a=7+2))
[[email protected] ~]# echo $a
9
[[email protected] ~]# for X in 1 2 3;do echo x=$X;done
x=1
x=2
x=3
[[email protected] ~]# for ((i=1;i<5;i++)); do echo $i; done
1
2
3
4
[[email protected] ~]# for ((i=5;i>0;i--)); do echo $i; done
5
4
3
2
1
[[email protected] ~]# for ((i=1;i<10;i++)); do ((k+=i)); echo $k; done
1
3
6
10
15
21
28
36
45
[[email protected] ~]# [ "1" = "1" ]
[[email protected] ~]# echo $?
0 ###0为肯定,1为否定
[[email protected] ~]# [ "1" = "2" ]
[[email protected] ~]# echo $?
1
[[email protected] ~]# while [ "1" = "1" ]; do echo yes; break; done
yes
#ping脚本
[[email protected] shell]# for a in {1..5}; do ping -c1 -w1 172.25.254.$a &> /dev/null && echo 172.25.254.$a is up || echo 172.25.254.$a is down; done
172.25.254.1 is down
172.25.254.2 is down
172.25.254.3 is down
172.25.254.4 is down
172.25.254.5 is down
[[email protected] shell]# read -p "Please input the ip address which you want to check: " IP; ping -c1 -w1 $IP &> /dev/null && echo $IP is up || echo $IP is down
Please input the ip address which you want to check: 172.25.254.16
172.25.254.16 is up
[[email protected] shell]# test "$a" = "$b" && echo yes || echo no
yes
[[email protected] shell]# test "1" = "2" && echo yes || echo no
no
[[email protected] shell]# b=0
[[email protected] shell]# [ -z "$b" ] && echo yes || echo no
no
[[email protected] shell]# echo $c
[[email protected] shell]# [ -z "$c" ] && echo yes || echo no
yes
[[email protected] shell]# vim filestyle.sh
#!/bin/bash
if
[ -e "$1" ]
then
[ -f "$1" -a ! -L "$1" ] && echo $1 is a file
[ -b "$1" ] && echo $1 is a block
[ -c "$1" ] && echo $1 is a c block
else
[ -n "$1" ] && echo $1 is not exist || echo "Please input the filename to check: "
fi
[[email protected] shell]# chmod +x filestyle.sh
[[email protected] shell]# sh filestyle.sh /mnt/lkjl
/mnt/lkjl is not exist
[[email protected] useradd]# vim userfile ##根据文件自动创建用户
[[email protected] useradd]# vim passfile
[[email protected] useradd]# cat userfile
westos1
westos2
westos3
[[email protected] useradd]# cat userfile
westos1
westos2
westos3
[[email protected] useradd]# vim useradd.sh
[[email protected] useradd]# chmod +x useradd.sh
[[email protected] useradd]# sh useradd.sh
ERROR: Please input userfile and password file after command !!
#!/bin/bash
if
[ -n "$1" -a -n "$2" ]
then
if
[ -e "$1" -a -e "$2" ]
then
MAXUSER=`wc -l $1 | cut -d ‘ ‘-f 1`
MAXPASS=`wc -l $2 | cut -d ‘ ‘-f 1`
[ "$MAXUSER" -eq "$MAXPASS" ]&&(
for NUM in $( seq 1 $MAXUSER )
do
USERNAME=`sed -n ${NUM}p $1`
PASSWORD=`sed -n ${NUM}p $2`
CKUSER=`getent passwd $USERNAME`
[ -z "$CKUSER" ]&&(
useradd $USERNAME
echo $PASSWORD |passwd --stdin $USERNAME
)||echo "$USERNAME exist !!"
done
)||(
echo $1 and $2 have different lines
)
elif
[ ! -e "$1" ]
then
echo "ERROR:$1 is not exsit"
else
echo "ERROR:$2 is not exsit"
fi
else
echo "ERROR: Please input userfile and password file after command !!"
fi
[[email protected] useradd]# vim case.sh ##case语句
#!/bin/bash
[ "$1" = "$2" ]
case $? in
0)
echo $1=$2
;;
1)
echo $1!=$2
;;
*)
echo what?
esac
[[email protected] useradd]# sh case.sh 1 1
1=1
[[email protected] useradd]# sh case.sh 1 2
1!=2
[roo[email protected] /]# yum install expect -y ####expect自动应答
[[email protected] shell]# vim ask.sh
#!/bin/bash
read -p "what‘s your name: " NAME
read -p "How old are you: " AGE
read -p "What is your class: " CLASS
echo $NAME is $AGE\‘s old,in $CLASS class.
[[email protected] shell]# sh ask.sh
what‘s your name: liu
How old are you: 20
What is your class: 19
liu is 20‘s old,in 19 class.
[[email protected] shell]# vim expect.exp
spawn /root/Desktop/shell/ask.sh
expect "name:"
send "liu\r"
expect "old"
send "17\r"
expect "class"
send "8\r"
expect eof
[[email protected] shell]# /root/Desktop/shell/expect.exp
spawn /root/Desktop/shell/ask.sh
what‘s your name: liu
How old are you: 17
What is your class: 8
[[email protected] shell]# vim pingcheck.sh
#!/bin/bash
for NUM in {15..20}
do
ping -c1 -w1 172.25.254.$NUM &> /dev/null && (
/mnt/ssh.exp 172.25.254.$NUM redhat hostname | grep -E "The|ECDSA|connecting|Warning|password|spawn" -v|sed "s/Permission\ denied\,\ please\ try\ again\./172.25.254.$NUM password is error/g"
)
done
[[email protected] mnt]# vim ssh.exp
#!/usr/bin/expect
set timeout 3
set IP [lindex $argv 0]
set PASS [lindex $argv 1]
set COMM [lindex $argv 2]
spawn ssh [email protected]$IP $COMM
expect {
"yes/no"
{send "yes/r";exp_continue}
"password:"
{send "$PASS\r"}
}
expect eof
[[email protected] shell]# chmod 777 pingcheck.sh
[[email protected] shell]# chmod 777 /mnt/ssh.exp
[[email protected] mnt]# ping 172.25.254.16
以上是关于apache,squid,shell脚本的主要内容,如果未能解决你的问题,请参考以下文章
shell脚本启动和停止apache 检查apache的运行状态 如果apache启动了就让它停止 ,如果停止了就启动apach
我的php想用root权限调用shell脚本,怎么让apache拥有root权限,该http.conf为root,apache启动不了