LNMT架构部署:Linux+Nginx+Mysql+Tomcat(负载均衡,动静分离)

环境描述:虚拟机准备两台,一台作为nginx服务器+mysql服务器,IP为:192.168.55.129;另外一台作为2台Tomcat服务器,IP为:192.168.55.130。客户端发来请求,首先由nginx处理,如果为静态内容直接由nginx响应,将结果直接给客户端;如果为动态内容,则由nginx反代至后端的Tomcat服务器。

在IP为192.168.55.129的服务器上安装和配置nginx关闭防火墙和selinux
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]#  sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config

创建系统用户nginx
[[email protected] ~]#  useradd -r -M -s /sbin/nologin nginx

安装依赖环境
[[email protected] ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

[[email protected] ~]# yum -y groups install ‘Development Tools‘

创建日志存放目录
[[email protected] ~]# mkdir -p /var/log/nginx
[[email protected] ~]# chown -R nginx.nginx /var/log/nginx/

下载nginx
[[email protected] ~]# cd /usr/src/
[[email protected] src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz

编译安装
[[email protected] ~]# cd /usr/src/
[[email protected] src]# ls
debug  kernels  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]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[[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.12.0]# make -j 2 && make install

nginx安装后配置配置环境变量
[[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

启动nginx
[[email protected] nginx-1.14.0]# nginx
[[email protected] nginx-1.14.0]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128     *:80                  *:*
LISTEN      0      128     *:22                  *:*
LISTEN      0      100    127.0.0.1:25                  *:*
LISTEN      0      128    :::22                 :::*
LISTEN      0      100       ::1:25                 :::*
在IP为192.168.55.129上安装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

下载二进制格式的mysql软件包
[[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

解压软件至/usr/local
[[email protected] src]#  tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/

[[email protected] src]# cd /usr/local/
[[email protected] local]# ls
bin    include  libexec                              sbin
etc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  share
games  lib64    nginx                                src
[[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/"
[[email protected] local]# ll
总用量 0
drwxr-xr-x.  2 root root   6 11月  5 2016 bin
drwxr-xr-x.  2 root root   6 11月  5 2016 etc
drwxr-xr-x.  2 root root   6 11月  5 2016 games
drwxr-xr-x.  2 root root   6 11月  5 2016 include
drwxr-xr-x.  2 root root   6 11月  5 2016 lib
drwxr-xr-x.  2 root root   6 11月  5 2016 lib64
drwxr-xr-x.  2 root root   6 11月  5 2016 libexec
lrwxrwxrwx.  1 root root  36 9月   6 17:30 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 9月   6 17:28 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 151 9月   6 17:08 nginx
drwxr-xr-x.  2 root root   6 11月  5 2016 sbin
drwxr-xr-x.  5 root root  49 9月   3 23:02 share
drwxr-xr-x.  2 root root   6 11月  5 2016 src

修改目录/usr/local/mysql的属主属组
[[email protected] local]# chown -R mysql.mysql /usr/local/mysql
[[email protected] local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月   6 17:30 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

添加环境变量
[[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

建立数据存放目录
[[email protected] local]# mkdir /opt/data
[[email protected] local]# chown -R mysql.mysql /opt/data/
[[email protected] local]#  ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月   6 17:34 data

初始化数据库
[[email protected] local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

配置mysql
[[email protected] local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
"/usr/local/include/mysql" -> "/usr/local/mysql/include/"
[[email protected] local]# echo ‘/usr/local/mysql/lib‘ > /etc/ld.so.conf.d/mysql.conf
[[email protected] local]#  ldconfig -v

生成配置文件
[[email protected] local]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket  = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

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

启动mysql
[email protected] local]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/yanyinglai.err‘.
... SUCCESS!
[[email protected] local]# ps -ef | grep mysql
root      42077      1  0 17:57 pts/2    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     42255  42077 15 17:57 pts/2    00:00:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      42291   4672  0 17:57 pts/2    00:00:00 grep --color=auto mysql

[[email protected] local]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN      0      128     *:80                  *:*
LISTEN      0      128     *:22                  *:*
LISTEN      0      100    127.0.0.1:25                  *:*
LISTEN      0      128    :::22                 :::*
LISTEN      0      100       ::1:25                 :::*
LISTEN      0      80     :::3306               :::*                  

修改密码,使用临时密码登录
[[email protected] local]# mysql -uroot -p
Enter password:

mysql> set password = password(‘yanyinglai123‘);
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> exit
Bye
在192.168.55.130这台虚拟机配置两台tomcat项目部署
关闭防火墙和selinux
[[email protected] ~]#
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]#  setenforce 0
[[email protected] ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config

Java环境安装
//安装jdk环境
[[email protected] ~]#  yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

[[email protected] ~]#  java -version
openjdk version "1.8.0_131"
OpenJDK Runtime Environment (build 1.8.0_131-b12)
OpenJDK 64-Bit Server VM (build 25.131-b12, mixed mode)

tomcat部署
//下载tomcat
[[email protected] ~]# cd /usr/src/
[[email protected] src]#  wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

//创建tomcat存放目录
[[email protected] src]# cd /usr/local/
[[email protected] local]# mkdir tomacat{1,2}
[[email protected] local]# ls
bin  games    lib    libexec  sbin   src       tomacat2
etc  include  lib64  nginx    share  tomacat1

//将tomcat解压存放目录

[[email protected] local]# cd /usr/src/
[[email protected] src]# ls
apache-tomcat-9.0.8.tar.gz  debug  kernels
[[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat1
[[email protected] src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/tomacat2

//创建软连接
[[email protected] src]# cd /usr/local/tomacat1
[[email protected] tomacat1]# ln -s apache-tomcat-9.0.8/ tomcat1
[[email protected] tomacat1]# ll
总用量 0
drwxr-xr-x. 9 root root 160 9月   5 21:52 apache-tomcat-9.0.8
lrwxrwxrwx. 1 root root  20 9月   5 21:54 tomcat1 -> apache-tomcat-9.0.8/
[[email protected] tomacat1]# cd /usr/local/tomacat2
[[email protected] tomacat2]# ln -s apache-tomcat-9.0.8/ tomcat2
[[email protected] tomacat2]# ll
总用量 0
drwxr-xr-x. 9 root root 160 9月   5 21:52 apache-tomcat-9.0.8
lrwxrwxrwx. 1 root root  20 9月   5 21:55 tomcat2-> apache-tomcat-9.0.8/
创建测试目录并创建测试文件
[[email protected] ~]#  mkdir /usr/local/tomacat1/tomcat1/webapps/test
[[email protected] ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test
[[email protected] ~]# cd /usr/local/tomacat1/tomcat1/webapps/test
[[email protected] test]# vi index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hellow World");
%>
</body>
</html>
[[email protected] test]# cd /usr/local/tomacat2/tomcat2/webapps/test
[[email protected] test]# vi index.jsp
[[email protected] test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("8888888888");
%>
</body>
</html>

//修改tomcat2的端口,否则端口起冲突。配置文件为:server.xml
[[email protected] ~]# cd /usr/local/tomacat2/tomcat2/
[[email protected] tomcat2]# ls
bin   lib      logs    RELEASE-NOTES  temp     work
conf  LICENSE  NOTICE  RUNNING.txt    webapps
[[email protected] tomcat2]# cd conf/
[[email protected] conf]# ls
catalina.policy       jaspic-providers.xsd  tomcat-users.xsd
catalina.properties   logging.properties    web.xml
context.xml           server.xml
jaspic-providers.xml  tomcat-users.xml

[[email protected] conf]# vi server.xml
[[email protected] conf]# /usr/local//tomacat1/tomcat1/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomacat1/tomcat1
Using CATALINA_HOME:   /usr/local/tomacat1/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomacat1/tomcat1/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomacat1/tomcat1/bin/bootstrap.jar:/usr/local/tomacat1/tomcat1/bin/tomcat-juli.jar
Tomcat started.
[[email protected] conf]# /usr/local//tomacat2/tomcat2/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomacat2/tomcat2
Using CATALINA_HOME:   /usr/local/tomacat2/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomacat2/tomcat2/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomacat2/tomcat2/bin/bootstrap.jar:/usr/local/tomacat2/tomcat2/bin/tomcat-juli.jar
Tomcat started.

//输入192.168.55.130:8080 访问

//输入192.168.55.130:8080/test 访问

//第二台tomcat
//输入192.168.55.130:8081访问

//在主机192.168.55.129上搭建nginx使用nginx实现负载均衡,修改配置文件配置nginx实现动静分离
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf

upstream web {
      server 192.168.55.130:8080;
      server 192.168.55.130:8081;
}

定义好upstream后,需要在server段内添加如下内容
             location ~* \.(do|jsp)$ {
            proxy_pass http://web;
        }
        location ~* tomcat\.(png|css)$ {
            proxy_pass http://web;
        }
        location / {
                 root   html;
                 index index.html index.htm;
        }

修改完配置文件检查语法错误,并重启服务
[[email protected] ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[[email protected] ~]# nginx -s reload

静态资源访问nginx
//输入192.168.56.129访问

//动态资源访问tomcat


刷新一下

原文地址:http://blog.51cto.com/13910274/2171365

时间: 2024-10-05 17:45:17

LNMT架构部署:Linux+Nginx+Mysql+Tomcat(负载均衡,动静分离)的相关文章

nginx+tomcat负载均衡+动静分离

1.服务器A安装ng,服务器B.C安装tomcat: 2.服务器A建立/data/www目录,用于发布静态文件: 3.ng无动静分离配置: user root root; worker_processes 8; pid /usr/local/nginx/nginx.pid; worker_rlimit_nofile 102400; events { use epoll; worker_connections 102400; } http { include mime.types; default

nginx+apache实现负载均衡+动静分离配置(编译安装)

一.编译安装nginx cd /usr/local/src wget http://nginx.org/download/nginx-1.6.3.tar.gz tar -zxvf nginx-1.6.3.tar.gz cd nginx-1.6.3 ./configure --prefix=/usr/local/nginx --with-file-aio --with-http_ssl_module --with-http_image_filter_module --with-http_sub_m

nginx配置优化+负载均衡+动静分离详解

nginx配置如下: #指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes 8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志最少/var/log目录是linux下的日志存放目录error_log /var/log/nginx/nginx_error.log crit;#指定进程id的存储位置pid /var/run/nginx.pid;#绑定worker进程和CPU,linux内核2.4以上可用wor

[转] nginx配置优化+负载均衡+动静分离(附带参数解析)

#指定nginx进程运行用户以及用户组user www www;#nginx要开启的进程数为8worker_processes  8;#全局错误日志文件#debug输出日志最为详细,而crit输出日志最少/var/log目录是linux下的日志存放目录error_log /var/log/nginx/nginx_error.log crit;#指定进程id的存储位置pid   /var/run/nginx.pid;#绑定worker进程和CPU,linux内核2.4以上可用worker_rlim

Linux配置Nginx+Tomcat负载均衡

tar -zxvf nginx-1.14.2.tar.gz -C /usr/local 一.Linux配置Nginx 一.下载Nginx 方式1:从http://nginx.org/en/download.html上下载稳定版,解压安装 方式2:直接在Linux上用命令下载: wget http://nginx.org/download/nginx-1.10.2.tar.gz -bash: wget: command not found 安装wget: yum -y install wget 再

Nginx+Tomcat+Keepalived+Memcache 负载均衡动静分离技术

一.概述 Nginx 作负载均衡器的优点许多,简单概括为: ①实现了可弹性化的架构,在压力增大的时候可以临时添加Tomcat服务器添加到这个架构里面去; ②upstream具有负载均衡能力,可以自动判断下面的机器,并且自动踢出不能正常提供服务的机器: Keepalived 可实现 Nginx负载均衡器双机互备,任意一台机器发生故障,对方都能够将虚拟IP接管过去. Memcache可以实现Tomcat服务器的Sission共享整个拓补如下: 注意: 1.由于服务器有限,IP相同的为同一台机.只是端

nginx多tomcat负载均衡

目的 先说说我要干什么,如题:使用nginx实现多个tomcat服务器的负载均衡. nginx 大名鼎鼎,相信很多人都听过,以前感觉很厉害,用了之后发现真的很厉害.nginx可以做以下几件事: 反向代理 负载均衡 动静分离 网页.图片缓存 需要明确的是nginx并不是应用服务器,也就是说nginx不能处理动态请求(和apache一样,熟悉apache应该知道),比如jsp.php等页面,nginx把这些请求转发给能处理这些页面的程序. 反向代理 好吧,之前我也为这个问题纠结挺久,为什么叫反向?既

[转]Nginx+mysql+php-fpm负载均衡配置实例

转 : http://www.jbxue.com/article/7923.html 介绍一个nginx.mysql.php-fpm环境下配置负载均衡的例子,有需要的朋友,可以参考下. 系统环境如下:前端Nginx:192.168.93.137后端web1:192.168.0.11后端web2:192.168.0.12 1.前端nginx配置: 复制代码代码示例: http {      ……        client_max_body_size 300m;        client_bod

Nginx安装-反向代理-负载均衡-动静分离

安装 1.需要素材 后两个用命令下载安装 openssl-1.0.1t.tar.gzzlib -1.2.8.tar.gz 2:在/usr/src/ 下吧 " nginx-1.16.1.tar.gz " "pcre-8.37.tar.gz" 这两个文件放进去并且解压然后在pcre-8.37这个文件下先 : ./configure 在敲 make && make install pcre-conffig --verison 查看版本 下面安装nginx

HAproxy负载均衡动静分离实现及配置详解

 HAproxy负载均衡动静分离实现及配置详解 HAproxy的介绍 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上. HAProxy实现了一种事件驱动.单一进程