原创日志

centos vps快速一键lnmp环境安全和基本安全配置

[ 2013-07-01 15:21:47 | 作者: admin ]
字号: | |
第一步,安装一键lnmp环境
http://lnmp.org 下载完整安装包,一键安装包,30分钟左右安装完成
引用
LNMP相关软件安装目录
Nginx 目录: /usr/local/nginx/
MySQL 目录 : /usr/local/mysql/
MySQL数据库所在目录:/usr/local/mysql/var/
PHP目录 : /usr/local/php/
PHPMyAdmin目录 : 0.9版为/home/wwwroot/phpmyadmin/ 1.0版为 /home/wwwroot/default/phpmyadmin/ 强烈建议将此目录重命名为其不容易猜到的名字。phpmyadmin可自己从官网下载新版替换。
默认网站目录 : 0.9版为 /home/wwwroot/ 1.0版为 /home/wwwroot/default/
Nginx日志目录:/home/wwwlogs/
/root/vhost.sh添加的虚拟主机配置文件所在目录:/usr/local/nginx/conf/vhost/
PureFtpd 目录:/usr/local/pureftpd/
PureFtpd web管理目录: 0.9版为/home/wwwroot/default/ftp/ 1.0版为 /home/wwwroot/default/ftp/
Proftpd 目录:/usr/local/proftpd/

LNMP相关配置文件位置
Nginx主配置文件:/usr/local/nginx/conf/nginx.conf
/root/vhost.sh添加的虚拟主机配置文件:/usr/local/nginx/conf/vhost/域名.conf
MySQL配置文件:/etc/my.cnf
PHP配置文件:/usr/local/php/etc/php.ini
php-fpm配置文件:/usr/local/php/etc/php-fpm.conf
PureFtpd配置文件:/usr/local/pureftpd/pure-ftpd.conf
PureFtpd MySQL配置文件:/usr/local/pureftpd/pureftpd-mysql.conf
Proftpd配置文件:/usr/local/proftpd/etc/proftpd.conf
Proftpd 用户配置文件:/usr/local/proftpd/etc/vhost/用户名.conf
引用
LNMP状态管理命令:

LNMP状态管理: /root/lnmp {start|stop|reload|restart|kill|status}
Nginx状态管理:/etc/init.d/nginx {start|stop|reload|restart}
MySQL状态管理:/etc/init.d/mysql {start|stop|restart|reload|force-reload|status}
Memcached状态管理:/etc/init.d/memcached {start|stop|restart}
PHP-FPM状态管理:/etc/init.d/php-fpm {start|stop|quit|restart|reload|logrotate}
PureFTPd状态管理: /etc/init.d/pureftpd {start|stop|restart|kill|status}
ProFTPd状态管理: /etc/init.d/proftpd {start|stop|restart|reload}

如重启LNMP,输入命令:/root/lnmp restart 即可,单独重启mysql:/etc/init.d/mysql restart
第二步:进行基本的安全配置
1、修改root密码,还可以建立新用户登陆ssh,限制root直接ssh

2、修改mysql和ssh默认端口,开通ftp的也需要修改默认端口
编辑/etc/ssh/sshd_config文件,重启一下 /etc/init.d/sshd restart

3、清理多余的用户和组
引用
userdel adm
userdel lp
userdel sync
userdel shutdown
userdel halt
userdel news
userdel uucp
userdel operator
userdel games
userdel gopher
userdel ftp
groupdel adm
groupdel lp
groupdel news
groupdel uucp
groupdel dip
4、停掉不需要的服务,如
引用
linux新型电源管理标准,建议笔记本使用
chkconfig acpid off

和 cron 相似的任务调度器,只不过它并不要求系统持续运行
chkconfig anacron off

不做打印服务器的就关了吧
chkconfig cups off

ipv6防火墙
chkconfig ip6tables off

邮件程序,如果不是邮件服务器就关了
chkconfig sendmail off
SELinux Troubleshooting
chkconfig setroubleshoot off
第三步:进行基本的iptables防火墙配置,命令保存后会自动生成 /etc/sysconfig/iptables 文件
#先清除已经有的iptables规则:
iptables -F
iptables -X
iptables -Z

#具体配置如下,注意ssh端口,保存前确认端口是否准确
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
iptables -A INPUT -m state –-state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -j ACCEPT
iptables -A INPUT -p tcp –-dport 22 -j ACCEPT
iptables -A INPUT -p tcp -–dport 80 -j ACCEPT
iptables -A INPUT -p tcp -–dport 21 -j ACCEPT
iptables -A INPUT -p tcp –-dport 20 -j ACCEPT
iptables -A INPUT -j REJECT
iptables -A FORWARD -j REJECT

iptables配置说明:
#允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT

# 允许已建立的或相关连的通行
iptables -A INPUT -m state –-state ESTABLISHED,RELATED -j ACCEPT

#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT

# 允许访问22端口
iptables -A INPUT -p tcp –-dport 22 -j ACCEPT

#允许访问80端口
iptables -A INPUT -p tcp –-dport 80 -j ACCEPT

#允许FTP服务的21和20端口
iptables -A INPUT -p tcp -–dport 21 -j ACCEPT
iptables -A INPUT -p tcp –-dport 20 -j ACCEPT
#如果有其他端口的话,规则也类似,稍微修改上述语句就行

#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT (注意:如果22端口未加入允许规则,SSH链接会直接断开。)
iptables -A FORWARD -j REJECT

iptables屏蔽IP:
#如果只是想屏蔽IP的话“3、开放指定的端口”可以直接跳过。
#屏蔽单个IP的命令是
iptables -I INPUT -s 123.45.6.7 -j DROP
#封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP
#封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP
#封IP段即从123.45.6.1到123.45.6.254的命令是
iptables -I INPUT -s 123.45.6.0/24 -j DROP

查看已添加的iptables规则:
iptables -L -n

删除已添加的iptables规则
将所有iptables以序号标记显示,执行:
iptables -L -n –line-numbers
比如要删除INPUT里序号为5的规则,执行:
iptables -D INPUT 5

添加iptables防火墙为开机启动:
chkconfig –-level 345 iptables on

保存防火墙规则:
service iptables save

重启Iptables:
service iptables restart



第四步,进行proftp用户的权限的配置
使用/root/vhost.sh 建立虚拟空间,然后/proftpd_vhost.sh建立ftp账号
这时候就建立了linux用户,如 ftpuser1 ,这个时候就指定好该用户对网站根目录的读写权限。
最不安全的办法就是网站目录为 777 权限,但是容易被入侵,可以使用 acl 指定用户对目录的权限。
acl以及用户权限命令(setfacl, getfacl)可以参考 http://blog.xg98.com/article.asp?id=2084 文章末尾
如下命令将指定用户权限
其中-m是修改,-d设置为默认,-R设置为遍历子目录所有文件
注意相关参数放操作对象的前面,不要写成 setfacl -d -R -m u:ftpuser1....
setfacl -m u:ftpuser1:rwx -d -R /home/vhost/wwwroot1
如果命令无法执行可能是磁盘的acl没有挂载,参考 http://blog.xg98.com/article.asp?id=2083
更多POSIX ACL的细节说明可以参考 http://blog.xg98.com/article.asp?id=1548

第五步,更细致的安全设置
1、为每项外部服务建立独立运行账号,设置目录权限
[最后修改由 admin, 于 2013-08-21 14:13:53]
评论Feed 评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=2078

这篇日志没有评论。

此日志不可发表评论。