錢紅雷
[摘 要]LAMP(Linux+Apache+MySQL+PHP)網(wǎng)站架構(gòu)是性能非常穩(wěn)定的Web框架,該框架包括:Linux操作系統(tǒng)、Apache網(wǎng)絡(luò)服務(wù)器、MySQL數(shù)據(jù)庫、PHP編程語言。本文描述的是LAMP搭建過程中如何編譯軟件的參數(shù)和配置文件設(shè)置,從而達到LAMP性能優(yōu)化的目的。
[關(guān)鍵詞]Linux;Apache;PHP;Mysql;httpd
doi:10.3969/j.issn.1673 - 0194.2016.06.120
[中圖分類號]TP393.05 [文獻標(biāo)識碼]A [文章編號]1673-0194(2016)06-0-01
本文從Linux系統(tǒng)、Mysql數(shù)據(jù)庫、安裝Apache和php三個方面入手,分別描述了LAMP系統(tǒng)是如何進行參數(shù)配置和性能優(yōu)化的。
1 Linux系統(tǒng)優(yōu)化
1.1 關(guān)閉無用的后臺守護進程
運行ntsysv選擇啟動哪些服務(wù),從而節(jié)省物理內(nèi)存消耗。
1.2 減少終端連接數(shù)
根據(jù)自己的實際需求,運行vi /etc/inittab修改。
1.3 編譯內(nèi)核啟動配置文件
#vi /etc/sysctl.conf
net.ipv4.tcp_syncookies = 1 //防范少量SYN攻擊
net.ipv4.tcp_tw_reuse = 1 //將TIME-WAIT sockets重新用于新的TCP連接
net.ipv4.tcp_tw_recycle = 1 //開啟TIME-WAIT sockets的快速回收
net.ipv4.tcp_keepalive_time = 1200 //TCP發(fā)送keepalive消息的頻度
net.ipv4.ip_local_port_range = 1024 65000 //表示用于向外連接的端口范圍
net.ipv4.tcp_max_syn_backlog = 8192 //容納更多等待連接的網(wǎng)絡(luò)連接數(shù)
2 設(shè)置Mysql數(shù)據(jù)庫
2.1 修改數(shù)據(jù)庫最大連接數(shù)和查詢緩存
#vi /etc/my.cnf
max_connections=20000 //設(shè)置mysql數(shù)據(jù)庫最大連接數(shù)
query_cache_size=32M //取決于查詢的實際情況,但最好設(shè)置為1024的倍數(shù)
query_cache_type=1 //將會緩存所有的結(jié)果
2.2 禁止遠程連接數(shù)據(jù)庫
確認(rèn)Mysql數(shù)據(jù)庫的host設(shè)置為localhost。
2.3 用戶目錄權(quán)限限制
確保mysqld運行時,只使用對數(shù)據(jù)庫目錄具有讀或?qū)憴?quán)限的linux用戶來運行。
# chown -R root /usr/local/mysql/ //mysql主目錄給root
# chown -R mysql.mysql /usr/local/mysql/var //確保數(shù)據(jù)庫目錄權(quán)限所屬mysql用戶
3 優(yōu)化編譯Apache和PHP
使用DSO動態(tài)編譯
3.1 編譯Apache
因為Apache是1.3.*的版本非常穩(wěn)定,所以用這個版本編譯,編譯前把Apache的最大連接數(shù)修改為2000(linux默認(rèn)256)
#vi httpd.h
define HARD_SERVER_LIMIT 50000
#./configure --prefix=/usr/local/apache --enable-module=so --enable-module=rewrite --enable-shared=max --enable-module=most
#make
#make install
3.2 編譯Apache的限制IP并發(fā)數(shù)的模塊
#/usr/local/apache/bin/apxs –c –i –a mod_limitipconn.c
3.3 編譯PHP
#./configure --prefix=/usr/local/php --with-mysql --with-apxs=/usr/local/apache/bin/apxs --with-xml --enable-ftp --enable-force-cgi-redirect --enable-trans-sid --enable-track-vars --enable-url-includes --enable-sockets --with-gd=/usr/local/gd --with-zlib-dir=/usr/lib --with-gdbm-dir=/usr/lib
#make
#make install
3.4 編輯httpd.conf文件
查找
#Options Indexes FollowSymLinks MultiViews //去掉"Indexes"
查找
在此范圍添加
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
把#ExtendedStatus On這一行注釋掉
添加
#this is my new mod
MaxConnPerIP 1(每個IP用戶的最大連接數(shù))