环境:
主机1:centos7(前端分发)
主机2:centos 7(后端处理数据)
主机3:centos 7(后端处理数据)
主机1
步骤:
1.先安装依赖包,防止后面乱七八糟的问题
yum -y install wget pcre openssl* gd gd2 gd-devel gd2-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make libxslt-devel
2.扩大主机1的连接数
ulimit -SHn 65535
3.下载软件 安装环境
添加用户
groupadd www
useradd -g www www
创建软件地址
cd /
mkdir soft
mkdir softinstall
cd soft
安装pcre
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure --prefix=/softinstall/pcre8 && make && make install
安装nginx
wget http://nginx.org/download/nginx-1.12.1.tar.gz
tar zxvf nginx-1.12.1.tar.gz
cd nginx-1.12.1
./configure --user=www --group=www --prefix=/softinstall/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/soft/pcre-8.38/ --with-http_realip_module --with-http_image_filter_module && make && make install
(PS:nginx暂时不支持pcre10)
开放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=9000/tcp --permanent
systemctl restart firewalld.service
配置反向代理
vi /softinstall/nginx/conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream child{
#ip_hash;
server 192.168.2.202:81;
server 192.168.2.203;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm index.php;
proxy_pass http://child;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
配置完成
主机2(192.168.2.202)
步骤:
1.同上安装nginx
2.编译PHP7
cd /soft
wget http://de2.php.net/distributions/php-7.1.7.tar.bz2
tar -xjf php-7.1.7.tar.bz2
cd php-7.1.7
./configure --prefix=/install/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-xmlrpc --with-xsl --with-zlib --with-mcrypt --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-xml --enable-zip
make && make install
配置文件
cp php.ini-production /install/php/etc/php.ini
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp /install/php/etc/php-fpm.conf.default /install/php/etc/php-fpm.conf
cp /install/php/etc/php-fpm.d/www.conf.default /install/php/etc/php-fpm.d/www.conf
3.访问前端分发机器
主机3
同主机2配置
测试负载
client代码
set_time_limit(0);
$num=1000;
$startTime= time();
for($i=0;$i<$num;$i++){
file_get_contents("http://192.168.2.201/");
}
$endTime= time();
echo'访问次数:'.$num.'
耗时:'.($endTime-$startTime).'秒';
?>
主机2代码和主机3代码
error_reporting(0);
$clickNum= intval(file_get_contents("clickNum.txt"));
echo'访问次数:'.strval($clickNum+1);
file_put_contents('clickNum.txt',strval($clickNum+1));
?>
测试1(轮询)结果:
upstream child{
#ip_hash;
server 192.168.2.202:81;
server 192.168.2.203;
}
测试2(ip_hash)结果:
upstream child{
ip_hash;
server 192.168.2.202:81;
server 192.168.2.203;
}
测试2(weight)结果:
upstream child{
#ip_hash;
server 192.168.2.202:81 weight=1;
server 192.168.2.203 weight=3;
}