Linux下apache安装及安全配置
《Linux下apache安装及安全配置》要点: Linux 64位系统的编译方法: 共有3种工作模式: prefork:一个主进程产生多个子进程,一个子进程响应一个哀求; worker:一个进程生成多个线程,一个线程响应一个哀求; event:基于事件驱动; 1、采用prefork方式需修改httpd-2.2.4/server/mpm/prefork.c的数值 #define DEFAULT_SERVER_LIMIT 5120 2、采用worker方式需修改server/mpm/worker/worker.c的数值 #define DEFAULT_THREAD_LIMIT 64 #define MAX_THREAD_LIMIT 20000 同理,部署好之后需对应地修改参数文件extra/httpd-mpm.conf中的对应模块参数, Prefork方式修改<IfModule mpm_prefork_module>,work方式修改<IfModule mpm_worker_module>,参数根据软硬件和应用的实际来调整. # 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 编译参数: ./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连接数: 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 配置及平安那些事: 服务脚本:/etc/rc.d/init.d/httpd 运行目录:/etc/httpd 配置文件:/etc/httpd/conf/ 主配置文件:httpd.conf 扩展配置文件:/etc/httpd/conf.d/*.conf socket: http: 80/tcp, https: 443/tcp 网页文件目录(DocumentRoot): 静态页面:/var/www/html/protect 动态页面(CGI): /var/www/html/cgi-bin/ 默认主页面:index.html index.php 【配置prefork模型】: apache服务默认就是以Prefork模式启动,无须更改启动模式,下面来演示如何更改其模型参数 编辑主配置文件:vim/etc/httpd/conf/httpd.conf找到与模块相关的参数:IfModule为指令,意为判断模块是否存在,如果存在那么参数则生效,反之如果不存在此模块,参数将不会生效 参数说明: <IfModule prefork.c> #如果存在这个模块,那么提供以下属性 StartServers 8 #刚启动web服务时启动几个空闲子进程 MinSpareServers 5 #最少空闲进程数 MaxSpareServers 20 #最大空闲进程数 ServerLimit 256 #限定最多允许并发进来的活动用户连接个数 【配置worker模型】 更改apache当前工作模式为worker模式: 编辑文件/etc/sysconfig/httpd,将以下参数的注释信息去掉: HTTPD=/usr/sbin/httpd.worker 更改模式后必须重新启动服务 [root@Centos ~]# service httpd restart <IfModuleworker.c> StartServers 4 #配置启动多少个用于监听的服务,注意:这里并不是服务器进程,而是线程 MaxClients 300 #最大支持的用户连接数 MinSpareThreads 25 #最小的空闲线程数 MaxSpareThreads 75 #最大的空闲线程数 ThreadsPerChild 25 #允许每个进程最多生成多少个线程 MaxRequestsPerChild 0 #设置一个独立的子进程将能处理的哀求数量 </IfModule> 1、暗藏Apache版本号的方法是修改Apache的配置文件 vim /etc/httpd/conf/httpd.conf 分别搜索关键字ServerTokens和ServerSignature,修改: ServerTokens OS 修改为 ServerTokens ProductOnly ServerSignature On 修改为 ServerSignature Off 2、建立平安的apache的目录结构. ServerRoot DocumentRoot ScripAlias Customlog Errorlog 均放在单独的目录环境中. DocumentRoot:设置Web站点的主目录,如:DocumentRoot "/tu" //该目录必须已经存在 注:如果更改主目录后,在浏览器中访问是出现如下错误提示: 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有关,最简单的办法就是把SELinux关掉或删掉 3、站点主页设置 Directorylndex:设置站点主页,如: Directorylndex index.htm index.html index.php 4. 是否允许目录列表 <Directory "......"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> PS-options: Indexes: 是否允许索引页面文件(不平安,建议关闭); FollowSynLinks: 是否跟随软链接文件(不平安,建议关闭); SymLinksifOwnerMatch:相对平安的跟随软链接指令(如果软链接文件的属主与网页访问用户匹配则允许) ExecCGI:是否允许执行CGI脚本; All None 5、一般设立独立账号启动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 设置不可执行权限: 如果要保护的Web目录不在Web主目录下,则目录建立好之后,需要在httpd.conf文件中进行别名设置: (1) #mkdir -p /var/abc (2) 打开httpd.conf文件,加入以下指令 Alias /protect/ "/var/abc/" 6、白名单控制 (1) 拒绝某类地址的用户对服务器的访问权(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) 允许某类地址的用户对服务器的访问权(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服务器,但不希望来自www.test.com的任何访问. Order Deny,Allow Deny from all Allow from test.sina.com 指不想让所有人访问,但希望给test.sina.com网站的来访. 7、配置服务器支持keep-alived: KeepAlive {On|Off} :启用之后支持一次连接可以发多个哀求(对于非常繁忙的服务器建议off KeepAliveTimeout 15 : 15秒之后断开长连接 MaxKeepAliveRequests 50:一次长连接之内最多允许50个哀求 8、制作暗码文件 先建立文件夹 /var/www/html/htpass,在该文件夹中用 “ #htpasswd [-c] 暗码文件名 用户名 ” 的格式制作暗码文件如: #htpasswd -c apass test1 #htpasswd apass test2 那么暗码文件/var/www/html/htpass中有两个账号test1 与 test2了 《Linux下apache安装及安全配置》是否对您有启发,欢迎查看更多与《Linux下apache安装及安全配置》相关教程,学精学透。编程之家PHP学院为您提供精彩教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |