CentOS7搭建Java環境(JDK、MySQL和Tomcat)

MrDarren發表於2020-05-24

安裝jdk

準備

  1. jdk的tar.gz包(本文準備的版本是jdk-8u212-linux-x64.tar.gz)
  2. 通過xftp上傳到/opt/software下

jdk環境配置流程

  1. 解壓到/opt/modules目錄下
    tar -zxvf jdk-8u212-linux-x64.tar.gz -C /opt/modules
  2. 進入解壓目錄下並列印路徑
    cd /opt/modules/jdk1.8.0_212
    pwd
    在這裡插入圖片描述
  3. 修改環境變數
    sudo vim /etc/profile
    或使用root賬戶
    在檔案末尾新增環境變數
    JAVA_HOME=/opt/modules/jdk1.8.0_212  
    PATH=$JAVA_HOME/bin:$PATH  
    export JAVA_HOME PATH  
    
    在這裡插入圖片描述
  4. 配置檔案立即生效
    在這裡插入圖片描述

安裝MySQL 5.6

準備

  1. mysql的原始碼tar.gz包(本文準備的版本是mysql-5.6.44.tar.gz)
  2. 通過xftp上傳到/opt/software下

MySQL安裝流程

  1. 解壓到modules目錄下
    tar -zxvf mysql-5.6.44.tar.gz -C /opt/modules/
  2. 解除安裝舊版本
    rpm -qa | grep mysql
    rpm -e mysql_libs(普通刪除)或rpm -e --nodeps mysql_libs(強力刪除)
  3. 安裝編譯程式碼需要的包
    yum -y install make gcc-c++ cmake bison-devel ncurses-devel
  4. 編譯原始碼包(這是一條命令)
    cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
  5. 編譯並安裝
    make && make install
  6. 新建mysql使用者組和使用者
    groupadd mysql
    useradd -g mysql mysql
  7. 修改/usr/local/mysql許可權
    chown -R mysql:mysql /usr/local/mysql
  8. 初始化配置
    進入安裝路徑,執行初始化配置指令碼,建立系統自帶的資料庫和表
    cd /usr/local/mysql
    scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
  9. 新增服務,拷貝服務指令碼到init.d目錄,並設定開機啟動
    在/usr/local/mysql下執行
    cp support-files/mysql.server /etc/init.d/mysql
    chkconfig mysql on
    service mysql start
  10. 修改root密碼
    cd /usr/local/mysql/bin
    ./mysql -uroot
    SET PASSWORD = PASSWORD('密碼');
    在這裡插入圖片描述
  11. 允許遠端連線
    update user set host = '%' where user = 'root';
  12. 開發埠
    • 檢視已開放埠
      firewall-cmd --zone=public --list-ports
    • 開放埠
      firewall-cmd --zone=public --add-port=3306/tcp --permanent
    • 關閉埠
      firewall-cmd --zone=public --remove-port=3306/tcp --permanent
    • 重啟防火牆
      firewall-cmd --reload

安裝Tomcat 7

準備

  1. tomcat的tar.gz包(本文準備的版本是apache-tomcat-7.0.93.tar.gz)
  2. 通過xftp上傳到/opt/software下

Tomcat安裝流程

  1. 解壓到modules目錄下
    tar -zxvf apache-tomcat-7.0.93.tar.gz -C /opt/modules/
  2. 進入conf目錄修改配置
    cd /opt/modules/apache-tomcat-7.0.93/conf
    • 初始化管理員賬號
      <?xml version='1.0' encoding='utf-8'?>
      <tomcat-users>
      	<role rolename="manager-gui"/>
      	<role rolename="manager-script" />
      	<user username="daye" password="luyuhao2010" roles="manager-gui, manager-script"/>
      </tomcat-users>
      
    • 優化tomcat配置
      具體配置引數連結1
      <?xml version='1.0' encoding='utf-8'?>
      <Server port="8006" shutdown="SHUTDOWN">
        <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
        <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
        <Listener className="org.apache.catalina.core.JasperListener" />
        <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
        <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
        <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
      
        <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">
      	<Executor name="tomcatThreadPool" 
              namePrefix="catalina-exec-"
              maxThreads="500" 
              minSpareThreads="30" 
              maxQueueSize="100"
              maxIdleTime="60000"
              prestartminSpareThreads = "true"/>
          <Connector port="8081" 
      		 protocol="org.apache.coyote.http11.Http11NioProtocol"
               executor="tomcatThreadPool"
               connectionTimeout="60000"
               maxConnections="10000"
               enableLookups="false"
               acceptCount="100"
               maxPostSize="10485760"
               redirectPort="8443"
               maxHttpHeaderSize="8192"
               compression="on"
               disableUploadTimeout="true"
               compressionMinSize="2048"
               acceptorThreadCount="2"
               compressableMimeType="text/html,text/plain,text/css,application/javascript,application/json,application/x-font-ttf,application/x-font-otf,image/svg+xml,image/jpeg,image/png,image/gif,audio/mpeg,video/mp4"
               URIEncoding="utf-8"
               processorCache="20000"
               tcpNoDelay="true"
               connectionLinger="5"
               server="Server Version 11.0"  />
          <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
          <Engine name="Catalina" defaultHost="localhost">
            <Realm className="org.apache.catalina.realm.LockOutRealm">
              <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
                     resourceName="UserDatabase"/>
            </Realm>
            <Host name="localhost"  appBase="webapps"
                  unpackWARs="true" autoDeploy="true">
              <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>
      
  3. tomcat啟動/關閉
    • cd /opt/modules/apache-tomcat-7.0.93/bin
    • 啟動:./startup.sh
    • 關閉:./shutdown.sh

參考連結

  1. tomcat配置說明參考連結

相關文章