码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-09-06 23:56:37      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:contex   防火墙   app   配置nginx   develop   text   bfd   etc   cmak   

环境描述:虚拟机准备两台,一台作为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
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# systemctl disable firewalld
[root@yanyinglai ~]# setenforce 0
[root@yanyinglai ~]#  sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config

创建系统用户nginx
[root@yanyinglai ~]#  useradd -r -M -s /sbin/nologin nginx

安装依赖环境
[root@yanyinglai ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

[root@yanyinglai ~]# yum -y groups install ‘Development Tools‘

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

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

编译安装
[root@yanyinglai ~]# cd /usr/src/
[root@yanyinglai src]# ls
debug  kernels  nginx-1.14.0.tar.gz
[root@yanyinglai src]# tar xf nginx-1.14.0.tar.gz
[root@yanyinglai src]# cd nginx-1.14.0
[root@yanyinglai nginx-1.14.0]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@yanyinglai 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

[root@yxr nginx-1.12.0]# make -j 2 && make install

nginx安装后配置配置环境变量
[root@yanyinglai nginx-1.14.0]# echo ‘export PATH=/usr/local/nginx/sbin:$PATH‘ > /etc/profile.d/nginx.sh
[root@yanyinglai nginx-1.14.0]#  . /etc/profile.d/nginx.sh

启动nginx
[root@yanyinglai nginx-1.14.0]# nginx
[root@yanyinglai 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
安装依赖包
[root@yanyinglai ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

创建用户和组
[root@yanyinglai ~]# groupadd -r -g 306 mysql
[root@yanyinglai ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

下载二进制格式的mysql软件包
[root@yanyinglai ~]# cd /usr/src/
[root@yanyinglai src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

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

[root@yanyinglai src]# cd /usr/local/
[root@yanyinglai local]# ls
bin    include  libexec                              sbin
etc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  share
games  lib64    nginx                                src
[root@yanyinglai local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"
[root@yanyinglai 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的属主属组
[root@yanyinglai local]# chown -R mysql.mysql /usr/local/mysql
[root@yanyinglai 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/

添加环境变量
[root@yanyinglai local]# echo ‘export PATH=/usr/local/mysql/bin:$PATH‘ > /etc/profile.d/mysql.sh
[root@yanyinglai local]# . /etc/profile.d/mysql.sh
[root@yanyinglai local]#  echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

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

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

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

生成配置文件
[root@yanyinglai 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

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

启动mysql
root@yanyinglai local]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/yanyinglai.err‘.
... SUCCESS!
[root@yanyinglai 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

[root@yanyinglai 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               :::*                  

修改密码,使用临时密码登录
[root@yanyinglai 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
[root@yanyinglai ~]#
[root@yanyinglai ~]# systemctl stop firewalld
[root@yanyinglai ~]# systemctl disable firewalld
[root@yanyinglai ~]#  setenforce 0
[root@yanyinglai ~]# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/selinux/config

Java环境安装
//安装jdk环境
[root@yanyinglai ~]#  yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel

[root@yanyinglai ~]#  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
[root@yanyinglai ~]# cd /usr/src/
[root@yanyinglai src]#  wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

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

//将tomcat解压存放目录

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

//创建软连接
[root@yanyinglai src]# cd /usr/local/tomacat1
[root@yanyinglai tomacat1]# ln -s apache-tomcat-9.0.8/ tomcat1
[root@yanyinglai 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/
[root@yanyinglai tomacat1]# cd /usr/local/tomacat2
[root@yanyinglai tomacat2]# ln -s apache-tomcat-9.0.8/ tomcat2
[root@yanyinglai 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/
创建测试目录并创建测试文件
[root@yanyinglai ~]#  mkdir /usr/local/tomacat1/tomcat1/webapps/test
[root@yanyinglai ~]# mkdir /usr/local/tomacat2/tomcat2/webapps/test
[root@yanyinglai ~]# cd /usr/local/tomacat1/tomcat1/webapps/test
[root@yanyinglai test]# vi index.jsp
[root@yanyinglai test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("Hellow World");
%>
</body>
</html>
[root@yanyinglai test]# cd /usr/local/tomacat2/tomcat2/webapps/test
[root@yanyinglai test]# vi index.jsp
[root@yanyinglai test]# cat index.jsp
<html>
<head>
<title>test page</title>
</head>
<body>
<%
out.println("8888888888");
%>
</body>
</html>

//修改tomcat2的端口,否则端口起冲突。配置文件为:server.xml
[root@yanyinglai ~]# cd /usr/local/tomacat2/tomcat2/
[root@yanyinglai tomcat2]# ls
bin   lib      logs    RELEASE-NOTES  temp     work
conf  LICENSE  NOTICE  RUNNING.txt    webapps
[root@yanyinglai tomcat2]# cd conf/
[root@yanyinglai 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

[root@yanyinglai conf]# vi server.xml
[root@yanyinglai 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.
[root@yanyinglai 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实现动静分离
[root@yanyinglai ~]# 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;
        }

修改完配置文件检查语法错误,并重启服务
[root@yanyinglai ~]# 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

[root@yanyinglai ~]# nginx -s reload

静态资源访问nginx
//输入192.168.56.129访问
技术分享图片

//动态资源访问tomcat
技术分享图片
技术分享图片
刷新一下
技术分享图片

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

标签:contex   防火墙   app   配置nginx   develop   text   bfd   etc   cmak   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!