主机名 | IP | 服务 |
---|---|---|
server1 | 172.25.79.1 | haproxy |
server2 | 172.25.79.2 | apache |
server3 | 172.25.79.3 | apache |
foundation79 | 172.25.79.250 | 用于测试 |
1.解压haproxy压缩包
[root@server1 ~]# tar zxf haproxy-1.7.3.tar.gz
[root@server1 ~]# cd haproxy-1.7.3
##命令find -name *.spec有这个文件表示可以用源码软件包安装
[root@server1 haproxy-1.7.3]# find -name *.spec
./examples/haproxy.spec
2.安装源码编译软件,源码编译haproxy软件
[root@server1 ~]# yum install rpm-build -y
##安装需要的安装包,否则会显示编译失败
[root@server1 ~]# yum install pcre-devel -y
[root@server1 ~]# yum install gcc -y
[root@server1 ~]# rpmbuild -tb haproxy-1.7.3.tar.gz
[root@server1 ~]# cd /root/rpmbuild/RPMS/x86_64/
[root@server1 x86_64]# ls
haproxy-1.7.3-1.x86_64.rpm
[root@server1 x86_64]# rpm -ivh haproxy-1.7.3-1.x86_64.rpm
[root@server1 ~]# groupadd -g 200 haproxy
[root@server1 ~]# useradd -u 200 -g 200 -M haproxy ##-M参数表示不自动建立用户的登陆目录
[root@server1 ~]# id haproxy
uid=200(haproxy) gid=200(haproxy) groups=200(haproxy)
5.找到haproxy的配置文件,并复制到/etc/haproxy目录下
[root@server1 ~]# cd haproxy-1.7.3/examples/
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
6.编辑配置文件
[root@server1 examples]# cd /etc/haproxy/
[root@server1 haproxy]# vim haproxy.cfg
global # 全局定义
maxconn 10000 # 最大连接数
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0 # 夲机日志
uid 200 # haproxy用户的uid
gid 200 # haproxy用户的gid
chroot /var/empty
daemon
defaults # 默认条件
mode http #默认使用 http 的 7 层模式 tcp: 4 层
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
stats uri /admin/stats
option prefer-last-server
retries 2
option redispatch
timeout connect 5s
timeout server 5s
# The public 'www' address in the DMZ
frontend public
bind *:80 name clear ##监听所有端口
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
#use_backend static if { hdr_beg(host) -i img }
#use_backend static if { path_beg /img /css }
default_backend static # 默认使用static后端服务器集群
# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin # 负载均衡算法rr
server statsrv1 172.25.79.2:80 check inter 1000 # 后端服务器server2
server statsrv2 172.25.79.3:80 check inter 1000 # 后端服务器server3
[root@server2 ~]# yum install httpd -y
[root@server2 ~]# /etc/init.d/httpd start
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# vim index.html
server2
[root@server3 ~]# yum install httpd -y
[root@server3 ~]# /etc/init.d/httpd start
[root@server3 ~]# cd /var/www/html
[root@server3 html]# vim index.html
server3
[root@server1 ~]# /etc/init.d/haproxy start
[root@server1 haproxy]# curl 172.25.79.1
server2
[root@server1 haproxy]# curl 172.25.79.1
server3
[root@server1 haproxy]# curl 172.25.79.1
server2
[root@server1 haproxy]# curl 172.25.79.1
server3
[root@server1 haproxy]# /etc/init.d/haproxy restart
[root@server1 haproxy]# vim /etc/rsyslog.conf
##打开13,14行的注释
13 $ModLoad imudp
14 $UDPServerRun 514
##添加日志存储位置
63 local0.* /var/log/haproxy.log
##重启日志服务
[root@server1 haproxy]# /etc/init.d/rsyslog restart
[root@server1 examples]# cd /etc/haproxy/
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy reload
[root@server1 haproxy]# yum install httpd -y
[root@server1 haproxy]# vim /etc/httpd/conf/httpd.conf
136 Listen 8080
[root@server1 haproxy]# cd /var/www/html/
[root@server1 html]# vim index.html
您已被拉黑...
[root@server1 html]# /etc/init.d/httpd start
[root@server3 html]# yum install php -y
[root@server3 html]# pwd
/var/www/html
[root@server3 html]# vim index.php
<?php
phpinfo()
?>
[root@server3 html]# /etc/init.d/httpd restart
[root@server1 haproxy]# vim haproxy.cfg
[root@server1 haproxy]# /etc/init.d/haproxy reload
[root@server1 haproxy]# vim haproxy.cfg
##写参数
50 acl write method POST
51 acl write metnod PUT
#读参数
50 acl read method GET
51 acl read metnod HEAD
51 use_backend static if write ##如果写,则默认访问static(server2),即写入到server2中
52
53 default_backend dynamic
[root@server2 html]# cd /var/www/html/upload/ ##此处upload可以新建
[root@server2 upload]# ls
index.php upload_file.php ##两个文件的内容在下面可以看到
[root@server2 upload]# chmod 644 *
[root@server2 upload]# mv * ..
[root@server2 upload]# cd ..
[root@server2 html]# chmod 777 upload
[root@server2 html]# ls
index.html index.php upload upload_file.php
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
[root@server2 html]# vim upload_file.php
5 && ($_FILES["file"]["size"] < 2000000)) ##修改为2000000
[root@server2 html]# scp -rp upload upload_file.php index.php root@172.25.19.3:/var/www/html/
[root@server2 html]# yum install php -y
[root@server2 html]# /etc/init.d/httpd restart