很多朋友都喜欢用ubuntu,很大原因是它安装LAMP非常简单,
尽管默认安装的lamp已经可以良好工作,但是系统管理员仍然可以找到一些优化加固的空间。
1、php加速
事实证明,php解释缓存可以提高php效率,apc/xcache/zendopcache等各个加速器原理都差不多,性能也相差不大,可以肯定是,php解释缓存,使用与不使用,性能相差数倍甚至更多。
1
|
apt-get
install
php-apc
|
2、php5-mysql 模块精简
php5-mysql 软件包默认包含了3种mysql连接方式,分别是mysql,pdo_mysql和mysqli,
三者虽各有优劣,但你用到的肯定只有其中的一种,通常是mysql。
1
2
3
4
|
cd
/etc/php5/conf
.d/
mv
mysqli.ini mysqli.ini.bak
mv
pdo_mysql.ini pdo_mysql.ini.bak
mv
pdo.ini pdo.ini.bak
|
3、隐藏apache和php的版本信息
1
2
3
|
sed
-i
's/ServerTokens OS/ServerTokens Prod/g'
/etc/apache2/conf
.d
/security
sed
-i
's/ServerSignature On/ServerSignature Off/g'
/etc/apache2/conf
.d
/security
sed
-i
'/expose_php/{s/On/Off/g}'
/etc/php5/apache2/php
.ini
|
4、安装mod_security
1
2
3
4
5
|
mod_security 是一个安全模块,可以防止多种攻击。
apt-get
install
libapache-mod-security
cd
/etc/modsecurity/
cp
modsecurity.conf-recommended modsecurity.conf
|
5、设置压缩和浏览器缓存
压缩数据可提升吞吐量,设置浏览器缓存可减少请求数,反向提高apache性能。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
cat
/etc/apache2/httpd
.conf
<IfModule mod_deflate.c>
<Location />
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has same problems...
BrowserMatch ^Mozilla
/4
gzip
-only-text
/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla
/4
\.0[678] no-
gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-
gzip
!
gzip
-only-text
/html
# Don't compress images and other
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-
gzip
dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-
gzip
dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-
gzip
dont-vary
AddOutputFilterByType DEFLATE text
/html
text
/plain
text
/xml
text
/css
AddOutputFilterByType DEFLATE application
/x-javascript
# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary
<
/Location
>
<
/IfModule
>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/*
"access plus 1 month"
ExpiresByType text
/css
"access plus 1 month"
ExpiresByType text
/javascript
"access plus 1 month"
ExpiresByType application
/x-javascript
"access plus 1 month"
<
/IfModule
>
<IfModule mod_headers.c>
Header
unset
Server
Header
unset
X-Powered-By
<
/IfModule
>
|
以上几点在实际中是不够的,另外给出3点额外的建议:
a、mysql独立出来
除非逼不得已,请务必单独给mysql数据库分配一台机器。要知道,lamp不是一个整体,而是一种耦合。
b、使用apache基于域名的虚拟主机
基于域名的主机由于通常无法直接通过IP地址访问,这可以减少很多扫描和盲注,让网站更安全。
c、反向代理
如果在lamp前面单独放置一台机器做nginx反向代理,把web服务器放入内网,这样在架构上会更安全,还可以通过nginx的负载均衡来增加web服务器的可用性。lamp如果直接面对公网,面临的危险显然更多。
本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/1432274,如需转载请自行联系原作者