本文介紹了CentOS7 64 Java,Tomcat,MySQL,Maven熱部署等服務器環境的搭建過程。
服務器:
已經將所需要的工具(Xshell,Xftp、FileZilla等sftp上傳工具,jdk-8u101-linux-x64.tar.gz和apache-tomcat-9.0.0.M10.tar.gz)上傳至百度雲 http://pan.baidu.com/s/1qYRms8G
1、tomcat官網 https://tomcat.apache.org/ 下載
Java環境配置
環境准備
通過 uname -r 判斷系統是多少位
- 64位 : 出現x86_64
- 32位 : 出現i686或i386
安裝Java JDK8.0
- 建立Java目錄,存放Java和Tomcat
- cd /usr/local/
- mkdir java
- cd java
- 使用FileZilla將下載好的jdk-8u101-linux-x64.tar.gz 和 apache-tomcat-9.0.0.M10.tar.gz上傳至Java目錄下(傳送的國外服務器很慢,國內幾乎是國外的十倍,但是也只有兩三百KB,也可能是電腦問題)
- 將上傳的jdk解壓,然后重命名為jdk
- tar -zxv -f jdk-8u101-linux-x64.tar.gz
- mv jdk1.8.0_101 jdk
- cd jdk
- 配置環境變量Environment=JAVA_HOME=/usr/local/Java/jdk
- vim /etc/profile
-
打開之后按鍵盤(i)進入編輯模式,將下面的內容復制到底部
JAVA_HOME=/usr/local/java/jdk PATH=$JAVA_HOME/bin:$PATH CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar export PATH JAVA_HOME CLASSPATH - 寫完之后我們按鍵盤(ESC)按鈕退出,然后按(:wq)保存並且關閉Vim。
- 使用
source /etc/profile命令使其立即生效 -
通過
java -version驗證Java是否配置成功。
安裝Tomcat9.0
- 在Java目錄下解壓上面一步已經上傳上去的Tomcat9.0
- tar -zxv -f apache-tomcat-9.0.0.M10.tar.gz
- mv apache-tomcat-9.0.0.M10 tomcat
- cd tomcat
- 啟動命令為 /usr/local/java/tomcat/bin/startup.sh
- 啟動完成后還需開放8080端口(CentOS7這個版本的防火牆默認使用的是firewall,與之前的版本使用iptables不一樣。 關於防火牆端口可以查看后面的參考文檔)
- firewall-cmd --zone=public --add-port=8080/tcp --permanent
出現success表明添加成功 - 更新防火牆規則即可: firewall-cmd --reload
- 重啟防火牆 systemctl restart firewalld.service
- firewall-cmd --zone=public --add-port=8080/tcp --permanent
- 然后再次在瀏覽器中輸入http://ip:8080,如果看到tomcat系統界面,說明安裝成功。
- Tomcat 8080 端口無法訪問
- 查看8080端口被那個程序占用(應該是Java) netstat -anp 然后再殺死占用進程。
- 可能是你的服務器提供商有安全組來控制端口,你需要去提供商那里開啟端口(PS:我的阿里雲服務器就是必須要設置端口安全組才可以訪問端口)
- 關閉命令為 /usr/local/Java/tomcat/bin/shutdown.sh
自啟動:
編輯文件 /usr/local/java/tomcat/bin/catalina.sh (根據你自己的jdk路徑進行修改) 在文件的正文開頭,即正式代碼前,大概在99行添加如下代碼
export JAVA_HOME=/usr/local/java/jdk
export JRE_HOME=/usr/local/java/jdk/jre

第一步:
vim /lib/systemd/system/tomcat.service
[Unit] Description=tomcat After=network.target [Service] Type=oneshot ExecStart=/usr/local/java/tomcat/bin/startup.sh //自已的tomcat目錄 ExecStop=/usr/local/java/tomcat/bin/shutdown.sh ExecReload=/bin/kill -s HUP $MAINPID RemainAfterExit=yes [Install] WantedBy=multi-user.target
(2).設置權限
chmod 754 tomcat.service
(3).啟動關閉服務,設置開機啟動
#啟動服務
systemctl start tomcat.service
#關閉服務
systemctl stop tomcat.service
#開機啟動
systemctl enable tomcat.service
MySQL
一、系統環境
yum update升級以后的系統版本為
[root@yl-web yl]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core)
一開始是通過這個命令來直接安裝的:
1 [root@yl-web yl]# yum install mysql 2 [root@yl-web yl]# yum install mysql-server 3 [root@yl-web yl]# yum install mysql-devel
安裝mysql和mysql-devel都成功,但是安裝mysql-server失敗,如下:
[root@yl-web yl]# yum install mysql-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirrors.sina.cn * extras: mirrors.sina.cn * updates: mirrors.sina.cn No package mysql-server available. Error: Nothing to do
查資料發現是CentOS 7 版本將MySQL數據庫軟件從默認的程序列表中移除,用mariadb代替了。
有兩種解決辦法:
1、方法一:安裝mariadb
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,采用GPL授權許可。開發這個分支的原因之一是:甲骨文公司收購了MySQL后,有將MySQL閉源的潛在風險,因此社區采用分支的方式來避開這個風險。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能輕松成為MySQL的代替品。
安裝mariadb,大小59 M。
[root@yl-web yl]# yum install mariadb-server mariadb
mariadb數據庫的相關命令是:
systemctl start mariadb #啟動MariaDB
systemctl stop mariadb #停止MariaDB
systemctl restart mariadb #重啟MariaDB
systemctl enable mariadb #設置開機啟動
所以先啟動數據庫
[root@yl-web yl]# systemctl start mariadb
然后就可以正常使用mysql了
[root@yl-web yl]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.41-MariaDB MariaDB Server Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]>
安裝mariadb后顯示的也是 MariaDB [(none)]> ,可能看起來有點不習慣。下面是第二種方法。
2、方法二:官網下載安裝mysql-server
# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm # rpm -ivh mysql-community-release-el7-5.noarch.rpm # yum install mysql-community-server
安裝成功后重啟mysql服務。
# service mysqld restart
初次安裝mysql,root賬戶沒有密碼。
[root@yl-web yl]# mysql -u root Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.26 MySQL Community Server (GPL) Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.01 sec) mysql>
設置密碼
mysql> set password for 'root'@'localhost' =password('password');
Query OK, 0 rows affected (0.00 sec)
mysql>
不需要重啟數據庫即可生效。
在mysql安裝過程中如下內容:
Installed:
mysql-community-client.x86_64 0:5.6.26-2.el7 mysql-community-devel.x86_64 0:5.6.26-2.el7
mysql-community-libs.x86_64 0:5.6.26-2.el7 mysql-community-server.x86_64 0:5.6.26-2.el7
Dependency Installed:
mysql-community-common.x86_64 0:5.6.26-2.el7
Replaced:
mariadb.x86_64 1:5.5.41-2.el7_0 mariadb-devel.x86_64 1:5.5.41-2.el7_0 mariadb-libs.x86_64 1:5.5.41-2.el7_0
mariadb-server.x86_64 1:5.5.41-2.el7_0
所以安裝完以后mariadb自動就被替換了,將不再生效。
[root@yl-web yl]# rpm -qa |grep mariadb [root@yl-web yl]#
三、配置mysql
1、編碼
mysql配置文件為/etc/my.cnf
最后加上編碼配置
[mysql] default-character-set =utf8
這里的字符編碼必須和/usr/share/mysql/charsets/Index.xml中一致。

2、遠程連接設置
把在所有數據庫的所有表的所有權限賦值給位於所有IP地址的root用戶。
mysql> grant all privileges on *.* to root@'%'identified by 'password';
如果是新用戶而不是root,則要先新建用戶
mysql>create user 'username'@'%' identified by 'password';
此時就可以進行遠程連接了。
Maven 熱部署
Maven 熱部署可以通過一行命令部署到本地服務器,沒有問題的話就一行命令部署到正式服務器。及其方便了開發和部署。因為我的Tomcat9遇到很多問題。
可以參考 maven自動部署到遠程tomcat教程 進行部署和測試。
下面是我遇到的一個錯誤,因為沒有配置IDEA的make 導致出錯。
[ERROR] Failed to execute goal org.apache.tomcat.maven:tomcat7-maven-plugin:2.2:deploy (default-cli) on project liwenhao: Cannot invoke Tomcat manager: Connect ion reset by peer: socket write error -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1]
可以通過將make如下配置
即可成功
war包部署在服務器亂碼

可以通過配置如下屬性,解決中文war包服務器亂碼。
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties>
配置完圖。
在我通過mvn tomcat7:deploy命令熱部署時,會出現mysql無法連接的情況,后來在我重新進行熱部署的時候,沒有出現這個問題。
猜測
應該是我的配置文件的問題
參考文檔

