操作系统:
Linux version 2.6.32-431.el6.x86_64(mockbuild@x86-023.build.eng.bos.redhat.com) (gcc version 4.4.7 20120313 (RedHat 4.4.7-4) (GCC) ) #1 SMP Sun Nov 10 22:19:54 EST 2013
IP地址:10.45.10.122
Web环境:Nginx+MySQL+PHP
zabbix版本:Zabbix2.2 LTS
备注:Linux下安装zabbix需要有LAMP或者LNMP运行环境
1、关闭firewall:
<span style="font-family:Microsoft YaHei;">#systemctlstop firewalld.service <span style="color:#3333ff;">#停止firewall</span>
#systemctldisable firewalld.service <span style="color:#3333ff;">#禁止firewall开机启动</span></span>
可以使用cat /proc/version命令查看当前需要配置服务器的版本,firewall是在7.0版本取代iptables。
Firewall的常用配置可以参考:http://www.2cto.com/Article/201503/384368.html
systemctl命令也是Linux新版本才出现的,这边用service命令。
chkconfig 命令可以查看所有服务状态。
2、iptables防火墙
# yuminstall iptables-services #公司的服务器一般都安装好了,可以跳过这个地方
# vi/etc/sysconfig/iptables-config #编辑防火墙配置文件,具体的内容根据自己需要配置
#Firewall configuration written by system-config-firewall
#Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
:wq! #保存退出
# systemctlrestart iptables.service #最后重启防火墙使配置生效
# systemctlenable iptables.service #设置防火墙开机启动
标准步骤是如上述所示,然并x,我们已经安装好了。所以应该是如下所示的样子。
1) 查看iptables当前的状态
#iptables -L -n #首先查看iptables当前的状态
ChainINPUT (policy ACCEPT)
target prot optsource destination
ChainFORWARD (policy ACCEPT)
target prot optsource destination
ChainOUTPUT (policy ACCEPT)
target prot optsource destination
ChainRH-Firewall-1-INPUT (0 references)
target prot optsource destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmp type 255
ACCEPT esp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT ah -- 0.0.0.0/0 0.0.0.0/0
ACCEPT udp -- 0.0.0.0/0 224.0.0.251 udpdpt:5353
ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:631
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:80
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:25
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
可以看出在安装的时候选择了有防火墙并且开房了22, 80, 25端口。
如果没有选择启用防火墙应该是这样的:
[root@hightest1~]# iptables -L -n
ChainINPUT (policy ACCEPT)
target prot opt source destination
ChainFORWARD (policy ACCEPT)
target prot opt source destination
ChainOUTPUT (policy ACCEPT)
target prot opt source destination
2) 对没有规则的服务设定预设规则
这边都只是例子而已,根据自己实际情况进行配置。
采用命令的方式,只在当时生效,如果想要重起后也起作用,那就要保存!!!写入到/etc/sysconfig/iptables文件里。
# iptables-P INPUT DROP #注意是大写P
#iptables -P OUTPUT ACCEPT
# iptables-P FORWARD DROP
当超出了IPTABLES里filter表里的两个链规则(INPUT,FORWARD)时,不在这两个规则里的数据包怎么处理呢,那就是DROP(放弃)。应该说这样配置是很安全的,要控制流入数据包,而对于OUTPUT链,也就是流出的包不用做太多限制,采取ACCEPT。
3) 添加规则
首先添加INPUT链,INPUT链的默认规则是DROP,所以需要ACCETP的链。
#iptables -A INPUT -p tcp --dport 22 -j ACCEPT #为了能采用远程SSH登陆,开启22端口.
# iptables -A OUTPUT -ptcp --sport 22 -j ACCEPT #如果你把OUTPUT设置成DROP的就要写上
#其他的端口也一样,如果开启了web服务器,OUTPUT设置成DROP的话,同样也要添加一条链
# iptables -A OUTPUT -p tcp --sport 80 -jACCEPT
#如果做了WEB服务器,开启80端口
# iptables -A INPUT -p tcp --dport 80 -jACCEPT
#如果做了邮件服务器,开启25,110端口
# iptables -A INPUT -p tcp --dport 110 -j ACCEPT
# iptables -A INPUT -p tcp --dport 25 -j ACCEPT
#如果做了FTP服务器,开启21端口
# iptables -A INPUT -p tcp --dport 21 -jACCEPT
# iptables-A INPUT -p tcp --dport 20 -j ACCEPT
#如果做了DNS服务器,开启53端口
#iptables -A INPUT -p tcp --dport 53 -j ACCEPT
如果你还做了其他的服务器,需要开启哪个端口都按照上面的方式书写即可。
上面主要写的都是INPUT链,凡是不在上面的规则里的,都会DROP。
#允许icmp包通过,也就是允许ping
# iptables -A OUTPUT -p icmp -j ACCEPT #OUTPUT设置成DROP
# iptables -A INPUT -p icmp -jACCEPT #INPUT设置成DROP的话
#允许loopback,不然会导致DNS无法正常关闭等问题
# iptables -A INPUT -i lo -p all -j ACCEPT #INPUT DROP
# iptables -A OUTPUT -o lo -p all -j ACCEPT #OUTPUTDROP
下面写OUTPUT链,OUTPUT链默认规则是ACCEPT,所以需要DROP的链,减少不安全的端口连接。
# iptables-A OUTPUT -p tcp --sport 31337 -j DROP
#iptables -A OUTPUT -p tcp --dport 31337 -j DROP
有些特洛伊木马会扫描端口31337到31340(即黑客语言中的 elite 端口)上的服务。既然合法服务都不使用这些非标准端口来通信,阻塞这些端口能够有效地减少网络可能被感染的机器和它们的远程主服务器进行独立通信的机会。
还有其他端口也一样:31335、27444、27665、20034 NetBus、9704、137-139(smb),2049(NFS)端口也应被禁止,相应的写法。
4) 另外查了一下资料关于这个dport和sport的含义。
① 先来翻译一下dport和sport的意思: dport:目的端口 ;sport:来源端口
dport 和sport字面意思来说很好理解,一个是数据要到达的目的端口,一个是数据来源的端口。
② 但是在使用的时候要分具体情况来对待,这个具体情况就是数据包的流动行为方式.
e.g.:/sbin/iptables-A INPUT -p tcp --dport 80 -j ACCEPT
l 这是一条从外部进入内部本地服务器的数据。
l 数据包的目的(dport)地址是80,就是要访问本地的80端口。
l 允许以上的数据行为通过。
综上所述:允许外部数据访问本地服务器80端口。
e.g.:/sbin/iptables-A INPUT -p tcp --sport 80 -j ACCEPT
l 这是一条从外部进入内部本地服务器的数据。
l 数据包的来源端口是(sport)80,就是对方的数据包是80端口发送过来的。
l 允许以上数据行为。
综上所述:允许外部的来自80端口的数据访问本地服务器。
input方式总结: dport指本地,sport指外部。
如果数据包是OUTPUT行为,那么就是另外一种理解方式:
e.g.:/sbin/iptables-A OUTPUT -p tcp --dport 80 -j ACCEPT
l 这是一条从内部出去的数据。
l 出去的目的(dport)端口是80。
l 允许以上数据行为。
综上所述:允许内部数据访问外部服务器80端口。
output行为总结:dport指外部,sport指本地。
iptables 所在目录/etc/sysconfig/iptables
5)其他命令
# serviceiptables status #查看iptables状态
# serviceiptables restart #iptables服务重启
# serviceiptables stop iptables #服务停止
再强调一遍:采用命令的方式,只在当时生效,如果想要重起后也起作用,那就要保存!!!写入到/etc/sysconfig/iptables文件里。
命令输完之后查看状态,它长这个样子。
重启之后它长这个样子。
也可以输入完上述命令之后敲如下命令
# /etc/rc.d/init.d/iptables save
千万不要干一件鱼唇的事情,好奇心害死猫:
# iptables-P INPUT DROP
# /etc/rc.d/init.d/iptables save
3、关闭SELINUX
# vi/etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存退出
# setenforce0 #使配置立即生效
4、安装准备
① 源码编译安装下载包
根据自己当前服务器选择合适的包
Mysql(5.7.10): http://dev.mysql.com/downloads/mysql/
Nginx: http://nginx.org/download/nginx-1.8.0.tar.gz
php: http://php.net/get/php-5.6.16.tar.gz/from/a/mirror
pcre (支持nginx伪静态):
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
openssl(nginx扩展): http://www.openssl.org/source/openssl-1.0.2e.tar.gz
zlib(nginx扩展):http://zlib.net/zlib-1.2.8.tar.gz
cmake(MySQL编译工具): https://cmake.org/files/v3.4/cmake-3.4.1-Linux-x86_64.tar.gz
libmcrypt(php扩展):
ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
yasm(php扩展):http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
t1lib(php扩展): 使用wgetftp://sunsite.unc.edu/pub/linux/libs/graphics/t1lib-5.1.2.tar.gz
(ftp连接不上,下载了如下连接的rpm)
gd库安装包: https://bitbucket.org/libgd/gd-libgd/downloads/libgd-2.1.0.tar.gz
libvpx(gd库需要):
http://storage.googleapis.com/downloads.webmproject.org/releases/webm/libvpx-1.5.0.tar.bz2(这个版本和1.4.0版本都需要打补丁)
补丁:
tiff(gd库需要): http://download.osgeo.org/libtiff/tiff-4.0.3.tar.gz
libpng(gd库需要):
ftp://ftp.simplesystems.org/pub/libpng/png/src/libpng16/libpng-1.6.20.tar.g
freetype(gd库需要):
http://ring.u-toyama.ac.jp/archives/graphics/freetype/freetype2/freetype-2.5.3.tar.gz
jpegsrc(gd库需要): http://www.ijg.org/files/jpegsrc.v9a.tar.gz
以上软件包使用lrzsz工具上传到/usr/local/src目录
lrzsz安装:yum install lrzsz
② 安装编译工具及库文件(使用yum命令安装)
yum install -y apr* autoconf automake bisonbzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-develfreetype freetype* freetype-devel gcc gcc-c++ gtk+-devel gd gettextgettext-devel glibc kernel kernel-headers keyutils keyutils-libs-develkrb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devellibselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm*libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-develperl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wgetzlib-devel
③ 约定:
软件源代码包存放位置:/usr/local/src