firewalld规则配置介绍

在基于RHEL7的服务器上,firewalld是一个可动态管理的防火墙服务,提供IPv4和IPv6防火墙规则定义和区域的支持。它可以直接代替iptables管理服务器的网络活动,能直接作用于内核的netfilter代码。
本文简要介绍了如何通过firewall-cmd命令管理和配置防火墙,假定所有命令都以root权限执行。

一. 启动和状态

1. 查询firewalld运行状态

$ firewall-cmd --state

如果返回not running,表示服务没有启动,输入下面命令启动firewallD

$ service firewalld start

对firewalld做了修改,通过下面的命令执行

$ firewall-cmd --reload

配置好firewalld之后,通过systemd命令使服务开机启动

$ systemctl enable firewalld

二. 规则设置

1. 查询firewalld当前规则

$ firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: dhcpv6-client http ssh ssh-custom
ports:
protocols:
masquerade: no
forward-ports:
sourceports:
icmp-blocks:
rich rules:

2. 添加删除service规则

添加httpservice

$ firewall-cmd --permanent --zone=public --add-service=http

删除httpservice

$ firewall-cmd --permanent --zone=public --remove-service=http

3. 添加删除port规则

允许放行7777 TCP端口

$ firewall-cmd --permanent -zone=public --add-port=7777/tcp

允许放行多个端口

$ firewall-cmd --permanent -zone=public --add-port=7777-8000/tcp

删除端口

$ firewall-cmd --permanent --zone=public --remove-port=7777/tcp

查看当前打开的端口

$ firewall-cmd --zone=public --list-ports

4. 通过”RICH LANGUAGE”创建复杂规则

rich rule 命令的格式和结构如下所示

rule [family="rule family"]
[ source [NOT] [address="address"] [mac="mac-address"] [ipset="ipset"] ]
[ destination [NOT] address="address" ]
[ element ]
[ log [prefix="prefix text"] [level="log level"] [limit value="rate/duration"] ]
[ audit ]
[ action ]

NOTE: 一条rule作用于一个特定的zone。一个zone可以包含多条rule。如果几条rule互相影响或产生冲突,第一条匹配的rule会生效。

  • 封锁一个IP
$ firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.1' reject"
  • 封锁一个IP段, 192.168.1.0 ~ 192.168.1.254
$ firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.0/24' reject"

更详细rich rule的介绍参见redhat文档:
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html

5. 关于192.168.1.0/24的说明

192.168.0.0/24
表示网络码占24位,就是说该网络上可以有254个主机(2^8-2)
IP范围:192.168.0.1~192.168.0.254 192.168.0.255是广播地址,不能被主机使用

192.168.0.0/16
表示网络码占16位,也就是说该网络上可以有65534个主机(2^16-2)
IP范围:192.168.0.1~192.168.255.254 192.168.255.255是广播地址,不能被主机使用。

Firewalld常用命令介绍

Firewalld防火墙中所使用到的命令可以分为三大类:安装卸载、维护和策略操作。

安装

在Centos7中默认已经安装了firewalld,如果需要自行安装,可以直接使用yum安装

[[email protected] ~]# yum install firewalld

如果需要使用图形化配置工具还需要安装firewall-config

[[email protected] ~]# yum install firewall-config

这样firewalld就安装完成了,不过要注意firewalld跟iptables不能同时使用,所以在使用firewalld之前首先需要将iptables停用。

维护命令

firewalld的维护相对来说是非常简单的,其命令主要分为三大类:启动停止和查询状态、设置开机自动启动以及更新规则。

启动停止和查询状态

在Centos7中默认将原来的服务管理工具service升级为了systemctl,其实原来的service只是一个脚本执行工具,而systemctl的功能非常强大,关于systemctl的详细用法大家可以阅读linux中国的一篇文章,地址是:https://linux.cn/article-5926-1.html,这篇文章写的非常详细,所以学生就不再给大家做补充了,下面来说一说怎么用它来操作firewalld。

启动

[[email protected] ~]# systemctl start firewalld

停止

[[email protected] ~]# systemctl stop firewalld

重启

[[email protected] ~]# systemctl restart firewalld

查询状态

[[email protected] ~]$ systemctl status firewalld

另外,对于firewalld来说还可以使用自身的firewall-cmd工具来查询运行状态

[[email protected] ~]$ firewall-cmd --state

设置开机自动启动

设置开机自动启动也是使用systemctl来操作的,命令如下

启用开机自动启动

[[email protected] ~]$ systemctl enable firewalld

禁用开机自动启动

[[email protected] ~]$ systemctl disable firewalld

查看自动启动状态

[[email protected] ~]$ systemctl is-enabled firewalld

当然,systemctl的这些命令不只适用于firewalld,也适用于其他服务,使用时只要将firewalld换成相应服务的名称就可以了。

更新规则

直接使用firewall-cmd修改的规则是不需要更新就可以直接生效的,但是如果加了–permanent参数,或者直接编辑xml文件之后就需要我们手动reload了,firewall-cmd提供了两个更新规则的参数:–reload和–complete-reload,前者只是更新需要更新规则,而且更新的过程中不会影响现有的连接,而后者在更新时会将所有的规则清除掉然后重建,而且为了安全考虑,在更新之前首先会将策略设置为DROP,等更新完成之后再恢复为ACCEPT,这样就会对现有连接造成影响,所以如果没有特殊需求我们应该尽量使用前者。具体命令如下

[[email protected] ~]# firewall-cmd --reload
[[email protected] ~]# firewall-cmd --complete-reload

策略操作

对于firewalld来说最重要的就是策略操作了,策略操作主要有三种方法:使用firewall-config操作、使用firewall-cmd操作和直接编辑xml文件,学生在上节已经给大家介绍过了,firewall-config是图形化工具,firewall-cmd是命令行工具,我们这里主要以命令行工具为主来给大家介绍。

firewall-cmd中关于规则的命令非常多,如果在这里全部列出来再给大家解释一遍应该效果也不会太好,所以学生在后面讲到具体相关内容时再给大家讲相应的命令,比如讲到zone的时候给大家介绍跟zone相关的命令,这样大家更容易理解和记忆。

firewall-cmd中的命令虽然非常多,但是有四大类使用频率非常高的命令:–add-xxx、–remove-xxx、–query-xxx、–get-xxx以及–list-xxx,这里前两个非常容易理解,一个是添加一个是删除,而后三个从名字上就不太容易区分了,下面学生给大家解释一下。

  • –query-xxx主要用于布尔值的查询,比如
[[email protected] ~]$ firewall-cmd --zone=public --query-service=ssh

这个命令用于查询在public这个zone中是否包含ssh这个服务。

  • –get-xxx主要用于获取特定的内容,比如
[[email protected] ~]$ firewall-cmd --get-zones

这样就可以获取到预设的zones,默认情况下返回结果为

block dmz drop external home internal public trusted work
  • –list-xxx主要用于按一定条件进行查询(不过有的list命名也不需要条件),比如
[[email protected] ~]$ firewall-cmd --zone=public --list-services

这个命令可以返回public这个zone中所包含的services。

query还是比较容易理解的,但是get和list从字面上并不容易区分,刚开始学生以为get命令用于获取单个结果,list用于获取多个结果,但后来发现并不是这样。当然,我们在使用的时候按照文档中的说明来使用就可以了。

另外,在firewall-cmd中有一个比较特殊的参数:

–permanent,他表示是否将修改后的规则保存下来,如果不加这个参数,那么所做的修改当时会立即生效,但是在firewalld重启之后就会丢失,而加上这个参数后所做的修改就会永久保存下来,不过这时的修改不会立即生效而是需要reload后才可以生效。其实这个也非常容易理解,当不加–permanent修改规则时firewalld会实际修改运行时的规则,而如果加了这个参数firewalld其实是去修改的xml配置文件,和我们直接编辑xml文件一样,所以就需要reload才可以生效。

CentOS 7 firewalld防火墙简单配置介绍

学习apache安装的时候需要打开80端口,由于centos 7版本以后默认使用firewalld后,网上关于iptables的设置方法已经不管用了,想着反正iptable也不会用,索性直接搬官方文档,学习firewalld了,好像比iptables要简单点了。

官方文档地址:https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Security_Guide/sec-Using_Firewalls.html#sec-Introduction_to_firewalld

1、firewalld简介

  • firewalld是centos7的一大特性,最大的好处有两个:支持动态更新,不用重启服务;第二个就是加入了防火墙的“zone”概念

  • firewalld有图形界面和工具界面,由于我在服务器上使用,图形界面请参照官方文档,本文以字符界面做介绍

  • firewalld的字符界面管理工具是 firewall-cmd

  • firewalld默认配置文件有两个:/usr/lib/firewalld/ (系统配置,尽量不要修改)和 /etc/firewalld/ (用户配置地址)

zone概念:

硬件防火墙默认一般有三个区,firewalld引入这一概念系统默认存在以下区域(根据文档自己理解,如果有误请指正):

  • drop:默认丢弃所有包
  • block:拒绝所有外部连接,允许内部发起的连接
  • public:指定外部连接可以进入
  • external:这个不太明白,功能上和上面相同,允许指定的外部连接
  • dmz:和硬件防火墙一样,受限制的公共连接可以进入
  • work:工作区,概念和workgoup一样,也是指定的外部连接允许
  • home:类似家庭组
  • internal:信任所有连接

对防火墙不算太熟悉,还没想明白public、external、dmz、work、home从功能上都需要自定义允许连接,具体使用上的区别还需高人指点

2、安装firewalld

root执行 # yum install firewalld firewall-config

3、运行、停止、禁用firewalld

  • 启动:# systemctl start firewalld
  • 查看状态:# systemctl status firewalld 或者 firewall-cmd –state
  • 停止:# systemctl disable firewalld
  • 禁用:# systemctl stop firewalld

4、配置firewalld

  • 查看版本:$ firewall-cmd –version
  • 查看帮助:$ firewall-cmd –help

查看设置:

  • 显示状态:$ firewall-cmd –state
  • 查看区域信息: $ firewall-cmd –get-active-zones
  • 查看指定接口所属区域:$ firewall-cmd –get-zone-of-interface=eth0
  • 拒绝所有包:# firewall-cmd –panic-on
  • 取消拒绝状态:# firewall-cmd –panic-off
  • 查看是否拒绝:$ firewall-cmd –query-panic

  • 更新防火墙规则:# firewall-cmd –reload
    # firewall-cmd –complete-reload

两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务

将接口添加到区域,默认接口都在public

# firewall-cmd --zone=public --add-interface=eth0

永久生效再加上 –permanent 然后reload防火墙

设置默认接口区域

# firewall-cmd --set-default-zone=public

立即生效无需重启

打开端口(貌似这个才最常用)
查看所有打开的端口:

# firewall-cmd --zone=dmz --list-ports

加入一个端口到区域:

# firewall-cmd --zone=dmz --add-port=8080/tcp

若要永久生效方法同上

打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹,这个不详细说了,详情参考文档

# firewall-cmd --zone=work --add-service=smtp

移除服务

# firewall-cmd --zone=work --remove-service=smtp

还有端口转发功能、自定义复杂规则功能、lockdown,由于还没用到,以后再学习。

centos7使用systemd管理supervisor进程

介绍

Systemd

Systemd 是 Linux 系统工具,用来启动守护进程,已成为大多数发行版的标准配置。可以通过 systemctl –version 命令来查看使用的版本

常用命令

# 立即启动一个服务
$ sudo systemctl start apache.service
# 立即停止一个服务
$ sudo systemctl stop apache.service
# 重启一个服务
$ sudo systemctl restart apache.service
# 杀死一个服务的所有子进程
$ sudo systemctl kill apache.service
# 重新加载一个服务的配置文件
$ sudo systemctl reload apache.service
# 重载所有修改过的配置文件
$ sudo systemctl daemon-reload
# 显示某个 Unit 的所有底层参数
$ systemctl show httpd.service

Supervisor

Supervisor是是一个用python写的进程管理程序,不仅仅可以用来管理进程,还可以用来做开机启动。它有但不限于以下一些功能:

重启机器后,能够自启动。
平时有个方便的进程查看方式。
能够有个方便的方式重启进程。

配置方法这里就不做记录了,不过要注意,默认的配置文件里面会把一些supervisor生成的重要文件放到 /tmp 目录下面,操作系统可能会把这些文件进行删除,导致 supervisorctl 命令由于找不到这些以前放到 /tmp 的文件而操作不了已经启动的supervisor进程。

方法

为了能够在机器启动之后自动启动supervisor,需要把supervisor进程配置进systemd,

步骤:

1、进入目录 /usr/lib/systemd/system/,增加文件 supervisord.service,来使得机器启动的时候启动supervisor,文件内容

# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

2、激活开机启动命令

systemctl enable supervisord.service

3、启动supervisor进程

systemctl start supervisord.service

4、关闭supervisor进程

systemctl stop supervisord.service

5、如果修改了supervisor.service文件,可以通过reload命令来重新加载配置文件

systemctl reload supervisord.service

检查

可以通过 ps 命令可以查看supervisor 是否启动,并且可以查看supervisor管理的进程是否启动。

从ceph对象中提取RBD中的指定文件

1. 前言

之前有个想法,是不是有办法找到rbd中的文件与对象的关系,想了很久但是一直觉得文件系统比较复杂,在fs 层的东西对ceph来说是透明的,并且对象大小是4M,而文件很小,可能在fs层进行了合并,应该很难找到对应关系,最近看到小胖有提出这个问题,那么就再次尝试了,现在就是把这个实现方法记录下来

这个提取的作用个人觉得最大的好处就是一个rbd设备,在文件系统层被破坏以后,还能够从rbd提取出文件,我们知道很多情况下设备的文件系统一旦破坏,无法挂载,数据也就无法读取,而如果能从rbd中提取出文件,这就是保证了即使文件系统损坏的情况下,数据至少不丢失

本篇是基于xfs文件系统情况下的提取,其他文件系统有时间再看看,因为目前使用的比较多的就是xfs文件系统

本篇也回答了一个可能会经常被问起的问题,能告诉我虚拟机里面的文件在后台存储在哪里么,看完本篇就知道存储在哪里了

2. XFS文件系统介绍

[root@lab8106 ~]# mkfs.xfs -f /dev/rbd0p1 
warning: device is not properly aligned /dev/rbd0p1
meta-data=/dev/rbd0p1            isize=256    agcount=9, agsize=162816 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1310475, imaxpct=25
         =                       sunit=1024   swidth=1024 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=8 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

XFS文件系统采取是AG管理的,每个AG维护自己的inode和数据,所以XFS文件系统是一种很容易扩展的文件系统,本篇里面主要用到的命令是xfs_bmap这个命令

[root@lab8106 ~]# xfs_bmap -lvp /etc/fstab
/etc/fstab:
 EXT: FILE-OFFSET      BLOCK-RANGE        AG AG-OFFSET        TOTAL FLAGS
   0: [0..7]:          26645424..26645431  1 (431024..431031)     8 00000

一个文件最小就是8个block(512b),也就是4k,这个因为上面默认的xfs的格式化就是data bsize=4K,这个值可以自行调整的,本篇尽量用默认常规的参数来讲例子

查看man xfs_bmap这个命令可以看到:

Holes are marked by replacing the startblock..endblock with hole. All the file offsets and disk blocks are in units of 512-byte blocks, no matter what the filesystem’s block size is.

意思是这个查询到的里面的计数单位都是512-byte,不管上层设置的block大小是多少,我们知道文件系统底层的sector就是512-byte,所以这个查询到的结果就可以跟当前的文件系统的sector的偏移量联系起来,这里强调一下,这个偏移量的起始位子为当前文件系统所在分区的偏移量,如果是多分区的情况,在计算整个偏移量的时候就要考虑分区的偏移量了,这个会在后面用实例进行讲解的

rbd的对象是不清楚内部分区的偏移量,所以在rbd层进行提取的时候是需要得到的是分区当中的文件相对整个磁盘的一个sector的偏移量

3. rbd的对象结构

[root@lab8106 ~]# rados -p rbd ls|grep data
rbd_data.25a636b8b4567.00000000000009ff
rbd_data.25a636b8b4567.00000000000001dd
rbd_data.25a636b8b4567.0000000000000000
rbd_data.25a636b8b4567.000000000000009f
rbd_data.25a636b8b4567.0000000000000459
rbd_data.25a636b8b4567.000000000000027e
rbd_data.25a636b8b4567.00000000000004ff
rbd_data.25a636b8b4567.000000000000027c
rbd_data.25a636b8b4567.000000000000027d
rbd_data.25a636b8b4567.0000000000000001
rbd_data.25a636b8b4567.000000000000013e
rbd_data.25a636b8b4567.00000000000003ba
rbd_data.25a636b8b4567.000000000000031b
rbd_data.25a636b8b4567.00000000000004f8

rbd被xfs格式化以后会产生一些对象,这些对象是以16进制名称的方式存储在后台的,也就是rbd大小一定的情况下对象数目是一定的,也就是名称也是一定的

[root@lab8106 ~]# parted -s /dev/rbd0 unit s print
Model: Unknown (unknown)
Disk /dev/rbd0: 20971520s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start      End        Size       File system  Name     Flags
 1      1953s      10485759s  10483807s  xfs          primari
 2      10485760s  20963327s  10477568s               primari

上面可以看到rbd0的sector个数为20971520s
20971520s*512byte=10737418240byte=10485760KB=10240MB
sector的大小一定,总rbd大小一定的情况下sector的数目也是一定的,本篇实例的rbd大小

[root@lab8106 ~]# rbd info zp
rbd image 'zp':
    size 10000 MB in 2500 objects
    order 22 (4096 kB objects)
    block_name_prefix: rbd_data.25a776b8b4567
    format: 2
    features: layering
    flags: 
    create_timestamp: Sat Jul 22 18:04:12 2017

4. sector和ceph object的对应关系的查询

这个就像个map一样,需要把这个关系给找到,一个sector的区间对应到object的map,这里我用python写个简单的方法来做查询,也可以自己用其他语言来实现

首先查询到rbd的对象数目

[root@lab8106 ~]# rbd info zp
rbd image 'zp':
    size 10000 MB in 2500 objects
    order 22 (4096 kB objects)
    block_name_prefix: rbd_data.25a776b8b4567
    format: 2
    features: layering
    flags: 
    create_timestamp: Sat Jul 22 18:04:12 2017

处理脚本如下:

vim getsecob.py

添加下面内容

#! /bin/python
# *-* conding=UTF-8 *-*

import commands

def main():
    getmap(2500)


def getmap(object):
    sector=int(object)*4096*1024/512
    print "object:"+str(object)
    print "sector:"+str(sector)
    incre=sector/object
    for item in range(int(object)):
        a=int(item*8192)
        b=int((item+1)*8192-1)
        print str([a,b])+"  -->  "+"%016x" %item

if __name__ == '__main__':
    main()

其中getmap后面为对象数目

输出是这个形式的:

[root@lab8106 ~]# python getsecob.py
object:2500
sector:20480000
[0, 8191]  -->  0000000000000000
[8192, 16383]  -->  0000000000000001
[16384, 24575]  -->  0000000000000002
[24576, 32767]  -->  0000000000000003
[32768, 40959]  -->  0000000000000004
[40960, 49151]  -->  0000000000000005
···

对rbd0进行分区,分区后的结果如下

[root@lab8106 ~]# parted -s /dev/rbd0 unit s print
Model: Unknown (unknown)
Disk /dev/rbd0: 20480000s
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start      End        Size       File system  Name     Flags
 1      1953s      10240000s  10238048s               primari
 2      10248192s  20471807s  10223616s               primari

这个是个测试用的image,大小为10G分成两个5G的分区,现在我们在两个分区里面分别写入两个测试文件,然后经过计算后,从后台的对象中把文件读出

mount /dev/rbd0p1 /mnt1
mount /dev/rbd0p2 /mnt2
cp /etc/fstab /mnt1
cp /etc/hostname /mnt2

首先获取文件在分区上的sector的偏移量

[root@lab8106 ~]# xfs_bmap -lvp /mnt1/fstab 
/mnt1/fstab:
 EXT: FILE-OFFSET      BLOCK-RANGE      AG AG-OFFSET        TOTAL FLAGS
   0: [0..7]:          8224..8231        0 (8224..8231)         8 01111

可以得到是(8224..8231)共8个sector
从上面的分区1的start的sector可以知道起始位置是1953,那么相对于磁盘的偏移量就变成了

(8224+1953..8231+1953) = (10177..10184)

这里说下,这个地方拿到偏移量后,直接通过对rbd设备进行dd读取也可以把这个文件读取出来,这个顺带讲下,本文主要是从对象提取:

dd if=/dev/rbd0 of=a bs=512 count=8 skip=10177

bs取512是因为sector的单位就是512b
这样就把刚刚的fstab文件读取出来了,skip就是文件的sector相对磁盘的起始位置,count就是文件所占的block数目

继续我们的对象提取方式,上面的(10177..10184)这个我们根据上面那个脚本输出的对象列表来找到对象

[8192, 16383] —> 0000000000000001
获取名称,这个因为我的是测试环境,就只有一个匹配,多个image的时候要过滤出对用的rbd的对象,用prifix过滤即可
[root@lab8106 ~]# rados -p rbd ls|grep 0000000000000001
rbd_data.25a776b8b4567.0000000000000001

下载对象

[root@lab8106 ~]# rados -p rbd get rbd_data.25a776b8b4567.0000000000000001 rbd_data.25a776b8b4567.0000000000000001

根据偏移量计算对象中的偏移量

(10177..10184)
[8192, 16383]  -->  0000000000000001

得到

10177-8192=1985

dd if=rbd_data.25a776b8b4567.0000000000000001 of=a bs=512 count=8 skip=1985

得到的文件a的内容即为之前文件的内容

准备取第二个分区的文件

[root@lab8106 ~]# xfs_bmap -lvp /mnt2/hostname 
/mnt2/hostname:
 EXT: FILE-OFFSET      BLOCK-RANGE      AG AG-OFFSET        TOTAL FLAGS
   0: [0..7]:          8224..8231        0 (8224..8231)         8 01111

8224+10248192..8231+10248192=10256416..10256423

从磁盘方式

[root@lab8106 ~]# dd if=/dev/rbd0 of=a bs=512 count=8 skip=10256416

从对象方式
10256416..10256423 对应
[10256384, 10264575] —> 00000000000004e4
对象偏移量

10256416-10256384=32
rados -p rbd get 
[root@lab8106 ~]# rados -p rbd get rbd_data.25a776b8b4567.00000000000004e4 rbd_data.25a776b8b4567.00000000000004e4

获取文件

[root@lab8106 ~]# dd if=rbd_data.25a776b8b4567.00000000000004e4 of=a bs=512 count=8 skip=32

如果文件比较大的情况,可能出现就是文件是跨对象的,那么还是跟上面的提取方法一样,然后进行提取后的文件进行合并即可

5. 总结

在存储系统上面存储的文件必然会对应到底层磁盘的sector,而sector也是会一一对应到后台的对象的,这个在本文当中得到了验证,所以整个逻辑就是,在文件系统层找到文件对应的sector位置,然后再在底层把sector和对象关系找好,就能从找到文件在对象当中的具体的位置,也就能定位并且能提取了,本篇是基于xfs的,其他文件系统只要能定位文件的sector,就可以在底层找到文件,这个以后会补充其他文件系统进来。

使用HAProxy Keepalived实现主备及负载均衡

HAProxy提供高可用性负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。根据官方数据,其最高极限支持10G的并发。

HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。其支持从4层至7层的网络交换,即覆盖所有的TCP协议。就是说,Haproxy甚至还支持Mysql的负载均衡。如果说在功能上,能以proxy反向代理方式实现WEB均衡负载,这样的产品有很多。包括Nginx,ApacheProxy,lighttpd,Cheroke等。但要明确一点的,Haproxy并不是Http服务器。以上提到所有带反向代理均衡负载的产品,都清一色是WEB服务器。简单说,就是他们能自个儿提供静态(html,jpg,gif..)或动态(php,cgi..)文件的传输以及处理。而Haproxy仅仅,而且专门是一款的用于均衡负载的应用代理。其自身并不能提供http服务。开始Haproxy主备高可用测试

未分类

测试系统:

  • CentOS6.7/Ubuntu15.04

IP信息:

  • 主Haproxy:192.168.15.132

  • 备Haproxy:192.168.15.133

  • VIP:192.168.15.135(www.test.com/img.test.com)

  • Real1:192.168.15.128

  • Real2:192.168.15.130

  • Real3:192.168.15.140

主LB与备LB均配置

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf         #开启数据包转发

echo "net.ipv4.ip_nonlocal_bind = 1" >> /etc/sysctl.conf   #允许监听非本地地址

sysctl -p

安装haproxy

cd /usr/local/src

CentOS系统:

yum install wget gcc gcc-c++ autoconf automake make

Ubuntu系统:

sudo apt-get install build-essential  libtool

wget http://pkgs.fedoraproject.org/repo/pkgs/haproxy/haproxy-1.4.24.tar.gz/86422620faa9759907563d5e0524b98c/haproxy-1.4.24.tar.gz

tar -xvzf haproxy-1.4.24.tar.gz

cd haproxy-1.4.24

make TARGET=linux2628 && make install  

#kernel版本大于2.6.28的,使用"TARGET=linux2628",否则使用"TARGET=linux26"。

添加haproxy用户:

useradd -d /var/lib/haproxy -s /bin/false haproxy

创建配置文件

mkdir -p /etc/haproxy 

cp -r  /usr/local/src/haproxy-1.4.24/examples/errorfiles  /etc/haproxy/errorfiles

cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy  #拷贝示例文件

cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak      #备份示例文件

负载均衡①:

vi /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 5000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon
defaults
        log     global
        mode    http          #所处理的类别 (#7层 http;4层tcp  )
        option  httplog
        option  httpclose
        option  dontlognull   #不记录健康检查的日志信息
        option  forwardfor    #后端服务器需要获得客户端真实ip时配置的参数,可以从Http Header中获得客户端ip
        option  redispatch    #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
        retries 3
        maxconn 4000
        contimeout      8000
        clitimeout      80000
        srvtimeout      80000
listen Web_LB  
      bind *:80
      mode http   #7层:http;4层:tcp
      cookie Web_LB  insert
      balance roundrobin
      option httpclose
      option forwardfor
      #option httpchk GET /index.html #心跳检测的文件
      server Real1 192.168.15.128:80 cookie Real1 check inter 1500 rise 3 fall 3 weight 1
      server Real2 192.168.15.130:80 cookie Real2 check inter 1500 rise 3 fall 3 weight 1
      server Real3 192.168.15.140:80 cookie Real3 check inter 1500 rise 3 fall 3 weight 1
      #服务器定义,"cookie Real1"表示serverid为Real1,"check inter 1500"是检测心跳频率,"rise 3"是3次正确认为服务器可用,"fall 3"是3次失败认为服务器不可用,weight代表权重
      srvtimeout 30000 
listen stats  192.168.15.135:9999
    mode http
    stats enable
    stats refresh 5s
    stats hide-version
    stats realm Haproxy Statistics #监控页面提示信息
    stats uri /haproxy-status
    stats auth test:123456
    acl allow src 192.168.15.0/16
    stats admin if TRUE  #手动启用/禁用后端服务器(haproxy-1.4.9以后版本)
    errorfile 403 /etc/haproxy/errorfiles/403.http
    errorfile 500 /etc/haproxy/errorfiles/500.http
    errorfile 502 /etc/haproxy/errorfiles/502.http
    errorfile 503 /etc/haproxy/errorfiles/503.http
    errorfile 504 /etc/haproxy/errorfiles/504.http

负载均衡②:

vi /etc/haproxy/haproxy.cfg
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 5000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon
defaults
        log     global
        mode    http 
        option  httplog
        option  httpclose
        option  dontlognull  
        option  forwardfor   
        option  redispatch   
        retries 3
        maxconn 4000
        contimeout      8000
        clitimeout      80000
        srvtimeout      80000
listen stats  192.168.15.135:9999
    mode http
    stats enable
    stats refresh 5s
    stats hide-version
    stats realm Haproxy Statistics 
    stats uri  /haproxy-status
    stats auth test:123456
    acl allow src 192.168.15.0/16
    stats admin if TRUE
frontend www
    bind *:80
    acl web hdr_reg(host) -i ^(www.test.com|test.com)$
    #acl后面是规则名称。如果请求的域名满足正则表达式中的2个域名返回true(-i是忽略大小写),则分发请求至webserver的作用域。
    acl img hdr(host) -i img.test.com
    #如果访问img.test.com就分发到imgserver这个作用域。
    use_backend webserver if web
    use_backend imgserver if img
    default_backend webserver

backend webserver
    mode http
    balance roundrobin               #默认的负载均衡方式
    #balance source                  #类似Nginx的ip_hash,balance source 保存session值
    #balance leastconn               #最小连接
    cookie  SERVERID insert indirect
    option  httpchk /index.php   
    server Real1 192.168.15.128:80 cookie Real1 check inter 1500 rise 3 fall 3 weight 1
    server Real2 192.168.15.130:80 cookie Real2 check inter 1500 rise 3 fall 3 weight 1
    server Real3 192.168.15.140:80 cookie Real3 check inter 1500 rise 3 fall 3 weight 1
    #服务器定义,"cookie Real1"表示serverid为Real1,"check inter 1500"是检测心跳频率,"rise 3"是3次正确认为服务器可用,"fall 3"是3次失败认为服务器不可用,weight代表权重

backend imgserver
    mode http
    balance  roundrobin  
    option  httpchk /index.php
    server Real1 192.168.15.128:80 check inter 1500 rise 3 fall 3 weight 1
    server Real2 192.168.15.130:80 check inter 1500 rise 3 fall 3 weight 1
    server Real3 192.168.15.140:80 check inter 1500 rise 3 fall 3 weight 1

    errorfile 403 /etc/haproxy/errorfiles/403.http
    errorfile 500 /etc/haproxy/errorfiles/500.http
    errorfile 502 /etc/haproxy/errorfiles/502.http
    errorfile 503 /etc/haproxy/errorfiles/503.http
    errorfile 504 /etc/haproxy/errorfiles/504.http

动静分离:

global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        maxconn 5000
        chroot /var/lib/haproxy
        user haproxy
        group haproxy
        daemon
defaults
        log     global
        mode    http 
        option  httplog
        option  httpclose
        option  dontlognull  
        option  forwardfor   
        option  redispatch   
        retries 3
        maxconn 4000
        contimeout      8000
        clitimeout      80000
        srvtimeout      80000
listen stats  192.168.15.135:9999
    mode http
    stats enable
    stats refresh 5s
    stats hide-version
    stats realm Haproxy Statistics 
    stats uri  /haproxy-status
    stats auth test:123456
    acl allow src 192.168.15.0/16
    stats admin if TRUE
frontend  main *:80       #前端代理
    acl url_static     path_beg   -i  /static /images /javascript /stylesheets
    acl url_static     path_end   -i  .jpg .gif .png .css .js
    acl url_dynamic    path_end   -i  .php
    use_backend static_servers    if url_static
    default_backend           dynamic_servers 
backend static_servers   #后端的静态请求响应
    balance     roundrobin
    server      static 192.168.15.128:80 inter 3000 rise 2 fall 3 check maxconn 5000
backend dynamic_servers  #后端的动态请求响应
    balance     roundrobin
    server  dynamic1 192.168.15.130:80 inter 3000 rise 2 fall 3 check maxconn 5000
    server  dynamic2 192.168.15.140:80 inter 3000 rise 2 fall 3 check maxconn 5000

    errorfile 403 /etc/haproxy/errorfiles/403.http
    errorfile 500 /etc/haproxy/errorfiles/500.http
    errorfile 502 /etc/haproxy/errorfiles/502.http
    errorfile 503 /etc/haproxy/errorfiles/503.http
    errorfile 504 /etc/haproxy/errorfiles/504.http

启动haproxy

cp /usr/local/src/haproxy-1.4.24/examples/haproxy.init  /etc/rc.d/init.d/haproxy

chmod +x  /etc/rc.d/init.d/haproxy

chkconfig haproxy on

cp /usr/local/src/haproxy-1.4.24/haproxy /usr/sbin/haproxy

/etc/init.d/haproxy start

设置HAProxy日志

“/etc/rsyslog.d”目录下创建haproxy日志配置文件

local0.=info -/var/log/haproxy.log         #haproxy.log保存http日志

local0.notice -/var/log/haproxy-status.log #haproxy-status.log记录haproxy状态变更

vi /etc/rsyslog.d/haproxy.conf

$ModLoad imudp       #imudp是模块名,支持UDP协议
$UDPServerRun 514   #允许514端口接收使用UDP和TCP协议转发过来的日志,rsyslog在默认情况下在514端口监听UDP
$template Haproxy,"%msg%n"
local0.=info -/var/log/haproxy.log;Haproxy
local0.notice -/var/log/haproxy-status.log;Haproxy
### keep logs in localhost ##
local0.* ~

vim /etc/sysconfig/rsyslog

SYSLOGD_OPTIONS="-c 2 -r -m 0"
#各参数作用:
#-c 指定运行兼容模式。
#-r 接收远程日志
#-x 在接收客户端消息时,禁用DNS查找。需和-r参数配合使用。
#-m 标记时间戳。单位是分钟,为0时,表示禁用该功能。

重启rsyslog服务

service rsyslog restart

日志轮转配置

vim /etc/logrotate.d/haproxy

/var/log/haproxy.log {
    missingok
    notifempty
    sharedscripts
    rotate 5
    daily
    compress
    postrotate
        reload rsyslog >/dev/null 2>&1 ||truep;true
    endscript
}

创建定时任务:

59 23 * * * root /usr/sbin/logrotate -f /etc/logrotate.conf >/dev/null 2>&1
service crond restart

配置keepalived

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

tar -zxvf keepalived-1.2.15.tar.gz

cd keepalived-1.2.15

./configure --sysconf=/etc/  --with-kernel-dir=/usr/src/kernels/2.6.32-573.8.1.el6.x86_64

make && make install

ln -s /usr/local/sbin/keepalived  /sbin/  

配置keepalived.conf:

主:

! Configuration File for keepalived
global_defs {
   notification_email {
    [email protected]
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Haprxoy_Master    
}
vrrp_script check_haproxy {
  script "/usr/local/src/check_haproxy.sh"
  interval 4
  weight 2
}
vrrp_instance VI_1 {
 #state MASTER
  state BAKCUP
  nopreempt 
  interface bond0
  smtp_alert
  virtual_router_id 66
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
 }
 track_script {
  check_haproxy
 }
 virtual_ipaddress {
  192.168.15.135/24 broadcast 192.168.15.255 dev bond0 label bond0:1
 }
}

备:

! Configuration File for keepalived
global_defs {
   notification_email {
     [email protected]
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id Haprxoy_BACKUP   
}
vrrp_script check_haproxy {
 script "/usr/local/src/check_haproxy.sh"
 interval 4
 weight 2
}
vrrp_instance VI_1 {
  state BACKUP
  interface bond0
  smtp_alert
  virtual_router_id 66
  priority 88
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
 }
 track_script {
  check_haproxy
 }
 virtual_ipaddress {
  192.168.15.135/24 broadcast 192.168.15.255 dev bond0 label bond0:1
 }
}

为防止haproxy异常关闭导致keepalived不自动切换

#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
     /etc/init.d/haproxy  start
fi
sleep 3
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then
       /etc/init.d/keepalived stop
fi

chmod +x /usr/local/src/check_haproxy.sh

Keepalived.conf配置完毕,启动keepalived服务:/etc/init.d/keepalived start

关闭其中任何一个服务,访问正常,测试OK。

未分类

未分类

未分类

遇到的问题:

备机启动报错“Starting proxy LOADBAL: cannot bind socket”,原因为nginx进程占用80端口造成,停止nginx后正常。如果“ip_nonlocal_bind”未设置为1(启动haproxy的时候,允许忽视VIP的存在)也会造成相同问题

启动keepalived后日志出现“didn’t respond to SIGTERM”,需将“interval”时间设置相对较长(同时检查iptables状态)

配置HAproxy实现动静态请求分离

一. 简述

在现实的应用环境中,往往根据业务请求的不同将相关的请求指定到不同的后端服务器中,例如客户是静态资源的请求,haproxy就将请求转发给静态服务器,如果是动态的请求就转发给静态服务器,haproxy实现动静分离是通过acl匹配规则来实现这一目的。

未分类

未分类

二. 具体的步骤:

(1)在192.168.180.4上配置static服务器

[root@Monitor conf]# vim /data/index.html 
<h1>192.168.180.4---static</h1>

[root@Monitor conf]# vim /usr/local/nginx/conf/nginx.conf       
worker_processes  1;
user appuser appuser;
error_log  /data/nginx/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
   sendfile        on;
    access_log      /data/nginx/access.log;
    keepalive_timeout  65;
    gzip  on;
    server_tokens off;

 server {
         listen    80;
         server_name  192.168.180.4;
         access_log  /data/nginx/nginx.access.log;
         index   index.php  index.html index.htm;
       # root       /data/www/;
         root      /data/;
         }
     }
[root@Monitor conf]# /usr/local/nginx/sbin/nginx -s reload

保存后直接加载nginx ,在浏览器上查看该页面

未分类

(2)在192.168.180.9安装配置php服务器。

[root@localhost www]# vim /www/html/www/index.php 
<h1>this is 192.168.180.9---dynamic for php page</h1>

[root@localhost www]# cat  /usr/local/nginx/conf/nginx.conf
worker_processes  1;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
   server {
       listen 80;
       location ~ .php$ {
                   root /www/html/www;
                   fastcgi_pass 127.0.0.1:9000;
                   fastcgi_index index.php;
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                 }
     }
 }

重新加载nginx ,在浏览器中查看如下界面:

未分类

(3)在192.168.180.2服务器中安装配置jsp测试界面

[root@ittestserver1 m]# vim 1.jsp
this is test jsp page
[root@ittestserver1 conf]# /usr/local/tomcat/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/jdk1.7.0_79
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

查看测试界面

未分类

(4)接下来是最重要的配置haproxy服务器

[root@localhost haproxy]# vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
 log 127.0.0.1 local2 info  ###[err warning info debug] 
 chroot /usr/local/haproxy
 pidfile /var/run/haproxy.pid ###haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
 maxconn 4000     ###最大连接数,默认4000
 user haproxy
 group haproxy
 daemon       ###创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"

#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will 
# use if not designated in their block
#---------------------------------------------------------------------
defaults
 mode http    ###默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
 log global   ###采用全局定义的日志
 option dontlognull  ###不记录健康检查的日志信息
 option httpclose  ###每次请求完毕后主动关闭http通道 
 option httplog   ###日志类别http日志格式 
 option forwardfor  ###如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip 
 option redispatch  ###serverId对应的服务器挂掉后,强制定向到其他健康的服务器
 timeout connect 10000 #default 10 second timeout if a backend is not found
 timeout client 300000 ###客户端连接超时
 timeout server 300000 ###服务器连接超时
 maxconn  60000  ###最大连接数
 retries  3   ###3次连接失败就认为服务不可用,也可以通过后面设置 
####################################################################
listen stats
  bind 0.0.0.0:1080   #监听端口 
  stats refresh 30s   #统计页面自动刷新时间 
  stats uri /stats   #统计页面url 
  stats realm Haproxy Manager #统计页面密码框上提示文本 
  stats auth admin:admin  #统计页面用户名和密码设置 
  stats hide-version   #隐藏统计页面上HAProxy的版本信息
  stats enable         ###启用管理界面
  stats admin if TRUE  ##如果登录成功就可以管理在线服务器
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
#frontend   www    # *表示haproxy监听所有地址,监听的端口为80
 bind 0.0.0.0:80
# bind *:8080
#######定义访问控制,表示url以.css .js .html .php结尾的分别调度到哪台服务器上访问
# acl  url_static    path_beg  -i   /static /images /javascript /stylesheets
 acl  url_static    path_end  -i   .jpg .gif .png .css .js .html
 acl  url_dynamic_php   path_end  -i   .php
 acl  url_dynamic_jsp   path_end  -i   .jsp
#######usr_backend表示使用backend服务,if表示如果满足url_static这个条件就调度到这台服务器上
 use_backend         static          if url_static  ###满足策略要求,则响应策略定义的backend静态页面
 use_backend         dynamic_php     if url_dynamic_php   ###满足策略要求,则响应策略定义的backend静态页面
  se_backend         dynamic_jsp     if url_dynamic_jsp  ###满足策略要求,则响应策略定义的backend静态页面
# default_backend     dynamic                  ###不满足则响应backend的默认动态页面
# default_backend     dynamic                  ###不满足则响应backend的默认动态页面

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------

backend static           ###定义后端静态请求响应
 balance  roundrobin     ###负载均衡模式轮询
 server  static 192.168.180.4:80 check ###后端服务器定义
#server  static 192.168.180.9:80 check ###后端服务器定义

backend dynamic_php           #####定义后端动态请求响应
 balance roundrobin
 server   phpsrv1 192.168.180.9:80 check maxconn 2000
# server   websrv1 dd192.168.180.9:80 check maxconn 2000
#server   websrv2 192.168.180.4:80 check maxconn 2000
# server   websrv2 192.168.180.2:443 check maxconn 2000
backend dynamic_jsp           #####定义后端动态请求响应                                                           
 balance roundrobin                                                                                               
 server   jspsrv1 192.168.180.2:8081 check maxconn 2000   

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------


errorfile 403 /etc/haproxy/errorfiles/403.http

errorfile 500 /etc/haproxy/errorfiles/500.http

errorfile 502 /etc/haproxy/errorfiles/502.http

errorfile 503 /etc/haproxy/errorfiles/503.http
[root@localhost haproxy]# systemctl restart haproxy.service

三. 测试

(1)测试static页面并查看haproxy的访问日志;

未分类

[root@localhost ~]# tail -f /var/log/haproxy.log 
Jul 20 18:07:22 localhost haproxy[6436]: 192.168.181.231:53672 [20/Jul/2017:18:07:22.371] main static/static 0/0/0/1/1 304 167 - - ---- 0/0/0/0/0 0/0 "GET /index.html HTTP/1.1"

(2)访问php页面

未分类

[root@localhost ~]# tail -f /var/log/haproxy.log 
Jul 20 18:08:36 localhost haproxy[6436]: 192.168.181.231:53834 [20/Jul/2017:18:08:36.261] main dynamic_php/phpsrv1 0/0/1/0/2 200 2332 - - ---- 0/0/0/0/0 0/0 "GET /index.php?=PHPE9568F35-D428-11d2-A769-00AA001ACF42 HTTP/1.1"

(3)访问jsp页面

未分类

[root@localhost ~]# tail -f /var/log/haproxy.log 
Jul 20 18:09:58 localhost haproxy[6436]: 192.168.181.231:54015 [20/Jul/2017:18:09:57.999] main dynamic_jsp/jspsrv1 0/0/1/2/3 200 188 - - ---- 0/0/0/0/0 0/0 "GET /1.jsp HTTP/1.1"

(4)查看haproxy监控页面

未分类

总之:haproxy可以利用acl规则匹配url做相应的请求跳转,比如动静分离,域名跳转等等应用需求,haproxy是一款性能很强大的四层以及七层代理server。HAProxy运行在 当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。

Debian系统apt-get包管理命令用法

apt-get 是Debian系Linux发行版(Debian,Ubuntu,Mint等)中的软件包管理工具。apt-get命令可以从网络随心所欲的管理Debian系统中的软件包,当要安装软件包时,自动从Debian的镜像服务器上下载所需要的软件和安全更新。

1. 安装软件包

apt-get install firefox

安装Firefox

2. 删除软件包

apt-get remove firefox

删除Firefox,但不会删除软件包的配置文件。

apt-get purge firefox

删除Firefox,并删除其配置文件。

apt-get clean

当apt-get安装或升级软件包时,会将deb安装包下载到文件系统的 /var/cache/apt/archives 目录下。软件安装完成后,这些deb安装包就没什么用了,apt-get clean 命令可以帮你删除这些deb安装包。

3. 更新本机的软件包索引

apt-get update

在安装任何软件之前,最好更新一下本机的软件包索引(package index)。本机软件包索引列出了软件仓库中所有可安装的软件包以及其版本信息。

4. 更新本机的软件包

apt-get upgrade

apt-get会告诉你哪些软件包会升级,并且询问你是否真的需要升级这些软件包。你可以在命令中添加-y 选项来自动回答yes.

5. 升级系统版本

apt-get dist-upgrade

Debian 9配置Apache MariaDB Drupal运行环境

Drupal是一个免费的开源内容管理系统,可用于创建在线内容,网站和用户社区。 它是用PHP语言编写的,使用MySQL作为数据库后端,并在GNU通用公共许可证下分发。 Drupal拥有超过17,000个插件来自定义其功能。 Drupal在所有Web服务器上运行,包括Apache,Nginx,IIS,Lighttpd以及后端数据库MySQL,MariaDB,MongoDB,SQLite,MS SQL Server,PostgreSQL等。

在本文中,我们将演示如何在Debian 9服务器上安装Drupal 8。

要求

  • 在您的系统上运行Debian 9的服务器。
  • Apache 2.x,MySQL或具有PDO的MariaDB。
  • 在您的服务器上设置sudo权限的非root用户。

1. 入门指南

首先,建议使用最新的稳定版本来更新系统。 您可以通过运行以下命令来执行此操作:

sudo apt-get update -y
sudo apt-get upgrade -y

一旦您的系统更新,您将需要安装一些所需的软件包到您的系统。 您可以通过运行以下命令来安装它们:

sudo apt-get install wget git unzip nano -y

2. 安装LAMP服务器

在开始安装Drupal之前,需要在服务器上安装并配置LAMP服务器(Apache,PHP和MySQL)。

首先,使用以下命令开始安装Apache Web服务器:

sudo apt-get install apache2 -y

安装完成后,您将需要启动Apache服务,并使其能够在下次系统引导时自动启动。 为此,请运行以下命令:

sudo systemctl start apache2
sudo systemctl enable apache2

接下来,通过运行以下命令来安装PHP所需的模块:

sudo apt-get install php7.0 libapache2-mod-php7.0 php7.0-cli php7.0-mcrypt php7.0-intl php7.0-mysql php7.0-curl php7.0-gd php7.0-soap php7.0-xml php7.0-zip -y

接下来,您需要在php.ini文件中进行一些更改:

sudo nano /etc/php/7.0/cli/php.ini

更改行如下所示:

memory_limit = 512M
date.timezone = UTC
cgi.fix_pathinfo=0
upload_max_filesize = 100M
post_max_size = 100M

完成后,保存并关闭文件。

3. 安装并配置MariaDB

Drupal需要用于数据库后端的MariaDB / MySQL,因此您需要安装它。 您可以通过运行以下命令来安装它:

sudo apt-get install mariadb-server -y

安装完成后,启动MariaDB服务,并通过运行以下命令使其在系统启动时自动启动:

sudo systemctl start mysql
sudo systemctl enable mysql

接下来,您将需要为数据库设置安全性。 您可以运行以下命令来保护MariaDB数据库:

sudo mysql_secure_installation

此脚本设置root密码,禁用远程root登录,删除测试数据库并删除匿名用户,如下所示:

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] n

 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

确保数据库后,Drupal需要一个空的MySQL数据库。 因此,您将需要为Drupal安装创建一个MySQL数据库和用户。

首先,使用以下命令登录到MySQL shell:

mysql -u root -p

在询问时输入root密码,然后使用以下命令创建Drupal的数据库:

MariaDB [(none)]>CREATE DATABASE drupaldb;

接下来,创建drupal数据库的用户,并使用以下命令向drupal数据库授予权限:

MariaDB [(none)]>GRANT ALL PRIVILEGES on drupaldb.* to ‘drupal’@’localhost’ identified by ‘password’;

接下来,运行FLUSH PRIVILEGES命令,以退出特权:

MariaDB [(none)]>FLUSH PRIVILEGES;

最后,使用以下命令退出MariaDB控制台:

MariaDB [(none)]>q

4. 安装并配置Drupal

首先,您需要从其官方网站下载最新的稳定版本的Drupal,否则您可以使用wget命令直接下载,如下所示:

wget https://ftp.drupal.org/files/projects/drupal-8.3.4.zip

之后,提取下载的zip文件,并将提取的Drupal目录移动到Apache根目录:

unzip drupal-8.3.4.zip
sudo mv drupal-8.3.4 /var/www/html/drupal

接下来,您将需要更改drupal目录的一些权限:

sudo chown -R www-data:www-data /var/www/html/drupal
sudo chmod -R 777 /var/www/html/drupal

接下来,您将需要为drupal创建一个Apache虚拟主机文件。 为此,请在/ etc / apache2 / sites-available /目录中创建一个新的drupal.conf文件:

sudo nano /etc/apache2/sites-available/drupal.conf

添加以下行:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /var/www/html/drupal
ServerName 192.168.15.189
ServerAlias www.example.com
<<Directory "/var/www/html/drupal/">
Options FollowSymLinks
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/drupal-error_log
CustomLog /var/log/apache2/drupal-access_log common
</VirtualHost>

完成后保存并关闭文件,然后使用以下命令启用虚拟主机:

sudo a2ensite drupal

您还需要激活重写模块。

sudo a2enmod rewrite

最后,重新启动Apache服务以使用以下命令应用此更改:

sudo systemctl restart apache2

5. 访问Drupal Web界面

一切都已安装和配置。 接下来,您将需要通过UFW防火墙允许Drupal。 默认情况下,UFW防火墙在Debian 9中禁用,因此您需要先启用它。

sudo ufw enable

然后,通过运行以下命令,通过UFW防火墙允许端口80:

sudo ufw allow 80

最后,打开您的网页浏览器,并导航到URL http://192.168.15.189启动Drupal Web安装程序。 您应该看到以下页面:

Apache

选择英文,然后点击保存并继续按钮,您应该看到以下图像:

Apache

选择安装配置文件,然后单击保存并继续按钮,然后验证所有要求,然后单击保存并继续按钮。 您应该看到以下图像:

Apache

在“数据库配置”页面中,提供数据库名称,数据库用户名和密码,数据库主机等所需的所有数据库详细信息,然后单击“保存并继续”按钮,您应该看到以下图像:

Apache

在Drupal站点配置页面中,提供您的站点名称,管理员用户名和密码,然后单击保存并继续按钮开始安装Drupal。 安装Drupal后,您应该在以下图像中看到Drupal仪表板:

Apache

结论

恭喜! 您已经在Debian 9服务器上成功安装并配置了Drupal。

Ubuntu系统配置samba实现文件夹共享

一. samba的安装:

sudo apt-get insall samba
sudo apt-get install smbfs

二. 创建共享目录:

mkdir /home/phinecos/share
sodu chmod 777 /home/phinecos/share

三. 创建Samba配置文件:

1. 保存现有的配置文件

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

2. 修改现配置文件

sudo gedit /etc/samba/smb.conf

在smb.conf最后添加

[share]
path = /home/phinecos/share
available = yes
browsealbe = yes
public = yes
writable = yes

四. 创建samba帐户

sudo touch /etc/samba/smbpasswd
sudo smbpasswd -a phinecos

然后会要求你输入samba帐户的密码

[如果没有第四步,当你登录时会提示 session setup failed: NT_STATUS_LOGON_FAILURE]

五. 重启samba服务器

sudo /etc/init.d/samba restart

六. 测试

smbclient -L //localhost/share

七,使用

可以到windows下输入ip使用了,在文件夹处输入 “\” + “Ubuntu机器的ip或主机名” + “\” + “share”