我用512M的vps,訪問量不大,但內存佔用很大,甚至宕機。php
我用top,而後shitf+m發現,httpd佔用內存極大。通過網上找資料設置後,用過一段時間終於沒再出現內存問題了。前端
首先查找配置文件的位置,能夠用以下命令:apache
find / -name httpd.conf
找到配置文件/usr/local/apache/conf/extra/httpd-mpm.conf,修改設置Apache MPM Prefork模塊服務器
StartServers 3
MinSpareServers 2
MaxSpareServers 5
ServerLimit 256
MaxClients 256
MaxRequestsPerChild 40性能
我原來的MaxRequestsPerChild是爲0,問題應該在此。優化
StartServers設置了服務器啓動時創建的子進程數量
MinSpareservers和MaxSpareServers分別設置空閒子進程的最小和最大數量網站
ServerLimit則是控制MaxClients所能使用的最大值。縮減MaxClients能讓運行動態內容(好比:WordPress)的服務器有很大的改變。若是你的VPS遭遇到流量的大幅增長,而你的MaxClients設置的過高的話,你的服務器將會無限循環工做於從物理內存交換頁面到虛擬內存中,最終致使宕機。通常計算適當的MaxClients值取決於你總共可用的系統內存除於每一個Apache進程使用的內存。例如,若是你還有500MB的內存可用於Apache,每一個Apache進程大約使用20MB的內存,你能夠設置你的MaxClients爲(512-12)/ 10 = 50(這個計算好像原文中有誤)。使用命令top能夠獲得你VPS實時內存的使用。this
MaxRequestsPerChild這個指令設定一個獨立的子進程將能處理的請求數量。在處理「MaxRequestsPerChild 數字」個請求以後,子進程將會被父進程終止,這時候子進程佔用的內存就會釋放,若是再有訪問請求,父進程會從新產生子進程進行處理。若是 MaxRequestsPerChild預設爲0(無限)或較大的數字(例如10000以上)能夠使每一個子進程處理更多的請求,不會由於不斷終止、啓動子進程下降訪問效率,但MaxRequestsPerChild設置爲0時,若是佔用了200~300M內存,即便負載下來時佔用的內存也不會減小。內存較大的服務器能夠設置爲0或較大的數字。內存較小的服務器不妨設置成50、100、200,以防內存溢出。spa
找到配置文件/usr/local/apache/conf/extra/httpd-default.conf,修改KeepAlive設置code
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 30
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to 「Off」 to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 120
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5
Timeout是一個鏈接多少時間後斷開,這個參數設置在30-60是通常的php程序都是適用的,至少要運行一些要佔用大量時間的php程序,那麼適當調高也是能夠的,但請不要過高,不然會影響apache性能,本次優化咱們使用30就很足夠了。
MaxKeepAliveRequests 是一個鏈接最大的請求量,對於頁面有較多的圖片等元素,能夠適當調高一點,對於通常的網頁設置在80-120是足夠的,咱們就設置爲120,若是設置過高會致使httpd長時間不能退出釋放內存的。
KeepAliveTimeout 是當用戶處理一次鏈接時,若是在該參數的時間內還有請求則會繼續執行,不須要從新建立新的鏈接,直到達到MaxKeepAliveRequests的最大值纔會退出。對於perfork模式下的,有人認爲是將KeepAlive Off會比較好,可是對於絕大多數的網站都會很少很多有些圖片元素,因此將該項打開,並將KeepTimeOut設置在2-5秒,不但有效提升服務器性能,也能加快頁面打開速度。
若是還須要優化能夠考慮修改啓動的模塊,Wordpress主要使用如下模塊,保留這些其餘的能夠註釋掉
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule expires_module modules/mod_expires.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule mime_module modules/mod_mime.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule negotiation_module modules/mod_negotiation.so
轉載請註明:apache佔用內存高解決辦法 - 前端開發