CentOS6.5+JDK+Tomcat+Mysql+Nginx负载均衡+Redis环境部署

前端之家收集整理的这篇文章主要介绍了CentOS6.5+JDK+Tomcat+Mysql+Nginx负载均衡+Redis环境部署前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1. CentOS6.5直接申请阿里云ECS实例,不再累述。@H_404_1@

Windows安装XShell作为SSH客户端,安装Xftp5作为文件传输的客户端。@H_404_1@

2. JDK安装:@H_404_1@

(1)下载JDK tar包,地址:@H_404_1@

http://www.Oracle.com/technetwork/Java/javase/downloads/jdk8-downloads-2133151.html@H_404_1@

选择Linuxx64:jdk-8u101-linux-x64.tar.gz@H_404_1@

通过Xftp5将tar包上传到CentOS6.5的root目录下@H_404_1@

解压tar包:#tar -zxvf jdk-8u101-linux-x64.tar.gz@H_404_1@

(2)JDK目录移植到/usr/java:@H_404_1@

#mkdir /usr/java@H_404_1@

#mv jdk1.8.0_92 /usr/java@H_404_1@

(3)设置环境变量:@H_404_1@

#vim /etc/profile@H_404_1@

按i键进入编辑状态,文档末尾追加如下内容:@H_404_1@

JAVA_HOME=/usr/java/jdk1.8.0_92@H_404_1@

CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$CATALINE_HOME/lib
PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/jre/bin
export PATH CLASSPATH JAVA_HOME@H_404_1@

按esc键退出编辑状态,输入:wq,保存退出@H_404_1@

让刚刚设置的环境变量立即生效:#source /etc/profile@H_404_1@

(4)验证:@H_404_1@

#java -version@H_404_1@

输出:@H_404_1@

Javaversion "1.8.0_92"
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14,mixed mode)
@H_404_1@

3. Tomcat安装:@H_404_1@

(1)下载Tomcat tar包,地址:@H_404_1@

http://tomcat.apache.org/download-70.cgi@H_404_1@

选择Core,tar.gz@H_404_1@

@H_404_1@

通过Xftp5将tar包上传到CentOS6.5的root目录下@H_404_1@

解压tar包:#tar -zxvf apache-tomcat-7.0.72.tar.gz@H_404_1@

@H_404_1@

(2)Tomcat目录移植到/usr/local:@H_404_1@

#mv apache-tomcat-7.0.72 /usr/local/tomcat@H_404_1@

(3)启动Tomcat@H_404_1@

#cd /usr/local/tomcat/bin@H_404_1@

#./startup.sh@H_404_1@

(4)验证:@H_404_1@

Windows浏览器访问http://****(CentOS外网地址):8080,出现Apache Tomcat的WEB界面(变态黄猫)@H_404_1@

4.MySQL安装:@H_404_1@

(1)安装MysqL:@H_404_1@

先查看当前Linux是否已安装MysqL:#rpm -qa | grep MysqL@H_404_1@

删除原先的MysqL:#rpm -e MysqL //普通删除模式@H_404_1@

#rpm -e --nodeps MysqL // 强力删除模式,上面命令删不干净就用这个@H_404_1@

安装:#yum install -y MysqL-server MysqL MysqL-deve@H_404_1@

(2)验证:@H_404_1@

#rpm -qi MysqL-server@H_404_1@

(3)启动MysqL:@H_404_1@

#service MysqLd start@H_404_1@

(4)设置开机自启动:@H_404_1@

先查看是否为开机自启动:#chkconfig --list | grep MysqLd@H_404_1@

MysqLd 0:off1:off2:off3:off4:off5:off6:off(可见非开机自启动)@H_404_1@

设置为开机自启动:#chkconfig MysqLd on@H_404_1@

重新查看:#chkconfig --list | grep MysqLd@H_404_1@

MysqLd 0:off1:off2:on3:on4:on5:on6:off(可见已设置为开机自启动)@H_404_1@

(5)为MysqL的root账号设置密码:@H_404_1@

#MysqLadmin -u root password 'root' // 设置密码为root@H_404_1@

(6)登录MysqL:@H_404_1@

#/MysqL -u root -p@H_404_1@

输入root密码登录@H_404_1@

(7)创建数据库、创建表、插入数据:@H_404_1@

创建数据库:@H_404_1@

show databases; // 查看现有数据库,注意MysqL中每条命令最后都要加分号@H_404_1@

create database wuyang; // 创建数据库wuyang@H_404_1@

show databases;@H_404_1@

use wuyang; // 切换操作wuyang为当前数据库@H_404_1@

创建表:@H_404_1@

show tables; // 查看现有表@H_404_1@

create table users (userId varchar(8),userName varchar(8)); // 创建表@H_404_1@

show tables;@H_404_1@

插入数据:@H_404_1@

insert into users values ('001','zhangsan');@H_404_1@

insert into users values ('002','lisi');@H_404_1@

select * from users; // 查询刚刚插入的数据@H_404_1@

desc users; // 查看表结构@H_404_1@

exit; // 登出@H_404_1@

(8)Tomcat中创建Servlet访问MysqL:@H_404_1@

建议先在Windows中创建,调通后再将编译后的Servlet.class和MysqL驱动器拷贝到CentOS@H_404_1@

创建Servlet的java文件:@H_404_1@

Eclipse创建Dynamic Web Project工程,Dynamic web module version选择2.5(选择3.0的话将不会自动生成web.xml文件)@H_404_1@

创建Serverlet.java继承自HttpServlet@H_404_1@

添加代码:@H_404_1@

import java.sql.*;@H_404_1@

doGet方法添加代码:@H_404_1@

try
{
Class.forName("com.MysqL.jdbc.Driver");
Connection ct = DriverManager.getConnection("jdbc:MysqL://****(CentOS外网地址):3306/wuyang?useEncode=true&characterEncoding=utf-8&user=root&password=root"); // MysqL默认端口3306
// Connection ct = DriverManager.getConnection("jdbc:MysqL://127.0.0.1:3306/wuyang?useEncode=true&characterEncoding=utf-8&user=root&password=root"); // 后面把Windows的Servlet.class文件拷贝到CentOS后,把上面的这句代码中的地址改成此句中的127.0.0.1,或者相应的MysqL所在内网IP
Statement sm = ct.createStatement();
ResultSet rs = sm.executeQuery("select * from users");

while(rs.next())
{
System.out.println("username=" + rs.getString(2));
response.getWriter().append("username=" + rs.getString(2));
}
}
catch(Exception e)
{
e.printStackTrace();
response.getWriter().append(e.toString());
}@H_404_1@

Ctrl+s保存后自动生成class文件@H_404_1@

(9)下载MysqL连接驱动器,地址:@H_404_1@

http://mvnrepository.com/artifact/MysqL/MysqL-connector-java@H_404_1@

下载MysqL-connector-java-5.1.40-bin.jar文件(jar文件在Windows和CentOS中是通用的)@H_404_1@

Windows放到tomcat/lib目录,CentOS同样放到tomcat/lib中@H_404_1@

(10)重启Tomcat(过程略):@H_404_1@

@H_404_1@

(11)验证:@H_404_1@

Windows浏览器访问http://****(CentOS外网地址):8080/****(对应webapps目录)/Servlet,出现:@H_404_1@

username=zhangsanusername=lisi@H_404_1@

5. Nginx安装:@H_404_1@

(1)安装Nginx:@H_404_1@

#yum -y install gcc gcc-c++ autoconf automake make // 安装gcc编译器及相关工具@H_404_1@

#yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel // 安装相关依赖的模块
@H_404_1@

#cd /usr/local/src@H_404_1@

#wget http://www.Nginx.org/download/Nginx-1.2.8.tar.gz // 下载Nginx@H_404_1@

#tar -zxvf ngingx-1.2.8.tar.gz@H_404_1@

#cd Nginx-1.2.8@H_404_1@

#./configure --prefix=/usr/local/Nginx // 通过编译源码的方式进行安装@H_404_1@

//------配置完毕后如下提示-----------//
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library

Nginx path prefix: "/usr/local/Nginx"
Nginx binary file: "/usr/local/Nginx/sbin/Nginx"
Nginx configuration prefix: "/usr/local/Nginx/conf"
Nginx configuration file: "/usr/local/Nginx/conf/Nginx.conf"
Nginx pid file: "/usr/local/Nginx/logs/Nginx.pid"
Nginx error log file: "/usr/local/Nginx/logs/error.log"
Nginx http access log file: "/usr/local/Nginx/logs/access.log"
Nginx http client request body temporary files: "client_body_temp"
Nginx http proxy temporary files: "proxy_temp"
Nginx http fastcgi temporary files: "fastcgi_temp"
Nginx http uwsgi temporary files: "uwsgi_temp"
Nginx http scgi temporary files: "scgi_temp"
//----------------------------------------//@H_404_1@

#make@H_404_1@

#make install@H_404_1@

(2)启动Nginx:@H_404_1@

#/usr/local/Nginx/sbin/Nginx -t@H_404_1@

#/usr/local/Nginx/sbin/Nginx@H_404_1@

检查Nginx进程,确认是否启动成功:#ps -ef | grep Nginx@H_404_1@

(3)验证:@H_404_1@

Windows浏览器访问http://****(CentOS外网地址):80,出现Welcome to Nginx!信息@H_404_1@

(4)配置Nginx负载均衡:@H_404_1@

在阿里云上申请另一个CentOS6.5的ECS实例(在Tomcat中设置不同的Servlet,或MysqL中插入不同的数据,以便后面进行验证)@H_404_1@

设置Nginx配置文件:@H_404_1@

#vim /usr/local/Nginx/conf/Nginx.conf@H_404_1@

文档末尾追加:@H_404_1@

upstream localhosts { // 默认轮询算法,权重算法下述每个server添加weight、max_fails、fail_timeout,不再累述@H_404_1@

server 127.0.0.1:8080; // 反向代理到本机Tomcat@H_404_1@

server ****(第二个CentOS内网地址):8080;@H_404_1@

}@H_404_1@

Nginx.conf文档中部找到server总结点,用#注释掉其中的整个location节点,重新添加location节点:@H_404_1@

location / {@H_404_1@

proxy_pass http://localhosts;@H_404_1@

proxy_set_header X-Real-IP $remote_addr;@H_404_1@

proxy_redirect default;@H_404_1@

}@H_404_1@

(5)重启Nginx:@H_404_1@

#/usr/local/Nginx/sbin/Nginx -s reload@H_404_1@

如果只是执行#/usr/local/Nginx/sbin/Nginx,会遇到错误Nginx: [emerg] bind() to 0.0.0.0:80 Failed (98: Address already in use)时@H_404_1@

#fuser -k 80/tcp // 关闭被占用的端口@H_404_1@

#/usr/local/Nginx/sbin/Nginx//重新启动Nginx@H_404_1@

直接运行#/usr/local/Nginx/sbin/Nginx -s reload,就不会有上述问题了@H_404_1@

(6)验证Nginx负载均衡:@H_404_1@

Windows浏览器连续刷新访问http://****(CentOS外网地址):80/****(对应webapps目录)/Servlet,依次交替(Nginx负载均衡的轮询算法)出现两个ECS实例的Servlet内容@H_404_1@

关闭其中一个CentOS中的Tomcat,再刷新浏览器,发现只会访问那个还开着的CentOS的Tomcat,说明Nginx在实现负载均衡的同时,保证了高可用。@H_404_1@

6.Redis安装:@H_404_1@

(1)先安装tcl,否则后面无法make test:#yum install -y tcl@H_404_1@

(2)安装Redis:@H_404_1@

#cd ~@H_404_1@

#wget http://download.redis.io/redis-stable.tar.gz@H_404_1@

#tar -zxvf redis-stable.tar.gz@H_404_1@

#cd redis-stable@H_404_1@

#make@H_404_1@

#make test@H_404_1@

(3)配置Redis:@H_404_1@

@H_404_1@

在make成功以后,会在src目录下多出一些可执行文件:redis-server,redis-cli等等。@H_404_1@

方便期间用cp命令复制到usr目录下运行。@H_404_1@

cp redis-server /usr/local/bin/@H_404_1@

cp redis-cli /usr/local/bin/@H_404_1@

然后新建目录,存放配置文件@H_404_1@

mkdir /etc/redis@H_404_1@

mkdir /var/redis@H_404_1@

mkdir /var/redis/log@H_404_1@

mkdir /var/redis/run@H_404_1@

mkdir/var/redis/6379@H_404_1@

在redis解压根目录中找到配置文件模板,复制到如下位置。@H_404_1@

cp redis.conf /etc/redis/redis.conf@H_404_1@

通过vim命令修改@H_404_1@

daemonize yes@H_404_1@

pidfile /var/redis/run/redis_6379.pid@H_404_1@

logfile /var/redis/log/redis_6379.log(默认是空的,这个不太好找)@H_404_1@

dir /var/redis/6379(默认是空的,这个也不太好找)@H_404_1@

(4)启动redis:@H_404_1@

#redis-server /etc/redis/redis.conf@H_404_1@

(5)验证:@H_404_1@

#redis-cli // 启动redis客户端@H_404_1@

>set foo bar@H_404_1@

OK@H_404_1@

>get foo@H_404_1@

"bar"@H_404_1@

(6)分布式访问Redis设置:@H_404_1@

第二个CentOS上同样也要部署Redis环境,主要是要部署redis-cli(Redis客户端)@H_404_1@

直接用第二个CentOS通过#redis-cli -h ****(第一个CentOS内网地址)-p 6379会出现错误:@H_404_1@

Could not connect to Redis at ****:6379: Connection refused@H_404_1@

因为Redis默认只能localhost登录,需要手动开启远程登录@H_404_1@

在第一个CentOS修改redis-server的配置文件并重启:@H_404_1@

#vim /etc/redis/redis.conf@H_404_1@

找到bind localhost并使用#注释掉@H_404_1@

这是再用第二个CentOS就能够通过#redis-cli -h ****(第一个CentOS内网地址)-p 6379访问第一个CentOS上的Redis了@H_404_1@

(7)Tomcat中Servlet访问Redis:@H_404_1@

先下载Redis客户端驱动jedis-2.1.0.jar,地址:@H_404_1@

http://files.cnblogs.com/liuling/jedis-2.1.0.jar.zip@H_404_1@

将解压后的jedis-2.1.0.jar放到目录:对应webapps下lib文件夹或对应webapps下WEB-INFO下lib文件夹中@H_404_1@

然后在java文件添加代码:@H_404_1@

import redis.clients.jedis.Jedis;@H_404_1@

// 连接本地的 Redis 服务 Jedis jedis = new Jedis("127.0.0.1",6379); // 经测试只能访问本机Redis或局域网所在IP,外网无法访问!! response.getWriter().append("Connection to server sucessfully"); // 查看服务是否运行 response.getWriter().append("Server is running: " + jedis.ping()); //获取前面设置的Key:foo response.getWriter().append(jedis.get("foo")); // 设置Key jedis.set("me","Redis tutorial"); // 获取刚刚设置的Key response.getWriter().append(jedis.get("me"));@H_404_1@ 原文链接:/centos/380328.html

猜你在找的CentOS相关文章