分类: 菜鸟Linux笔记

菜鸟学习LINUX笔记

  • Linux vps修改时间改成北京时间

    1.先登陆你的ssh,命令:date 看下时间是不是北京时间,如果不是我们把他改成北京时间。

    2.命令:

    rm -rf /etc/localtime

    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

     

    3.再用date命令看下,你的vps的时间改变了吗?大部分Linux vps的母鸡是允许每个vps修改时间的!

    如果此时你的vps时间已经改成北京时间就不用看下面了,如果此时你的vps时间还没有修改好。再试下命令:

    ntpdate stdtime.sinica.edu.tw

    使用ntpdate stdtime.sinica.edu.tw 时候如果提示这个命令,先安装 ntpdate就可以了。

     

    centos 系统的vps用命令:

    yum -y install ntpdate ntp

    Ubuntu系统的:

    sudo apt-get install -y ntpdate ntp

  • 关于apache2伪静态问题

    刚把博客搬了一个新家,之前是用一键安装包配置的VPS,虽然配置完了能很正好的运行,但是有很多想地方想修改却不知道从何处下手。这次搬家,全是手动配置的APACHE2环境,其实手运配置还是蛮简单的,只要自己细心点,但是配置完成后发现怎么也无法实现伪静态,开始以为是伪静态规则出了问题,但经过很多次试验,感觉发现不是伪静态规则的问题,看了看PHPINFO信息,原来里面没有mod_rewrite模块,这下犯愁了,因为APACHE2安装是用默认安装的,难道默认安装apache2没有安装mod_rewrite模块?试着用google和百度查看一些如何重新加入mod_rewrite模块的方法,网上有大多数是垃圾文章,还好没试,不然试了又要重新配置VPS。看了一些资料,原来默认安装的apache2是有安装mod_rewrite模块,只是没有启动而已

    a2enmod rewrite 启动

    然后系统会提示你重新加载apache2 ,最后再用phpinfo查看,就已经有mod_rewrite模块了

  • Optimize Apache and MySQL for a 256MB VPS

    sudo nano /etc/mysql/my.cnf

     

    Add the following line into the section [mysqld]:

    skip-innodb

     

    Next locate the line skip-external-locking and add skip-locking below that line.

     

    Now find the section labeled “Fine Tuning”. Change/add the settings in that section to match those below:

     

    key_buffer = 16K

    max_allowed_packet = 1M

    thread_stack = 64K

    thread_cache_size = 4

    sort_buffer = 64K

    net_buffer_length = 2K

     

    Save the file (ctrl+x) and restart MySQL: sudo /etc/init.d/mysql restart

    If you run MySQLtuner again you will see that the “memory” warning is gone, ignore the aother warnings for the moment (you need to run the script after a few days again to get exact test results)

     

    Optimize Apache in prefork mode

    If you followed the Ubuntu tutorial we’ve mentioned in the first paragraph, your Apache setup should run in prefork mode. The default settings are much to high, open the file sudo nano /etc/apache2/apache2.conf andchange the following settings:

     

     

    Timeout 45

    KeepAlive On

    MaxKeepAliveRequests 200

    KeepAliveTimeout 3

     

    StartServers 5

    MinSpareServers 5

    MaxSpareServers 10

    MaxClients 30

    MaxRequestsPerChild 2000

     

    That’s all, restart Apache using sudo /etc/init.d/apache2 restart. If you know more tweaks, please share.

  • 购买VPS后简单测试

    一、CPU信息及性能查看

    cat /proc/cpuinfo

    二、硬盘IO性能测试

    root@h:~# dd if=/dev/zero of=test bs=64k count=4k oflag=dsync && rm test

    4096+0 records in

    4096+0 records out

    268435456 bytes (268 MB) copied, 6.19022 s, 43.4 MB/s

    root@h:~# dd if=/dev/zero of=test bs=64k count=16k conv=fdatasync && rm test

    16384+0 records in

    16384+0 records out

    1073741824 bytes (1.1 GB) copied, 6.32504 s, 170 MB/s

    测试结果,如果超过10M,对正常建站就无影响。超过50M,就是非常给力状态。

  • nginx wolf 支持伪静态文件.htaccess

    if (!-e $request_filename) {

    rewrite ^/admin(.+)$ /admin/index.php?$1 last;

    rewrite ^/(.+)$ /index.php?WOLFPAGE=$1 last;

    }

  • ssh只允许指定的用户登陆

    方法1:在/etc/pam.d/sshd文件第一行加入

    auth required pam_listfile.so item=user sense=allow file=/etc/sshusers

    onerr=fail

    然后在/etc下建立sshusers文件,编辑这个文件,加入你允许使用ssh服务的用户名,不用重新启动sshd服务。

     

    方法2:pam规则也可以写成deny的

    auth required pam_listfile.so item=user sense=deny file=/etc/sshusers

    onerr=succeed

     

    方法3:在sshd_config中设置AllowUsers,格式如

    AllowUsers a,b,c

    重启sshd服务,则只有a/b/c3个用户可以登陆

  • linux 限制root用户SSH登录

    1、修改SSHD服务的配置文件/etc/ssh/sshd_config

    将#PermitRootLogin yes 改为PermitRootLogin no

    2、重启sshd服务使配置生效

    /etc/rc.d/init.d/sshd restart

     

    为了增强linux的安全性,应该限制只有wheel组的用户可以使用su命令切换到root和其他用户:

     

    1、修改su命令的认证配置文件/etc/pam.d/su

     

    去掉以下两行前的#号注释

    #auth sufficient pam_wheel.so trust use_uid (去年注释后ROOT组成员使用su切换到root不需要密码)

    #auth required pam_wheel.so use_uid  (去年注释后只允许ROOT组成员使用su切换到root)

    2、创建一个普通用户,然后将这个用户加入到wheel组即可!以后就只有这个普通用户和root可以使用su命令了

    usermod -G 组 用户

     

    su命令使用简介:

     

    su 直接切换到root用户,但保持原用户的环境变量;

    su – 切换到root用户,并使用root用户的环境变量;

     

    su -l test 切换到test用户,并使用test的环境变量;

  • nginx 禁止ip访问及未绑定的域名跳转

    ngixn 禁止ip访问和未绑定ip的访问

     

    server {

    listen 80 default;

    server_name _;

    return 500;

    }

     

     

     

    nginx 将未绑定的域名跳转

     

    server {

    listen 80 default;

    rewrite ^(.*) http://www.kogoogle.com permanent;

    }

  • 优化 MySQL 效能

    最理想是在安装时已做好优化,但如果已经安装好 MySQL 后才需要做优化的话,可以通过设定 /etc/my.cnf 来做。如果不熟悉 my.cnf 内的设定也没关係,可以直接使用 MySQL 的范例档,在 /usr/share/mysql/ 下会有 my-huge.cnf, my-large.cnf, my-medium.cnf 及 my-small.cnf 几个档案,根据伺服器的硬件而选择适合的档案:

     

    my-huge.cnf: 适合 1GB – 2GB RAM的主机使用。

    my-large.cnf: 适合 512MB RAM的主机使用。

    my-medium.cnf: 只有 32MB – 64MB RAM 的主机使用,或者有 128MB RAM 但需要运行其他伺服器,例如 web server。

    my-small.cnf: 记忆体少於 64MB 时适用这个,MySQL 会佔用较少资源。

    我选用了 my-large.cnf,只要执行以下指令便完成了,但如果你原先已经有 my.cnf,请先备份起来及自行调整:

     

    shell> cp /usr/share/mysql/my-large.cnf /etc/my.cnf

    shell> /etc/init.d/mysqld restart

  • nginx报错nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

    nginx报错nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

    仔细参考nginx中文文档发现,发送信号USR2时,自动启动新的可执行文件,再手动启动的话就会报错

    本质上也算是一种地址冲突吧

     

    如果平时安装时遇见该错误,就是80端口被占用,

    找出占用80端口的程序杀掉即可

    代码如下:

    shell>netstat -tunlp

    shell>pkill 占用80端口的程序

    即可消除该错误