美文网首页
部署LNMT

部署LNMT

作者: 唯爱熊 | 来源:发表于2019-10-25 15:25 被阅读0次

    背景信息

    . 操作系统: CentOS 7.6
    .Tomcat版本:Tomcat 9.0.27
    .JDK版本: JDK 1.8.0_231

    下载安装包

    1.下载Aapche Tomcat.
    登录https://tomcat.apache.org官方网站进行下载,这里下载使用的是Tomcat 9.0.27,你可以根据需求自行下载所需的版本。

    [root@web01 ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.27/bin/apache-tomcat-9.0.27.tar.gz
    

    2.下载JDK
    jDK有开源的openjdk也有orcal提供的jdk,centos源默认自带openjdk,你可以使用yum进行安装,这里登录https://www.oracle.com/technetwork/cn/java/javase/downloads进行下载,这里下载使用的是JDK 1.8.0_231(下载到本地之后再上传),你可以根据需求自行下载所需的版本。

    安装前准备

    1.关闭防火墙
    1.1查看防火墙的状态

    [root@web01 ~]# systemctl status firewalld.service
    ● firewalld.service - firewalld - dynamic firewall daemon
       Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
       Active: active (running) since Fri 2019-10-25 15:53:56 CST; 32s ago
         Docs: man:firewalld(1)
     Main PID: 7564 (firewalld)
       CGroup: /system.slice/firewalld.service
               └─7564 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
    
    Oct 25 15:53:56 web01 systemd[1]: Starting firewalld - dynamic firewall daemon...
    Oct 25 15:53:56 web01 systemd[1]: Started firewalld - dynamic firewall daemon.
    

    .如果防火墙的状态是inactive,则防火墙为关闭状态。
    .如果防火墙的状态参数为active,则防火墙为开启状态本实例为开启状态,这里需要关闭防护墙。
    1.2关闭防火墙。如果防火墙为关闭状态可以忽略此步骤。
    .如果您想临时关闭防火墙,运行命令systemctl stop firewalld。
    .如果您想永久关闭防火墙,运行命令systemctl disable firewalld。
    2.关闭selinux。
    2.1运行命令getenforce查看SELinux的当前状态

    [root@web01 ~]# getenforce
    Enforcing
    

    .如果SELinux状态参数是Disabled, 则SELinux为关闭状态。
    .如果SELinux状态参数是Enforcing,则SELinux为开启状态。本示例中SELinux为开启状态,因此需要关闭SELinux。
    2.1.1关闭SELinux。如果SELinux为关闭状态可以忽略此步骤。
    .如果您想临时关闭SELinux,运行命令setenforce 0。
    .如果您想永久关闭SELinux,运行命令vi /etc/selinux/config编辑SELinux配置文件。回车后,把光标移动到SELINUX=enforcing这一行,按i键进入编辑模式,修改为SELINUX=disabled, 按Esc键,然后输入:wq并回车来保存并关闭SELinux配置文件。
    3.重启系统使设置生效。
    4.创建一般用户www来运行Tomcat。

    [root@web01 ~]# useradd www
    [root@web01 ~]# id www
    uid=1000(www) gid=1000(www) groups=1000(www)
    

    5.创建网站根目录

    [root@web01 ~]# mkdir -p /data/jpress
    

    6.修改网站根目录下的文件权限为www。

    [root@web01 ~]# chown -R www.www /data/jpress
    

    安装JDK

    1.创建JDK的安装目录

    [root@web01 ~]# mkdir /usr/java
    

    2.上传下载的JDK包依次运行以下命令解压jdk-8u231-linux-x64.tar.gz到/usr/java

    [root@web01 ~]# chmod +x jdk-8u231-linux-x64.tar.gz 
    [root@web01 ~]# tar xzf jdk-8u231-linux-x64.tar.gz -C /usr/java
    

    3.设置环境变量

    [root@web01 /usr/java]# echo -e "# set java environment\nexport JAVA_HOME=/usr/java/jdk1.8.0_231\nexport CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib\nexport PATH=$JAVA_HOME/bin:$PATH">>/etc/profile
    

    4.运行如下命令加载环境变量。

    [root@web01 /usr/java]# source /etc/profile
    

    5.运行java --version命令显示JDK版本信息。
    返回结果如图所示,表示JDK已经安装成功。

    [root@web01 ~]# java -version
    java version "1.8.0_231"
    Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
    Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
    

    安装Tomcat

    1.依次运行以下命令解压apache-tomcat-9.0.27.tar.gz,重命名为tomcat目录,并设置用户权限。

    [root@web01 ~]# tar xf apache-tomcat-9.0.27.tar.gz 
    [root@web01 ~]# mv apache-tomcat-9.0.27 /usr/local/tomcat
    [root@web01 ~]# chown -R www.www /usr/local/tomcat
    

    在/usr/local/tomcat/目录下:
    bin:存放Tomcat的一些脚本文件,包含启动和关闭Tomcat服务脚本。
    conf:存放Tomcat服务器的各种全局配置文件,其中最重要的是server.xml和web.xml。
    webapps:Tomcat的主要Web发布目录,默认情况下把Web应用文件放于此目录。
    logs:存放Tomcat执行时的日志文件。
    2.配置server.xml文件。
    2.1运行以下命令切换到/usr/local/tomcat/conf/目录。

    [root@web01 ~]# cd /usr/local/tomcat/conf/
    

    2.2运行以下命令重命名server.xml文件。

    [root@web01 /usr/local/tomcat/conf]# mv server.xml server.xml.bak
    

    2.3新建一个server.xml文件。
    a.运行命令vi server.xml创建server.xml文件。
    b. 按下i 键,添加以下内容。

    <?xml version="1.0" encoding="UTF-8"?>
    <Server port="8006" shutdown="SHUTDOWN">
    <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/>
    <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/>
    <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/>
    <Listener className="org.apache.catalina.core.AprLifecycleListener"/>
    <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
     type="org.apache.catalina.UserDatabase"
     description="User database that can be updated and saved"
     factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
     pathname="conf/tomcat-users.xml"/>
    </GlobalNamingResources>
    <Service name="Catalina">
    <Connector port="8080"  #port:定义下面<host></host>的监听端口是8080
     protocol="HTTP/1.1"
     connectionTimeout="20000"
     redirectPort="8443"
     maxThreads="1000"
     minSpareThreads="20"
     acceptCount="1000"
     maxHttpHeaderSize="65536"
     debug="0"
     disableUploadTimeout="true"
     useBodyEncodingForURI="true"
     enableLookups="false"
     URIEncoding="UTF-8"/>
    <Engine name="Catalina" defaultHost="localhost">#defaultHost:定义该端口下,默认虚拟主机是那个一个
    <Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
      resourceName="UserDatabase"/>
    </Realm>
    <Host name="localhost" appBase="/data/jpress" unpackWARs="true" autoDeploy="true">#appBase:定义该应用存放路径,可以把war包直接上传到该目录,实现自动化部署
    #autoDeploy:定义是否可以自动部署
    #name:绑定站点的域名
    #unpackWARS:定义是否自动解压war包
    <Context path="" docBase="/data/jpress" debug="0" reloadable="true" crossContext="true"/>
    #path:定义本“Context”访问时的地址,比如定义path为123,则访问该“Context”时,应该是http://host/123/,所以这个地址是访问链接里面的地址,并不是系统目录
    #docBase:定义本“context”(类似nginx的location)所在路径,它是一个目录,如果是绝对路径就与appBase没有关系,如果是相对路径,则指的是相对appBase的一个目录
    #reloadable:定义本“Context”是否允许自动加载,即当前应用有所改变时,不重启tomcat就可以自动加载
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log." suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
    </Engine>
    </Service>
    </Server>
    说明:tomcat的网站目录设置比较麻烦,appBase、docBase和Context的path的设置都对网站目录有影响,建议appBase最好不要动,即webapps目录,而docBases设置一个相对路径(相对webapps的一个目录),path就设定位“/”,最终结合在一起,就是webapps/docBasesh设置的目录。
    

    c.按ESC键,输入:wq并回车以保存并关闭文件。
    3.设置JVM内存参数。
    a.运行vi /usr/local/tomcat/bin/setenv.sh命令创建/usr/local/tomcat/bin/setenv.sh文件。
    b.按下i键,添加以下内容。

    JAVA_OPTS='-Djava.security.egd=file:/dev/./urandom -server -Xms256m -Xmx496m -Dfile.encoding=UTF-8'
    

    c. 按下Esc键,输入:wq并回车以保存并关闭文件。
    4.启动Tomcat并设置开机自启动
    这里根据系统的不同启动方式有两种:
    a. 适用于CentOS6及其以前的版本,设置Tomcat自启动脚本。
    i.运行以下命令下载脚本。

    [root@web01 ~]# wget https://github.com/lj2007331/oneinstack/raw/master/init.d/Tomcat-init
    

    ii.运行以下命令重命名Tomcat-init。

    [root@web01 ~]# mv Tomcat-init /etc/init.d/tomcat
    

    iii.运行以下命令为/etc/init.d/tomcat添加可执行权限。

    [root@web01 ~]# chmod +x /etc/init.d/tomcat
    

    iv.运行以下命令设置启动脚本JAVA_HOME。

    [root@web01 ~]# sed -i 's@^export JAVA_HOME=.*@export JAVA_HOME=/usr/java/jdk1.8.0_231@' /etc/init.d/tomcat
    

    v.依次运行以下命令设置Tomcat开机自启动。

    [root@web01 ~]#chkconfig --add tomcat
    [root@web01 ~]#chkconfig tomcat on
    

    vi.运行以下命令启动Tomcat。

    [root@web01 ~]#service tomcat start  
    

    b. 利用systemctl管理Tomcat启动、停止、重启及开机启动。
    i.用service来管理服务的时候,是在/etc/init.d/目录中创建一个脚本文件,来管理服务的启动和停止,在systemctl中,也类似,文件目录有所不同,在/usr/lib/systemd/system目录下创建一个脚本文件tomcat,
    i.使用 vi /usr/lib/systemd/system/tomcat.service创建tomcat.service脚本。按i输入如下内容:

    [Unit]
    Description=Tomcat
    After=network.target
    
    [Service]
    Type=forking
    PIDFile=/usr/local/tomcat/pid
    ExecStart=/usr/local/tomcat/bin/catalina.sh start
    ExecReload=/usr/local/tomcat/bin/catalina.sh restart
    ExecStop=/usr/local/tomcat/bin/catalina.sh stop
    
    [Install]
    WantedBy=multi-user.target
    [Unit] 表示这是基础信息
    Description 是描述
    After 是在那个服务后面启动,一般是网络服务启动后启动
    [Service] 表示这里是服务信息
    Type 是服务类型
    PIDFile 是服务的pid文件路径, 开启后,必须在tomcat的bin/catalina.sh中加入CATALINA_PID参数
    ExecStart 是启动服务的命令
    ExecReload 是重启服务的命令
    ExecStop 是停止服务的指令
    [Install] 表示这是是安装相关信息
    WantedBy 是以哪种方式启动:multi-user.target表明当系统以多用户方式(默认的运行级别)启动时,这个服务需要被自动运行。
    

    ii.在/usr/local/tomcat/bin/catalina.sh中加入CATALINA_PID参数以及JAVA_HOME的路径参数,需要在# OS specific support.$var must be set to either true or false.上加入如下参数

    CATALINA_PID=/usr/local/tomcat/pid
    export JAVA_HOME=/usr/java/jdk1.8.0_231
    export JAVA_HOME=/usr/java/jdk1.8.0_231/jre
    注意:rpm安装的JDK无需写入java_home参数
    

    使用vi /usr/local/tomcat/bin/catalina.sh在# OS specific support. $var must be set to either true or false这行下添加如上的参数。
    iii.创建软链接
    创建软链接是为了下一步系统初始化时自动启动服务

    ln -s /lib/systemd/system/tomcat.service /etc/systemd/system/multi-user.target.wants/tomcat.service
    

    ln -s 是创建软链接
    ln -s 原文件 目标文件(快捷方式的决定地址)
    如果创建软连接的时候出现异常,不要担心,看看/etc/systemd/system/multi-user.target.wants/ 目录是否正常创建软链接为准,有时候报错只是提示一下,其实成功了。

    $ ll /etc/systemd/system/multi-user.target.wants/
    total 8
    drwxr-xr-x 2 root root 4096 Mar 30 15:46 ./
    drwxr-xr-x 13 root root 4096 Mar 13 14:18 ../
    lrwxrwxrwx 1 root root 31 Nov 23 14:43 tomcat.service -> /lib/systemd/system/tomcat.service
    ...略...
    iv. 刷新配置
    刚刚配置的服务需要让systemctl能识别,就必须刷新配置

    [root@web01 ~]#systemctl daemon-reload
    

    如果没有权限可以使用sudo

    sudo systemctl daemon-reload
    

    v.启动,重启,停止。
    启动tomcat

    [root@web01 ~]#systemctl start tomcat
    

    重启tomcat

    [root@web01 ~]#systemctl restart tomcat
    

    停止tomcat

    [root@web01 ~]#systemctl stop tomcat
    

    5.tomcat 开机自启动
    服务加入开机启动

    [root@web01 ~]#systemctl enable tomcat
    

    禁止开机启动

    [root@web01 ~]#systemctl disable tomcat
    
    1. 查看状态
    [root@web01 ~]#systemctl status tomcat
    ● tomcat.service - Tomcat
       Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
       Active: active (running) since Fri 2019-10-25 20:37:22 CST; 1min 42s ago
      Process: 8673 ExecStart=/usr/local/tomcat/bin/catalina.sh start (code=exited, status=0/SUCCESS)
     Main PID: 8679 (java)
       CGroup: /system.slice/tomcat.service
               └─8679 /usr/java/jdk1.8.0_231/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/...
    
    Oct 25 20:37:22 web01 systemd[1]: Starting Tomcat...
    Oct 25 20:37:22 web01 catalina.sh[8673]: Tomcat started.
    Oct 25 20:37:22 web01 systemd[1]: Started Tomcat.
    

    安装nginx

    1.使用nginx官方源进行安装。
    i.使用vi /etc/yum.repos.d/nginx.repo创建nginx.repo源文件。
    按下i键,添加以下内容。

    [nginx-stable]
    name=nginx stable repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=1
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    
    [nginx-mainline]
    name=nginx mainline repo
    baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
    gpgcheck=1
    enabled=0
    gpgkey=https://nginx.org/keys/nginx_signing.key
    module_hotfixes=true
    

    ii.按下Esc键,输入:wq并回车以保存并关闭文件。
    iii.下载安装nginx。

    [root@web01 ~]# yum -y install nginx
    

    iv.启动nginx并设置开机自启动

    [root@web01 ~]#systemctl start nginx
    [root@web01 ~]#systemctl enable nginx
    

    安装mariadb数据库

    i.使用yum方式安装mariadb数据库。

    [root@web01 ~]# yum install mariadb-server mariadb -y
    

    ii.启动mariadb数据库并添加入开机自启动。

    [root@web01 ~]# systemctl start mariadb
    [root@web01 ~]# systemctl enable mariadb
    

    iii.配置mariadb的账号和密码并做初步的优化
    注意:此处除了设置新密码,其他操作默认回车即可。

    [root@web01 ~]# mysql_secure_installation
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MariaDB to secure it, we'll need the current
    password for the root user.  If you've just installed MariaDB, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none): 
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MariaDB
    root user without the proper authorisation.
    
    Set root password? [Y/n] 
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    
    By default, a MariaDB installation has an anonymous user, allowing anyone
    to log into MariaDB without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] 
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] 
     ... Success!
    
    By default, MariaDB comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] 
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] 
     ... Success!
    
    Cleaning up...
    
    All done!  If you've completed all of the above steps, your MariaDB
    installation should now be secure.
    
    Thanks for using MariaDB!
    

    部署博客jpress

    1.配置nginx代理。
    i.使用vi /etc/nginx/conf.d/proxy.conf 创建代理主机。
    按i输入如下内容:

    server {
     listen 80;
     server_name jpress.weiaixiong.com;
     location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
        }
     }
    

    ii.按下Esc键,输入:wq并回车以保存并关闭文件。
    iii.使用nginx -t 检查是否有错。

    [root@web01 ~]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    

    iv.重新启动nginx

    [root@web01 ~]# systemctl restart ngnx
    

    2.创建数据库

    [root@web01 /data/jpress]# mysql -uroot -p123456
    MariaDB [(none)]> create database jpress;
    Query OK, 1 row affected (0.00 sec)
    

    3.上传源码至站点目录/data/jpress。

    [root@web01 ~]# cd /data/jpress/
    [root@web01 /data/jpress]# rz -E
    rz waiting to receive.
    [root@web01 /data/jpress]# ls
    jpress-v2.0.8.war
    [root@web01 /data/jpress]# systemctl restart tomcat.service
    [root@web01 /data/jpress]# ls
    jpress-v2.0.8  jpress-v2.0.8.war
    [root@web01 /data/jpress]# cd jpress-v2.0.8/
    [root@web01 /data/jpress/jpress-v2.0.8]# ls
    META-INF  robots.txt  static  templates  WEB-INF
    [root@web01 /data/jpress/jpress-v2.0.8]# cp -a ./* /data/jpress/
    

    4.测试访问并进一步连接数据路进行安装。


    5.安装完成后测试结果如图。


    相关文章

      网友评论

          本文标题:部署LNMT

          本文链接:https://www.haomeiwen.com/subject/grgsvctx.html