简单的Haproxy实验

来源:互联网 发布:已断开此网络登陆超时 编辑:程序博客网 时间:2024/05/19 10:39

环境准备:
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 foundNOTE: 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 currentpassword for the root user.  If you've just installed MariaDB, andyou 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 MariaDBroot 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 anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n]    #删除匿名账号 ... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures 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 canaccess.  This is also intended only for testing, and should be removedbefore 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB![ root@localhost ~]# mysql -uroot -p#登录数据库[ root@localhost /usr/bin]#mysql -uroot -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 25Server version: 5.5.56-MariaDB MariaDB ServerCopyright (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.cofDocumentRoot "/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.phpdefine('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.cfglisten 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
这里写图片描述