TypeCodes

阿里云CentOS主机 LNMP 环境之web安全篇

前面几篇文章介绍了阿里云的CentOS 6.5系统中如何搭建和配置 LNMP 环境,这篇文章主要说说 web 站点的安全配置。

1 修改 CentOS 系统的主机名(关系不大, 看个人喜好)

默认同开通的阿里云 Linux 主机都是以 AY+随机数 命名的,修改方法如下:

[root@typecodes ~]# vi /etc/sysconfig/network
###### 修改主机名HOSTNAME, 例如 HOSTNAME=typecodes , 然后:wq保存退出
HOSTNAME=typecodes
###### 接着执行 hostname 命令
[root@typecodes ~]# hostname typecodes
###### 退出ssh, 然后重新登录即可
2 控制脚本访问权限

在 /etc/php.ini 的第 375 行开始,有个重要的web安全点: open_basedir 。只要将其设置为自己的 web 站点根路径,就可以限制 web 站点中的所有文件(包括.php脚本等)操作都只能在这个目录(即web根路径)下,从而防止 CentOS 系统中的其它文件/目录受到攻击。

375 ; open_basedir, if set, limits all file operations to the defined directory
376 ; and below.  This directive makes most sense if used in a per-directory
377 ; or per-virtualhost web server configuration file. This directive is
378 ; *NOT* affected by whether Safe Mode is turned On or Off.
379 ; http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.open-basedir
380 open_basedir = /usr/wwwroot/typecodes;
3 关闭错误消息显示

像 PHP、JSP、ASP.NET 等动态web在脚本处理失败时,可能会在客户端的错误页面上暴露出一些重要信息从而被利用。因此,对于 PHP 站点,我们可以通过配置 /etc/php.ini 关闭错误信息来避免,处理位置从第 528 行开始。

528 ; Production Value: Off
529 ; http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
530 display_errors = Off
4 针对 nginx 的安全设置
###### 消除目录浏览漏洞,  nginx.conf  http 属性中配置
[root@typecodes ~]# vi /etc/nginx/nginx.conf
autoindex Off

###### 目录安全配置,  /etc/nginx/conf.d/default.conf的 server 属性配置: 直接访问该目录下面的任意.php脚本文件, 都跳到404界面. 这点也在前面文章 https://typecodes.com/web/aliyuncentoslnmpnginxconf.html 提到了。

[root@typecodes ~]# vi /etc/nginx/conf.d/default.conf
location ~ .*/plugins/.*\.(php|php5)$ {
    deny  all;
}
5 禁止显示 PHP 和 Nginx 的版本号

对于 Php 和 Nginx 这两款开源属性的程序,出于安全考虑可以不把站点正在使用的版本号公布出来。

###### 禁止显示 Php 版本, 配置文件是 /etc/php.ini
 427 ; Decides whether PHP may expose the fact that it is installed on the server
 428 ; (e.g. by adding its signature to the Web server header).  It is no security
 429 ; threat in any way, but it makes it possible to determine whether you use PHP
 430 ; on your server or not.
 431 ; http://www.php.net/manual/en/ini.core.php#ini.expose-php
 432 expose_php = Off

###### 禁止显示 Nginx 版本,  /etc/nginx/nginx.conf  http 属性中添加下面这行
server_tokens off;

###### 测试效果(其实很多站长工具提供的查询功能也是利用curl请求命令)

[root@typecodes ~]# curl -I typecodes.com
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Fri, 15 Aug 2014 13:10:46 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://typecodes.com/
打赏支持

Comments »