Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/48289765
Operating system: CentOS6.5
This document mainly explains how to build nginx+tomcat+memcached Load Balancer Cluster server under CentOS6.5, Nginx is responsible for load balancing, Tomcat is responsible for the actual service, memcached is responsible for synchronizing Tomcat's session, Achieve the purpose of session sharing.
1. Installing 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 repositories first.
1.1 Installing GCC, gcc-c++
installation command:
#sudo yum install gcc# sudo yum install gcc-c++
1.2 Installing OpenSSL
OpenSSL official website: http://www.openssl.org/
Installed 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 specifying the installation directory #sudo Make#sudo make install
"Note": the config command is used here instead of the usual configure command
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.
1.3 Installing Pcre
Pcre Official website: http://www.pcre.org/
Installed 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 complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.
"Note": if the C + + compiler is not installed, the installation of this software will be error-free!
1.4 Installing Zlib
Zlib Official website: http://www.zlib.net/
Installed 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 complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.
1.5 Installing Nginx
Installed 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 specifying the installation directory--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 source path for #指的是zlib--with-http_ssl_module#sudo Make#make Install
After the installation is complete, check to see if the installation is successful under/usr/local/. If the installation error occurs, you need to re-compress and reinstall.
1.5.1 Configuration 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; #服务进程数量, which is generally equal to the number of CPUs worker_processes 1; #全局错误日志定义, it is recommended to turn on the error level log. [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) one way, but only for linux2.6 above the core, can greatly improve the performance of Nginx. Linux recommends using EPOLL,FREEBSD to use 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), default 1.1, front End If squid2.5 please use 1.0 gzip_http_version 1.1; #gzip压缩比, the 1 compression ratio is the fastest, the 9 compression ratio is the largest but the processing speed is the slowest (transmission is fast but consumes CPU) Gzip_comp_level 2; #和http头有关系, add a vary head, to the proxy server, some browsers support compression, and some do not support, so avoid wasting does not support compression, so according to the client's HTTP header to determine whether it is necessary to compress the Gzip_varyon; #gzip压缩类型, do not add text/html, otherwise there will be a warning message gzip_types text/plain text/javascript text/css application/xmlapplication/ X-javascript Application/json; #设定负载均衡的服务器列表, you can set multiple upstream, but the Mysvr name distinguishes UpstreammyClusterServer1 {#weigth参数表示权值, the higher the weight, the greater the probability of being allocated #本机上的Squid Open 3128 Port 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; #域名可以有多个, separated by a space server_name 127.0.0.1; #字符编码方式 CharSet Utf-8; #设定本虚拟主机的访问日志. Turning off logs can reduce IO and improve performance. #access_log Logs/host.access.log Main; #默认请求 location/{#定义服务器的默认网站根目录位置 root html; #定义首页索引文件的名称 Index INDEx.html index.htmindex.jsp; #请求转向mysvr defined list of servers Proxy_pass http://myClusterServer1; Proxy_redirect default; #跟代理服务器连接的超时时间, it is important to note that the time out period cannot exceed 75 seconds, and when a server is dropped, it is forwarded to another server for 10 seconds. Proxy_connect_timeout 10; } #error_page 404/404.html; #redirect Server error pages to the static page/50x.html # Error_page 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 close Nginx
(1) Start
# sudo /usr/local/nginx-1.6.1/sbin/nginx
Ensure that the 80 port of the system is not occupied by other programs
Restart:
# sudo /usr/local/nginx-1.6.1/sbin/nginx- s Reload
(2) Close:
Ps-ef | grep nginx
Locate the main process ID, and then kill, as follows:
# kill-9 [Process number]
(3) Check whether the start is successful:
Netstat-ano | Grep80
Result Input Description Start successful
Open the browser to access the IP of this machine if the browser appears welcome to nginx! Indicates that Nginx is installed and running successfully. If you have configured a load Balancer server, you will see the Web pages in Tomcat, such as:
2. Memcache Installation
Memcached Official website: http://memcached.org/
Install memcached need to 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 Installing Livevent
To see if you have installed: # RPM QA | grep libevent
If it is installed and the version is less than 1.3, first pass:
# rpm-e Libevent--nodeps
To uninstall.
# 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 complete, go to the directory specified in prefix to see if there is a libevent-2.0.21-stable directory, as shown in.
2.2 Installing 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 loading situation
After the installation is complete, check to see if there is a memcached-1.4.20 directory in the directory specified by prefix, as shown in.
2.2.2 Viewing memcached and libevent version information
First navigate to the bin directory of the memcached:
# Cd/usr/local/memcached-1.4.20/bin
Execute command:
# sudo./memcached-i
2.2.3 Start memcached
# sudo./memcached-d-v-p 12000-m 512-u zht
Explanation:-D indicates that the MEMCACHED;-V is running as a daemon for output extract and error messages;-p Specifies the port number to listen on, and-m specifies the maximum memory that can be used, mb;-u specifies the account that runs memcached, not the root user.
Use # Ps-ef | grep memcached to view the process.
Follow the basic options: |
Description |
-P |
TCP port for Listening (default: 11211) |
-D |
Run memcached as Daemon |
-U |
Account running memcached, non-root user |
-M |
Maximum memory usage, in MB, default is MB |
-C |
Number of soft connections, default is 1024 (maximum number of concurrent connections) |
-V |
Output warnings and error messages |
-vv |
Print client request and return information |
-H |
Printing Help information |
-I. |
Print copyright information for memcached and libevent |
2.2.4 using Telnet to verify that the service is available
Win7 Install Telnet: Control Panel, programs and features, open or install Windows features, tick the Telnet server, Telnet client.
CentOS Install Telnet Client:
# sudo yum install Telnet
Windows command:
Under CentOS (Linux) command:
# telnet 127.0.0.1 12000
Trying 127.0.0.1 ...
Connected to localhost (127.0.0.1).
Escape character is ' ^] '.
After the connection is successful, enter the command manually: stats
You can see the following information:
Manual input: Quit//exit
Connection Closed by foreign host
2.2.5 Stop memcached Service
# Ps-ef | grep memcached
Locate the memcached process ID number, and then force Kill:
Kill-9 2439
3. Install the tomcat+ configuration memcached
Tomcat official website: http://tomcat.apache.org/
3.1 Installing Tomcat
This time using Tomcat version: apache-tomcat-7.0.55.tar.gz
Unzip the tomcat into any directory.
3.2 Configuring memcached for Tomcat
3.2.1 Adding a library file for Tomcat
Tomcat to support memcached management session, you need to call some jar library files:
Add dependent jar packages for mem and MSM: |
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 |
"Note":
msm1.6.5 relies on couchbase, need to add couchbase-client jar package, otherwise the launch will be reported: java.lang.noclassdeffounderror:com/couchbase/client/ Couchbaseclient.
Tomcat6 and TOMCAT7 use different MSM support packages: Memcached-session-manager-tc6-1.6.5.jar and Memcached-session-manager-tc7-1.6.5.jar, only one can be selected, or an error is initiated.
The version of the Lib package in the MSM source code is too low: spymemcached need to use 2.10.2, otherwise start tomcat error:
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 need to use version 0.10, otherwise error:
caused By:java.lang.ClassNotFoundException:de.javakaffee.kryoserializers.DateSerializer
Partial file Download Address: http://code.google.com/p/memcached-session-manager/downloads/list
Other documents to find out for themselves.
After downloading, place these library files in the Tomcat\lib directory.
3.2.2 Configuring memcached for Tomcat
Configuration file directory: Tomcat\conf\context.xml
Open configuration file, in ... node, add the following:
"Parameter description":
DocBase: consistent with the appbase in, the site Deployment directory.
memcachednodes:memcached server information, when multiple servers are separated using spaces, such as:
n1:127.0.0.1:12001 n2:127.0.0.1:12002 n3:127.0.0.1:12003
The configuration file looks like this:
There are also appbase= "deployment Directories" for configuring nodes in Server.xml
3.2.3 Test session Sharing
The test JSP code is as follows: index.jsp
Starting multiple tomcat at the same time, I deployed 3, opened the browser to access the first Tomcat, then accessed the second and third Tomcat, the page session information is as follows:
As can be seen from the figure, three Tomcat SessionID are the same: 5FBF6D6B6F37BE8248ED965536427005-N1, as long as not close the browser, no matter how refreshing, SessionID is unchanged. As a result, three Tomcat realizes session information sharing through memcached.
4. Installing the Samba shared file service
To see if Samba is already installed:
# Rpm-qa | grep Samba
4.1 Installing Samba
Using the network installation is quick and easy, the command is as follows:
# sudo yum install samba samba-client
4.2 Configuring Shared Folders
(1) Modify the configuration file:
# sudo vi/etc/samba/smb.conf
[SharedFolder]
Path =/home/zht/sharedfolder #共享目录的路径writeable = yes #是否允许写browsable = yes #是否允许以浏览目录内容valid user = Zht #访问目录的账户名
(2) Add an account
Add ZHT Account:
# sudosmbpasswd-a Zht
Follow the prompts to export the access password.
4.3 Turn on the port number
Port 137 (UDP) --netbios name server and Nmbdport 138 (UDP)--netbios datagram Serviceport 139 (TCP)--file and Printe R Sharing and Smbdport 389 (TCP)--for LDAP (Active Directory Mode) Port 445 (TCP)--netbios is moved to 445 after Beyond (CIFS) Port 901 (TCP)--for SWAT
(1) CentOS use graphical add port number: sudo setup;
(2) Use the following command to open:
# iptables-i input-p UDP--dport [port number]-j accept# iptables-i input-p TCP--dport [port number]-j ACCEPT
Save:
# sudo service iptables save
4.4 Turn off SELinux
View Status:
# Getenforce
Shut down:
# Setenforce 0
SELinux several states:
Enforcing: Mandatory mode, representing the SELinux operation and the correct limit;
Permissive: Tolerant mode, representing the SELinux operation, but only the warning information, not practical restrictions;
Disable: Off, SELinux not running.
Permanent shutdown Method:
# sudo vi/etc/selinux/config# SELinux = enforcing commented out # Selinuxtype = Targeted commented out
Add a line at the end:
SELINUX = disable
Save Close:
: Wq
Rebooting the system
4.5 Starting and stopping services
(1) Check the running status:
# sudo service SMB status
(2) Start and stop services:
# SUDO/ETC/INIT.D/SMB Start/stop/restart
Or
# sudo service SMB Start/stop/restart
(3) Set on start:
# sudo chkconfig--level + SMB on
Automatically run the SMB service on 3, Level 5
(4) Verification Services
# smbclient-l//192.168.101.249-u Rxyy
Or
# smbclient//192.168.101.249/sharedfolder-u Rxyy
5 Installing the configuration JDK
This use of the JDK version is: jdk-7u60-linux-x64.tar.gz
5.1 Uninstalling an installed JDK
View the JDK that is already installed
# Rpm-qa | grep jdk*
Uninstalling the JDK:
# sudo yum-y remove [package name]
As shown in the following:
Install the new JDK: Unzip the jdk-7u60-linux-x64.tar.gz to the specified directory.
5.2 Configuring Java Environment variables
# sudo vi/etc/profile
Add at the end:
#JDK
Exportjava_home=/home/zht/balanceserver/java/jdk1.7.0_60export jre_home= $JAVA _home/jreexport CLASSPATH=$ CLASSPATH:.: $JRE _home/lib: $JAVA _home/libexport path= $PATH: $JAVA _home/bin: $JRE _home/bin
As shown in the following:
6 Installing the MySQL database
6.1 Uninstalling the installed MySQL
To view the MySQL databases that have been installed:
# Rpm-qa | grep MySQL
To uninstall an installed MySQL database:
# RPM-E [Package name] //Normal Uninstall, possibly because the dependency cannot unload # RPM-E--nodeps [package name] //Forced uninstall, have dependencies can also uninstall
Or
# yum-y Remove [package name] //automatically handle dependencies on uninstallation
6.2 Installing MySQL
Installing the MySQL database requires the installation of 4 components: Mysql--shared-compat, Mysql-server, Mysql-client, and Mysql-workbench, and the installation methods are as follows.
(1) Install MySQL shared library:
# RPM-IVH mysql-shared-compat-5.6.20-1.el6.x86_64.rpm
(2) Install MySQL server::
# RPM-IVH mysql-server-5.6.20-1.el6.x86_64.rpm
(3) Install the MySQL client:
# RPM-IVH mysql-client-5.6.20-1.el6.x86_64.rpm
(4) Installing the Mysql-workbench tool:
# RPM-IVH mysql-workbench-community-6.1.7-1.el6.x86_64.rpm
"description": RPM installation package, under CentOS can be directly double-click to run the installation, the system will automatically handle dependencies.
Also attached: Yum installation method
To view the installable versions on Yum:
# Yumlist | grep MySQL
Installation:
# yum install-y mysql-server MySQL mysql-devel
6.3 Configuring MySQL
Configuration file directory:
/USR/MY.CNF or/ETC/MYSQL/MY.CNF
After opening the file, add the following configuration after [mysqld]:
#字符编码
Character-set-server=utf-8
#存储引擎
default-storage-engine=innodbinnodb_flush_log_at_trx_commit=2
#缓冲池
innodb_additiinnodb_buffer_pool_size=1024m
#最大连接数
Max_c/pre>#允许的最大包大小 (for example, a BLOB field)
max_allowed_packet=16m
6.4 Starting MySQL
(1) Start MySQL:
# sudo service MySQL start/stop/restart
Or
# Sudo/etc/rc.d/init.d/mysql Start/stop/restart
(or # Sudo/etc/rc.d/init.d/mysqld Start/stop/restart)
When you start MySQL error, go to/var/liv/mysql/xxx.err to view the error message.
(2) Log in to MySQL
# mysql-u root-p# Enter password # mysql> show databases;# mysql> show variables like '%character_set% ' # mysql> ...
(3) Create a remote connection user
Enable Sxzl user on-premises (localhost) logon rights, password Hymmldr.
# mysql> Grant all privileges on * * Tosxzl@localhost identified by ' Hymmldr. ' with GRANT option;
Enable SXZL user remote (%) logon rights, password Hymmldr.
# mysql> Grant all privileges on * * to sxzl@ "%" identified by ' Hymmldr. ' with GRANT option;
Refresh Permissions
# mysql> flush Privileges;
The first installation requires the MySQL password to be reset, as described below.
6.5 Reset MySQL Password (official website method)
Start MySQL, and then execute the following command:
# Ps-ef | Grep-i MySQL
View the path to the Mysqld_safe, for example:/usr/bin/mysqld_safe
Stop MySQL, and then start MySQL in a safe way:
# sudo /usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
5 Seconds after execution:
# sudo/usr/bin/mysql-u Root MySQL
To reset the root user's password:
# mysql> Update user SET Password=password (' root ') where user= ' root '
Refresh Permissions
# mysql> Flush privileges;# mysql> exit;
Then log in to MySQL again:
# mysql-u Root-p
# Enter password
Then execute:
# mysql> show databases;
Error: You must SET PASSWORD before executing thisstatement.
Workaround, reset the password once:
# mysql> SET password=password (' root '); # mysql> flush privileges;
Once setup is complete, you can open mysql-workbench to connect to the database.
7 Command Description: Yum
Yum Installation Options Description:
Yum-y Install package name (Support *): Auto select y, automatic yum Install package name (support *): Manually select Y or nyum-y Remove [package name]: Automatic Processing dependent yum Remove pack name (not supported *) rpm -IVH Package Name (support *): Install RPM Package
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The above describes the Nginx+tomcat+memcached load Balancing Cluster service build, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.