当前位置: 代码迷 >> 综合 >> 【Linux】Linux Web服务器部署LNMT(Linux+Ngnix+MySQL/Mariadb+Tomcat)
  详细解决方案

【Linux】Linux Web服务器部署LNMT(Linux+Ngnix+MySQL/Mariadb+Tomcat)

热度:95   发布时间:2023-12-17 14:22:27.0

Linux Web服务器部署LNMT(Linux+Ngnix+MySQL/Mariadb+Tomcat)

操作系统:中标麒麟服务器端操作系统(方法与Cenos相似)

目录

1.安装tomcat

 2.tomcat应用部署

3.安装Nginx

4.同一个项目启动2个tomcat

 5.配置Nginx实现负载均衡

6.MySQL/Mariadb安装


1.安装tomcat

官网下载

https://tomcat.apache.org/

下载tomcat 8
选择Binary Distributions->Core->tar.gz
apache-tomcat-8.5.53.tar.gz

下载好之后

root$ cp apache-tomcat-8.5.53.tar.gz /opt/
root$ cd /opt
root$ tar xf apache-tomcat-8.5.53.tar.gz
root$ rm apache-tomcat-8.5.53.tar.gz
root$ cd apache-tomcat-8.5.53/bin
root$ ./catalina.sh run

运行之后看到下面信息说明启动成功

23-Mar-2020 17:27:43.021 信息 [main] org.apache.catalina.startup.Catalina.start

Server startup in 3181 ms

 2.tomcat应用部署

root$ cd /opt/apache-tomcat-8.5.53/webapps/
root$ cp docs/appdev/sample/sample.war ./
root$ cd ../bin/
root$ ./shutdown.sh
root$ ./catalina.sh run

我们可以通过访问http://localhost:8080进行访问,远程可以通过http://服务器ip地址:8080

如果远程访问出错,可能是防火墙,设置防火墙开放8080端口访问

firewall-cmd --zone=public --add-port=8080/tcp --permanent
systemctl restart firewalld

3.安装Nginx

yum install nginx -y

启动nginx之前,必须保证apache服务为关闭状态,否则nginx启动会失败,因为apache和nginx默认端口都为80

systemctl stop httpd
systemctl start ngnix

我们可以通过http://localhost进行访问

配置Nginx实现反向代理和负载均衡

4.同一个项目启动2个tomcat

再复制一份tomcat,并修改端口号等,进行启动

cd /opt
cp -r apache-tomcat-8.5.53 apache-tomcat-8.5.53-copy2

 修改apache-tomcat-8.5.53-copy2/conf/server.xml,修改三个端口号

将对外端口改为8081,保证和apache-tomcat-8.5.53的对外端口不发生冲突,

    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8081
    -->
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

把8005改成8006

<Server port="8006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

把8009改成8010

    <!-- Define an AJP 1.3 Connector on port 8010 -->
    <!--
    <Connector protocol="AJP/1.3"
               address="::1"
               port="8010"
               redirectPort="8443" />
    -->
 

 

 5.配置Nginx实现负载均衡

vi /etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    upstream tomcat_server{
      #ip_hash;
      server 127.0.0.1:8080 weight=1;
      server 127.0.0.1:8081 weight=1;


    }


    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

    location ~*/sample{
      proxy_next_upstream http_502 http_504 error timeout invalid_header;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_pass http://tomcat_server;


    }

    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

 配置文件里添加红色部分

systemctl restart nginx

通过查看日志来查看浏览器请求次数

tail -f /opt/apache-tomcat-8.5.53/logs/localhost_access_log.2020-03-23.txt 

6.MySQL/Mariadb安装

见https://blog.csdn.net/rong11417/article/details/105044750

 

 

  相关解决方案