这篇文章主要介绍“php网站怎么实现负载均衡”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“php网站怎么实现负载均衡”文章能帮助大家解决问题。 php网站实现负载均衡的方法:1、准备好虚拟机;2、通过yum方式安装php;3、加入配置“pid = /usr/local/php-fpm/var/run/php-fpm.pid”;4、通过yum方式安装nginx;5、编写nginx启动脚本,添加配置为“NGINX_SBIN=”/usr/local/nginx/sbin/nginx””。本教程操作环境:linux5.9.8系统、PHP8.1版、DELL G3电脑nginx+php 实现代理与负载均衡 (1台nginx,2台php)实验准备 3台虚拟机
192.168.239.136/192.168.239.140/192.168.239.144
1 安装php 192.168.239.140/192.168.239.144安装之前先做好这些准备工作(安装基础环境)
yum-ygroupinstall"DevelopmentTools""DevelopmentLibraries" yuminstall-yepel-release yuminstall-ylibxml2-devel.x86_64openssl-devel.x86_64bzip2-devel.x86_64libjpeg-turbo-devel.x86_64libjpeg-turbo-static.x86_64libpng-devel.x86_64libpng-static.x86_64freetype-devel.x86_64libstdc++libstdc++-develcompat-libstdc++-33libstdc++-staticgcclibmcrypt-devellrzszgccgcc-c++makemanvimtreeunzipwgetcurllua-devellua-staticGeoIPGeoIP-develpatchlibxml2-devellibxsltlibxslt-develgdgd-develautoconfm4pcre-devel.x86_64pcre-static.x86_64lrzszntplibcurl-devel.x86_64libtool-ltdl-devel.x86_64
下载php源码包到 /usr/local/src cd /usr/local/src
解压 cd 解压免费云主机域名目录 useradd -s /sbin/nologin php-fpm
./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--enable-fpm--with-fpm-user=php-fpm--with-fpm-group=php-fpm--with-mysql=mysqlnd--with-libxml-dir--with-gd--with-jpeg-dir--with-png-dir--with-freetype-dir--with-iconv-dir--with-zlib-dir--with-mcrypt--enable-soap--enable-gd-native-ttf--enable-ftp--enable-mbstring--enable-exif--enable-zend-multibyte--disable-ipv6--with-pear--with-curl make&&makeinstall cpphp.ini-development/usr/local/php-fpm/etc/php.ini拷贝配置文件 cpsapi/fpm/init.d.php-fpm/etc/init.d/php-fpm拷贝启动脚本 mv/usr/local/php-fpm/etc/php-fpm.conf.default/usr/local/php-fpm/etc/php-fpm.conf >/usr/local/php-fpm/etc/php-fpm.conf vim!$加入如下配置 [global] pid=/usr/local/php-fpm/var/run/php-fpm.piderror_log=/usr/local/php-fpm/var/log/php-fpm.log [www]listen=192.168.239.140:9000(IP是php本机IP或者0.0.0.0,不能是127.0.0.1)user=php-fpmgroup=php-fpmlisten.owner=nobodylisten.group=nobodypm=dynamicpm.max_children=50pm.start_servers=20pm.min_spare_servers=5pm.max_spare_servers=35pm.max_requests=500rlimit_files=1024 slowlog=/usr/local/php-fpm/var/slow.logrequest_slowlog_timeout=1 php_admin_value[open_basedir]=/data/www/:/tmp/ /usr/local/php-fpm/sbin/php-fpm-t chmod755/etc/init.d/php-fpm /etc/init.d/php-fpmstartnetstat-lnpt查看是否有监听192.168.239.140:9000 mkdir-p/tmp/tmp vim/tmp/tmp/index.php创建动态测试文件,写入如下 dongtaiqingqiu1
2.第二台安装php (安装编译都不变,配置文件IP更改为 192.168.239.144:9000 最后index.php 中dongtaiqingqiu 1改为dongtaiqingqiu2 )3.安装nginx下载源码包到 /usr/local/src cd /usr/local/src
wget http://nginx.org/download/nginx-1.6.2.tar.gz
解压 cd 解压目录
yuminstall-ypcre-devel ./configure--prefix=/usr/local/nginx--with-pcre make makeinstall
4. 编写nginx启动脚本
vim/etc/init.d/nginx加入如下配置 #!/bin/bash #chkconfig:-3021 #description:httpservice. #SourceFunctionLibrary ./etc/init.d/functions #NginxSettings NGINX_SBIN="/usr/local/nginx/sbin/nginx" NGINX_CONF="/usr/local/nginx/conf/nginx.conf" NGINX_PID="/usr/local/nginx/logs/nginx.pid" RETVAL=0 prog="Nginx" start(){ echo-n$"Starting$prog:" mkdir-p/dev/shm/nginx_temp daemon$NGINX_SBIN-c$NGINX_CONF RETVAL=$? echo return$RETVAL } stop(){ echo-n$"Stopping$prog:" killproc-p$NGINX_PID$NGINX_SBIN-TERM rm-rf/dev/shm/nginx_temp RETVAL=$? echo return$RETVAL } reload(){ echo-n$"Reloading$prog:" killproc-p$NGINX_PID$NGINX_SBIN-HUP RETVAL=$? echo return$RETVAL } restart(){ stop start } configtest(){ $NGINX_SBIN-c$NGINX_CONF-t return0 } case"$1"in start) start ;; stop) stop ;; reload) reload ;; restart) restart ;; configtest) configtest ;; *) echo$"Usage:$0{start|stop|reload|restart|configtest}" RETVAL=1 esac exit$RETVAL
保存后,执行
chmoda+x/etc/init.d/nginx或者chmod755!$修改权限 servicenginxstart启动nginx
5. 配置反向代理
>/usr/local/nginx/conf/nginx.conf vim/usr/local/nginx/conf/nginx.conf加入如下配置 usernobodynobody; worker_processes2; error_log/usr/local/nginx/logs/nginx_error.logcrit; pid/usr/local/nginx/logs/nginx.pid; worker_rlimit_nofile51200; events { useepoll; worker_connections6000; } http { includemime.types; default_typeapplication/octet-stream; server_names_hash_bucket_size3526; server_names_hash_max_size4096; log_formatcombined_realip'$remote_addr$http_x_forwarded_for[$time_local]' '$host"$request_uri"$status' '"$http_referer""$http_user_agent"'; sendfileon; tcp_nopushon; keepalive_timeout30; client_header_timeout3m; client_body_timeout3m; send_timeout3m; connection_pool_size256; client_header_buffer_size1k; large_client_header_buffers84k; request_pool_size4k; output_buffers432k; postpone_output1460; client_max_body_size10m; client_body_buffer_size256k; client_body_temp_path/usr/local/nginx/client_body_temp; proxy_temp_path/usr/local/nginx/proxy_temp; fastcgi_temp_path/usr/local/nginx/fastcgi_temp; fastcgi_intercept_errorson; tcp_nodelayon; gzipon; gzip_min_length1k; gzip_buffers48k; gzip_comp_level5; gzip_http_version1.1; gzip_typestext/plainapplication/x-javascripttext/csstext/htmapplication/xml; includevhosts/*.conf; }
配置代理
mkdir/usr/local/nginx/conf/vhosts vi/usr/local/nginx/conf/vhosts/php.conf加入如下配置 upstreammysvr{ #weigth参数表示权值,权值越高被分配到的几率越大 server192.168.239.144:9000weight=2; server192.168.239.140:9000weight=1; } server { listen80; server_namewww.q.com; indexindex.htmlindex.htmindex.php; root/usr/local/nginx/html; location~.php${ includefastcgi_params; fastcgi_passmysvr; fastcgi_indexindex.php; fastcgi_paramSCRIPT_FILENAME/tmp/tmp$fastcgi_script_name; } } /usr/local/nginx/sbin/nginx-t检测配置文件 /usr/local/nginx/sbin/nginx-sreload(直接重新加载配置,不需要重启) vim/usr/local/nginx/html/1.html创建静态测试文件,写入 jingtaifangwen
设置本地解析修改windons hosts(地址 c:windowssystem32driversetchosts)记事本打开,最下面添加一行配置
192.168.239.136www.q.com
测试:最好用谷歌浏览器 (如下显示 代理成功)访问 www.q.com 显示的是nginx 默认页访问 www.q.com/.html 显示的jingtaifangwen访问 www.q.com/index.php 多刷新几次 会显示 dongtaifangwen1 一次 dongtaifangwen2 两次关于“php网站怎么实现负载均衡”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注百云主机行业资讯频道,小编每天都会为大家更新不同的知识点。
这篇“vue怎么将变量赋值”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue怎么将变量赋值”文章吧。1.首先,新建一个common.vue文件…
免责声明:本站发布的图片视频文字,以转载和分享为主,文章观点不代表本站立场,本站不承担相关法律责任;如果涉及侵权请联系邮箱:360163164@qq.com举报,并提供相关证据,经查实将立刻删除涉嫌侵权内容。