《Linux下apache安裝及安全配置》要點:
本文介紹了Linux下apache安裝及安全配置,希望對您有用。如果有疑問,可以聯(lián)系我們。
Linux 64位系統(tǒng)的編譯方法:
共有3種工作模式:
prefork:一個主進(jìn)程產(chǎn)生多個子進(jìn)程,一個子進(jìn)程響應(yīng)一個哀求;
worker:一個進(jìn)程生成多個線程,一個線程響應(yīng)一個哀求;
event:基于事件驅(qū)動;
1、采用prefork方式需修改httpd-2.2.4/server/mpm/prefork.c的數(shù)值
#define DEFAULT_SERVER_LIMIT 5120
2、采用worker方式需修改server/mpm/worker/worker.c的數(shù)值
#define DEFAULT_THREAD_LIMIT 64
#define MAX_THREAD_LIMIT 20000
同理,部署好之后需對應(yīng)地修改參數(shù)文件extra/httpd-mpm.conf中的對應(yīng)模塊參數(shù),
Prefork方式修改<IfModule mpm_prefork_module>,work方式修改<IfModule mpm_worker_module>,參數(shù)根據(jù)軟硬件和應(yīng)用的實際來調(diào)整.
# cd /root/tar/
# tar xzvf httpd-2.2.3.tar.gz
# vi ./srclib/apr-util/Makefile
將
APRUTIL_LIBS = -lsqlite3 /usr/lib/libexpat.la /root/tar/httpd-2.2.3/srclib/apr/libapr-1.la -luuid -lrt -lcrypt -lpthread -ldl
改為
APRUTIL_LIBS = -lsqlite3 /usr/lib64/libexpat.la /root/tar/httpd-2.2.3/srclib/apr/libapr-1.la -luuid -lrt -lcrypt -lpthread -ldl
編譯參數(shù):
./configure --prefix=/opt/apache224 \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util \
LDFLAGS="-L/usr/lib64 -L/lib64" \
--enable-so \
--enable-access=shared \
--enable-auth_anon=shared \
--enable-auth_dbm=shared \
--enable-auth=shared \
--enable-auth_db=shared \
--enable-digest=shared \
--enable-proxy=shared \
--enable-proxy-connect=shared \
--enable-proxy-ftp=shared \
--enable-proxy-http=shared \
--enable-module=alias \
--enable-log_config=shared \
--enable-module=dir \
--enable-mime=shared \
--disable-maintainer-mode \
--with-mpm=worker \
--enable-rewrite=shared
啟用mod_deflate模塊,其余模塊酌情加載:
# vi httpd.conf
LoadModule deflate_module modules/mod_deflate.so
在文件最后加入下面這段配置
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
# DeflateFilterNote Input instream
# DeflateFilterNote Output outstream
# DeflateFilterNote Ratio ratio
# LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
# CustomLog logs/deflate_log deflate
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|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary
AddOutputFilterByType DEFLATE text/*
AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript
AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
</IfModule>
實時檢測HTTPD連接數(shù):
watch -n 1 -d "pgrep httpd|wc -l"
too many open files error:
# ulimit -a
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
file size (blocks, -f) unlimited
pending signals (-i) 1024
max locked memory (kbytes, -l) 32
max memory size (kbytes, -m) unlimited
open files (-n) 1024
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 77823
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
操作:
# vi /etc/security/limits.conf
加上:
* soft nofile 65535
* hard nofile 65535
# vi /etc/pam.d/login
加上:
session required /lib/security/pam_limits.so
配置及平安那些事:
服務(wù)腳本:/etc/rc.d/init.d/httpd
運行目錄:/etc/httpd
配置文件:/etc/httpd/conf/
主配置文件:httpd.conf
擴(kuò)展配置文件:/etc/httpd/conf.d/*.conf
socket:
http: 80/tcp,
https: 443/tcp
網(wǎng)頁文件目錄(DocumentRoot):
靜態(tài)頁面:/var/www/html/protect
動態(tài)頁面(CGI): /var/www/html/cgi-bin/
默認(rèn)主頁面:index.html index.php
【配置prefork模型】:
apache服務(wù)默認(rèn)就是以Prefork模式啟動,無須更改啟動模式,下面來演示如何更改其模型參數(shù)
編輯主配置文件:vim/etc/httpd/conf/httpd.conf找到與模塊相關(guān)的參數(shù):IfModule為指令,意為判斷模塊是否存在,如果存在那么參數(shù)則生效,反之如果不存在此模塊,參數(shù)將不會生效
參數(shù)說明:
<IfModule prefork.c> #如果存在這個模塊,那么提供以下屬性
StartServers 8 #剛啟動web服務(wù)時啟動幾個空閑子進(jìn)程
MinSpareServers 5 #最少空閑進(jìn)程數(shù)
MaxSpareServers 20 #最大空閑進(jìn)程數(shù)
ServerLimit 256 #限定最多允許并發(fā)進(jìn)來的活動用戶連接個數(shù)
【配置worker模型】
更改apache當(dāng)前工作模式為worker模式:
編輯文件/etc/sysconfig/httpd,將以下參數(shù)的注釋信息去掉:
HTTPD=/usr/sbin/httpd.worker
更改模式后必須重新啟動服務(wù)
[root@Centos ~]# service httpd restart
<IfModuleworker.c>
StartServers 4 #配置啟動多少個用于監(jiān)聽的服務(wù),注意:這里并不是服務(wù)器進(jìn)程,而是線程
MaxClients 300 #最大支持的用戶連接數(shù)
MinSpareThreads 25 #最小的空閑線程數(shù)
MaxSpareThreads 75 #最大的空閑線程數(shù)
ThreadsPerChild 25 #允許每個進(jìn)程最多生成多少個線程
MaxRequestsPerChild 0 #設(shè)置一個獨立的子進(jìn)程將能處理的哀求數(shù)量
</IfModule>
1、暗藏Apache版本號的方法是修改Apache的配置文件
vim /etc/httpd/conf/httpd.conf
分別搜索關(guān)鍵字ServerTokens和ServerSignature,修改:
ServerTokens OS 修改為 ServerTokens ProductOnly
ServerSignature On 修改為 ServerSignature Off
2、建立平安的apache的目錄結(jié)構(gòu).
ServerRoot DocumentRoot ScripAlias Customlog Errorlog 均放在單獨的目錄環(huán)境中.
DocumentRoot:設(shè)置Web站點的主目錄,如:DocumentRoot "/tu" //該目錄必須已經(jīng)存在
注:如果更改主目錄后,在瀏覽器中訪問是出現(xiàn)如下錯誤提示:
Forbidden You don't have permission to access / on this server.
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
那么很可能的原因是與SELinux有關(guān),最簡單的辦法就是把SELinux關(guān)掉或刪掉
3、站點主頁設(shè)置
Directorylndex:設(shè)置站點主頁,如:
Directorylndex index.htm index.html index.php
4. 是否允許目錄列表
<Directory "......">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
PS-options:
Indexes: 是否允許索引頁面文件(不平安,建議關(guān)閉);
FollowSynLinks: 是否跟隨軟鏈接文件(不平安,建議關(guān)閉);
SymLinksifOwnerMatch:相對平安的跟隨軟鏈接指令(如果軟鏈接文件的屬主與網(wǎng)頁訪問用戶匹配則允許)
ExecCGI:是否允許執(zhí)行CGI腳本;
All
None
5、一般設(shè)立獨立賬號啟動apache
#mkdir -p /var/www/html/protect
#chmod 755 /var/www/html/protect
#chown -R web.web /var/www/html/protect
#cd /var/www/html/protect
#echo "This is a protect page" > index.html
#cd /var/www/html/protect
#chmod -R 770 templets uploads images
設(shè)置不可執(zhí)行權(quán)限:
如果要保護(hù)的Web目錄不在Web主目錄下,則目錄建立好之后,需要在httpd.conf文件中進(jìn)行別名設(shè)置:
(1) #mkdir -p /var/abc
(2) 打開httpd.conf文件,加入以下指令
Alias /protect/ "/var/abc/"
6、白名單控制
(1) 拒絕某類地址的用戶對服務(wù)器的訪問權(quán)(Deny)
如:Deny from all
Deny from test.cnn.com
Deny from 211.136.141.234
Deny from 192.168.1.0/255.255.0.0
(2) 允許某類地址的用戶對服務(wù)器的訪問權(quán)(Allow)
如:Allow from all
Allow from test.cnn.com
Allow from 211.136.141.237
Allow from 192.168.2.0/255.255.0.0
Deny和Allow指令后可以輸入多個變量.
(3)
Order Allow, Deny
Allow from all
Deny from www.test.com
指想讓所有的人訪問Apache服務(wù)器,但不希望來自www.test.com的任何訪問.
Order Deny, Allow
Deny from all
Allow from test.sina.com
指不想讓所有人訪問,但希望給test.sina.com網(wǎng)站的來訪.
7、配置服務(wù)器支持keep-alived:
KeepAlive {On|Off} :啟用之后支持一次連接可以發(fā)多個哀求(對于非常繁忙的服務(wù)器建議off
KeepAliveTimeout 15 : 15秒之后斷開長連接
MaxKeepAliveRequests 50:一次長連接之內(nèi)最多允許50個哀求
8、制作暗碼文件
先建立文件夾 /var/www/html/htpass , 在該文件夾中用 “ #htpasswd [-c] 暗碼文件名 用戶名 ” 的格式制作暗碼文件如:
#htpasswd -c apass test1
#htpasswd apass test2
那么暗碼文件/var/www/html/htpass中有兩個賬號test1 與 test2了
《Linux下apache安裝及安全配置》是否對您有啟發(fā),歡迎查看更多與《Linux下apache安裝及安全配置》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/13042.html