博文

目前显示的是标签为“Apache”的博文

apache中如何只允许本地和另一IP访问

httpd.conf 中设置 Options All AllowOverride None Order Deny,Allow Deny From all Allow From 192.168.0.7 Allow From 127.0.0.1

Apache关掉Etag、Last-Modified及apache版本的方法

Etag关掉的方法如下,加一个none   FileETag none   要关掉Last-Modified的方法麻烦点,先想好你要去掉Last-Modified 的标签。然后用header模块来控制   LoadModule headers_module modules/mod_headers.so   <FilesMatch “\.(gif|jpg|png)”> Header unset Last-Modified </FilesMatch>   去掉apache版本号 ServerSignature off ServerTokens prod

Apache的prefork模式和worker模式

prefork模式 这个多路处理模块(MPM)实现了一个非线程型的、预派生的web服务器,它的工作方式类似于Apache 1.3。它适合于没有线程安全库,需要避免线程兼容性问题的系统。它是要求将每个请求相互独立的情况下最好的MPM,这样若一个请求出现问题就不会影响到其他请求。   这个MPM具有很强的自我调节能力,只需要很少的配置指令调整。最重要的是将MaxClients设置为一个足够大的数值以处理潜在的请求高峰,同时又不能太大,以致需要使用的内存超出物理内存的大小。   worker模式 此多路处理模块(MPM)使网络服务器支持混合的多线程多进程。由于使用线程来处理请求,所以可以处理海量请求,而系统资源的开销小于基于进程的MPM。但是,它也使用了多进程,每个进程又有多个线程,以获得基于进程的MPM的稳定性。   控制这个MPM的最重要的指令是,控制每个子进程允许建立的线程数的ThreadsPerChild指令,和控制允许建立的总线程数的MaxClients指令。   prefork和worker模式的切换 1.将当前的prefork模式启动文件改名 mv httpd httpd.prefork 2.将worker模式的启动文件改名 mv httpd.worker httpd 3.修改Apache配置文件 vi /usr/local/apache2/conf/extra/httpd-mpm.conf 找到里边的如下一段,可适当修改负载等参数: <IfModule mpm_worker_module> StartServers 2 MaxClients 150 MinSpareThreads 25 MaxSpareThreads 75 ThreadsPerChild 25 MaxRequestsPerChild 0 </IfModule> 4.重新启动服务 /usr/local/apache2/bin/apachectl restart 即可换成worker方式启动apache2   处于稳定性和安全性考虑,不建议更换apache2的运行方式,使用系统默认prefork即可。另外很多php模块不能工作在worker模式下,例如redhat linux自带的php也不能支持线程安全。所以最好不要切换...

禁止显示或发送Apache版本号(设置ServerTokens)

默认地,服务器HTTP响应头会包含apache和php版本号。像下面的,这是有危害的,因为这会让黑客通过知道详细的版本号而发起已知该版本的漏洞攻击。 1.Server: Apache/2.2.17 (Unix) PHP/5.3.5 为了阻止这个,需要在httpd.conf设置ServerTokens为Prod,这会在响应头中显示“Server:Apache”而不包含任何的版本信息。 1.# vi httpd.conf 2.ServerTokens Prod 下面是ServerTokens的一些可能的赋值: ServerTokens Prod 显示“Server: Apache” ServerTokens Major 显示 “Server: Apache/2″ ServerTokens Minor 显示“Server: Apache/2.2″ ServerTokens Min 显示“Server: Apache/2.2.17″ ServerTokens OS 显示 “Server: Apache/2.2.17 (Unix)” ServerTokens Full 显示 “Server: Apache/2.2.17 (Unix) PHP/5.3.5″ (如果你这指定任何的值,这个是默认的返回信息)

启用gzip模块优化网页

Nginx   http { : include conf/mime.types; : gzip on; : gzip_min_length 1000; : gzip_buffers 4 8k; : gzip_http_version 1.1; : gzip_types text/plain application/x-javascript text/css text/html application/xml; } 重启nginx IIS6 打开Internet信息服务(IIS)管理器,右击”网站”→“属性”,选择”服务”。在”HTTP压缩”框中选中”压缩应用程序文件”和”压缩静态文件”,按需要设置”临时目录”和”临时目录的最大限制”; 在Internet信息服务(IIS)管理器,右击”Web服务扩展”→“增加一个新的Web服务扩展…“,在”新建Web服务扩展”框中输入扩展 名”HTTPCompression”,添加”要求的文件”为C:\WINDOWS\system32\inetsrv\gzip.dll,其中 Windows系统目录根据您的安装可能有所不同,选中”设置扩展状态为允许”; 使用文本编辑器打开C:\Windows\System32\inetsrv\MetaBase.xml(建议先备份),找到Location =”/LM/W3SVC/Filters/Compression/gzip”,如果需要压缩动态文件,则将 HcDoDynamicCompression设置为”TRUE”,并在HcScriptFileExtensions中增加您要压缩的动态文件后缀名, 如aspx;如果需要压缩静态文件,则将HcDoStaticCompression和HcDoOnDemandCompression设置 为”TRUE”,并在HcFileExtensions中增加您需要压缩的静态文件后缀名,如xml、css 等;HcDynamicCompressionLevel和HcOnDemandCompLevel表示需要的压缩率,数字越小压缩率越低; 编辑完毕后保存MetaBase.xml文件;如果文件无法保存,则可能IIS正在使用该文件。打开”开始”→“管理工具”→“服务”,停止”IIS Admin Service”后,即可保存; 最后,重新启动IIS。 Apache 编辑ht...

Apache VirtualHost设定

通常在一台Web Server上,會有數個不一樣的網站要執行,這時候各個網站的domain name就必須靠VirtualHost設定 VirtualHost基本設定 #指定主機所有port 80所指定的虛擬主機 NameVirtualHost *:80   <Virtualhost *:80> #DNS ServerName wp.johnsonlu.org   #Web Root Path DocumentRoot /var/www/test   #Alias可以將DocumentRoot底下的路徑對應成特定目錄路徑 Alias /doc/ /var/www/test/doc/   #控制目錄權限 <Directory “/usr/share/doc/”> #Options可以透過+號或-號來控制目錄權限,例如關閉Index目錄 #FollowSymLinks是允許連到DocumentRoot以外的其他目錄、MultiViews是多國語言支援 Options -Indexes FollowSymLinks MultiViews   #AllowOverride是用來設定Override權限。None代表關閉.htaccess;FileInfo則是開啟 AllowOverride None   #Order負責控制Deny和Allow執行順序 Order Deny,Allow #控制要拒絕的連線 Deny from all #控制可以通過的連線 Allow from 127.0.0.0/255.0.0.0 </Directory>   #Log ErrorLog /var/log/error_log </Virtualhost> Alias也可以透過正規表示法設定成pattern,可以參考mod_alias Directory設定可以參考Directory Directive Allow和Deny的部份可以參考mod_access_compat     另外也可以把VirtualHost設定成動態的pattern (需要開啟mod_vhost_alias module) <VirtualHost *:...

Apache压力测试工具 ApacheBench

這是Apache內建的壓力測試工具 指令為(ab or ab2) ab -k -c 10 -n 10 http://xxxx.com/ -k 是代表KeepAlive -c concurrent 代表同時進行壓力測試的數量 -n request number 代表request的數量

Apache mod_deflate(gzip)

在Apache中,可以啟用mod_deflate來壓縮網頁,傳送給client後再由client端解壓縮,可以大大節省頻寬的使用 開啟方法: #先到apache的httpd.conf中把mod_deflate.so註解拿掉(如果預設有引入的話就代表已經有使用deflate壓縮囉) LoadModule deflate_module libexec/apache22/mod_deflate.so #Ubuntu可以使用指令開啟 a2enmod deflate 接著可以在Apache設定檔(httpd.conf)中自訂一些設定 <IfModule mod_deflate.c> #壓縮等級1~9,等級越高壓縮效能越好 DeflateCompressionLevel 9 #設定要壓縮的類型 AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-httpd-php #自訂Log格式及存放位置 DeflateFilterNote ratio LogFormat ‘”%r” %b (%{ratio}n) “%{User-agent}i”‘ deflate CustomLog /var/log/deflate_log deflate </IfModule> 其他更細部的操作,例如BrowserMatch等等,可以直接參考Apache mod_deflate的說明 設定完成後重新啟動Apache即可,可以透過這個網站測試

FreeBSD中显示Apache进程运行情况脚本

在FreeBSD中查看进程情况最常用的就是ps命令了,我们当然可以利用ps来查看apache进程运行的情况(假定apache的运行用户为www)脚本如下: ps -U www -axo comm,pcpu,pmem,vsz,rss,mwchan | awk ‘/^httpd/ {++state[$NF];cpu+=$2;mem+=$3;vsz+=$4;rss+=$5;} END {for(key in state){total+=state[key];print(key,”\t”,state[key]);}print(“total\t” total “\ncpu\t” cpu “%\nmem\t” mem “%\nvsz\t” vsz “k\nrss\t” rss “k”);}’ 输出大致结果如下: select 1 accept 9 total 10 cpu 7.1% mem 6.1% vsz 227152k rss 128768k 呵呵,这样便看到了apache的进程运行的统计情况了。

合理设置apache参数

在/usr/local/etc/apache22/httpd.conf中加载MPM配置(去掉前面的注释): # Server-pool management (MPM specific) Include etc/apache22/extra/httpd-mpm.conf 可见的MPM配置在/usr/local/etc/apache22/extra/httpd-mpm.conf,但里面根据httpd的工作模式分了很多块,哪一部才是当前httpd的工作模式呢?可通过执行 apachectl -l 来查看: Compiled in modules: core.c prefork.c http_core.c mod_so.c 看到prefork 字眼,因此可见当前httpd应该是工作在prefork模式,prefork模式的默认配置是: <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 150 MaxRequestsPerChild 0 </IfModule> 2.要加到多少? 连接数理论上当然是支持越大越好,但要在服务器的能力范围内,这跟服务器的CPU、内存、带宽等都有关系。 查看当前的连接数可以用: ps aux | grep httpd | wc -l 或: pgrep httpd|wc -l 计算httpd占用内存的平均数: ps aux|grep -v grep|awk ‘/httpd/{sum+=$6;n++};END{print sum/n}’ 由于基本都是静态页面,CPU消耗很低,每进程占用内存也不算多,大约200K。 服务器内存有2G,除去常规启动的服务大约需要500M(保守估计),还剩1.5G可用,那么理论上可以支持1.5*1024*1024*1024/200000 = 8053.06368 约8K个进程,支持2W人同时访问应该是没有问题的(能保证其中8K的人访问很快,其他的可能需要等待1、2秒才能连上,而一旦连上就会很流畅) 控制最大连接数的MaxClients ,因此可以尝试配置为: <IfModule mpm_prefork_module> StartSer...

Optimize Apache and MySQL for a 256MB VPS

sudo nano /etc/mysql/my.cnf   Add the following line into the section [mysqld]: skip-innodb   Next locate the line skip-external-locking and add skip-locking below that line.   Now find the section labeled “Fine Tuning”. Change/add the settings in that section to match those below:   key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K thread_cache_size = 4 sort_buffer = 64K net_buffer_length = 2K   Save the file (ctrl+x) and restart MySQL: sudo /etc/init.d/mysql restart If you run MySQLtuner again you will see that the “memory” warning is gone, ignore the aother warnings for the moment (you need to run the script after a few days again to get exact test results)   Optimize Apache in prefork mode If you followed the Ubuntu tutorial we’ve mentioned in the first paragraph, your Apache setup should run in prefork mode. The default settings are much to high, open the file sudo nano /etc/apache2/apache2.conf andchange the following settings:    ...

Apache/MySQL/PHP/phpMyAdmin on FreeBSD

1. Introduction: This article describes how to setup Apache, MySQL, PHP and phpMyAdmin on a server running FreeBSD. The article was written for the software versions below but is likely to work on newer versions without too much difficulty.  2. Software: Operating System:        FreeBSD 7.0 for i386       Download Apache:                           2.2.8                                Installed from Ports Collection MySQL Server:            5.0.51a                             Installed from Ports Collection PHP & Extensions:        5.2.5                                Installed from Ports Collection p...