Nginx+apache+mysql+php+memcached+squid to build a cluster web environment _nginx

Source: Internet
Author: User
Tags current time epoll install php memcached memory usage php mysql sqlite disk usage
ServerThe bigUserThe load of the quantityScheme
First, the preface
TwoCompileInstallation
Third, install mySql, Memcache
Four, install Apache, PHP, Eaccelerator,Php-memcache
V. Installation of Squid
Six, PostScript


First, preface, preparation work
Currently, LAMPDevelopmentModeis the first choice for web development, how to build an efficient, reliable, stable webServicehas always been a hot topic, this article is an attempt at this topic.
We use theArchitectureThe figure is as follows:

Reference----------------------------------------------------
|Client| ===> | Load Balancer | ===> | Reverse proxy/Cache| ===> | Web Server | ===> |DatabaseServer |
-------- ---------- ------------- --------- ------------
Nginx Squid apache,php MySQL
Eaccelerator/memcache Preparatory work:
Referral server: Intel (R) Xeon (TM) CPU 3.00GHz * 2, 2GB mem, Scisc hard Drive
OperationSystem: 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 chosen because of the 5.2.0Enginerelatively more efficient)
Eaccelerator 0.9.5 (accelerated PHP engine, but also can encrypt PHP sourceProgram)
Memcache 1.2.0 (Used for cachingData)
Libevent 1.2a (required by memcache working mechanism)
MySQL 5.0.27 (choose binary version, save compile work)
Nginx 0.5.4 (used as a load balancer)
Squid-2.6.stable6 (provides professional caching while doing a reverse proxy)Function)

Ii. Compiling and installing
One,) installation Nginx
1.) Installation
Nginx is pronounced [engine x], established by the Russian Igor Sysoev.Project, based on BSD license. He was said to have been a member of the F5, English homepage: http://Nginx. Net. Some of Russia's big websites have been using it for more than two years, and have been performing extraordinary things.
The compilation parameters for Nginx are as follows:

[Root@localhost]#./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, it needs to be explained that because of the nginx configurationFileI want to use the regular, so I need pcre.Module's support. I have installed the RPM package for Pcre and Pcre-devel, but Ngxin is not able to find the. h/.so/.a/.la file correctly, so I'm a little bit flexible:

[Root@localhost] #mkdir/usr/include/pcre/.libs/
[Root@localhost] #cp/usr/lib/libpcre.a/usr/include/pcre/.libs/libpcre.a
[Root@localhost] #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 made and make 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;
#全局错误Logand PID files
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.360mini.com;
CharSet gb2312;
#设定本虚拟主机的访问日志
Access_log Logs/www.360mini.com.access.log Main;
#如果访问/img/*,/js/*,/css/* Resources, then take the local file directly, not through squid
#如果这些文件较多, this approach is not recommended because the cache effect through squid is better
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 are usedApacheProvided by the HTPASSWDToolsTo produce, the content is roughly as follows:
3.) View Nginx Run status
Enter the address http://192.168.8.1/NginxStatus/, enter the authentication 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 current number of active connections
The third digit of the third line indicates the total number of requests received by the Nginx to the current time, and if the upper limit is reached, the upper limit will need to be increased.
Line four is the Nginx queue state

Third, the installation of MySQL, memcache



1.) Install MySQL, the following steps:
[Root@localhost] #tar ZXFMysql-standard-5.0.27-Linux-i686.tar.gz-c/usr/local/server
[Root@localhost] #mv/usr/local/server/mysql-standard-5.0.27-linux-i686/usr/local/server/mysql
[Root@localhost] #cd/usr/local/server/mysql
[root@localhost]#./scripts/mysql_install_db--basedir=/usr/local/server/mysql \
--datadir=/usr/local/server/mysql/data--user=nobody
[root@localhost] #cp/usr/local/server/mysql/support-files/my-large.cnf \
/usr/local/server/mysql/data/my.cnf

2.) Modify the MySQL configuration, add some optimization parameters, as follows:
[Root@localhost] #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 slightly modified according to the specific requirements. Run the followingCommandYou can start the MySQL server:
/usr/local/server/mysql/bin/mysqld_safe \
--DEFAULTS-FILE=/USR/LOCAL/SERVER/MYSQL/DATA/MY.CNF &

Because MySQL is not installed in the standard directory, you must modify the location of the My_print_defaults file in the Mysqld_safe to pass
Mysqld_safe to start the MySQL server.
3.) Memcache + libevent installation compile:
[Root@localhost] #cd libevent-1.2a
[Root@localhost]#./configure--prefix=/usr/&& make && make install
[Root@localhost] #cd. /memcached-1.2.0
[Root@localhost]#./configure--prefix=/usr/local/server/memcached--with-libevent=/usr/
[Root@localhost] #make && make install

Note: If the libevent is not installed in the/usr directory, then the libevent-1.2a.so.1 Copy/link to the/usr/lib, otherwise
memcached cannot load properly. Run the following command to start the memcached:
[Root@localhost]#/usr/local/server/memcached/bin/memcached \
-L 192.168.8.1-d-p 10000-u nobody-m 128

Indicates that the memcached is started in a daemon manner, listening on the 192.168.8.1 10000 port, running the user as nobody, assigning
128MB of memory.

Four, install Apache, PHP, Eaccelerator, Php-memcache



Iv. install Apache, PHP, Eaccelerator, Php-memcache due to Apache
PHP 2 under the static mode of compiling is very troublesome, so here the Dynamic module (DSO) approach. 1.) Install Apache 2.2.3
[root@localhost]#./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: Here, remove some unnecessary modules, if you need to use these modules, please remove some of the parameters.
2.) Install PHP 5.2.0
[Root@localhost]#./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
[Root@localhost] #make && make install

Note: If you do not need GD or PDO modules, please remove them.
3.) Install eAccelerator-0.9.5
[Root@localhost] #cd eAccelerator-0.9.5
[Root@localhost] #export php_prefix=/usr/local/server/php
[root@localhost]# $PHP _prefix/bin/phpize
[Root@localhost]#./configure--enable-eaccelerator=shared--with-php-config= $PHP _prefix/bin/php-config
[Root@localhost] #make && make install

4.) Install Memcache module
[Root@localhost] #cd memcache-2.1.0
[Root@localhost] #export php_prefix=/usr/local/server/php
[root@localhost]# $PHP _prefix/bin/phpize
[Root@localhost]#./configure--enable-eaccelerator=shared--with-php-config= $PHP _prefix/bin/php-config
[Root@localhost] #make && make install

5.) Modify the php.ini configuration and then modify the php.ini to modify/add something 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, to take advantage of Apache's expires module, add a few lines like the following:
Expiresactive on
Expiresbytype text/html "Access plus 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 rarely updated, so I chose the "access" rule, and if the update is relatively frequent, you can use the "modification" rule, or you can take the "access" rule, but when the file is updated, perform a "touch "Command, the time to refresh the file."



V. Installation of Squid



V. Installation of Squid
[Root@localhost]#./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
[Root@localhost] #make && make install

or use the following installation method:
[Root@localhost] #yum Install squid

In the case of a 2.6 kernel, the Epoll IO mode can be supported, and the older kernel can only select poll or other modes, and remember to bring the option to support large files, otherwise in Access
Log files such as 2G when the error will be. The configuration of squid is approximately as follows:
#设定缓存目录为/var/cache1 and/var/lib/squid, each processing cache size of 128MB, when cache space usage reaches 95%
#新的内容将取代旧的而不直接添加到目录中 until the space falls to 90% before stopping the activity.
#/var/cache1 Max 1024mb,/var/lib/squid Max 5000MB, all 16*256 level subdirectories
Cache_dir aufs/var/cache1 1024 16 256
Cache_dir Aufs/var/lib/squid 5000 16 256
CACHE_MEM 128 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.yejr.com
Cache_mgr 1638651355@qq.com 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 are gzip
ACL Apache Rep_headerServer^apache
Broken_vary_encoding Allow Apache
#设置access log and make it the same format as Apache to facilitate awstats analysis
Emulate_httpd_log on
Logformat Apache%>a%ui%un [%tl] "%rm%ru-http/%rv"%Hs%

Initialize and start squid
[Root@localhost]#/usr/local/server/squid/sbin/squid-z
[Root@localhost]#/usr/local/server/squid/sbin/squid

The first command is to initialize the squid cache Hashits directory, only one execution at a time.

Six, PostScript


Six, PostScript one, the need to enable squid to make the changes needed to better use squid cache function, not to enable it on the can, we need to do the following several adjustments:
1, enable the Apache Mod_expires module, modify httpd.conf, add the following content:
#expiresdefault "modification plus 2 weeks" expiresactive
Onexpiresbytype text/html "Access plus 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 role of the above configuration is to specify the cache rules for various types of files, for those pictures/flash static files are always cache up, according to their needs to make appropriate adjustments.
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, so as to avoid abnormal cache generation.
3, modify Application For example, there is a PHP program code:
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 outputting an HTTP header message to let squid know that the default cache length for this page is one day.
Ii.) squidclient Brief Introduction
* Get squid running status information: Squidclient-p mgr:info
* Get squid Memory usage: squidclient-p MGR:MEM
* Get squid already cached list: Squidclient-p mgr:objects. Use it carefully,it may crash
* disk usage for squid: squidclient-p MGR:DISKD
* To force an update url:squidclient-p 80-m PURGE htt p://www.360mini.com/static.php
* More see: Squidclient-h or Squidclient-p Mgr:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.