H5直播站点运维笔记一 服务器篇

服务器篇

服务器采购腾讯云S2系列,Intel Xeon E5-2680 v4(2.4 GHz),4核16G,内网带宽1.5Gbps,公网带宽5Mbps。

一、CentOS6基础操作

使用版本CentOS release 6.10 (Final):

1. 查看服务器版本

(1)查看Linux版本
cat /etc/redhat-release
(2)查看内核版本
uname -a

2. 查看服务器性能

(1)实时监控Linux系统状况,cpu、内存、用户数、进程
top
动态监视进程活动与实时系统负载状态

-d 指定每两次屏幕信息刷新之间的时间间隔。当然用户可以使用s交互命令来改变之。
-p 通过指定监控进程ID来仅仅监控某个进程的状态。
-q 该选项将使top没有任何延迟的进行刷新。如果调用程序有超级用户权限,那么top将以尽可能高的优先级运行。
-S 指定累计模式
-s 使top命令在安全模式中运行。这将去除交互命令所带来的潜在危险。
-i 使top不显示任何闲置或者僵死进程。
-c 显示整个命令行而不只是显示命令名

(2)第三方实时进程监控
htop

Htop与top命令非常相似,功能更加丰富、更先进的交互性和实时性的进程监控工具
安装
yum install epel-release
yum install htop

(3)第三方磁盘I/O监控
iotop
实时监测和显示磁盘I/O

安装
yum install iotop

(4)第三方系统实时活动情况报告
sar 1
(System ActivityReporter系统活动情况报告)文件的读写情况、系统调用的使用情况、磁盘I/O、CPU效率、内存使用状况、进程活动及IPC有关的活动

安装 yum -y install sysstat
常用选项:
-A 包括绝大多数选项。
-b 报告I/O和传输速率统计。
-B 报告分页统计。
-c 报告进程创建活动。
-d 报告每个活动的块设备。(指磁盘)
-e [ hh:mm:ss ] 设置sar命令执行的结束时间,时间为24小时制格式。
-I { irq | SUM | ALL | XALL } :报告给定中断的统计。
-n { DEV | EDEV | NFS | NFSD | SOCK | ALL } :报告网络统计。
-P { cpu | ALL } :报告每个处理器的统计信息,可以统计指定或全部处理器。
-q 报告队列长度和平均负载。
-r 报告内存和交换空间的利用情况统计。
-R 报告内存统计。
-u 报告CPU利用率。
interval 两次输出之间的间隔时间。
count 按照这个时间间隔统计的次数。

(5)服务器处理连接任务情况
netstat -n | awk '/^tcp/ {++y[$NF]} END {for(w in y) print w, y[w]}'
(6)查看所有磁盘分区信息
fdisk -l
(7)查看文件系统磁盘格式、空间占用、挂载位置情况
df -h
(8)查看网卡
ip a s
(9)查看端口
netstat -ant //列出所有TCP的连接
netstat -npl //查看服务器端口工作状态
netstat -s //显示自从系统启动以来,各个协议的总体数据信息。
(10)查看进程
ps -ef
(11)查看IO使用磁盘平均使用率
iostat -x 1 2
(12)查看进程明细
pidstat -w -t -l -C “关键字”
(13)监控网络数据收发
sar -n TCP,UDP 1
(14)查看系统资源使用限制
ulimit -a

[root@VM-0-15-centos ~]# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 31384
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 65536
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 31384
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

3. 文件查找

find / -name 文件名

[root@VM-0-15-centos ~]# find / -name redis
/data/redis
/etc/rc.d/init.d/redis
/etc/redis
[root@VM-0-15-centos ~]# find / -name php-fpm.conf
/root/php-7.3.15/sapi/fpm/php-fpm.conf
/usr/local/php/etc/php-fpm.conf

4. 软件查找

(1)查看软件是否安装
rpm -qa | grep 软件名 //查看软件是否安装返回版本信息

[root@VM-0-15-centos ~]# rpm -qa | grep vsftp
vsftpd-2.2.2-24.el6.x86_64

rpm -qal | grep 软件名 //返回软件安装包相关所有文件路径

[root@VM-0-15-centos ~]# rpm -qal | grep vsftp
/etc/logrotate.d/vsftpd
/etc/pam.d/vsftpd
/etc/rc.d/init.d/vsftpd
/etc/vsftpd
/etc/vsftpd/ftpusers
/etc/vsftpd/user_list
/etc/vsftpd/vsftpd.conf
/etc/vsftpd/vsftpd_conf_migrate.sh
/usr/sbin/vsftpd
/usr/share/doc/vsftpd-2.2.2
/usr/share/doc/vsftpd-2.2.2/AUDIT
/usr/share/doc/vsftpd-2.2.2/BENCHMARKS
/usr/share/doc/vsftpd-2.2.2/BUGS
/usr/share/doc/vsftpd-2.2.2/COPYING
/usr/share/doc/vsftpd-2.2.2/Changelog
/usr/share/doc/vsftpd-2.2.2/EXAMPLE
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/README.configuration
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/README.configuration
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/PER_IP_CONFIG/hosts.allow
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_HOSTS
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_HOSTS/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/README.configuration
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/logins.txt
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS_2
/usr/share/doc/vsftpd-2.2.2/EXAMPLE/VIRTUAL_USERS_2/README
/usr/share/doc/vsftpd-2.2.2/FAQ
/usr/share/doc/vsftpd-2.2.2/INSTALL
/usr/share/doc/vsftpd-2.2.2/LICENSE
/usr/share/doc/vsftpd-2.2.2/README
/usr/share/doc/vsftpd-2.2.2/README.security
/usr/share/doc/vsftpd-2.2.2/REWARD
/usr/share/doc/vsftpd-2.2.2/SECURITY
/usr/share/doc/vsftpd-2.2.2/SECURITY/DESIGN
/usr/share/doc/vsftpd-2.2.2/SECURITY/IMPLEMENTATION
/usr/share/doc/vsftpd-2.2.2/SECURITY/OVERVIEW
/usr/share/doc/vsftpd-2.2.2/SECURITY/TRUST
/usr/share/doc/vsftpd-2.2.2/SIZE
/usr/share/doc/vsftpd-2.2.2/SPEED
/usr/share/doc/vsftpd-2.2.2/TODO
/usr/share/doc/vsftpd-2.2.2/TUNING
/usr/share/doc/vsftpd-2.2.2/vsftpd.xinetd
/usr/share/man/man5/vsftpd.conf.5.gz
/usr/share/man/man8/vsftpd.8.gz
/usr/share/augeas/lenses/dist/vsftpd.aug
/usr/lib/python2.6/site-packages/sos/plugins/vsftpd.py
/usr/lib/python2.6/site-packages/sos/plugins/vsftpd.pyc
/usr/lib/python2.6/site-packages/sos/plugins/vsftpd.pyo

(2)查找软件安装路径
whereis 软件名

[root@VM-0-15-centos ~]# whereis php
php: /usr/bin/php /usr/local/php

(3)查找软件运行文件所在路径(执行文件)
which 软件名

[root@VM-0-15-centos ~]# which php  
/usr/bin/php

5. 用户系统

(1)用户组列表
cat /etc/group
(2)用户列表
cat /etc/passwd
(3)查看可以登录系统的用户
cat /etc/passwd | grep -v /sbin/nologin | cut -d : -f 1
(4)查看登录用户
who
(5)查看用户登录历史记录
last
(6)root权限修改其他用户密码
passwd

6. 远程登录工具

(1)SecureCRT (个人推荐)
(2)PuTTY

二、FTP ( VSFTPD )

1.工具

FileZilla

2.启动ftp服务

(1)查询是否安装
rpm -q vsftpd

[root@VM-0-15-centos ~]# rpm -q vsftpd
vsftpd-2.2.2-24.el6.x86_64

(2)检查vsftpd是否运行
netstat -ntpl | grep vsftpd

[root@VM-0-15-centos ~]# netstat -ntpl | grep vsftpd
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      1645/vsftpd 

(3)启动vsftpd服务
service vsftpd start
(4)开机自启动
chkconfig vsftpd on

[root@VM-0-15-centos ~]# chkconfig --list vsftpd
vsftpd          0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@VM-0-15-centos ~]# chkconfig vsftpd on
[root@VM-0-15-centos ~]# chkconfig --list vsftpd
vsftpd          0:off   1:off   2:on    3:on    4:on    5:on    6:off

3.用户权限设置

(1)用户

  • 增加用户ftpuser,ftpuser用户的主目录为/home/myweb
    useradd ftpuser -d /home/myweb -s /sbin/nologin

-g 所属组 -d 用户主目录 -s 所用的shell

  • 为用户ftpuser设置密码
    passwd ftpuser
  • 设置目录权限和目录归属用户组
    chmod –R 777 /home/myweb
    chown -R mywebuser.mywebuser /home/myweb

(2)用户组

  • 创建用户组
    groupadd webgroup
  • 给已有的用户增加工作组
    usermod -G groupname username //添加到指定组,但以前添加的组会清空
    gpasswd -a username groupname //添加用户到一个组,并保留以前添加的组

4.配置访问权限

(1)阻止匿名访问和切换根目录

  • 编辑配置文件
    vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO #禁用匿名用户
chroot_local_user=YES # 禁止切换根目录
chroot_list_enable=YES #是否启用chroot_list_file配置项指定的用户列表文件
chroot_list_file=/etc/vsftpd/chroot_list #只允许此列表用户能切换到用户自身目录的上级目录
allow_writeable_chroot=YES #允许用户自身目录写权限

(2)shell接口白名单

  • 查看ftp用户使用的shell
    cat /etc/passwd
ftpuser:x:1000:1001::/home/myweb:/sbin/nologin
  • 添加shell白名单
    vim /etc/shells
/sbin/nologin

(3)添加登录白名单

  • 编辑配置文件
    vi /etc/vsftpd/vsftpd.conf
#配置用户拦截机制,启用 /etc/vsftpd/user_list 文件且设置为白名单
userlist_enable=YES
userlist_deny=NO

userlist_deny=YES 时user_list是黑名单

  • 编辑user_list列表
    vim /etc/vsftpd/user_list
用户名1
用户名2
...
在列表中添加用户名

(4)重启vsftpd服务

  • service vsftpd restart

三、Nginx

使用版本 nginx/1.16.1

1.查询Nginx进程

ps -ef | grep nginx

[root@VM-0-15-centos ~]# ps -ef | grep nginx
root      1744     1  0 Feb13 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       1746  1744  0 Feb13 ?        00:00:03 nginx: worker process                                          
root     22781 18418  0 15:22 pts/0    00:00:00 grep nginx

2.查询Nginx运行状态

service nginx status

[root@VM-0-15-centos ~]# service nginx status
nginx (pid 1746 1744) is running...

3.查找配置

(1)查找nginx目录
find /(查找范围) -name nginx -type d

[root@VM-0-15-centos ~]# find / -name nginx -type d            
/usr/local/nginx

(2)查找nginx配置文件
find /(查找范围) -name nginx.conf -print

[root@VM-0-15-centos ~]# find /usr -name nginx.conf -print
/usr/local/nginx/conf/nginx.conf

4.测试配置文件

/usr/local/nginx/sbin/nginx -t

[root@VM-0-15-centos ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

5.启动Nginx

(1)启动
/usr/local/nginx/sbin/nginx
(2)关闭
/usr/local/nginx/sbin/nginx -s stop
(3)重启
/usr/local/nginx/sbin/nginx -s reload

6.开机自启动

chkconfig --add nginx
chkconfig nginx on

[root@VM-0-15-centos ~]# chkconfig --list nginx
nginx           0:off   1:off   2:off   3:off    4:off   5:off   6:off
[root@VM-0-15-centos ~]# chkconfig nginx on
[root@VM-0-15-centos ~]# chkconfig --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

7.配置文件1 格式范例

server {
   listen 80;
   server_name domain.com;
   set $root_path '/data/www/laravel/public';
   
   location / {
      root $root_path;
      index index.php index.html;

      # Nginx找不到文件时,转发请求给后端Apache
      error_page 404 @proxy;

      # css, js 静态文件设置有效期1天
      location ~ .*\.(js|css)$ {
         access_log off;
         expires      1d;
      }

      # 图片设置有效期3天
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
         access_log off;
         expires      3d;
      }
   }

   # 动态文件.php请求转发给后端Apache
   location ~ \.php$ {
     #proxy_redirect off;
     #proxy_pass_header Set-Cookie;
     #proxy_set_header Cookie $http_cookie;

      # 传递真实IP到后端
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass   http://127.0.0.1:8080;
   }

   location @proxy {
      proxy_set_header Host $http_host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_pass http://127.0.0.1:8080;
   }
}

8.配置文件2 LiveWeb实例

server {
    listen 80;
    server_name bi.mydomain.cn;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}
server {
    listen  443 ssl;
    server_name bi.brandsstory.cn;
    index index.php;
    root /home/web/bi/public;
    ssl_certificate /usr/local/nginx/cert/1_bi.mydomain.cn_bundle.crt;
    ssl_certificate_key  /usr/local/nginx/cert/2_bi.mydomain.cn.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    location / {
        #为了使Laravel正常工作,所有的请求都应该被传递给Laravel本身,即所有的请求					都被传递给了index.php,Laravel的应用主文件。如果这一步没有配置,那么我们					只能够打开项目主页,其余页面将无法跳转。
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ~ \.php$ {
        root /home/web/bi/public;
        #trim on;
        #fastcgi_pass  127.0.0.1:9001;
        fastcgi_pass   unix:/dev/shm/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        #fastcgi_index index.php;
}

9.性能优化

(1)查看进程权限
  • 查找nginx 的worker进程ID
    ps -elf |grep nginx
[root@VM_0_15_centos ~]# ps -elf |grep nginx
1 S root     27008     1  0  80   0 - 11909 sys_rt Aug31 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
5 S www      27009 27008  0  80   0 - 24758 ep_pol Aug31 ?        02:01:25 nginx: worker process                                          
0 R root     27801 27641  0  80   0 - 25831 -      11:26 pts/0    00:00:00 grep nginx
  • 查看权限
[root@VM_0_15_centos ~]# cat /proc/27009/limits
Limit                     Soft Limit           Hard Limit           Units     
Max cpu time              unlimited            unlimited            seconds   
Max file size             unlimited            unlimited            bytes     
Max data size             unlimited            unlimited            bytes     
Max stack size            10485760             unlimited            bytes     
Max core file size        0                    unlimited            bytes     
Max resident set          unlimited            unlimited            bytes     
Max processes             63691                63691                processes 
Max open files            65535                65535                files     
Max locked memory         65536                65536                bytes     
Max address space         unlimited            unlimited            bytes     
Max file locks            unlimited            unlimited            locks     
Max pending signals       63691                63691                signals   
Max msgqueue size         819200               819200               bytes     
Max nice priority         0                    0                    
Max realtime priority     0                    0                    
Max realtime timeout      unlimited            unlimited            us   
(2)最大客户数
  • nginx作为http服务器的时候

公式
max_clients = worker_processes * worker_connections/2

  • nginx作为反向代理服务器的时候

公式
max_clients = worker_processes * worker_connections/4

(3)工作进程数
  • 默认:worker_processes: 1
  • 调大:worker_processes: CPU核心数,(双核4线程,可以设置为4)

一般一个进程足够了,你可以把连接数设得很大。(worker_connections: 10,000)
如果有SSL、gzip这些比较消耗CPU的工作,而且是多核CPU的话,可以设为和CPU的数量一样。(worker_processes: CPU核心数)
或者要处理很多很多的小文件,而且文件总大小比内存大很多的时候,也可以把进程数增加,以充分利用IO带宽(主要似乎是IO操作有block)

(4)开启多核CPU
  • worker_cpu_affinity 0001 0010 0100 1000;

//0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推
//2核是 01,四核是0001,8核是00000001,有多少个核,就有几位数,1表示该内核开启,0表示该内核关闭。

(5)单个工作进程允许连接数
  • 默认:worker_connections: 1024
  • 调大:worker_connections: 65535

1)connections不是随便设置的,而是与两个指标有重要关联,一是内存,二是操作系统级别的“进程最大可打开文件数”。
2)内存:每个连接数分别对应一个read_event和write_event事件,一个连接数大概占用232字节,2个事件总占用96字节,一个连接总共占用328字节,65535个连接数大概会占用内存: 65535 * 328 / 1024 / 1024 = 20.5M
3)单进程最大可打开文件数:可打开文件数受限于操作系统,可通过 ulimit -n 命令查询,以前是1024,现在是65535,
4)worker_rlimit_nofile 65535;
nginx提供的最大可打开文件数指令,该指令与使用ulimit对用户的设置是同样的效果,此指令的值将覆盖ulimit的值

(6)backlog积压
  • 默认:512
  • 调大:listen 80 default backlog=4096;

四、Apache

1.查询Apache进程

ps -ef | grep httpd

2.查看Apache是否安装

find / -name 'httpd'

3.启动Apache

(1)启动
service httpd start
(2)停止
service httpd stop
(3)重启
service httpd restart
(4)开机自启动
chkconfig --add httpd
chkconfig httpd on

4.配置文件

编辑httpd配置文件
vim /etc/httpd/conf/httpd.conf
(1)全局环境

ServerTokens OS //告知用户端WWW服务器的版本与操作系统
ServerRoot "/etc/httpd"  //Apache安装路径
PidFile run/httpd.pid //放置PID的目录,方便软件管理,是相对路径
Timeout 120 //持续连接如果等待超过120秒则连接断开
KeepAlive Off //是否允许持续性的连接,预设为否
MaxKeepAliveRequests 100 //保持的连接的人数,改成0就是说没有人数的限制
KeepAliveTimeout 15 //在允许KeepAlive条件下,保持连接的超出时间
Listen 80 //监听的端口号
LoadModule access_module modules/mod_access.so
……(下面省略)显示载入的模块组
Include conf.d/*.conf使得目录/etc/httpd/conf.d/下的配置文件*.conf都会被读入
User apache //子进程的用户
Group apache //由谁启动Apache服务器

(2)主服务配置

ServerAdmin root@localhost //网页出错给谁发信通知
UseCanonicalName Off //是否使用标准主机名称,预设为否
DocumentRoot "/var/www/html" //网页存放的路径,文档的根目录
DirectoryIndex index.html  //定义主页文件
AddDefaultCharset UTF-8  //字符编码,中文需要改为gb2312或者gbk
<Directory "/var/www/html">   # 网站容器开始标识
	Options Indexes FollowSymLinks   //找不到主页时,以目录的方式呈现,并允许链接到网站根目录以外
	AllowOverride None //none不使用.htaccess控制,all允许
	Require all granted  //granted表示运行所有访问,denied表示拒绝所有访问
</Directory>    # 容器结束

五、CentOS 6的service 与 7的systemctl 命令对比

从 CentOS 7.x 开始,CentOS 开始使用 systemd 服务来代替 daemon,原来管理系统启动和管理系统服务的相关命令全部由 systemctl命 令来代替。
service 与 systemctl 命令对比

daemon命令systemctl命令
service [服务] startsystemctl start [服务]
service [服务] stopsystemctl stop [服务]
service [服务] restartsystemctl restart [服务]

本章完


Laravel 框架篇

…详见运维笔记 《Laravel 框架篇》

数据库篇

…详见运维笔记 《数据库篇》

压测篇

…详见运维笔记 《压测篇》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值