The server's large user load scheme
First, preface
Second, compile and install
Third, install MySQL, memcache
Iv. installation of Apache, PHP, Eaccelerator, Php-memcache
V. Installation of Squid
Vi. PostScript
First, preface, preparatory work
At present, the lamp development mode is the first choice of web development, how to build an efficient, reliable and stable Web server has always been a hot topic, this article is an attempt at this topic.
We use the following architectural diagram:
Reference--– ———-————-——— ————
| Client | ===> | Load Balancer | ===> | Reverse proxy/Cache | ===> | Web Server | ===> | database server |
——– ———- ————- ——— ————
Nginx Squid apache,php MySQL
Eaccelerator/memcache Preparatory work:
Referral server: Intel (R) Xeon (TM) CPU 3.00GHz * 2, 2GB mem, Scisc HDD
Operating system: CentOs4.4, kernel version 2.6.9-22.ELSMP,GCC version 3.4.4
Software:
Apache 2.2.3 (can use MPM mode)
PHP 5.2.0 (This version is selected because the 5.2.0 engine is relatively more efficient)
Eaccelerator 0.9.5 (acceleration PHP engine, also can encrypt PHP source program)
Memcache 1.2.0 (for cache common data)
Libevent 1.2a (required for memcache working mechanism)
MySQL 5.0.27 (choose Binary version, save compilation work)
Nginx 0.5.4 (used as load balancer)
Squid-2.6.stable6 (provides professional cache function while doing reverse proxy)
Second, compile and install
Installing Nginx
1.) Installation
The Nginx pronunciation is [engine x], a project established by the Russian Igor Sysoev, based on the BSD license. It is said that he was one of the members of F5, English home: http://nginx.net. Some of Russia's big Web sites have been using it for more than two years, and have been remarkable.
The compiler parameters of Nginx are as follows:
[[Email protected]]#./configure–prefix=/usr/local/server/nginx–with-openssl=/usr/include \
–with-pcre=/usr/include/pcre/–with-http_stub_status_module–without-http_memcached_module \
–without-http_fastcgi_module–without-http_rewrite_module–without-http_map_module \
–without-http_geo_module–without-http_autoindex_module
Here, need to explain, because Nginx configuration file I want to use the regular, so need Pcre module support. I have installed the RPM package for Pcre and Pcre-devel, but Ngxin does not find the. h/.so/.a/.la file correctly, so I am slightly flexible:
[[email protected]] #mkdir/usr/include/pcre/.libs/
[[email protected]] #cp/usr/lib/libpcre.a/usr/include/pcre/.libs/libpcre.a
[[email protected]] #cp/usr/lib/libpcre.a/usr/include/pcre/.libs/libpcre.la
Then, modify the Objs/makefile about 908 lines, commenting out the following:
./configure–disable-shared
Next, you will be able to perform the make and makes install normally.
2.) Modify the configuration file/usr/local/server/nginx/conf/nginx.conf
The following is my nginx.conf content, for reference only:
#运行用户
User nobody nobody;
#启动进程
Worker_processes 2;
#全局错误日志及PID文件
Error_log Logs/error.log Notice;
PID Logs/nginx.pid;
#工作模式及连接数上限
Events {
Use Epoll;
Worker_connections 1024;
}
#设定http服务器, using its reverse proxy function to provide load balancing support
HTTP {
#设定mime类型
Include Conf/mime.types;
Default_type Application/octet-stream;
#设定日志格式
Log_format Main ' $remote _addr– $remote _user [$time _local] '
"$request" $status $bytes _sent '
' "$http _referer" "$http _user_agent" '
' $gzip _ratio ';
Log_format Download ' $remote _addr– $remote _user [$time _local] '
"$request" $status $bytes _sent '
' "$http _referer" "$http _user_agent" '
' "$http _range" "$sent _http_content_range";
#设定请求缓冲
Client_header_buffer_size 1k;
Large_client_header_buffers 4 4k;
#开启gzip模块
gzip on;
Gzip_min_length 1100;
Gzip_buffers 4 8k;
Gzip_types Text/plain;
Output_buffers 1 32k;
Postpone_output 1460;
#设定access Log
Access_log Logs/access.log Main;
Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;
Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
Keepalive_timeout 65;
#设定负载均衡的服务器列表
Upstream Mysvr {
#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.
#本机上的Squid开启3128端口
Server 192.168.8.1:3128 weight=5;
Server 192.168.8.2:80 weight=1;
Server 192.168.8.3:80 weight=6;
}
#设定虚拟主机
server {
Listen 80;
server_name 192.168.8.1 www.enew.com.cn;
CharSet gb2312;
#设定本虚拟主机的访问日志
Access_log Logs/www.enew.com.cn.access.log Main;
#如果访问/img/*,/js/*,/css/* Resources, then take the local file directly, not through squid
#如果这些文件较多, this method is not recommended because the cache effect is better through squid
Location ~ ^/(IMG|JS|CSS)/{
root/data3/html;
Expires 24h;
}
#对 "/" Enable load balancing
Location/{
Proxy_pass Http://mysvr;
Proxy_redirect off;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Client_max_body_size 10m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 90;
Proxy_send_timeout 90;
Proxy_read_timeout 90;
Proxy_buffer_size 4k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
Location/nginxstatus {
Stub_status on;
Access_log on;
Auth_basic "Nginxstatus";
Auth_basic_user_file conf/htpasswd;
}
}
}
Note: The contents of the conf/htpasswd file can be generated using the HTPASSWD tools provided by Apache, as follows:
3.) View Nginx running status
Enter the address http://192.168.8.1/NginxStatus/, enter the verification account password, you can see similar to the following:
Active connections:328
Server accepts handled requests
9309 8982 28890
Reading:1 Writing:3 waiting:324
The first line indicates the number of connections currently active
The third number in the third row indicates the total number of requests that Nginx has received at the current time, and if the upper limit is reached, the upper limit will need to be increased.
Row four is the status of the Nginx queue
Installing MySQL, memcache
1.) Install MySQL with the following steps:
[[email protected]] #tar zxf mysql-standard-5.0.27-linux-i686.tar.gz-c/usr/local/server
[[email protected]] #mv/usr/local/server/mysql-standard-5.0.27-linux-i686/usr/local/server/mysql
[[email protected]] #cd/usr/local/server/mysql
[[Email protected]]#./scripts/mysql_install_db–basedir=/usr/local/server/mysql \
–datadir=/usr/local/server/mysql/data–user=nobody
[[email protected]] #cp/usr/local/server/mysql/support-files/my-large.cnf \
/usr/local/server/mysql/data/my.cnf
2.) Modify the MySQL configuration and add some optimization parameters as follows:
[[email protected]] #vi/usr/local/server/mysql/data/my.cnf
The main contents are as follows:
[Mysqld]
Basedir =/usr/local/server/mysql
DataDir =/usr/local/server/mysql/data
user = Nobody
Port = 3306
Socket =/tmp/mysql.sock
Wait_timeout = 30
Long_query_time=1
#log-queries-not-using-indexes = TRUE
Log-slow-queries=/usr/local/server/mysql/slow.log
Log-error =/usr/local/server/mysql/error.log
external-locking = FALSE
Key_buffer_size = 512M
Back_log = 400
Table_cache = 512
Sort_buffer_size = 2M
Join_buffer_size = 4M
Read_buffer_size = 2M
Read_rnd_buffer_size = 4M
Myisam_sort_buffer_size = 64M
Thread_cache_size = 32
Query_cache_limit = 2M
Query_cache_size = 64M
Thread_concurrency = 4
Thread_stack = 128K
Tmp_table_size = 64M
Binlog_cache_size = 2M
Max_binlog_size = 128M
Max_binlog_cache_size = 512M
Max_relay_log_size = 128M
Bulk_insert_buffer_size = 8M
Myisam_repair_threads = 1
Skip-bdb
#如果不需要使用innodb就关闭该选项
#skip-innodb
Innodb_data_home_dir =/usr/local/server/mysql/data/
Innodb_data_file_path = Ibdata1:256m;ibdata2:256m:autoextend
Innodb_log_group_home_dir =/usr/local/server/mysql/data/
Innodb_log_arch_dir =/usr/local/server/mysql/data/
Innodb_buffer_pool_size = 512M
Innodb_additional_mem_pool_size = 8M
Innodb_log_file_size = 128M
Innodb_log_buffer_size = 8M
Innodb_lock_wait_timeout = 50
Innodb_flush_log_at_trx_commit = 2
Innodb_file_io_threads = 4
Innodb_thread_concurrency = 16
Innodb_log_files_in_group = 3
The above configuration parameters should be modified according to the specific needs. Run the following command to start the MySQL server:
/usr/local/server/mysql/bin/mysqld_safe \
–DEFAULTS-FILE=/USR/LOCAL/SERVER/MYSQL/DATA/MY.CNF &
Since MySQL is not installed in the standard directory, you must modify the location of the My_print_defaults file in Mysqld_safe to
Mysqld_safe to start the MySQL server.
3.) Memcache + libevent Install compile and install:
[[email protected]] #cd libevent-1.2a
[[Email protected]]#./configure–prefix=/usr/&& make && make install
[[email protected]] #cd. /memcached-1.2.0
[[Email protected]]#./configure–prefix=/usr/local/server/memcached–with-libevent=/usr/
[[email protected]] #make && make install
Note: If the libevent is not installed in the/usr directory, then you need to copy/link the libevent-1.2a.so.1 to/usr/lib, otherwise
Memcached does not load properly. Run the following command to start memcached:
[[Email protected]]#/usr/local/server/memcached/bin/memcached \
-L 192.168.8.1-d-p 10000-u nobody-m 128
means to start memcached with daemon, listen on port 10000 on 192.168.8.1, run the user as nobody, assign it
128MB of memory.
Install Apache, PHP, Eaccelerator, Php-memcache
IV.) Installation of Apache, PHP, Eaccelerator, Php-memcache due to Apache
PHP 2 under the static way of compiling is very troublesome, so here is the use of dynamic module (DSO) mode. 1.) Install Apache 2.2.3
[[Email protected]]#./configure–prefix=/usr/local/server/apache–disable-userdir–disable-actions \
–disable-negotiation–disable-autoindex–disable-filter–disable-include–disable-status \
–disable-asis–disable-auth–disable-authn-default–disable-authn-file–disable-authz-groupfile \
–disable-authz-host–disable-authz-default–disable-authz-user–disable-userdir \
–enable-expires–enable-module=so
Note: In this case, some unnecessary modules are canceled, if you need to use these modules, please remove some of the parameters.
2.) Install PHP 5.2.0
[[Email protected]]#./configure–prefix=/usr/local/server/php–with-mysql \
–with-apxs2=/usr/local/server/apache/bin/apxs–with-freetype-dir=/usr/–with-png-dir=/usr/\
–with-gd=/usr/–with-jpeg-dir=/usr/–with-zlib–enable-magic-quotes–with-iconv \
–without-sqlite–without-pdo-sqlite–with-pdo-mysql–disable-dom–disable-simplexml \
–enable-roxen-zts
[[email protected]] #make && make install
Note: If you do not need a GD or PDO module, please remove it yourself.
3.) Install eAccelerator-0.9.5
[[email protected]] #cd eAccelerator-0.9.5
[[email protected]] #export php_prefix=/usr/local/server/php
[Email protected]]# $PHP _prefix/bin/phpize
[Email protected]]#./configure–enable-eaccelerator=shared–with-php-config= $PHP _prefix/bin/php-config
[[email protected]] #make && make install
4.) Install the Memcache module
[[email protected]] #cd memcache-2.1.0
[[email protected]] #export php_prefix=/usr/local/server/php
[Email protected]]# $PHP _prefix/bin/phpize
[Email protected]]#./configure–enable-eaccelerator=shared–with-php-config= $PHP _prefix/bin/php-config
[[email protected]] #make && make install
5.) Modify the PHP.ini configuration and modify the php.ini, modify/Add similar to the following:
Extension_dir = "/usr/local/server/php/lib/"
extension= "Eaccelerator.so"
Eaccelerator.shm_size= "32″; set Eaccelerator shared memory to 32MB
Eaccelerator.cache_dir= "/usr/local/server/eaccelerator"
Eaccelerator.enable= "1″
Eaccelerator.optimizer= "1″
Eaccelerator.check_mtime= "1″
Eaccelerator.debug= "0″
Eaccelerator.filter= "*.php"
eaccelerator.shm_max= "0″
Eaccelerator.shm_ttl= "0″
Eaccelerator.shm_prune_period= "3600″
Eaccelerator.shm_only= "0″
Eaccelerator.compress= "1″
Eaccelerator.compress_level= "9″
Eaccelerator.log_file = "/usr/local/server/apache/logs/eaccelerator_log"
Eaccelerator.allowed_admin_path = "/usr/local/server/apache/htdocs/ea_admin"
extension= "Memcache.so"
Here, it is best to add the default file type cache mechanism in Apache configuration, that is, using the Apache Expires module, add a few lines like the following:
Expiresactive on
Expiresbytype text/html "Access plus ten minutes"
Expiresbytype text/css "Access plus 1 day"
Expiresbytype image/jpg "Access 1 month"
Expiresbytype image/gif "Access 1 month"
Expiresbytype image/jpg "Access 1 month"
Expiresbytype Application/x-shockwave-flash "Access plus 3 day"
This setting is because my static files are usually very rarely updated, so I chose the "access" rule, if the update is relatively frequent, you can use the "modification" rule, or the "access" rule, but when the file is updated, execute "touch "Command to refresh the file time.
Installing squid
V.) Installation of Squid
[[Email protected]]#./configure–prefix=/usr/local/server/squid–enable-async-io=100–disable-delay-pools– disable-mem-gen-trace–disable-useragent-log–enable-kill-parent-hack–disable-arp-acl–enable-epoll– Disable-ident-lookups–enable-snmp–enable-large-cache-files–with-large-files
[[email protected]] #make && make install
or use the following installation methods:
[[email protected]] #yum install squid
If the kernel is 2.6, to support Epoll io mode, the old kernel can only select poll or other modes, and remember to bring the option to support large files, otherwise in Access
Log and other files reached 2G when the error will be. Setting squid configuration is probably as follows:
#设定缓存目录为/var/cache1 and/var/lib/squid, cache size is 128MB per processing, when cache space usage reaches 95%
#新的内容将取代旧的而不直接添加到目录中 until the space drops to 90% to stop the activity.
#/var/cache1 Max 1024mb,/var/lib/squid Max 5000MB, 16*256 sub-directory
Cache_dir aufs/var/cache1 1024 16 256
Cache_dir Aufs/var/lib/squid 5000 16 256
Cache_mem MB
Cache_swap_low 90
Cache_swap_high 95
#设置存储策略等
Maximum_object_size 4096 KB
Minimum_object_size 0 KB
Maximum_object_size_in_memory KB
Ipcache_size 1024
Ipcache_low 90
Ipcache_high 95
Cache_replacement_policy LRU
Memory_replacement_policy LRU
#设置超时策略
Forward_timeout seconds
Connect_timeout seconds
Read_timeout 3 minutes
Request_timeout 1 minutes
Persistent_request_timeout seconds
Client_lifetime minutes
Shutdown_lifetime 5 Seconds
Negative_ttl seconds
#限制一个ip最大只能有16个连接
ACL Overconnlimit maxconn 16
Http_access Deny Overconnlimit
#限制baidu Spider Access
#acl Antibaidu Req_header user-agent baiduspider
#http_access Deny Antibaidu
#常规设置
Visible_hostname cache.enew.com
cache_mgr [email protected]
Client_persistent_connections off
Server_persistent_connections on
Cache_effective_user Nobody
Cache_effective_group Nobody
Tcp_recv_bufsize 65535 bytes
Half_closed_clients off
#设定不缓存的规则
Hierarchy_stoplist Cgi-bin
ACL QUERY Urlpath_regex Cgi-bin
Cache Deny QUERY
#不要相信ETag because there is gzip
ACL Apache Rep_header Server ^apache
Broken_vary_encoding Allow Apache
#设置access log and format it in the same format as Apache, facilitating awstats analysis
Emulate_httpd_log on
Logformat Apache%>a%ui%un [%tl] "%rm%ru http/%rv"%Hs%
Initialize and start squid
[[Email Protected]]#/usr/local/server/squid/sbin/squid-z
[[Email Protected]]#/usr/local/server/squid/sbin/squid
The first command is to initialize the squid cache Hashits directory first and only once.
Vi. PostScript
Six, PostScript one, want to enable squid to change the need to make better use of squid cache function, not to enable it, we need to make the following several adjustments:
1, enable the Apache Mod_expires module, modify the httpd.conf, add the following content:
#expiresdefault "modification plus 2 weeks" expiresactive
Onexpiresbytype text/html "Access plus ten minutes" expiresbytype
Image/gif "Modification plus 1 month" Expiresbytype image/jpeg "modification
Plus 1 month "Expiresbytype image/png" modification plus 1
Month "Expiresbytype text/css" Access plus 1 day "Expiresbytype
Application/x-shockwave-flash "Access plus 3 day"
The function of the above configuration is to specify the cache rules for various types of files, and for those static files such as Picture/flash always cache up, can be adjusted according to their needs.
2, modify the php.ini configuration, as follows:
Session.cache_limiter = NoCache
The function of the above configuration is to cancel the cache function in PHP by default and avoid the abnormal cache generation.
3, modify the application for example, there is a PHP program page static.php, it holds some query database results, and the data update is not frequent, so we can consider the cache. Just add the following code in the static.php:
Header (' cache-control:max-age=86400
, Must-revalidate '); header (' Pragma: '); header (' last-modified: ').
Gmdate (' d, D M Y h:i:s '). ' GMT '); Header ("Expires:". Gmdate (' d, D M Y
H:i:s ', time () + ' 86400′). ' GMT ');
The above code means, output an HTTP header information, let squid know this page default cache length of one day.
II.) Brief introduction of Squidclient
* Get squid Running status information: Squidclient-p mgr:info
* Get squid Memory usage: squidclient-p mgr:mem
* Get a list of squid already cached: Squidclient-p mgr:objects. Use it carefully,it may crash
* Get squid disk usage: squidclient-p MGR:DISKD
* Force update of a url:squidclient-p 80-m PURGE http://www.enew.com.cn/static.php
* For more information please see: squidclient-h or Squidclient-p Mgr:
Original address: Http://blog.csdn.net/rushcc2006/archive/2009/11/11/4796892.aspx
Portal site Architecture Nginx+apache+mysql+php+memcached+squid