ubuntu上Apache2实现负载均衡

实验环境

虚拟机三台

tomcat1 192.168.103.192 tomcat+mysql

tomcat2 192.168.103.195 tomcat

apache2 192.168.103.217 apache2

tomcat目录放在/data/software


一、首先在tomcat1上安装mysql和redis、tomcat

mysql的安装

1.安装mysql相关依赖包

apt-get -y install gcc libncurses5-dev zlib1g-dev libssl-dev bison build-essential


2.MySQL5.5放弃了./configure预编译方式,改用cmake了,所以我们先要安装cmake。

wget http://www.cmake.org/files/v2.8/cmake-2.8.7.tar.gz

tar -zvxf cmake-2.8.7.tar.gz

cd cmake-2.8.7

./configure

make && make install


3.下载Mysql源码包,加压缩后编译安装mysql。

mkdir /var/lib/mysql /var/log/mysql  创建mysql 目录

wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.30.tar.gz

tar -zvxf mysql-5.5.30.tar.gz

cd mysql-5.5.30

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/lib/mysql -DDEFAULT_CHARSET=utf8  -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DWITH_SSL=system -DWITH_DEBUG=0

make && make install

# 备注:对于csv,myisam,myisammrg,heap在cmake中是不需要明确指定存储引擎的名称,因为它们是强制性安装。


4.创建用户组和用户,创建相关目录并赋权。

#创建mysql组和mysql用户

groupadd mysql;

useradd -r -g mysql mysql 或 useradd -s /sbin/nologin mysql

chmod +w /usr/local/mysql

chown -R mysql:mysql /usr/local/mysql /var/lib/mysql /var/log/mysql


5.初始化数据库,并给数据库赋权

/usr/local/mysql/scripts/mysql_install_db --user=mysql --pid-file=/var/lib/mysql/mysql.pid --datadir=/var/lib/mysql/ --basedir=/usr/local/mysql

赋权:

chown -R mysql:mysql /var/lib/mysql


6.修改配置文件。

将解压后的tar包下的support-files/my-medium.cnf复制/etc并替换为/my.cnf. (根据需求复制相应配置)

cp support-files/my-medium.cnf /etc/my.cnf

#在[client]下修改socket值为/var/lib/mysql/mysql.sock,

修改mysqld下的socket值为/var/lib/mysql/mysql.sock,并指定mysql目录、mysql数据目录、mysql错误日志目录,配置如下,并设定数据库编码为UTF-8修改后保存退出

[client]

socket          = /var/lib/mysql/mysql.sock

# The MySQL server

[mysqld]

port            = 3306

socket          = /var/lib/mysql/mysql.sock

basedir = /usr/local/mysql

datadir = /var/lib/mysql/

log-error = /var/log/mysql/mysql-error.log

pid-file = /var/lib/mysql/mysql.pid

character-set-server = utf8


7.设置mysql开机启动。

启动mysql

/usr/local/mysql/bin/mysqld_safe &

ps -A|grep mysql


设置mysql开机启动。

cp support-files/mysql.server /etc/init.d/mysql

chmod +x /etc/init.d/mysql

chkconfig --add mysql

chkconfig mysql on

测试mysql是否能启动、暂停、重启                        

service mysql restart (start|stop|restart)

或/etc/init.d/mysql restart (start|stop|restart)

将部分mysql命令加入到/usr/bin中:

ln /usr/local/mysql/bin/mysql /usr/bin/

ln /usr/local/mysql/bin/mysqladmin /usr/bin/

ln /usr/local/mysql/bin/mysqldump /usr/bin/


安装成功后,设置mysql root密码

mysql>set password =password('123456');

mysql>GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword' WITH GRANT OPTION; 可以从任何主机连接到mysql

mysql>flush privileges;

二、安装tomcat

  1. 首先配置java环境

#vi ~/.bashrc       在末尾添加参数

export JAVA_HOME=/data/software/jdk1.7.0_79

export PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

#java -version

java version "1.7.0_79"

Java(TM) SE Runtime Environment (build 1.7.0_79-b15)

Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

2.解压配置tomcat

#tar zxvf apache-tomcat-7.0.62.tar.gz

#cd apache-tomcat-7.0.62/webapps/

然后上传登陆系统login,然后数据库指定到本地数据库地址(这里根据自己需求)

进入到tomcat的bin目录下启动服务

spacer.gifwKiom1VmiHCSn6YoAAUFT5BpfKg757.jpg

wKioL1Vmiv3hsP8mAAC5viD1Yks685.jpg

tomcat2 上只安装tomcat和以上步骤一样,login配置要指向192.168.103.192这个数据库

wKioL1Vmi4qj4elUAADZ4tEIoIc555.jpg三、安装Apache2

1 安装apache2 

apt-get install apache2  

service apache2 stop  

2 加载代理模块

a2enmod proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http lbmethod_byrequests

3 修改配置 sudo vi /etc/apache2/mods-enabled/proxy.conf

 ProxyRequests Off  

<Proxy *>  

    Order deny,allow  

    Deny from all  

    #Allow from .your_domain.com  

</Proxy> 

4 修改配置 vim /etc/apache2/sites-available/000-default.conf

负载均衡配置

    ProxyRequests Off

     Proxypass / balancer://proxy/ 

     <Proxy balancer://proxy>

         Order Deny,Allow

         Allow from all

         BalancerMember http://192.168.103.192:8080

         BalancerMember http://192.168.103.195:8080

     </Proxy>

</VirtualHost>

service apache2 restart

wKioL1VmjH2RpJHWAANrpyhxkiU158.jpg

然后访问地址192.168.103.217/login 第一次访问会跳转到192.168.103.192第二次会转到192.168.103.195,实验完成。

你可能感兴趣的:(apache,tomcat,mysql,ubuntu)