Linux--nginx to dual Tomcat (LNMT, Lamt), use of dynamic-static separation in load balancing

Source: Internet
Author: User
Tags stop script server memory server port

Environment Arrangement:
Configuring the source for installation 163

[[email protected] ~]# cd /etc/yum.repos.d///创建备份目录[[email protected] yum.repos.d]# mkdir /etc/repo-bf//将原yum仓库的文件备份到repo-bf[[email protected] yum.repos.d]# mv * /etc/repo-bf//下载163的源到yum仓库[[email protected] yum.repos.d]# curl -o 163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo//将改为版本的7[[email protected] yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/163.repo[[email protected] yum.repos.d]# sed -i ‘s/enabled=0/enabled=1/g‘ /etc/yum.repos.d/163.repo//自动配置yum的软件仓库,也可以自己配置[[email protected] yum.repos.d]# yum -y install epel-release[[email protected] yum.repos.d]# yum clean all//安装编译环境[[email protected] yum.repos.d]# yum -y install gcc gcc-c++[[email protected] yum.repos.d]# yum -y install wget

Tomcat Project Deployment
Java Environment installation

[[email protected] yum.repos.d]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

To view the installed version

[[email protected] yum.repos.d]# java -versionopenjdk version "1.8.0_181"OpenJDK Runtime Environment (build 1.8.0_181-b13)OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Tomcat deployment
Download tomcat, download it on the website
Here is the download core

[[email protected] yum.repos.d]# cd /usr/src/[[email protected] src]# wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-9/v9.0.11/bin/apachmcat-9.0.11.tar.gz

Unzip the deployment, only need to decompress without installation

Decompression deployment, only need to decompress without installation [[email protected] src]# lsapache-tomcat-9.0.11.tar.gz debug kernels extract to/usr/local/[[email  protected] src]# Tar XF apache-tomcat-9.0.11.tar.gz-c/usr/local/The software of the extracted Tomcat package, named tomcat[[email  Protected] src]# cd/usr/local[[email protected] local]# ln-s apache-tomcat-9.0.11/tomcat[[email protected ] local]# lsapache-tomcat-9.0.11 bin etc games include Lib Lib64 libexec sbin share src Tomcat[[email prot Ected] local]# ll//Soft connect lrwxrwxrwx.               1 root root 20 September 5 12:03 Tomcat-apache-tomcat-9.0.8/because it is a Java program, General server memory is at least 8-16g, and Tomcat cannot run only one on a single server Enter the Tomcat package configuration, as well as introduction [[[Email protected] local]# CD tomcat/[[email protected] tomcat]# lsbin//binary Program location STA rtup.sh-startup script startdown.sh-Stop script catalina.sh-control start and stop configtest.sh-check configuration file for error----conf//config file Server.xml//Run two and need to be modified Port number-----lib-----logs//log directory readme.md RUNNING.txt-----webapps//program and Web page put this Java, in the release, is to put the development of the package here, extracted out of the development of the war package written in which Needs to be configured in theQ Development, Example: Classes/config.properties configuration program Parameters BUILDING.txt contributing.md LICENSE NOTICE release-notes temp work 

Create a directory in WebApps and edit the page in the directory to write a Hello Worid Java interface

[[email protected] local]# cd webapps/[[email protected] webapps]# mkdir test[[email protected] webapps]# cd test[[email protected] test]# vim index.jsp[[email protected] test]# cat index.jsp

Start

//检查[[email protected] webapps]# /usr/local/tomcat/bin/configtest.sh//启动[[email protected] webapps]# /usr/local/tomcat/bin/catalina.sh startUsing CATALINA_BASE: /usr/local/tomcatUsing CATALINA_HOME: /usr/local/tomcatUsing CATALINA_TMPDIR: /usr/local/tomcat/tempUsing JRE_HOME: /usrUsing CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jarTomcat started.
[[email protected] webapps]# ss -anltState Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 :::8080 :::* LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* LISTEN 0 100 :::8009 :::* 



Now, one more Tom Cat.

再次回到tomcat包的目录[[email protected] local]# cd /usr/src/解压并将解压后的文件移动到/usr/local/,重命名为apache-tomcat-9.0.11-tom2[[email protected] src]# tar xf apache-tomcat-9.0.11.tar.gz[[email protected] src]# mv apache-tomcat-9.0.11 /usr/local/apache-tomcat-9.0.11-tom2将解压后的apache-tomcat-9.0.11-tom2包创建软件接,命名tomcat2[[email protected] src]# cd /usr/local[[email protected] local]# ln -s apache-tomcat-9.0.11-tom2/ tomcat2[[email protected] local]# lsapache-tomcat-9.0.11 bin games lib libexec share tomcatapache-tomcat-9.0.11-tom2 etc include lib64 sbin src tomcat2[[email protected] local]# ll //软连接

Enter WebApps, and configure the Web page to start TOMCAT2

  Modify the port configuration first [[email protected] local]# CD tomcat2/conf[[email protected] conf]# Lscatalina.policy Co Ntext.xml jaspic-providers.xsd server.xml tomcat-users.xsdcatalina.properties jaspic-providers.xml Log Ging.properties tomcat-users.xml web.xml[[email protected] conf]# vim server.xml//on the original port slightly change can be <server Port= "8015" shutdown= "shutdown" ><connector port= "8081" protocol= "http/1.1" <connector port= "8019" protocol= "ajp/1.3" redirectport= "8453"/>[[email protected] conf]# cd/usr/local/tomcat2/webapps/[[email  Protected] webapps]# Lsdocs examples Host-manager manager root[[email protected] webapps]# mkdir test[[email  ;p rotected] webapps]# CD test[[email protected] test]# vim index.jsp[[email protected] test]# cat index.jsp  

Start

check [[email protected] webapps]#/usr/local/tomcat2/bin/configtest.sh//start [[email protected] webapps]#/ usr/local/tomcat2/bin/catalina.sh start[[email protected] webapps]# ss-anltstate Recv-Q Send-Q Local Addre                                 Ss:port Peer address:port LISTEN 0 128 *:111                  *:* LISTEN 0 5 192.168.122.1:53 *:* LISTEN 0 *:22 *:* LIST             EN 0 127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 1:: ffff:127.0.0.1:80                                ::* LISTEN 0 128::: 111                 :::* LISTEN 0::: 8080:::* LISTEN 0 10                    0::: 8081:::* LISTEN 0 100                                 ::: 8019:::* LISTEN 0 128::: 22                   :::* LISTEN 0:: 1:631:::* LISTEN 0:: 1:25:::* LIS                    TEN 0 1:: ffff:127.0.0.1:8005:::* LISTEN 0 100 ::: 8009:::* [[email protected] webapps]# CD

Different ports


Second Tomcat with IP different Port Web page also built successfully

Start building nging and databases below
Can be built on other servers or on this server,

Use Nginx to achieve load balancing
You can set the IP and port of the reverse Proxy Tomcat Web page on the other server.
Install dependent packages

//创建用户[[email protected] ~]# useradd -r -M -s /sbin/nologin nginx //安装编译环境[[email protected] ~]# yum -y groups list[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
Create log storage directory [[email protected] ~]# mkdir-p/var/log/nginx [[email protected] ~]# chown-r nginx.nginx/var/log/ Nginx//download Nginx, compile and install [[email protected] ~]# cd/usr/src/[[email protected] src]# [[email protected] src ]# yum-y Install wget[[email protected] src]# wget http://64.123.28.133/files/21490000000827F6/nginx.org/ Download/nginx-1.14.0.tar.gz[[email protected] src]# tar xf nginx-1.14.0.tar.gz [[email protected] src]# CD Nginx-1.14.0[[email protected] nginx-1.14.0]# yum-y install gcc gcc-c++[[email protected] nginx-1.14.0]#. Configure--prefix=/usr/local/nginx--user=nginx--group=nginx--with-debug--with-http_ssl_module--with-http_ Realip_module--with-http_image_filter_module--with-http_gunzip_module \--with-http_gzip_static_module-- With-http_stub_status_module--http-log-path=/var/log/nginx/access.log--error-log-path=/var/log/nginx/error.log [[email protected] nginx-1.14.0]# make-j 4 && make install//set environment variables [[email] protected] nginx-1.14.0]# echo ' export path=/usr/local/nginx/sbin: $PATH ' >/etc/profile.d/nginx.sh[[email  protected] nginx-1.14.0]#. /etc/profile.d/nginx.sh//start [[email protected] nginx-1.14.0]# Nginx

Nginx configuration file
Master configuration file/usr/local/nginx/conf/nginx.conf
2. Install MySQL

//安装依赖包[[email protected] ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel创建用户和组[[email protected] ~]# groupadd -r -g 306 mysql[[email protected] ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

Download MySQL package in binary format

[[email protected] ~]# cd/usr/src/[[email protected] src]# wget https://downloads.mysql.com/archives/get/ file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz decompression software to/usr/local/[[email protected] ~]# Cd/usr/src/[[email  protected] src]# lsdebug kernels mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz nginx-1.14.0 nginx-1.14.0.tar.gz[[ Email protected] src]# tar XF mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz-c/usr/local/[[email protected] src]# Ls/usr/local/bin etc games include Lib Lib64 libexec mysql-5.7.22-linux-glibc2.12-x86_64 nginx sbin Shar E src[[email protected] src]# cd/usr/local/[[email protected] local]# LN-SV Mysql-5.7.22-linux-glibc2.12-x86_64/mysql "MySQL", "mysql-5.7.22-linux-glibc2.12-x86_64/"//Modify the owner [[email  Protected] local]# chown-r mysql.mysql/usr/local/mysql[[email protected] local]# ll/usr/local/mysql- Dlrwxrwxrwx. 1 MySQL MySQL 36 August 16:13/usr/local/mysql-mysql-5.7.22-linux-glibc2.12-x86_64///setting environment variables [[email protected] local]# echo ' export path=/usr/local/mysql/bin: $PATH ' >/etc/profile.d/mysql.sh[[ Email protected] local]#. /etc/profile.d/mysql.sh[[email protected] local]# echo $PATH/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/ local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin//set up data storage directory [[email protected] local]# cd/usr/local/ Mysql[[email protected] mysql]# mkdir/opt/data[[email protected] mysql]# chown-r mysql.mysql/opt/data/// Initializes the database and finally generates a temporary password [[email protected] mysql]#/usr/local/mysql/bin/mysqld--initialize--user=mysql--datadir= /OPT/DATA/2018-09-05T09:17:21.026072Z 1 [Note] A temporary password is generated for [email protected]:? 3/(1tqf= Gwu

Configure MySQL

[[email protected] mysql]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql"/usr/local/include/mysql" -> "/usr/local/mysql/include/"[[email protected] mysql]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf[[email protected] mysql]# ldconfig -v//生成配置文件[[email protected] ~]# cat > /etc/my.cnf <<EOF[mysqld]basedir = /usr/local/mysqldatadir = /opt/datasocket = /tmp/mysql.sockport = 3306pid-file = /opt/data/mysql.piduser = mysqlskip-name-resolveEOF

Configuring Startup Scripts

[email protected] mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld[[email protected] mysql]# sed -ri ‘s#^(basedir=).*#\1/usr/local/mysql#g‘ /etc/init.d/mysqld[[email protected] mysql]# sed -ri ‘s#^(datadir=).*#\1/opt/data#g‘ /etc/init.d/mysqld

Start MySQL

[[email protected] mysql]# service mysqld startstarting mysql.logging to '/opt/data/localhost.localdomain.err '. success! [[email protected] mysql]# ps-ef|grep mysqlroot 24348 1 0 16:25 pts/0 00:00:00/bin/sh/usr/local/mysq L/bin/mysqld_safe--datadir=/opt/data--pid-file=/opt/data/mysql.pidmysql 24526 24348 2 16:25 pts/0 00:00:00/usr /local/mysql/bin/mysqld--basedir=/usr/local/mysql--datadir=/opt/data--plugin-dir=/usr/local/mysql/lib/plugin-- User=mysql--log-error=localhost.localdomain.err--pid-file=/opt/data/mysql.pid--socket=/tmp/mysql.sock--port=       3306root 24556 2316 0 16:25 pts/0 00:00:00 grep--color=auto mysql[[email protected] mysql]# ss-antlstate                     Recv-q send-q Local address:port Peer address:port LISTEN 0 128                                  *:111 *:* LISTEN 0 128 *:80     *:*             LISTEN 0 5 192.168.122.1:53 *:* LISTEN             0 *:22 *:* LISTEN 0 128                                  127.0.0.1:631 *:* LISTEN 0 100 127.0.0.1:25                               *:* LISTEN 0 1:: ffff:127.0.0.1:8015                  :::* LISTEN 0::: 111:::*       LISTEN 0:: 8080:::* LISTEN 0                    +:: 8081:::* LISTEN 0 100                                ::: 8019:::* LISTEN 0 128::: 22 :::* LISTEN 0:: 1:631:::*         LISTEN 0:: 1:25:::* LISTEN 0 1                    :: ffff:127.0.0.1:8005:::* LISTEN 0 100                               ::: 8009:::* LISTEN 0 80::: 3306        :::*

Modify temporary password

[[email protected] mysql]# mysql -uroot -pEnter password:mysql> set password = password(‘guohui123‘);Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> quit
[[email protected] nginx-1.14.0]# cd /usr/local/nginx/配置nginx实现动静分离[[email protected] nginx]# vim conf/nginx.conf  keepalive_timeout  65;    upstream web {    server 192.168.56.11:8080;              //设置web设置集群,实行负载均衡,tomcat网站192.168.56.11:8080和tomcat网站192.168.56.11:8081    server 192.168.56.11:8081;}    server {        listen       80;        server_name  localhost;        access_log  logs/host.access.log  main;   location / {            root   html;            index  index.html index.htm;}        location ^\.(do | jsp)$ {        proxy_pass http://web;          //proxy_pass        }
//测试            
[[email protected] nginx]# nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[[email protected] nginx]# nginx -s reload

Browser testing, load balancing is achieved, such as the need for each IP access to the page is not changed, you can add Ip_hash in the upstream web


Refresh

Linux--nginx to dual Tomcat (LNMT, Lamt), use of dynamic-static separation in load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.