Rootop 服务器运维与web架构

2014-11-20
发表者 Venus
log_format可设置的参数格式及说明已关闭评论

log_format可设置的参数格式及说明

log_format可设置的参数格式及说明如下:

参数 说明 示例
$remote_addr 客户端地址 119.40.32.45
$remote_user 客户端用户名称
$time_local 访问时间和时区 [20/Nov/2014:10:52:09 +0800]
$request 请求方法、URI和HTTP协议 GET /index.html HTTP/1.1
$http_host 请求地址,即浏览器中你输入的地址(IP或域名) 115.29.149.25/www.rootop.org
$status HTTP请求状态 200
$upstream_status upstream状态 200
$body_bytes_sent 发送给客户端文件内容大小 547
$http_referer url跳转来源 http://www.baidu.com/
$http_user_agent 用户终端浏览器等信息 “Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol SSL协议版本 TLSv1
$ssl_cipher 交换数据中的算法 RC4-SHA
$upstream_addr 后台upstream的地址,即真正提供服务的主机地址 192.168.10.23:80
$request_time 整个请求的总时间 00.007
$ssl_protocol SSL协议版本 TLSv1
$http_x_forwarded_for 客户端地址(nginx做反向代理时) 119.40.32.45

2014-11-19
发表者 Venus
在centos下编译PostgreSQL数据库已关闭评论

在centos下编译PostgreSQL数据库

PostgreSQL官网:http://www.postgresql.org

readline是一个开源的跨平台程序库,提供了交互式的文本编辑功能。postgresql需要readline的支持。
wget -c https://ftp.postgresql.org/pub/source/v9.3.5/postgresql-9.3.5.tar.gz
[root@rootop postgresql-9.3.5]# yum install readline readline-devel

[root@rootop postgresql-9.3.5]# ./configure --prefix=/usr/local/pgsql

[root@rootop postgresql-9.3.5]# make
[root@rootop postgresql-9.3.5]# make install

添加系统账户:
[root@rootop ~]# useradd postgres
[root@rootop ~]# passwd postgres

创建数据目录:
[root@rootop ~]# mkdir /usr/local/pgsql/data
[root@rootop ~]# chown postgres:postgres /usr/local/pgsql/data/

初始化数据库:
[root@rootop ~]# su postgres  #切换到postgres用户执行
[postgres@rootop ~]$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/
根据提示可以通过 /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l logfile start 启动服务。
推荐下面的脚本启动方式,启动以后会在tcp上监听5432端口。
[postgres@rootop ~]$ lsof -i:5432
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 5140 postgres 3u IPv4 2394876345 0t0 TCP localhost:postgres (LISTEN)

复制管理脚本(root操作):
[root@rootop postgresql-9.3.5]# cp contrib/start-scripts/linux /etc/init.d/postgresql
[root@rootop postgresql-9.3.5]# chmod o+x /etc/init.d/postgresql
编辑启动脚本,注意以下部分为实际信息:
#安装路径
prefix=/usr/local/pgsql
#数据目录
PGDATA=”/usr/local/pgsql/data”
#启动用户
PGUSER=postgres
#日志路径
PGLOG=”$PGDATA/serverlog”
然后就可以通过service postgresql start|stop|restart|reload|status 管理了。

开机启动:
[root@AY131126202614070132Z ~]# chkconfig postgresql on

相关配置文件:
通过 /usr/local/pgsql/data/postgresql.conf 可以配置监听地址、端口及连接数等。
listen_addresses =
port =
max_connections =
通过 /usr/local/pgsql/data/pg_hba.conf 可以配置允许远程连接的地址。
host all all 127.0.0.1/32 trust

登陆数据库:

[root@AY131126202614070132Z ~]# /usr/local/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres
psql (9.3.5)
Type "help" for help.

postgres=# \l  #查看已有的数据库
 List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
 template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
 | | | | | postgres=CTc/postgres
(3 rows)

postgres=# \q   #退出
psql 支持的参数可以通过/usr/local/pgsql/bin/psql --help 获取

安装完成。

附手册:http://www.php100.com/manual/PostgreSQL8/tutorial.html

2014-11-18
发表者 Venus
iptables DNAT目的地址映射已关闭评论

iptables DNAT目的地址映射

在百度知道里看到个提问,说在服务器上做了pptp vpn服务,客户端拨入vpn以后发布客户端的某个服务,比如iis。想实现其它非vpn客户端可以访问这个iis服务。(这种场景很少,应该是临时性的)

正好有台服务器安装了vpn,可以试一下,我本地windows装了一个iis,更改监听端口为8082。本地访问测试通过。

首先打开系统的路由转发功能:
[root@Rootop ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1         #0改为1
[root@Rootop ~]# sysctl -p  #使配置生效

添加iptables规则:
因为要做DNAT目的地址转发,也就是流入的数据包做转向。所以要在PREROUTING链上做规则。
POSTROUTING用来做数据流出方向的源地址更改,也就是SNAT源地址转换。

[root@Rootop ~]# iptables -t nat -A PREROUTING -p tcp -d 42.96.158.236 --dport 8082 -j DNAT --to 192.168.100.101:8082

对nat表的PREROUTING链,匹配tcp协议,目的地址为42.96.158.236,端口为8082进行目的地址转换到192.168.100.101的8082端口,也可以是其它端口(vpn客户端获取的ip)
通过客户端测试访问,通过。

iptables的其它常用命令:
1、列出filter表INPUT、OUTPUT、FORWARD当前规则并显示规则号码:
[root@Rootop ~]# iptables -L –line-numbers (不指定表名,默认为filter表等同于 iptables -t filter -L –line-numbers注意line前面是两个杠,博客系统自动合并)

2、删除规则:
[root@Rootop ~]# iptables -D INPUT 2 #2为INPUT链的规则号码
3、列出nat表规则:
[root@Rootop ~]# iptables -t nat -L –line-numbers #这里就需要指定表名(注意line前面是两个杠,博客系统自动合并)

4、删除PREROUTING链的第一条规则:
[root@Rootop ~]# iptables -t nat -D PREROUTING 1

2014-11-12
发表者 Venus
shell脚本统计apache日志中页面访问量已关闭评论

shell脚本统计apache日志中页面访问量

需求:在程序中有两个页面,用于某种用途,现需要统计这两个页面在每分钟内的访问量,包括总访问次数,成功次数(状态码200),失败次数。然后写入到mysql中。

mysql字段:id(自增长)、time(实际统计时间)、year、month、day、hour、visit(总访问量)、success、fail、target(目标)

脚本下载地址:https://www.rootop.org/rs/shell_scripts/analysis_apache_log.sh

#!/bin/bash
#Create By  : www.rootop.org
#MailAddress: venus@rootop.org
#Version    : analysis_apache_log ver1.0
#Data       : 2014/11/12
#Desc       : analysis apache log for witch pages with one minutes
#install bc packages with yum install -y bc if not found bc command

logdir=/var/log/httpd/80/access
logfile=$logdir/$(date +%Y%m%d)_access_log

month=$(date|awk '{print$2}')
day=$(date|awk '{print$3}')
year=$(date|awk '{print$6}')
hour=$(date|awk '{print$4}'|cut -d: -f1)
min=$(date|awk '{print$4}'|cut -d: -f2)

# get current system times to /tmp
echo $month > /tmp/month.txt
echo $day > /tmp/day.txt
echo $year > /tmp/year.txt
echo $hour > /tmp/hour.txt
echo $min > /tmp/min.txt

# compute run minute
if [ "$min" == "00" ]; then
        RUN_MINUTE=59
        else
                if [ "$(awk -F '' '{print$(NF-1)}' /tmp/min.txt)" == "0" ]; then
			echo $(awk -F '' '{print$NF}' /tmp/min.txt) > /tmp/min2.txt
                        echo $(cat /tmp/min2.txt) -1|bc > /tmp/run_minute.txt

				if [ "cat /tmp/run_minute.txt" == "0" ]; then
                                	RUN_MINUTE=00
				else
					RUN_MINUTE=0$(cat /tmp/run_minute.txt)
                                fi

                else

                         echo $(cat /tmp/min.txt) -1|bc > /tmp/run_minute.txt
				if [ "$(cat /tmp/run_minute.txt)" == "9" ]; then
					RUN_MINUTE=09
				else
	                        	RUN_MINUTE=$(cat /tmp/run_minute.txt)
				fi
                fi

fi

# compute run hour
if [ "$hour" == "00" ]; then
        if [ "$min" == "00" ]; then
        RUN_HOUR=23
	else
		RUN_HOUR=$hour
        fi
	else
		if [ "$min" == "00" ]; then
			if [ "$(awk -F '' '{print$(NF-1)}' /tmp/hour.txt)" == "0" ]; then
			echo $(awk -F '' '{print$NF}' /tmp/hour.txt) > /tmp/hour2.txt
			echo $(cat /tmp/hour2.txt) -1|bc >/tmp/run_hour.txt
				if [ "$(cat /tmp/run_hour.txt)" == "0" ]; then
					RUN_HOUR=00
	                          else
                                	RUN_HOUR=0$(cat /tmp/run_hour.txt)
				fi
			else
				echo $(cat /tmp/hour.txt) -1|bc >/tmp/run_hour.txt
				RUN_HOUR=$(cat /tmp/run_hour.txt)
			fi
		else
			RUN_HOUR=$hour
		fi
fi

# get run log file
if [ "$hour" == "00" ]; then
	if [ "$min" == "00" ]; then
	RUN_LOG=$logdir/$(date -d last-day +%Y%m%d_access_log)
		else
		RUN_LOG=$logfile
	fi
	else
		RUN_LOG=$logfile
fi

# begin log analysis
p1=cnbpush.php
p2=jlpush.php

if [ "$hour" == "00" ]; then
	if [ "$min" == "00" ]; then
		INSERT_TIME=$(date -d '-1 hours' +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE
		else
		INSERT_TIME=$(date +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE
	fi
	else
		INSERT_TIME=$(date +%Y%m%d |cut -c 3-8)$RUN_HOUR$RUN_MINUTE

fi

echo $INSERT_TIME | cut -c 1-2 > /tmp/IY
echo $INSERT_TIME | cut -c 3-4 > /tmp/IM
echo $INSERT_TIME | cut -c 5-6 > /tmp/ID
echo $INSERT_TIME | cut -c 7-8 > /tmp/IH
INSERT_YEAR=20$(cat /tmp/IY)
INSERT_MONTH=$(cat /tmp/IM)
INSERT_DAY=$(cat /tmp/ID)
INSERT_HOUR=$(cat /tmp/IH)

CN_TOTAL_ACCESS=$(cat $RUN_LOG | grep "$p1" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
CN_SUCC_ACCESS=$(cat $RUN_LOG | grep "$p1" | grep "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
CN_FAIL_ACCESS=$(cat $RUN_LOG | grep "$p1" | grep -v "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)

JL_TOTAL_ACCESS=$(cat $RUN_LOG | grep "$p2" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
JL_SUCC_ACCESS=$(cat $RUN_LOG | grep "$p2" | grep "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)
JL_FAIL_ACCESS=$(cat $RUN_LOG | grep "$p2" | grep -v "200" | awk -F ':' '{print$2$3}'| grep "$RUN_HOUR$RUN_MINUTE" | wc -l)

DB_NAME=shell_log
DB_USER=root
DB_PASS=root
DB_TAB=log

mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "INSERT INTO $DB_TAB(time,year,month,day,hour,visit,success,fail,target) VALUES ('$INSERT_TIME', '$INSERT_YEAR', '$INSERT_MONTH', '$INSERT_DAY', '$INSERT_HOUR', '$CN_TOTAL_ACCESS', '$CN_SUCC_ACCESS', '$CN_FAIL_ACCESS', 'cnbpush');"
mysql -u$DB_USER -p$DB_PASS $DB_NAME -e "INSERT INTO $DB_TAB(time,year,month,day,hour,visit,success,fail,target) VALUES ('$INSERT_TIME', '$INSERT_YEAR', '$INSERT_MONTH', '$INSERT_DAY', '$INSERT_HOUR', '$JL_TOTAL_ACCESS', '$JL_SUCC_ACCESS', '$JL_FAIL_ACCESS', 'jlpush');"

exit

放到任务计划,一分钟一执行。

2014-10-30
发表者 Venus
vsftpd一键安装配置脚本 ver1.0已关闭评论

vsftpd一键安装配置脚本 ver1.0

vsftpd一键安装配置脚本,可配合 lnmp1.0 使用(为lnmp1.0而扩展编写)。

接受用户输入ftp指定的用户名密码,可锁定用户家目录为nginx站点默认根目录,比如/usr/local/nginx/html 。在开启iptables服务时,需手动修改iptables,放行21、30000到30100端口。

下载地址:https://www.rootop.org/rs/onekey/install_vsftpd1.0.sh  推荐wget方式下载。

安装方法:

sh install_vsftpd1.0.sh  #适用于新系统,没有安装过vsftpd的机器

SourceCode:

#!/bin/bash
#Create By : www.rootop.org
#MailAddress: venus@rootop.org
#Version : install_vsftpd1.0
#Data : 2014/10/30
#Desc : for yum version
#Tested : CentOS6

# install vsftpd service
yum install -y vsftpd
clear
echo -e "\033[31m vsftpd service install done\033[0m"
echo "----------------------------------------------"
# add system user map to virtual user
#useradd -s /sbin/nologin vuser

cd /etc/vsftpd/
cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak

read -p "pls input a username: " user
echo "----------------------------------------------"
read -p "pls input a password: " pass
echo "----------------------------------------------"
echo -e "\033[31m whitch directory would u wanna to lock in? input full path. for example: /usr/local/nginx/html\033[0m"
echo -e "\033[31m the last character don't be end with /. use ctrl+c to go back command line. \033[0m"
echo "----------------------------------------------"
read -p "pls input a full path: " dire
echo "----------------------------------------------"
echo "your username is \"$user\" ,password is \"$pass\" , directory is \"$dire\" ."
echo "----------------------------------------------"

echo "initializaton vsftpd configuraton"
echo "----------------------------------------------"
# add virtual user from user in put
echo $user > vuser && echo $pass >> vuser

# create database file
db_load -T -t hash -f /etc/vsftpd/vuser /etc/vsftpd/vuser.db

# open virtual user
echo guest_enable=yes >> vsftpd.conf
# set map to system user
echo guest_username=www >> vsftpd.conf
# set vuser profile directory
echo user_config_dir=/etc/vsftpd/user_config >> vsftpd.conf
# set passive modu and port
echo pasv_enable=YES >> vsftpd.conf
echo pasv_min_port=30000 >> vsftpd.conf
echo pasv_max_port=30100 >> vsftpd.conf

# set pam
mv /etc/pam.d/vsftpd /etc/pam.d/vsftpd.bak
touch /etc/pam.d/vsftpd

if [ "$(uname -a | grep x86_64 | wc -l)" == "1" ]; then

cat >/etc/pam.d/vsftpd<<EOF
auth required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser
account required /lib64/security/pam_userdb.so db=/etc/vsftpd/vuser
EOF

 else
cat >/etc/pam.d/vsftpd<<EOF
auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vuser
account required /lib/security/pam_userdb.so db=/etc/vsftpd/vuser
EOF
fi
# make directory for vuser
mkdir user_config
cd user_config
touch $user

cat >$user<<EOF
local_root=$dire
download_enable=yes
anon_upload_enable=yes
anon_mkdir_write_enable=yes
anon_other_write_enable=yes
local_umask=022
anon_umask=022
file_open_mode=0755
EOF

chown -R www:www $dire
 if [ "$(echo $?)" == "0" ]; then
 echo "change permission for $dire done"
 echo "----------------------------------------------"
 else
 echo -e "\033[31m change permission for $dire false\033[0m"
 echo -e "\033[31m install ignore\033[0m"
 exit
 fi

echo "config vsftpd done"
echo "----------------------------------------------"
if [ "$(ps aux | grep vsftpd | grep -v "grep" | wc -l)" == "1" ]; then
 service httpd restart && chkconfig vsftpd on
 echo "----------------------------------------------"
else
service vsftpd start && chkconfig vsftpd on
echo "----------------------------------------------"
fi
echo -e "\033[31m if your iptables is open ,pls accept 21,30000-30100 port\033[0m"
echo "----------------------------------------------"
echo "vsftpd install done"
echo "----------------------------------------------"
exit

如果作为单独安装,需要修改:
#useradd -s /sbin/nologin vuser
去掉前面注释
echo guest_username=www >> vsftpd.conf
www改为vuser
chown -R www:www $dire
www:www改为vuser:vuser