Home >Backend Development >PHP Tutorial >Nginx+Tomcat+Memcached load balancing cluster service construction

Nginx+Tomcat+Memcached load balancing cluster service construction

WBOY
WBOYOriginal
2016-07-29 09:15:541647browse
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/48289765

Operating system: CentOS6.5

This document mainly explains how to build an Nginx+Tomcat+Memcached load balancing cluster server under CentOS6.5. Nginx is responsible for load balancing, Tomcat is responsible for actual services, and Memcached is responsible for synchronizing Tomcat’s Session. The purpose of session sharing.

1. Install Nginx

Nginx official website: http://nginx.org/

Download the latest stable version. Before installing Nginx, you need to install the gcc, openssl, pcre and zlib software libraries.

1.1 Install gcc, gcc-c++

Installation command:

#sudo yum install gcc
# sudo yum install gcc-c++

1.2 Install openssl

openssl official website: http://www.openssl.org/

Installation version: openssl-1.0.1i.tar .gz

Installation command:

#tar -zxvf openssl-1.0.1i.tar.gz
#cd openssl-1.0.1i
#sudo ./config --prefix=/usr/local/openssl-1.0.1i    #prefix指定安装目录
#sudo make
#sudo make install

[Note]: The config command is used here instead of the usual configure command

After the installation is completed, go to /usr/local/ to check whether the installation is successful. If an installation error occurs, you need to recompress and reinstall.

1.3 Install pcre

pcre official website: http://www.pcre.org/

Installation version: pcre-8.35.tar.gz

Installation command:

#tar -zxvf pcre-8.35.tar.gz
#cd pcre-8.35
#sudo ./configure --prefix=/usr/local/pcre-8.35    #prefix指定安装目录
#sudo make
#sudo make install

After the installation is completed, go to /usr/local/ Check below to see if the installation is successful. If an installation error occurs, you need to recompress and reinstall.

【Note】: If the c++ compiler is not installed, the installation of this software will report an error!

1.4 Install zlib

zlib official website: http://www.zlib.net/

Installation version: zlib-1.2.8.tar.gz

Installation command:

#tar -zxvf zlib-1.2.8.tar.gz
#cd zlib-1.2.8
#sudo ./configure --prefix=/usr/local/zlib-1.2.8    #prefix指定安装目录
#sudo make
#sudo make install

After the installation is completed, go to /usr/ Check whether the installation is successful under local/. If an installation error occurs, you need to recompress and reinstall.

1.5 Install Nginx

Installation version: nginx-1.6.1.tar.gz

Installation command:

#tar -zxvf nginx-1.6.1.tar.gz
#cd nginx-1.6.1
#sudo ./configure
--prefix=/usr/local/nginx-1.6.1                #prefix指定安装目录
--with-openssl=/home/zht/src/openssl-1.0.1i    #指的是openssl源码路径
--with-pcre=/home/zht/src/pcre-8.3.5          #指的是pcre的源码路径
--with-zlib=/home/zht/src/zlib-1.2.8           #指的是zlib 的源码路径
--with-http_ssl_module
#sudo make
#make install

After the installation is completed, go to /usr/local/ to check whether the installation is successful. If an installation error occurs, you need to recompress and reinstall.

1.5.1 Configure Nginx

Configuration file directory:/usr/local/nginx-1.6.1/conf/nginx.conf

# cd /usr/local/nginx-1.6.1/conf
# sudo vi nginx.conf

[The modified configuration file is as follows]:

#创建进程的用户和用户组

user      zht zht;

#服务进程数量,一般等于CPU数量

worker_processes 1;


#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]

error_log logs/error.log error;

#error_log logs/error.log  notice;

#error_log logs/error.log  info;

 

#记录进程ID的文件

#pid       logs/nginx.pid;

 

 

events {

    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能.Linux建议使用epoll,FreeBSD建议使用kqueue.

    useepoll;

    #一个worker_processe允许的最近并发连接数量

   worker_connections  1024;

}

 

 

http {

   include       mime.types;

   default_type application/octet-stream;

 

   #log_format  main  '$remote_addr - $remote_user [$time_local]"$request" '

    #                  '$status $body_bytes_sent"$http_referer" '

    #                  '"$http_user_agent""$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

   sendfile        on;

   #tcp_nopush     on;

 

    #http连接的持续时间

   keepalive_timeout  65;

 

    #gzip压缩设置

    gzip  on;           #开启gzip

   gzip_min_length 1k;  #最小压缩文件大小

   gzip_buffers 4 16k;  #压缩缓冲区

    #http的协议版本(1.0/1.1),默认1.1,前端如果是squid2.5请使用1.0

   gzip_http_version 1.1;

    #gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)

   gzip_comp_level 2;   

    #和http头有关系,加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩

    gzip_varyon;

    #gzip压缩类型,不用添加text/html,否则会有警告信息

   gzip_types text/plain text/javascript text/css application/xmlapplication/x-javascript application/json;

 

    #设定负载均衡的服务器列表,可以设置多个upstream,但mysvr名字要区分

    upstreammyClusterServer1 {

       #weigth参数表示权值,权值越高被分配到的几率越大

        #本机上的Squid开启3128端口

       server 127.0.0.1:8081  weight=5;

       server 127.0.0.1:8082  weight=5;

       server 127.0.0.1:8083  weight=5;

    }

 

    server {

       #nginx监听的端口号

       listen       80;

        #域名可以有多个,用空格隔开

       server_name  127.0.0.1;

       

        #字符编码方式

       charset utf-8;

 

        #设定本虚拟主机的访问日志。关闭日志可以减少IO,提高性能。

       #access_log logs/host.access.log  main;

 

        #默认请求

       location / {

            #定义服务器的默认网站根目录位置

           root   html;

            #定义首页索引文件的名称

           index  index.html index.htmindex.jsp;

            #请求转向mysvr 定义的服务器列表

           proxy_pass    http://myClusterServer1;

           proxy_redirect default;

            #跟代理服务器连接的超时时间,必须留意这个time out时间不能超过75秒,当一台服务器当掉时,过10秒转发到另外一台服务器。

           proxy_connect_timeout 10;

        }

 

       #error_page  404              /404.html;

 

        #redirect server error pages to the static page /50x.html

        #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

        }

 

        #proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

       #location ~ \.php$ {

       #    proxy_pass   http://127.0.0.1;

        #}

 

        #pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

       #location ~ \.php$ {

       #    root           html;

       #    fastcgi_pass   127.0.0.1:9000;

       #    fastcgi_index  index.php;

       #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

       #    include        fastcgi_params;

        #}

 

        #deny access to .htaccess files, if Apache's document root

        #concurs with nginx's one

        #

       #location ~ /\.ht {

       #    deny  all;

        #}

    }

 

 

    # anothervirtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen      8000;

    #    listen      somename:8080;

    #    server_name somename  alias  another.alias;

 

    #    location / {

    #        root  html;

    #        index index.html index.htm;

    #    }

    #}

 

 

    # HTTPSserver

    #

    #server {

    #    listen      443 ssl;

    #    server_name localhost;

 

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_cache    shared:SSL:1m;

    #    ssl_session_timeout  5m;

 

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers  on;

 

    #    location / {

    #        root  html;

    #        index index.html index.htm;

    #    }

    #}

 

}
1.5.2 Start and shut down Nginx

(1) Start

# sudo  /usr/local/nginx-1.6.1/sbin/nginx

Make sure that port 80 of the system is not occupied by other programs

Restart:

# sudo  /usr/local/nginx-1.6.1/sbin/nginx  -s  reload

(2) Shut down:

ps -ef | grep nginx

Find the main process ID, and then Just kill, such as:

# kill -9 [进程号]


(3) Check whether the startup is successful:

netstat -ano | grep80

If the result is entered, it means the startup is successful

Open the browser to access the IP of this machine, if the browser displays Welcome to nginx! means that Nginx has been installed and run successfully. If the load balancing server has been configured, you will see the website page in Tomcat, as shown below:

2. Memcache installation

Memcached official website: http://memcached.org/

You need to install memcached first Install libevent, libevent official website: http://libevent.org/

This installation version:

memcached-1.4.20.tar.gz

libevent-2.0.21-stable.tar.gz

2.1 Install livevent

Check whether it is installed: # rpm qa | grep libevent

If it is installed and the version is lower than 1.3, uninstall it first:

# rpm -e libevent --nodeps

.

# tar zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# sudo ./configure --prefix=/usr/local/libevent-2.0.21-stable      #prefix指定安装路径
# sudo make
# sudo make install

After the installation is completed, go to the directory specified by prefix to see if the libevent-2.0.21-stable directory exists, as shown in the figure below.

2.2 Install Memcached

# tar zxvf memcached-1.4.20.tar.gz
# cd memcached-1.4.20
# sudo ./configure
--prefix=/usr/local/memcached-1.4.20
--with-libevent=/usr/local/libevent-2.0.21-stable
# sudo make
# sudo make install
2.2.1 Check the installation status

After the installation is completed, go to the directory specified by prefix to check whether there is a memcached-1.4.20 directory, as shown in the figure below.

2.2.2 View memcached and libevent version information

First locate the bin directory of Memcached:

# cd /usr/local/memcached-1.4.20/bin

Execute the command:

# sudo ./memcached -i

2.2.3 Start memca ched

# sudo ./memcached -d -v -p 12000 -m 512 -u zht

Explanation: -d means running memcached in daemon mode; -v means outputting extract and error information; -p specifies the listening port number; -m specifies the maximum memory that can be used, in MB; -u specifies the account for running memcached , non-root user.

Use # ps -ef | grep memcached to view the process.

Focus on basic options:

Description

-p

Listening TCP port (default: 1121 1)

-d

Run memcached in daemon mode

-u

运行memcached的账户,非root用户

-m

最大的内存使用,单位是MB,缺省是 64 MB

-c

软连接数量,缺省是1024(最大并发连接数)

-v

输出警告和错误信息

-vv

打印客户端的请求和返回信息

-h

打印帮助信息

-i

打印memcached和libevent的版权信息

2.2.4使用telnet验证服务是否可用

Win7安装telnet:控制面板-> 程序和功能->打开或安装Windows功能->勾选Telnet服务端、Telnet客户端。


CentOS安装telnet客户端:

# sudo yum install telnet

Windows命令:

 

CentOS(Linux)下命令:

# telnet 127.0.0.1 12000

Trying 127.0.0.1...

Connected to localhost (127.0.0.1).

Escape character is '^]'.

连接成功后,手动输入命令:stats

即可看到如下信息:


手动输入:quit   //退出

Connection closed by foreign host


2.2.5停止memcached服务

# ps -ef | grep memcached

找到memcached进程ID号,然后强制杀死:

kill - 9 2439

3.安装Tomcat+配置memcached

Tomcat官网:http://tomcat.apache.org/

3.1安装Tomcat

本次使用Tomcat版本:apache-tomcat-7.0.55.tar.gz

将Tomcat解压到任意目录下。

3.2为Tomcat配置memcached

3.2.1为Tomcat添加库文件

Tomcat要支持memcached管理Session,需要调用一些jar库文件:

添加mem和msm的依赖jar包:

couchbase-client-1.2.2.jar

javolution-5.4.3.1.jar

kryo-1.03.jar

kryo-serializers-0.10.jar

memcached-session-manager-1.6.5.jar

memcached-session-manager-tc6-1.6.5.jar

minlog-1.2.jar

msm-kryo-serializer-1.6.5.jar

reflectasm-0.9.jar

spymemcached-2.10.2.jar

【注意】:

msm1.6.5依赖了Couchbase,需要添加couchbase-client的jar包,否则启动会报:java.lang.NoClassDefFoundError:com/couchbase/client/CouchbaseClient。

Tomcat6和Tomcat7使用不同msm支持包:memcached-session-manager-tc6-1.6.5.jar和memcached-session-manager-tc7-1.6.5.jar,只可选一,否则启动报错。

msm源码中的lib包版本太低:spymemcached需要使用2.10.2,否则启动tomcat报错:

java.lang.NoSuchMethodError:net.spy.memcached.MemcachedClient.set(Ljava/lang/String;ILjava/lang/Object;)Lnet/spy/memcached/internal/OperationFuture;

atde.javakaffee.web.msm.BackupSessionTask.storeSessionInMemcached(BackupSessionTask.java:227)

kryo-serializers需要使用0.10版本,否则报错:

Caused by:java.lang.ClassNotFoundException: de.javakaffee.kryoserializers.DateSerializer

部分文件下载地址:http://code.google.com/p/memcached-session-manager/downloads/list

其他的文件自己找。

下载后,将这些库文件放到tomcat\lib目录下。

3.2.2为Tomcat配置memcached

配置文件目录:tomcat\conf\context.xml

打开配置文件,在...节点中添加如下内容:

<ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
        memcachedNodes="n1:127.0.0.1:12000"
        requestUriIgnorePattern=".*\.(png|gif|jpg|css|js|ico|jpeg|htm|html)$"
        sessi
        sessi
        copyCollecti transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"/> 

【参数说明】:

docBase:与中的appBase一致,网站部署目录。

memcachedNodes:memcached服务器信息,多个服务器时,使用空格分开,如:

n1:127.0.0.1:12001 n2:127.0.0.1:12002  n3:127.0.0.1:12003

配置文件如下图所示:

 

还有在server.xml中配置节点的appBase="部署目录"

 

3.2.3测试Session共享

测试JSP代码如下:index.jsp

同时启动多个Tomcat,我部署了3个,打开浏览器去访问第一个Tomcat,然后在访问第二个和第三个Tomcat,页面Session信息如下:


由图可以看到,三个Tomcat的SessionID都是一样的:5FBF6D6B6F37BE8248ED965536427005-n1,只要不关闭浏览器,不管怎么刷新,SessionID都是不变了。由此可以,三个Tomcat通过memcached实现了Session信息共享。

4.安装Samba共享文件服务

查看是否已经安装samba:

# rpm -qa | grep samba

4.1安装samba

使用网络安装快速方便,命令如下:

# sudo yum install samba samba-client

4.2配置共享文件夹

(1)修改配置文件:

# sudo vi /etc/samba/smb.conf

[SharedFolder]

path = /home/zht/SharedFolder          #共享目录的路径
writeable = yes                       #是否允许写
browsable = yes                      #是否允许以浏览目录内容
valid user = zht                       #访问目录的账户名

(2)添加账户

添加zht账户:

# sudosmbpasswd -a zht

按照提示输出访问密码。

4.3开启端口号

Port 137 (UDP)  --NetBIOS name server and nmbd
Port 138 (UDP) --NetBIOS datagram service
Port 139 (TCP) --File and printer sharing and smbd
Port 389 (TCP) --for LDAP(Active Directory Mode)
Port 445 (TCP) --NetBIOS was moved to 445 after 2000 and beyond(CIFS)
Port 901 (TCP) --for SWAT

(1) CentOS使用图形化添加端口号:sudo setup;

(2) 使用以下命令开启:

# iptables -I INPUT -p udp --dport [端口号] -j ACCEPT
# iptables -I INPUT -p tcp --dport [端口号] -j ACCEPT

保存:

# sudo service iptables save

4.4关闭SELinux

查看状态:

# getenforce

关闭:

# setenforce 0

SELinux几种状态:

enforcing:强制模式,代表SELinux运行中且正确限制;

permissive:宽容模式,代表SELinux运行中,不过仅有警告信息,并不实际限制;

disable:关闭,SELinux未运行。

永久关闭方法:

# sudo vi /etc/selinux/config
# SELINUX = enforcing          注释掉
# SELINUXTYPE = targeted      注释掉

在末尾添加一行:

SELINUX = disable

保存关闭:

:wq

重启系统

 

4.5启动与停止服务

(1)查看运行状态:

# sudo service smb status

(2)启动和停止服务:

# sudo /etc/init.d/smb   start/stop/restart

或者

# sudo service smb    start/stop/restart

(3)设置开启启动:

# sudo chkconfig --level 35 smb on

在3,5级别上自动运行smb服务

(4)验证服务

# smbclient -L //192.168.101.249 -U rxyy

或者

# smbclient //192.168.101.249/sharedFolder -U rxyy

5安装配置JDK

本次使用JDK版本为:jdk-7u60-linux-x64.tar.gz

5.1卸载已安装的JDK

查看已经安装的JDK

# rpm -qa | grep jdk*

卸载JDK:

# sudo yum -y remove [包名]

如下图所示:

安装新的JDK:将jdk-7u60-linux-x64.tar.gz解压缩到指定目录下即可。


5.2配置Java环境变量

# sudo vi /etc/profile

在末尾添加:

#JDK

exportJAVA_HOME=/home/zht/BalanceServer/Java/jdk1.7.0_60
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$CLASSPATH:.:$JRE_HOME/lib:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

如下图所示:

6安装MySQL数据库

6.1卸载已安装的mysql

查看已经安装的mysql数据库:

# rpm -qa | grep mysql

卸载已安装的mysql数据库:

# rpm -e [包名]                //普通卸载,可能因为依赖无法卸载
# rpm -e --nodeps [包名]         //强制卸载,有依赖也能卸载

或者

# yum -y remove [包名]         //卸载时自动处理依赖

6.2安装mysql

    安装MySQL数据库需要安装4个组件:mysql--shared-compat 、mysql-server、mysql-client和mysql-workbench,安装方法如下。

(1)安装mysql共享库:

# rpm -ivh MySQL-shared-compat-5.6.20-1.el6.x86_64.rpm

(2)安装mysql服务端::

# rpm -ivh MySQL-server-5.6.20-1.el6.x86_64.rpm

(3)安装mysql客户端:

# rpm -ivh MySQL-client-5.6.20-1.el6.x86_64.rpm

(4)安装mysql-workbench工具:

# rpm -ivh mysql-workbench-community-6.1.7-1.el6.x86_64.rpm

【说明】:RPM安装包,在CentOS下可以直接双击运行安装,系统会自动处理依赖。

另附:yum安装方法

查看yum上可安装的版本:

# yumlist | grep mysql

安装:

# yum install -y mysql-server mysql mysql-devel

6.3配置mysql

配置文件目录:

/usr/my.cnf或者/etc/mysql/my.cnf

打开文件后,在[mysqld]后面添加如下配置:

#字符编码

character-set-server=utf-8

#存储引擎

default-storage-engine=INNODB
innodb_flush_log_at_trx_commit=2

#缓冲池

innodb_additi
innodb_buffer_pool_size=1024M

#最大连接数

max_c/pre><p>#允许的最大包大小(例如一个Blob字段)</p><p></p><pre name="code">max_allowed_packet=16M

 

6.4启动mysql

(1)启动mysql:

# sudo service mysql  start/stop/restart

# sudo /etc/rc.d/init.d/mysql  start/stop/restart

(或 # sudo /etc/rc.d/init.d/mysqld  start/stop/restart)

启动mysql出错时,请到/var/liv/mysql/xxx.err查看错误信息。


(2)登录MySQL

# mysql -u root -p
# 输入密码
# mysql> show databases;
# mysql> show variables like '%character_set%'
# mysql> ...

(3)创建远程连接用户

启用sxzl用户在本地(localhost)的登录权限,密码hymmldr.

# mysql> grant all privileges on *.* tosxzl@localhost identified by 'hymmldr.' with grant option;

启用sxzl用户远程(%)登录权限,密码hymmldr.

# mysql> grant all privileges on *.* to sxzl@"%"identified by 'hymmldr.' with grant option;

刷新权限

# mysql> flush privileges;

第一次安装需要重设mysql密码,详见如下。

6.5重设mysql密码(官网方法)

启动mysql,然后执行如下命令:

# ps -ef | grep -i mysql

查看mysqld_safe的路径,例如:/usr/bin/mysqld_safe


停止mysql,然后以安全方式启动mysql:

# sudo  /usr/bin/mysqld_safe  --skip-grant-tables  >/dev/null 2>&1  &

5秒钟后执行:

# sudo /usr/bin/mysql -u root mysql

重设root用户的密码:

# mysql> update user SET PASSWORD=PASSWORD('root')where user='root'

刷新权限

# mysql> flush privileges;
# mysql> exit;

接着再次登录mysql:

# mysql -u root -p

# 输入密码

然后执行:

# mysql> show databases;

报错:You must SET PASSWORD before executing thisstatement.

解决办法,重设一次密码:

# mysql> SET PASSWORD=PASSWORD('root');
# mysql> flush privileges;

 

设置完毕后,就可以打开mysql-workbench连接到数据库了。

7命令说明:yum

yum安装选项说明:

yum -y install 包名(支持*) :自动选择y,全自动
yum install 包名(支持*) :手动选择y or n
yum -y remove [package name] :自动处理依赖
yum remove 包名(不支持*)
rpm -ivh 包名(支持*):安装rpm包

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了Nginx+Tomcat+Memcached负载均衡集群服务搭建,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn