Ubuntu安裝Tomcat以及Mysql(Javaweb項目發佈)

一、服務器安裝Tomcat

    1、更換國內源

sudo nano etc/apt/souces.list

       將下面的內容寫入該文件 

# 默認註釋了源碼鏡像以提高 apt update 速度,如有需要可自行取消註釋
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-security main restricted universe multiverse

# 預發佈軟件源,不建議啓用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse

       保存後更新源

sudo apt-get update

2、安裝jdk 環境

sudo apt install default-jdk

3、創建Tomcat用戶

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat

4、下載Tomcat

wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.34/bin/apache-tomcat-8.5.34.zip

5、解壓Tomcat

unzip apache-tomcat-*.zip

6、將解壓好的Tomcat文件移到tomcat目錄下並創建符號鏈接

sudo mv apache-tomcat-*/ /opt/tomcat/
sudo ln -s /opt/tomcat/apache-tomcat-* /opt/tomcat/latest

7、更改tomcat目錄擁有權限

sudo chown -R tomcat: /opt/tomcat

8、給tomcat/bin目錄下的.sh文件可執行權限

sudo chmod +x /opt/tomcat/latest/bin/*.sh

 9、創建一個單元文件方便服務器開啓

sudo nano  /etc/systemd/system/tomcat.service

10、將下面文本拷貝進去

[Unit]
Description=Tomcat 8.5 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/default-java"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
[Install]
WantedBy=multi-user.target

11、通過下面文件打開tomcat並檢查狀態

sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl status tomcat

出現下面的提示就成功了

二、安裝Mysql 5.6

  1、添加mysq了5.6的源並更新源

sudo apt-get install software-properties-common
sudo add-apt-repository 'deb http://archive.ubuntu.com/ubuntu trusty universe'
sudo apt-get update

2、安裝mysql

sudo apt install mysql-server-5.6
sudo apt install mysql-client-5.6

3、設置開機自啓

update-rc.d mysql defaults

4、在etc/mysql/目錄下創建或修改my.cnf文件

sudo nano /etc/mysql/my.cnf

   將下面內容複製到該文件裏面

# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

# This will be passed to all mysql clients
# It has been reported that passwords should be enclosed with ticks/quotes
# escpecially if they contain "#" chars...
# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

# Here is entries for some specific programs
# The following values assume you have at least 32M ram

!includedir /etc/mysql/conf.d/
# add here
[mysqld]
default-time_zone = '+8:00'
lower_case_table_names=1

5、重啓mysql服務

sudo service mysql restart

三、將Javaweb項目打包成war包,上傳到tomcat目錄下的webapp裏,重啓tomcat就可以在外網訪問了

 1、在菜單欄選擇build-->build artifacts

2、在項目目錄下找到該文件

3、將該文件夾壓縮成.war

4、用xftp將war文件上傳到tomcat的webapp目錄下

5、重啓tomcat服務器,服務器就會自動解壓war包,就可以進行訪問了路徑就是http://<服務器ip>:8080/test/<網頁>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章