简单的Haproxy实验

环境准备:
A:Haproxy:172.18.33.11
B:web资源服务器:172.18.33.22
C:数据库服务器:172.18.33.33
# 当用户访问A(Haproxy)机器时,A机器反向代理到后端

这里写图片描述

B机器搭建web服务存放资源:
[ root@localhost ~]#yum -y install httpd php php-mysql
#安装apache服务器及php环境

[ root@localhost ~]# wget https://cn.wordpress.org/wordpress-4.8.1-zh_CN.tar.gz
#下载wordpress博客源码

[ root@localhost ~]#  mkdir /web
#创建博客文件存放穆勒

[ root@localhost ~]#  tar xf wordpress-4.8.1-zh_CN.tar.gz -C /web 
#把wordpress源码解压到刚刚创建的目录

[ root@localhost ~]#  chmod 777 /web/*
#这里为了方便设置的最大权限
C机器数据库服务器:
[ root@localhost ~]# yum -y install mariadb-server
#安装mariadb数据库

[ root@localhost ~]# systemctl start mariadb
#启动数据库服务

[ root@localhost /usr/bin]#./mysql_secure_installation 
#初始化数据库并设置数据库账号密码

 /usr/local/mysql/bin/mysql_secure_installation: line 379: find_mysql_client: command not found

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):     #回车
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y  #是否设置root密码
New password:   
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n]    #删除匿名账号
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n]   #是否禁止root账号远程登录,生产环境中一定要禁止
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y    #是否清除测试数据库
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  #重载
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

[ root@localhost ~]# mysql -uroot -p
#登录数据库
[ root@localhost /usr/bin]#mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> create database wordpress; 
#创建wordpress数据库

MariaDB [(none)]> grant all  on wordpress.* to 'yyc'@'172.18.%.%' identified by '123'; 
#创建数据库账号并且授权给wordpress数据库
编辑A机器的web服务的配置文件:
[ root@localhost ~]# vim /etc/httpd/conf/httpd.cof
DocumentRoot "/web"
#搜索到DocumentRoot,把路径改成刚刚创建的web目录

[ root@localhost ~]# service httpd restart 
#重启web服务

[ root@localhost ~]# mv wp-config-sample.php wp-config.php
#重命名wordpress配置文件名

[ root@localhost ~]# vim wp-config.php
define('DB_NAME', 'wordpress'); 
#数据库名

/** MySQL数据库用户名 */
define('DB_USER', 'yyc');
#数据库账号

/** MySQL数据库密码 */
define('DB_PASSWORD', '123');
#数据库密码

/** MySQL主机 */
define('DB_HOST', '172.18.33.44');
#数据库主机地址

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
A机器搭建Haproxy服务:
[ root@localhost ~]#yum -y install haproxy
#安装haproxy服务

[ root@localhost ~]#vim /etc/haproxy/haproxy.cfg
listen http
        bind 172.18.33.11:80
        #指定绑定地址和端口
        default_backend websrv1
        #默认代理到哪里
backend websrv1
        balance roundrobin
        #轮询调度,这里只准备了一台主机
        server web1 172.18.33.33:80 check


[ root@localhost ~]# systemctl restart haproxy

访问:172.18.33.11
这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值