摘要:一、前言,准备工作 当前,LAMP开发模式是WEB开发的首选,如何搭建一个高效、可靠、稳定的WEB服务器一直是个热门主题,本文就是这个主题的一次尝试。 我们采用的架构图如下: -------- ---------- ------------- --------- ------------ | 客户端 | ===> |负载均衡
-------- ---------- ------------- --------- ------------ | 客户端 | ===> |负载均衡器| ===> |反向代理/缓存| ===> |WEB服务器| ===> |数据库服务器| -------- ---------- ------------- --------- ------------ Nginx Squid Apache,PHP MySQL/memcache eAccelerator
[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
[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
./configure --disable-shared
#运行用户 user nobody nobody; #启动进程 worker_processes 2; #全局错误日志及PID文件 error_log logs/error.log notice; pid logs/nginx.pid; #工作模式及连接数上限 events { use epoll; worker_connections 1024; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 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参数表示权值,权值越高被分配到的几率越大 #本机上的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.yejr.com; charset gb2312; #设定本虚拟主机的访问日志 access_log logs/www.yejr.com.access.log main; #如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid #如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好 location ~ ^/(img|js|css)/ { root /data3/Html; expires 24h; } #对 "/" 启用负载均衡 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; } } }
如果没有报错,那么就可以开始运行Nginx了,执行以下命令即可:
备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:
3.) 查看 Nginx 运行状态 输入地址 http://192.168.8.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容:
Active connections: 328 server accepts handled requests 9309 8982 28890 Reading: 1 Writing: 3 Waiting: 324 第一行表示目前活跃的连接数 第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。 第四行看不懂 :(
[root@localhost]#tar zxf mysql-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
[root@localhost]#vi /usr/local/server/mysql/data/my.cnf
[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
/usr/local/server/mysql/bin/mysqld_safe --defaults-file=/usr/local/server/mysql/data/my.cnf &
[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
[root@localhost]#/usr/local/server/memcached/bin/memcached -l 192.168.8.1 -d -p 10000 -u nobody -m 128
[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
[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
[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
[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
extension_dir = "/usr/local/server/php/lib/" extension="eaccelerator.so" eaccelerator.shm_size="32" ;设定eaccelerator的共享内存为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"
ExpiresActive On ExpiresByType text/html "access plus 10 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"
[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
#设定缓存目录为 /var/cache1 和 /var/lib/squid,每次处理缓存大小为128MB,当缓存空间使用达到95%时 #新的内容将取代旧的而不直接添加到目录中,直到空间又下降到90%才停止这一活动 #/var/cache1 最大1024MB,/var/lib/squid 最大 5000MB,都是 16*256 级子目录 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 80 KB ipcache_size 1024 ipcache_low 90 ipcache_high 95 cache_replacement_policy lru memory_replacement_policy lru #设置超时策略 forward_timeout 20 seconds connect_timeout 15 seconds read_timeout 3 minutes request_timeout 1 minutes persistent_request_timeout 15 seconds client_lifetime 15 minutes shutdown_lifetime 5 seconds negative_ttl 10 seconds #限制一个ip最大只能有16个连接 acl OverConnLimit maxconn 16 http_access deny OverConnLimit #限制baidu spider访问 #acl AntiBaidu req_header User-Agent Baiduspider #http_access deny AntiBaidu #常规设置 visible_hostname cache.yejr.com cache_mgr webmaster@yejr.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 因为有gzip acl apache rep_header Server ^Apache broken_vary_encoding allow apache #设置access log,并且令其格式和apache的格式一样,方便awstats分析 emulate_httpd_log on logformat apache %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %
[root@localhost]#/usr/local/server/squid/sbin/squid -z [root@localhost]#/usr/local/server/squid/sbin/squid
#expiresdefault "modification plus 2 weeks" expiresactive on expiresbytype text/html "access plus 10 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"
session.cache_limiter = nocache
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\');
*取得squid运行状态信息: squidclient -p 80 mgr:info *取得squid内存使用情况: squidclient -p 80 mgr:mem *取得squid已经缓存的列表: squidclient -p 80 mgr:objects. use it carefully, it may crash *取得squid的磁盘使用情况: squidclient -p 80 mgr:diskd *强制更新某个url: squidclient -p 80 -m PURGE http://www.yejr.com/static.php *更多的请查看:squidclient -h 或者 squidclient -p 80 mgr:
本文出自 “老叶茶馆” 博客,转载请与作者联系!
相关文章推荐
虚拟主机的专业参数,分别都是什么意思?2022-09-09
中非域名注册规则是怎样的?注册域名有什么用处? 2022-01-10
HostEase新年活动促销 美国/香港主机全场低至五折2021-12-28
HostGator下载完整备份教程分享2021-12-28
Flink中有界数据与无界数据的示例分析2021-12-28