Lua语言

Lua语言

一、变量

local str (局部)
str = nil (全局)

二、数据类型

nil, boolean, number, string, function, userdata, thread, table

三、操作符

==(等于)~=(不等于)>=(大于)<=(小于)and(与) or(或) not(非)

..(连接字符串)#(字符串长度)

四、字符串操作

1   string.upper(argument):将全部字符转换为大写并返回
2   string.lower(argument):将全部字符转换为小写并返回
3   string.gsub(maingString,findString,replaceString):替换
4   string.strfind(mainString,findString):在主字符串中查找 find
5   string.reverse(arg):将输入字符串颠倒并返回
6   string.format(...):返回格式化后的字符串
7   string.char(arg) 和 string.byte(arg):字符转换数值
8   string.len(arg):返回输入字符串的长度
9   string.rep(string,n): 将输入字符串 string 重复 n 次
10  ..:连接两个字符串

五、循环

-- while
while( true )
do
    print("This loop will run forever.")
end 

-- for

六、判断语句

if(a > b)then
    print(a)
else
    print(b)
end

七、数组

-- 定义数组
array = {"Lua", "Tutorial"} 
-- 使用
array[0]
-- 遍历
for key,value in ipairs(array) 
do 
    print(key, value) 
end

八、文件操作

-- 隐式文件描述符 

file = io.open("test.lua", "r")
io.input(file)
print(io.read())
io.close(file) 

file = io.open("test.lua", "a")
io.output(file)
io.write("-- End of the test.lua file")
io.close(file) 

-- 显示文件描述符 

file = io.open("test.lua", "r")
print(file:read())
file:close()

file = io.open("test.lua", "a")
file:write("--test")
file:close()

九、调试

print()

十、变量销毁

str = nil

发布日期:
分类:服务器

备份硬盘

备份硬盘



# dd
dd if=/dev/sdb of=/www/sdb.img

发布日期:
分类:服务器

Ubuntu安装php

Ubuntu安装php


#!/bin/bash


####################################### 初始化系统环境 #######################################

apt-get update
apt-get install -y gcc g++ make wget libxml2 libxml2-dev

####################################### 下载源码包 #######################################

wget -c http://cn2.php.net/distributions/php-7.0.7.tar.gz

####################################### 开始安装 PHP #######################################

tar zxvf php-7.0.7.tar.gz
cd php-7.0.7
./configure --prefix=/usr/local/php
make && make install

# 添加PHP常用命令
ln -s /usr/local/php/bin/php /usr/bin/php
ln -s /usr/local/php/bin/phpize /usr/bin/phpize
ln -s /usr/local/php/bin/php-config /usr/bin/php-config

# 配置文件
cp php.ini-production /usr/local/php/lib/php.ini
cd ..

# 修改PHP配置文件
sed -i 's/post_max_size = 8M/post_max_size = 50M/g' /usr/local/php/lib/php.ini
sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 50M/g' /usr/local/php/lib/php.ini
sed -i 's/;date.timezone =/date.timezone = PRC/g' /usr/local/php/lib/php.ini
sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/php/lib/php.ini

# 创建PHP配置文件快捷方式
ln -s /usr/local/php/lib/php.ini /etc/php.ini

LNMP脚本:lnmp

发布日期:
分类:服务器

Ubuntu安装Nginx

Ubuntu安装Nginx


#!/bin/bash

####################################### 初始化系统环境 #######################################

apt-get update
apt-get install -y gcc make libpcre3 libpcre3-dev libssl-dev

####################################### 下载源码包 #######################################

wget http://nginx.org/download/nginx-1.10.0.tar.gz

####################################### 开始安装Nginx #######################################

# 新建www用户
useradd -s /sbin/nologin www

# 解压并安装
tar zxvf nginx-1.10.0.tar.gz
cd nginx-1.10.0
sed -i 's:CFLAGS="$CFLAGS -g":# CFLAGS="$CFLAGS -g":g' auto/cc/gcc
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
make && make install
cd ..

# 快捷方式
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/nginx.conf /etc/nginx.conf

# 启动 Nginx
nginx

发布日期:
分类:服务器

使用Keepalived实现高可用

使用Keepalived实现高可用


yum install -y gcc make wget openssl openssl-devel
# apt-get install gcc make wget openssl libssl-dev

wget http://www.keepalived.org/software/keepalived-1.2.20.tar.gz

tar zxvf keepalived-1.2.20.tar.gz
cd keepalived-1.2.20
./configure --prefix=/usr/local/keepalived
make && make install
发布日期:
分类:服务器

RSA加密

RSA加密

1. 生成私钥和公钥

# 私钥
openssl genrsa -out rsa_private_key.pem 1024

# 公钥
openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
function createOpenSSLKeys($cnf)
{
    $private_key = '';
    $public_key = '';
    $config = [
        "digest_alg" => "sha512",
        "private_key_bits" => 2048,
        "private_key_type" => OPENSSL_KEYTYPE_RSA,
        'config' => $cnf
    ];
    $res = openssl_pkey_new($config);
    openssl_pkey_export($res, $private_key, null, $config);
    $public_key = openssl_pkey_get_details($res)['key'];
    return [
        'public_key' => $public_key,
        'private_key' => $private_key
    ];
}

2. 使用

$data = 'wogaslfgnladfghlanflgnaldnglanfhlnlabh';

$public_key = file_get_contents('rsa_public_key.pem');
$public = openssl_pkey_get_public($public_key);

$encode = '';

// 公钥加密
openssl_public_encrypt($data, $encode, $public);

$private_key = file_get_contents('rsa_private_key.pem');
$private = openssl_pkey_get_private($private_key);

$decode = '';

// 私钥解密
openssl_private_decrypt($encode, $decode, $private);

echo $decode;
发布日期:
分类:服务器

用户表数据库结构

用户表数据库结构



--
-- 表的结构 `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(60) NOT NULL,
  `email` varchar(100) NOT NULL,
  `auth_key` varchar(32) NOT NULL DEFAULT '',
  `password_hash` varchar(128) NOT NULL,
  `status` tinyint(3) NOT NULL DEFAULT '10',
  `registered` datetime NOT NULL,
  PRIMARY KEY (`user_id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


--
-- 表的结构 `user_meta`
--

CREATE TABLE IF NOT EXISTS `user_meta` (
  `user_meta_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint(20) unsigned NOT NULL,
  `meta_key` varchar(200) NOT NULL,
  `meta_value` text NOT NULL,
  PRIMARY KEY (`user_meta_id`),
  UNIQUE KEY `user_key` (`user_id`,`meta_key`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;


发布日期:
分类:服务器

MySQL双主架构

MySQL双主架构



yum install -y mysql-server

# my.cnf
log-bin=mysql-bin
server-id=1
relay_log = mysql-relay-bin
log_slave_updates = 1
sync_binlog = 1
auto-increment-offset = 1
auto-increment-increment = 2

# FLUSH TABLES WITH READ LOCK;
# UNLOCK TABLES;
# stop slave;
# reset slave;
# reset master;

GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'172.21.23.%' IDENTIFIED BY '123456';

CHANGE MASTER TO MASTER_HOST='172.21.23.69',
MASTER_USER='slave',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=0;

iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

start slave;
show slave status\G

 

发布日期:
分类:服务器

自建DNS服务器

自建DNS服务器

1. 使用bind搭建内网DNS服务器



#!/bin/bash

yum install -y bind bind-utils bind-chroot

sed -i 's:127.0.0.1:any:g' /etc/named.conf
sed -i 's:localhost:any:g' /etc/named.conf

# dig > /var/named/named.ca
curl ftp://ftp.rs.internic.net/domain/named.cache > /var/named/named.ca

/sbin/iptables -I INPUT -p tcp --dport 53 -j ACCEPT
/sbin/iptables -I INPUT -p udp --dport 53 -j ACCEPT

2. 自定义域名解析



chown named.named -R /var/named

setsebool -P named_write_master_zones on

zone "mogublog.net" IN {
    type master;
    file "named.mogublog.net";
    allow-update { 127.0.0.1; };
};

// named.mogublog.net

$TTL 1D
@   IN  SOA  mogublog.net. root.mogublog.net. (
                            0       ; serial
                            1D      ; refresh
                            1H      ; retry
                            1W      ; expire
                            3H )    ; minimum


@   IN  NS      ns.mogublog.net.
NS  IN  A       127.0.0.1
www IN  A       8.8.8.8

// update

[root@server named]# nsupdate
> server 127.0.0.1
> update add www.mogublog.net 86400 IN A 8.8.8.8
> send
> quit

发布日期:
分类:服务器

Nginx日志分割

一 自动按照日期分割

if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
    set $year $1;
    set $month $2;
    set $day $3;
}

access_log /www/logs/access_$year-$month-$day.log;

二 定时任务分割

#!/bin/bash

## 获取昨天的 yyyy-MM-dd
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)

## 移动文件
mv /www/logs/access.log /www/logs/access_${YESTERDAY}.log

## 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
发布日期:
分类:服务器