lnmt 简介

lnmt: 表示的是 linux + nginx + mysql + tomcat 的简称
Apache 为HTML页面服务,而Tomcat实际上运行JSP页面和Servlet,Tomcat实际上运行JSP页面和Servlet,Tomcat处理静态HTML的能力不如Apache服务器
了解更多Tomcat 内容请参考 Tomcat 文章
实验环境

服务器主机名IP
nginx-server192.168.169.30
tomcat-server192.168.169.20

安装yum源

[root@nginx-server ~]# cd /etc/yum.repos.d/
[root@nginx-server yum.repos.d]# curl -o CentOS7-Base-163.repo  http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@nginx-server yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@nginx-server yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@nginx-server yum.repos.d]# yum -y install epel-release

在tomcat-server 这台服务器上做以上同样的操作
关闭防火墙、关闭selinux

[root@nginx-server ~]# systemctl stop firewalld
[root@nginx-server ~]# systemctl disable firewalld
[root@nginx-server ~]# setenforce 0
[root@nginx-server ~]# sed -ri 's/(^SELINUX=).*/\1disabled/g' /etc/selinux/config 

安装Nginx
创建系统用户和组

[root@tomcat-server ~]# groupadd -r nginx  
[root@tomcat-server ~]# useradd -r -M -s /sbin/nologin -g nginx nginx

安装依赖环境

[root@nginx-server ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

安装开发包

[root@nginx-server ~]# yum -y groups mark install 'Development Tools'

建立存放日志目录,并且修改属主和属组

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

下载nginx

[root@nginx-server ~]# cd /usr/src/
[root@nginx-server src]# yum install -y wget vim 
[root@nginx-server src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz

编译安装nginx

[root@nginx-server src]# tar xf  nginx-1.12.0.tar.gz 
[root@nginx-server src]# cd nginx-1.12.0
[root@nginx-server 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_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@nginx-server nginx-1.12.0]# make  && make install

nginx 安装之后的配置
加入环境变量

[root@nginx-server ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx-server ~]# . /etc/profile.d/nginx.sh

启动nginx

[root@nginx-server ~]# nginx 
[root@nginx-server ~]# ss -anlt
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                                                 :::*   

安装mysql数据库

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

创建mysql 用户和组

[root@nginx-server ~]# groupadd -r mysql
[root@nginx-server ~]# useradd -r -M -s /sbin/nologin -g mysql mysql

下载mysql 二进制软件包

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

创建软连接

[root@nginx-server src]# cd /usr/local/
[root@nginx-server local]# ln -s mysql-5.7.22-linux-glibc2.12-x86_64/  mysql

修改属主和属组

[root@nginx-server local]# chown -R mysql.mysql /usr/local/mysql

添加至环境变量

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

创建数据存放的目录

[root@nginx-server local]# mkdir -p /opt/data
[root@nginx-server local]# chown mysql.mysql /opt/data/

初始化数据库

[root@nginx-server local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-09-05T19:23:25.077048Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-05T19:23:26.527447Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-05T19:23:26.690456Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-05T19:23:26.752569Z 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: 29d39f8b-b141-11e8-b789-000c291f67af.
2018-09-05T19:23:26.753611Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-05T19:23:26.754431Z 1 [Note] A temporary password is generated for root@localhost: 8V0rh,E(ExkL

保存密码

[root@nginx-server ~]# echo '8V0rh,E(Exk' >> 1.txt

配置mysql

[root@nginx-server ~]# ln  -s /usr/local/mysql/include/ /usr/local/include/mysql
[root@nginx-server ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf

生成配置文件

[root@nginx-server ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data/
socket = /tmp/mysql.sock
port = 3306
pid-file = /tmp/data/mysql.pid
user = mysql
skip-name-resolve
> EOF

创建存放pid 对应的文件,和/etc/my.cnf 里面的pid-file 对应的位置一样,并且修改属主和属组

[root@nginx-server ~]# mkdir /tmp/data
[root@nginx-server ~]# touch /tmp/data/mysql.pid
[root@nginx-server ~]# chown  -R mysql.mysql /tmp/data/

启动服务脚本

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

启动服务

[root@nginx-server ~]# service mysqld start 
Starting MySQL. SUCCESS

修改密码使用临时密码登录

[root@nginx-server ~]# cat 1.txt 
8V0rh,E(Exk
[root@nginx-server ~]# mysql -uroot -p 
Enter password:
mysql> set password = password('chen');
Query OK, 0 rows affected, 1 warning (0.01 sec)



在 tomcat-server 服务器上,安装tomcat

java环境安装
安装jdk 环境

[root@tomcat-server ~]#  yum -y install java-1.8.0-openjdk java-1.8.0-openjdk-devel 

查看安装的版本

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

解压部署tomcat

[root@tomcat-server src]# tar xf apache-tomcat-9.0.8.tar.gz  -C /usr/local/
[root@tomcat-server src]# cd /usr/local/
[root@tomcat-server local]# ln -s apache-tomcat-9.0.8/ tomcat

写一个’chens’的java 页面

[root@tomcat-server local]# cd /usr/local/tomcat/webapps/ROOT/
[root@tomcat-server ROOT]# mv index.jsp /opt/      //默认tomcat 访问的就是ROOT目录下的index.jsp
[root@tomcat-server ROOT]# vim index.jsp

<html>
<head>
        <title> test  page</title>
</head>
<body>
        <%
                out.println("chens");
        %>
</body> 
</html>

启动tomcat

[root@tomcat-server ROOT]# /usr/local/tomcat/bin/catalina.sh start
[root@tomcat-server ROOT]# ss -anlt
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      128                            :::22                                         :::*                  
LISTEN     0      100                           ::1:25                                         :::*                  
LISTEN     0      1                ::ffff:127.0.0.1:8005                                       :::*                  
LISTEN     0      100                            :::8009    

**在tomcat-server服务器上配置两个tomcat服务 **
配置第二个tomcat 服务

[root@tomcat-server ~]# cd /usr/local/
[root@tomcat-server local]# mkdir chens
[root@tomcat-server local]# cd /usr/src/
[root@tomcat-server src]# tar xf apache-tomcat-9.0.8.tar.gz -C /usr/local/chens/
[root@tomcat-server src]# cd /usr/local/
[root@tomcat-server local]# ln -s chens/apache-tomcat-9.0.8/   tomcat2

由于服务相同,会产生端口被占用的情况,所以需要修改tomcat2 的端口

[root@tomcat-server ~]# vim  /usr/local/tomcat2/conf/server.xml
<Server port="8015" shutdown="SHUTDOWN">   //这个地方改为8015端口
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />


    <Connector port="8050" protocol="HTTP/1.1"  //这个地方改为8050端口
               connectionTimeout="20000"
               redirectPort="8443" />

 <Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />    //这个地方改为8019端口

重启tomcat 服务

[root@tomcat-server ROOT]# /usr/local/tomcat/bin/catalina.sh stop
[root@tomcat-server ROOT]# /usr/local/tomcat/bin/catalina.sh start

在nginx-server服务器上对nginx 进行配置
在 nginx 上配置反向代理 和 负载均衡实现动静分离

[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
    #gzip  on;
    upstream web {                    实现负载
        server 192.168.169.20:8080;
        server 192.168.169.20:8050;
        }

    server {                        
        listen       80;
        server_name  localhost;

        access_log  logs/host.access.log  main;

	     location ~* \.(jsp|do)$ {          //以 .jsp 结尾的url 交给tomcat处理,实现动静分离
               proxy_pass http://web;
        }


       location  \ {                静态的文件交给nginx本身去处理   
          root   html;
          index index.jsp  index.html index.htm;
       }


重新加载nginx配置

[root@nginx-server ~]# nginx -s reload

在浏览器上测试,出现以下效果则成功

输入nginx 服务器的IP地址
这里写图片描述

再次访问或者刷新
这里写图片描述

Logo

更多推荐