标签存档: LAMP

监测服务器的shell脚本

#! /bin/bash
#jiance.sh

#硬盘使用率
ds=`df|awk '{if(NR==4){print int($5)}}'`
#内存使用率
used=`free -m|awk '{if(NR==2){print int($3)}}'`

tot=`free -m|awk '{if(NR==2){print int($2)}}'`

per=$(($used*100/$tot))
#apache2是否正常运行
apa=`netstat -tanpl | grep 80 | awk '{print $4}' | sed -n '1p'`
#mysql是否正常运行
mysql=`netstat -tanpl | grep 3306 | awk '{print $4}' | sed -n '1p'`
#统计
res1="硬盘使用率:"$ds"%\n内存使用率:"$per"%\n"

#echo -e $res1

if [ $apa != "0.0.0.0:80" ];then
        str1="apache2已经停止运行"
else
        str1="apache2正常运行"
fi

if [ $mysql != "127.0.0.1:3306" ];then
        str2="myqsl已经停止运行"
else
        str2="mysql正常运行"
fi
#统计输出
text=$res1$str1"\n"$str2;
#监控发邮件
if [ $ds  -gt 45 ];then
        echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi

if [ $per -gt 55 ];then
        echo -e $text|mail -s "hotsun.link的运行状态" 1324928751@qq.com
fi

Httpd配置多个顶级域名

先找到httpd.conf文件后添加:

LoadModule vhost_alias_module modules/mod_vhost_alias.so
#
#
#
Include conf/extra/httpd-vhosts.conf

然后是extra目录内的httpd-vhosts.conf文件:

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.localhost
    DocumentRoot "/var/www/site1"
    ServerName site1.com
    ServerAlias *.site1.com
    ErrorLog "logs/localhost-error_log"
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.localhost
    DocumentRoot "/var/www/site2"
    ServerName site2.com
    ServerAlias *.site2.com
    ErrorLog "logs/localhost-error_log"
</VirtualHost>

httpd绑定二级域名的文件目录

第一步:

开启mod_rewrite模块,默认是开启的,这里可以查下是否开启。

第二步:

查看302行:“AllowOverride None” 修改成 “AllowOverride All”。

第三步:

在httpd.conf最后添加如下命令

RewriteEngine on
RewriteMap lowercase int:tolower
RewriteMap vhost txt:/etc/httpd/vhost.map
RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1

第四部:

在目录 /etc/httpd/下创建目录 vhost.map 文件,用来绑定二级域名对应的目录。

文本内容填写如:bbs.baidu.com /var/www/html/bbs。

第五部:

重启apache服务器

终端输入:service httpd restart   回车,完成。

Node.js 回调函数

Node.js 异步编程的直接体现就是回调。

异步编程依托于回调来实现,但不能说使用了回调后程序就异步化了。

回调函数在完成任务后就会被调用,Node 使用了大量的回调函数,Node 所有 API 都支持回调函数。

例如,我们可以一边读取文件,一边执行其他命令,在文件读取完成后,我们将文件内容作为回调函数的参数返回。这样在执行代码时就没有阻塞或等待文件 I/O 操作。这就大大提高了 Node.js 的性能,可以处理大量的并发请求。

阻塞代码实例:

创建一个文件 input.txt ,内容如下:

今天自学Node.js

创建 main.js 文件, 代码如下:

var fs = require("fs");
var data = fs.readFileSync('input.txt');
console.log(data.toString());
console.log("程序执行结束!");

以上代码执行结果如下:

$ node main.js
今天自学Node.js
程序执行结束!

非阻塞代码实例:

创建 main.js 文件, 代码如下:

var fs = require("fs");
fs.readFile('input.txt', function (err, data) {
        if (err) return console.error(err);
        console.log(data.toString());
    });
console.log("程序执行结束!");

以上代码执行结果如下:

$ node main.js
今天自学Node.js
程序执行结束!

以上两个实例我们了解了阻塞与非阻塞调用的不同。第一个实例在文件读取完后才执行完程序。 第二个实例我们不需要等待文件读取完,这样就可以在读取文件时同时执行接下来的代码,大大提高了程序的性能。

因此,阻塞是按顺序执行的,而非阻塞是不需要按顺序的,所以如果需要处理回调函数的参数,我们就需要写在回调函数内。

Apache设置防盗链

盗链,就是盗取链接,假如我们的网站有很多好看的图片,别人可以查看我们网站图片的链接,然后应用在他的网站上,这样的话,去访问他的网站,实际上消耗的是我们的流量(因为实际链接在我们这里),这样我们就不得不去配置防盗链,使得别人不能复制我们图片的链接。

[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
    DocumentRoot "/data/www"
    ServerName www.test.com
    ErrorLog "logs/test.com_error_log"
    CustomLog "logs/test.com_access_log" combined
    SetEnvIfNoCase Referer "^http://.*\.test\.com" local_ref    # 表示只有 test.com 和 abc.com 的访问才允许,其他的都拒绝
    SetEnvIfNoCase Referer ".*\. abc\.com" local_ref
    <filesmatch "\.(txt|doc|mp3|zip|rar|jpg|gif|png)">
        Order Allow,Deny
        Allow from env=local_ref
        Deny from all
    </filesmatch>
</VirtualHost>