VPS优化之Linux添加swap交换分区(虚拟内存)

相信很多使用VPS的朋友和我一样,没有太多的米买更多的内存来进行升级,通常使用swap交换分区来缓解系统资源紧张的压力,间接给VPS提高一下性能。

下面说一下VPS添加swap交换分区的方法(只有虚拟化为Xen、KVM、VMWare的VPS可以,OpenVZ不支持添加swap)。

1、确认是否有SWAP,可以执行free -m 命令查看,如果有swap一行且total项下不为零的话就是存在swap,就不适合再进行添加swap。

2、以下以添加2GB SWAP为例,生成文件块:(SWAP一般设置为内存的2倍,并非完全以此为标准,只适合4GB以下内存,count后面的数为要设置的swap大小xxMB)dd if=/dev/zero of=/var/swapfile bs=1M count=2048

3、创建swap文件/sbin/mkswap /var/swapfile

4、激活swap文件/sbin/swapon /var/swapfile

5、查看一下swap是否正确:/sbin/swapon -s

另外为了安全建议执行:chmod 0600 /var/swapfile

改一下权限
6、到fstab文件中添加开机自动挂载设置vi /etc/fstab

在末尾增加以下内容:/var/swapfile swap swap defaults 0 0也可以执行如下命令,感觉这样更方便echo “/var/swapfile swap swap defaults 0 0” >>/etc/fstab

SWAP删除方法:

/sbin/swapoff /var/swapfilerm -f /var/swapfile

删掉添加的自动挂载swap的设置vi /etc/fstab

顺便附上DD命令的参数介绍:

dd命令
功能:把指定的输入文件拷贝到指定的输出文件中,并且在拷贝过程中可以进行格式转换。可以用该命令实现DOS下的diskcopy命令的作用。先用dd命令把软盘上的数据写成硬盘的一个寄存文件,再把这个寄存文件写入第二张软盘上,完成diskcopy的功能。需要注意的是,应该将硬盘上的寄存文件用rm命令删除掉。系统默认使用标准输入文件和标准输出文件。
语法:dd [选项] if =输入文件(或设备名称)。

of =输出文件(或设备名称)。

ibs = bytes 一次读取bytes字节,即读入缓冲区的字节数。

skip = blocks 跳过读入缓冲区开头的ibs*blocks块。

obs = bytes 一次写入bytes字节,即写入缓冲区的字节数。

bs = bytes 同时设置读/写缓冲区的字节数(等于设置ibs和obs)。

cbs = byte 一次转换bytes字节。

count=blocks 只拷贝输入的blocks块。

puttygen.biz

阿里云debian 9更新源

更换源方法

  • 修改/etc/apt/sources.list中的文件
vim /etc/apt/sources.list
#如果提示找不到vim,请安装vim
apt-get install vim

请自行根据源与主机位置远近原则合适的源,复制粘贴到vim进入的sources.list文件中替代之前的源文件。

163源:

deb http://mirrors.163.com/debian/ jessie main non-free contrib
deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.163.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib

阿里云源:

# jessie
deb http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ jessie-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ jessie-backports main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security/ jessie/updates main non-free contrib

台湾源:

# jessie
deb http://ftp.tw.debian.org/debian/ stretch main contrib non-free
deb-src http://ftp.tw.debian.org/debian/ stretch main contrib non-free

香港源:

deb http://ftp.hk.debian.org/debian/ jessie main
deb-src http://ftp.hk.debian.org/debian/ jessie main
deb http://security.debian.org/ jessie/updates main
deb-src http://security.debian.org/ jessie/updates main
# jessie-updates  , previously known as 'volatile'
deb http://ftp.hk.debian.org/debian/ jessie-updates main
deb-src http://ftp.hk.debian.org/debian/ jessie-updates main

替换好sources.list文件内源之后,按ESC,输入:wq,保存之后接着更新

apt-get update

更换好合适的源之后,主机软件安装速度应该会有显著改变。

编译安装的nginx自动切割和压缩日志

默认nginx不会自动切割日志,当日志文件越来越大当时候,不仅浪费磁盘空间,nginx的性能也会降低。可以使用Linux的logrotate来解决这个问题。

logrotate 可以自动对日志进行切割、压缩和删除。而且自动化处理,不需要人为操作,使用非常方便。

配置 logrotate

进入 logrotate.d 目录

cd /etc/logrotate.d/

创建 nginx 文件,并写入如下内容

/Nginx日志目录/*.log
{
    daily
    rotate 30
    missingok
    dateext
    compress
    delaycompress
    notifempty
    sharedscripts
    postrotate
        if [ -f /test/nginx-1.14.2/logs/nginx.pid ]; then
            kill -USR1 `cat /test/nginx-1.14.2/logs/nginx.pid`
        fi
    endscript
}

调试脚本,测试脚本是否正确

sudo /usr/sbin/logrotate -d -f /etc/logrotate.d/nginx

手动运行运行脚本分割日志

sudo /usr/sbin/logrotate -f /etc/logrotate.d/nginx

logrotate 参数说明

配置说明
daily指定转储周期为每天
weekly指定转储周期为每周
monthly指定转储周期为每月
rotate count指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
compress通过gzip 压缩转储以后的日志
nocompress不做gzip压缩处理
create mode owner group轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate不建立新的日志文件
delaycompress和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress覆盖 delaycompress 选项,转储同时压缩
missingok如果日志丢失,不报错继续滚动下一个日志
ifempty即使日志文件为空文件也做轮转,这个是logrotate的缺省选项
notifempty当日志文件为空时,不进行轮转
mail address把转储的日志文件发送到指定的E-mail 地址
olddir directory转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir转储后的日志文件和当前日志文件放在同一个目录下
sharedscripts运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
dateext使用当期日期作为命名格式
dateformat .%s配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
size(minsize) log-size当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem),例如 size 100M

doxycycline online