LNMT架构部署:Linux+Nginx+Mysql+Tomcat

LNMT架构部署:Linux+Nginx+Mysql+Tomcat

LNMT架构图:

这里写图片描述

环境描述:

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

在IP为192.168.228.23的服务器上安装和配置nginx(这里我使用源码安装nginx)

关闭防火墙和selinux

[root@yxr ~]# systemctl stop firewalld
[root@yxr ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@yxr ~]# setenforce 0
[root@yxr ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config 

创建系统用户nginx

[root@yxr ~]# useradd -r -M -s /sbin/nologin nginx

安装依赖环境

[root@yxr ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@yxr ~]# yum -y groups mark install 'Development Tools'

创建日志存放目录

[root@yxr ~]# mkdir -p /var/log/nginx
[root@yxr ~]# chown -R nginx.nginx /var/log/nginx/

下载nginx

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

编译安装

[root@yxr src]# ls
debug  kernels  nginx-1.12.0.tar.gz
[root@yxr src]# tar xf nginx-1.12.0.tar.gz 
[root@yxr src]# cd nginx-1.12.0/
[root@yxr nginx-1.12.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@yxr nginx-1.12.0]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@yxr nginx-1.12.0]# . /etc/profile.d/nginx.sh 

启动nginx

[root@yxr nginx-1.12.0]# nginx 
[root@yxr nginx-1.12.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.228.23上安装mysql(这里我使用二进制安装)

安装依赖包

[root@yxr ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

创建用户和组

[root@yxr ~]# groupadd -r -g 306 mysql
[root@yxr ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

下载二进制格式的mysql软件包

[root@yxr ~]# cd /usr/src/
[root@yxr src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

解压软件至/usr/local

[root@yxr src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@yxr src]# cd /usr/local/
[root@yxr local]# ls
bin  games    lib    libexec                              nginx  share
etc  include  lib64  mysql-5.7.22-linux-glibc2.12-x86_64  sbin   src
[root@yxr local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@yxr local]# ll
total 0
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx.  1 root root  36 Sep  5 17:45 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Sep  5 17:45 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 151 Sep  5 17:29 nginx
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Aug 10 14:26 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src

修改目录/usr/local/mysql的属主属组

[root@yxr local]# chown -R mysql.mysql /usr/local/mysql
[root@yxr local]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Sep  5 17:45 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

添加环境变量

[root@yxr local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@yxr local]# . /etc/profile.d/mysql.sh 
[root@yxr local]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

建立数据存放目录

[root@yxr local]# mkdir /opt/data
[root@yxr local]# chown -R mysql.mysql /opt/data/
[root@yxr local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Sep  5 17:51 data

初始化数据库

[root@yxr local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-05T09:54:31.999648Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-05T09:54:34.926885Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-05T09:54:35.240951Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-05T09:54:35.314460Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: b1e771d6-b0f1-11e8-8761-000c29989243.
2018-09-05T09:54:35.317332Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-05T09:54:35.322159Z 1 [Note] A temporary password is generated for root@localhost: sQae/i06ukPC  //临时密码

配置mysql

[root@yxr local]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@yxr local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@yxr local]# ldconfig -v

生成配置文件

[root@yxr 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@yxr local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@yxr local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@yxr local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 

启动mysql

[root@yxr local]# service mysqld start
Starting MySQL.Logging to '/opt/data/yxr.err'.
. SUCCESS! 
[root@yxr local]# ps -ef | grep mysql
root      35507      1  0 18:05 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     35685  35507  3 18:05 pts/1    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=yxr.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      35717   1552  0 18:05 pts/1    00:00:00 grep --color=auto mysql
[root@yxr 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@yxr local]# mysql -uroot -p

mysql> set password = password('yaoxiaorong!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye

在192.168.228.30这台虚拟机配置两台tomcat项目部署

关闭防火墙和selinux

[root@arongya ~]# systemctl stop firewalld
[root@arongya ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@arongya ~]# setenforce 0
[root@arongya ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config

安装jdk环境

安装jdk环境
[root@arongya ~]# yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel
查看安装的版本
[root@arongya ~]# java -version
openjdk 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部署

下载tomcat

[root@arongya ~]# cd /usr/src/
[root@arongya src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz

解压部署,安装两个tomcat服务器,需要解压两次

[root@arongya src]# ls
apache-tomcat-9.0.8.tar.gz  debug  kernels
[root@arongya src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@arongya src]# cd /usr/local/
[root@arongya local]# ls
apache-tomcat-9.0.8  etc    include  lib64    sbin   src
bin                  games  lib      libexec  share
[root@arongya local]# mv apache-tomcat-9.0.8/ yaoxiaorong   //为了避免混淆两台,这里重新创建一个目录存放第一台tomcat
[root@arongya local]# ls yaoxiaorong/
bin   lib      logs    RELEASE-NOTES  temp     work
conf  LICENSE  NOTICE  RUNNING.txt    webapps
[root@arongya local]# cd /usr/src/
[root@arongya src]# ls
apache-tomcat-9.0.8.tar.gz  debug  kernels
[root@arongya src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/
[root@arongya src]# cd /usr/local/
[root@arongya local]# ls
apache-tomcat-9.0.8  etc    include  lib64    sbin   src
bin                  games  lib      libexec  share  yaoxiaorong

两台tomcat建立软链接

[root@arongya local]# ln -s yaoxiaorong/ tomcat
[root@arongya local]# ln -s apache-tomcat-9.0.8/ tomcat2
[root@arongya local]# ll
total 0
drwxr-xr-x. 9 root root 160 Sep  5 18:27 apache-tomcat-9.0.8
drwxr-xr-x. 2 root root   6 Nov  5  2016 bin
drwxr-xr-x. 2 root root   6 Nov  5  2016 etc
drwxr-xr-x. 2 root root   6 Nov  5  2016 games
drwxr-xr-x. 2 root root   6 Nov  5  2016 include
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib
drwxr-xr-x. 2 root root   6 Nov  5  2016 lib64
drwxr-xr-x. 2 root root   6 Nov  5  2016 libexec
drwxr-xr-x. 2 root root   6 Nov  5  2016 sbin
drwxr-xr-x. 5 root root  49 Aug 24 09:15 share
drwxr-xr-x. 2 root root   6 Nov  5  2016 src
lrwxrwxrwx. 1 root root  12 Sep  5 18:28 tomcat -> yaoxiaorong/
lrwxrwxrwx. 1 root root  20 Sep  5 18:29 tomcat2 -> apache-tomcat-9.0.8/
drwxr-xr-x. 9 root root 160 Sep  5 18:24 yaoxiaorong

修改其中一台tomcat配置文件,避免端口冲突,这里修改tomcat2的配置文件,配置文件在/usr/local/tomcat2/conf/server.xml

修改3个地方
...
[root@arongya local]# vim /usr/local/tomcat2/conf/server.xml 
<Server port="8006" shutdown="SHUTDOWN">   //将8005修改成8006或者其他
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
...
   <Connector port="8081" protocol="HTTP/1.1"  8080修改成8081或者其他
               connectionTimeout="20000"
               redirectPort="8444" />  8443修改成8444或者其他
...
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />   8009修改成8010 8443修改成8444

在/usr/local/tomcat/webapps和/usr/local/tomcat/webapps中写不同字样的java页面
tomcat

[root@arongya ~]# cd /usr/local/tomcat/webapps/
[root@arongya webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@arongya webapps]# mkdir test
[root@arongya webapps]# vim index.jsp
[root@arongya webapps]# cat index.jsp 
<html>
<head>
     <title>test page</title>
</head>
<bady>
    <%
      out.println("hello world");
    %>
</body>
</html>
[root@arongya webapps]# mv index.jsp test/

tomcat2

[root@arongya webapps]# cd /usr/local/tomcat2/webapps/
[root@arongya webapps]# ls
docs  examples  host-manager  manager  ROOT
[root@arongya webapps]# mkdir test
[root@arongya webapps]# cd test/
[root@arongya test]# vim index.jsp
[root@arongya test]# cat index.jsp 
<html>
<head>
     <title>test page</title>
</head>
<body>
    <%
      out.println("wwww.ddd");
    %>
</body>
</html>

开启2台tomcat服务

[root@arongya test]# /usr/local/tomcat/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@arongya test]# /usr/local/tomcat2/bin/catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat2
Using CATALINA_HOME:   /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
[root@arongya test]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128           *:22                        *:*                  
LISTEN     0      100    127.0.0.1:25                        *:*                  
LISTEN     0      100          :::8080                     :::*                  
LISTEN     0      100          :::8081                     :::*                  
LISTEN     0      128          :::22                       :::*                  
LISTEN     0      100         ::1:25                       :::*                  
LISTEN     0      1      ::ffff:127.0.0.1:8005                     :::*                  
LISTEN     0      1      ::ffff:127.0.0.1:8006                     :::*                  
LISTEN     0      100          :::8009                     :::*                  
LISTEN     0      100          :::8010                     :::*        

验证:浏览器分别输入192.168.228.30:8081/test和192.168.228.30:8080/test,能访问说明部署成功
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

配置nginx

在nginx配置文件修改配置文件,配置负载均衡和两个tomcat的反向代理


    sendfile        on;
    keepalive_timeout  65;

        upstream web {   //添加此内容,实现负载均衡
          server 192.168.228.30:8080;
          server 192.168.228.30:8081;
        }

    server {
        listen       80;
        server_name  localhost;


        location / {
            root   html;
            index  index.html index.htm;
        }

        location ~* \.(jsp|do)$ {    \\添加此行内容,实现动静分离
             proxy_pass http://web;
        }

检查语法并加载nginx

[root@yxr local]# vim /usr/local/nginx/conf/nginx.conf
[root@yxr local]# 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@yxr local]# nginx -s reload

在浏览器验证

这里写图片描述
这里写图片描述
这里写图片描述

发布了12 篇原创文章 · 获赞 8 · 访问量 5388
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术工厂 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览