service在centos6,debian7 和centos7,debian8 的不同

我是一个很老套的人,一直在坚持使用centos 6 和 debian 7,但是无奈很多软件已经取消了对centos 6 和debian 8 的支持,现在只能加班加点开始适应debian 8 和 centos 7.

在centos 6 和debian 7中,脚本一直使用的是/etc/init.d/xxx, 在系统升级到Centos 7 和 Debian 8后,虽然以前的启动脚本仍然可以使用,但是在centos 7 和 debian8中, 系统的service 已经由sysvinit 转向了systemd,进而也有了很多的变化

CentOS 7 集成了 RHEL 7 的新特性,systemd的使用也大幅提高了系统服务的运行效率,同时也变的简单而易用了许多.

Unit 的文件位置,一般主要有三个目录:

/lib/systemd/system
/run/systemd/system
/etc/systemd/system

这三个目录的配置文件优先级一次从低到高,如果同意选项三个地方都配置了,优先级高的会覆盖优先级低的. 

对于第三方或者自己定义的service 来说,我们一般放在/etc/systemd/service下面。

更多

一晃就是一年

真的是时间飞逝,一晃就是一年.

上一篇更新还是在去年的8月18号,在过去的一年时间里,发生了很多事情,其中最大的好事就是大胖儿子出生了,给整个家庭带来了不少欢声笑语.

压力更大,我也需要更加努力了

Novogara 的母鸡切小鸡Benchmark

这是荷兰一台服务器切成的小鸡的benchmark,看来都比quadranet 的要好。。

 

Benchmark started on Fri Aug 18 05:03:52 EDT 2017
Full benchmark log: /root/bench.log

System Info
-----------
Processor : Intel(R) Xeon(R) CPU E3-1270 v3 @ 3.50GHz
CPU Cores : 2
Frequency : 3500.076 MHz
Memory : 1821 MB
Swap : 3903 MB
Uptime : 3 min,

OS : CentOS release 6.9 (Final)
Arch : x86_64 (64 Bit)
Kernel : 2.6.32-696.6.3.el6.x86_64
Hostname : XXX



Speedtest (IPv4 only)
---------------------
Your public IPv4 is xxxxxx

Location Provider Speed
CDN Cachefly 109MB/s

Atlanta, GA, US Coloat 15.1MB/s
Dallas, TX, US Softlayer 6.85MB/s
Seattle, WA, US Softlayer 12.7MB/s
San Jose, CA, US Softlayer 12.4MB/s
Washington, DC, US Softlayer 9.26MB/s

Tokyo, Japan Linode 7.41MB/s
Singapore Softlayer 6.73MB/s

Rotterdam, Netherlands id3.net 97.7MB/s
Haarlem, Netherlands Leaseweb 58.8MB/s



Disk Speed
----------
I/O (1st run) : 87.2 MB/s
I/O (2nd run) : 75.8 MB/s
I/O (3rd run) : 75.8 MB/s
Average I/O : 79.6 MB/s

Linode 1GB Benchmark

这是新开的一台DNS 的服务器,正好拿来测试比较一下

 

Benchmark started on Mon Jul 17 13:19:13 UTC 2017
Full benchmark log: /root/bench.log

System Info
-----------
Processor : Intel(R) Xeon(R) CPU E5-2680 v2 @ 2.80GHz
CPU Cores : 1
Frequency : 2799.988 MHz
Memory : 988 MB
Swap : MB
Uptime : 7 min,

OS : \S
Arch : x86_64 (64 Bit)
Kernel : 4.9.15-x86_64-linode81
Hostname : xx.xxxx.xxx



Speedtest (IPv4 only)
---------------------
Your public IPv4 is 162.xx.xx.xxx

Location Provider Speed
CDN Cachefly 105MB/s

Atlanta, GA, US Coloat 49.0MB/s
Dallas, TX, US Softlayer 27.7MB/s
Seattle, WA, US Softlayer 18.8MB/s
San Jose, CA, US Softlayer 12.4MB/s
Washington, DC, US Softlayer 50.1MB/s

Tokyo, Japan Linode 10.9MB/s
Singapore Softlayer 3.64MB/s

Rotterdam, Netherlands id3.net 6.50MB/s
Haarlem, Netherlands Leaseweb 73.5MB/s



Disk Speed
----------
I/O (1st run) : 210 MB/s
I/O (2nd run) : 611 MB/s
I/O (3rd run) : 607 MB/s
Average I/O : 476 MB/s

Quadranet 母鸡切小鸡benchmark

这是在我的一台母鸡上切的小鸡,进行的benchmark

母鸡是普通硬盘 RAID 10

 

System Info
-----------
Processor : Intel(R) Xeon(R) CPU E3-1241 v3 @ 3.50GHz
CPU Cores : 8
Frequency : 3500.070 MHz
Memory : 7839 MB
Swap : 10199 MB
Uptime : 14:10,

OS : CentOS release 6.9 (Final)
Arch : x86_64 (64 Bit)
Kernel : 2.6.32-696.3.2.el6.x86_64
Hostname : www.google.com



Speedtest (IPv4 only)
---------------------
Your public IPv4 is XX.XX.XXX.XX

Location Provider Speed
CDN Cachefly 84.9MB/s

Atlanta, GA, US Coloat 15.2MB/s
Dallas, TX, US Softlayer 56.5MB/s
Seattle, WA, US Softlayer 66.4MB/s
San Jose, CA, US Softlayer 90.8MB/s
Washington, DC, US Softlayer 29.1MB/s

Tokyo, Japan Linode 16.7MB/s
Singapore Softlayer 10.7MB/s

Rotterdam, Netherlands id3.net 5.33MB/s
Haarlem, Netherlands Leaseweb 48.1MB/s



Disk Speed
----------
I/O (1st run) : 41.3 MB/s
I/O (2nd run) : 37.0 MB/s
I/O (3rd run) : 37.2 MB/s
Average I/O : 38.5 MB/s

CentOS 上使用 NFS 的教程以及介绍

NFS 可以把NFS server 上的文件及文件共享给NFS client 服务器

两个概念,NFS Server(Master) 和 NFS Client

NFS Server 可以把本服务器上的文件和文件夹共享给NFS Client, NFS Client 可以在共享的文件和文件夹上进行读写操作,并且实时返回给NFS Master

NFS Server 一般作为存储服务器,常见为使用RAID 5 和 RAID 10 的存储服务器,容量比较大. 对于一些图片站和网盘站点,网站所在的服务器一般为NFS Client,NFS Client 把图片和文件直接存储在NFS Server上

为了方便对NFS进行user id 和 group id 进行统一管理,我们一般需要NFS Server 和 NFS Client 使用同一种操作系统, 比如说centos 和 centos. 当然了,分别使用不同的操作系统,比如说centos 和 debian 也是可以的,就是有点麻烦而已.

下面进行详细的安装步骤:

无论是CentOS 6 和 CentOS 7, 安装步骤基本都一致,这里推荐CentOS 7. RHEL系的操作系统越新越好用

服务器配置:

NFS Server Hostname: server.unixmen.local
NFS Server IP Address: 192.168.1.101/24

 

NFS Client Hostname: client.unixmen.local
NFS Client IP Address: 192.168.1.102/24

NFS服务器端配置:

CentOS 6:

yum install nfs-utils nfs-utils-lib

chkconfig nfs on 
service rpcbind start
service nfs start

CentOS 7:

yum install nfs-utils nfs-utils-lib

systemctl enable rpcbind
systemctl enable nfs-server
systemctl enable nfs-lock
systemctl enable nfs-idmap

systemctl start rpcbind
systemctl start nfs-server
systemctl start nfs-lock
systemctl start nfs-idmap

因为CentOS 7 在系统上改了好多,所以安装稍微有点不同,其实都是同一个库

下面配置共享目录,假设我们需要共享/home 目录给client

vi /etc/exports

然后添加下面:

/home  192.168.0.102 (rw,sync,no_root_squash,no_subtree_check)

参数的解释:

rw: 这个选项允许client server 在共享目录上进行读和写的操作

ro: 只读

sync: 将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;

no_root_squash: 登入 NFS 主机使用分享目录的使用者,如果是 root 的话,那么对于这个分享的目录来说,他就具有 root 的权限!

root_squash: 在登入 NFS 主机使用分享之目录的使用者如果是 root 时,那么这个使用者的权限将被压缩成为匿名使用者,通常他的 UID 与 GID 都会变成 nobody 那个系统账号的身份

all_squash: 将远程访问的所有普通用户及所属组都映射为匿名用户或用户组 (nfsnobody)

no_all_squash: 与all_squash 相反, 默认配置

anonuid=xxx: 将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
anongid=xxx: 将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

这里需要注意的, NFS 进行映射的时候,用的是uid 和 gid, 而非是用户名和group 的名字. 所以通常的做法是, 在NFS server 和 NFS client 上使用相同的用户名和用户组,以及相同UID和 GID

相同的用户名和用户组这个好解决, UID 和 GID不同的话,需要update uid 和 gid以使他们相同. 在centos 上我们经常使用军哥的lnmp,用户名和用户组都为www,加入uid 为501, gid 为501, 则我们可以用

(rw, sync, no_subtree_check, all_squash, anonuid=501, anongid=501)

async: 将数据先保存在内存缓冲区中,必要时才写入磁盘;

subtree: 若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

配置完成以后,我们需要export这个配置来时他生效:

exportfs -a

下面我们需要配置NFS Client

安装NFS:

yum install nfs-utils nfs-utils-lib

mount 共享目录给 /home

mount -t nfs 192.168.1.101:/home /home

然后我们就可以通过df -h 看到mount 的目录或者mount 来查看

如果需要开机启动时自动mount,我们需要编辑/etc/fstab

192.168.1.101:/home /home nfs rw,sync,hard,intr 0 0

移除NFS mount

umount /home

Nginx 处理请求的顺序问题

对于Name-Based virtual servers, nginx 只会测试request 中的header field “Host” 来决定把这个请求route 给谁. 如果这个请求不存在Host这个field, 或者Host 的值不能match 任何一个server_name, 那么nginx就会把这个请求route 给这个port 的default server. 如果我们没有在conf中指明哪个server block 为default_server, 那么nginx 就会把第一个server block认为是default server.

name-based virtual server 的例子如下:

server {
 listen 80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 80;
 server_name example.com www.example.com;
 ...
}

default_server 的例子如下:

server {
 listen 80 default_server;
 server_name example.net www.example.net;
 ...
}

 

对于mixed name-based and ip-based virtual server, 例如:

server {
 listen 192.168.1.1:80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 192.168.1.1:80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 192.168.1.2:80;
 server_name example.com www.example.com;
 ...
}

nginx 的处理顺序是先ip和port 的组合,然后才是server_name.

举个例子就是,如果在192.168.1.1:80上收到了一个www.example.com 的请求的话,那么这个请求就会被发到192.168.1.1:80 的default server, 因为对于这个ip和port 的组合,里面没有这个server_name.

 

MySql 的 ONLY_FULL_GROUP_BY

最近把mysql 升级到了5.7.18 然后就发现了一个很明显的变化, ONLY_FULL_GROUP_BY 这个mysql mode 被加了进来, 按照sql 99 的标准这是好事.

但是实际上, mysql 的这个版本只能说almost identical or very close to, 不能说完全相等

简单的说,就是

That is, MySQL 5.7.5m15 will by default reject only those GROUP 
BY-queries that include non-aggregated expressions in the SELECT-list that 
are not functionally dependent upon the GROUP BY-list.

This not only means that you cannot mess up your GROUP BY-queries anymore 
(as MySQL will now reject an improper GROUP BY query), 
it will also not require you to write non-sensical "dummy" aggregates
 over expressions that can only have one value per aggregated 
result row. Hurrah!

详细的解释请看这篇文章:

http://rpbouman.blogspot.nl/2014/09/mysql-575-group-by-respects-functional.html

php-fpm 内存过高,CPU占有率过高带来的优化和调整

如果不是昨天我的论坛遭受到了NTP DDos攻击,我也不会遇到这个问题.

首先,这些问题都是和php-fpm 的配置有关,如果你使用的是军哥的LNMP,那么这个配置文件位于

/usr/local/php/etc/php-fpm.conf

首先先解决内存过高的问题,这个是由于php-fpm占用了内存以后并没有及时释放造成的,但是我们可以通过配置文件强制他释放内存,可以在配置文件里面加上

pm.max_requests = 500

500这个值适用于大部分的服务器配置,但是如果你的内存过大或者过小,可以适当增大或者减小这个值,取决于你的内存的实际使用率. 

既然说到了php-fpm 的配置,那么就得好好的说下php-fpm 的 process management

php-fpm 的process management 有两种工作方式,一种是static, 一种是dynamic, 对于小的cloud 或者vps 一般用static,但是其实大部分时间我们都在使用dynamic

pm = dynamic
pm.max_children = 160
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 40

和process management 有直接关系的是这四个参数, pm.max_children 在pm=static 的时候有用,在pm = dynamic 的时候, 剩下三个参数才开始起作用

这里问题的关键在于,如何设置pm.max_children 的值

对于不同的php application,每个php-fpm instance 占用的内存也不一样,需要具体application 具体分析,但是这里我们可以去个average , 30M

假设你的服务器有8G的内存,其中的6GB 可以划给php-fpm, 那么

pm.max_children = 6000MB / 30MB =200

也就是说在这种情况下,200这个值是比较合适的. 这里需要注意的是,赋予max_children 一个很高的值,并不代表会带来高性能,也许会拖垮整个服务器

剩下的三个参数,按照实际情况,设置的差不多就行了

你可以用如下的命令来实际查看一下single php-fpm usage:

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

另外别忘了开启php-fpm 的 slowlog,这样方便你以后查看日志来进行debug

slowlog = /usr/local/php/var/log/slow.log

对于CPU 占有率过高的问题,这个其实并不是php-fpm 的问题,而是你写的php 代码的问题,这个时候可以通过设置pm.status_path 开查看到底是哪个php application 占用的cpu 资源

详细可以参见这篇文章:

https://brandonwamboldt.ca/understanding-the-php-fpm-status-page-1603/