Centos Java 项目基本环境搭建
1.环境准备工作:
在这里首先我们要关注我们将要部署的目标服务器是否是阿里的云服务器如果是,那么我们不需要更换YUM的源,如果不是进入系统第一件事我们需要更换掉yum源,这样在后面我们安装各种组件的时候我们可以节省很多不必要的时间和规避很多麻烦
-
1.1 更换Yum源的代码如下:
以root账户登入Centos服务器
ssh root@192.168.1.251
[root@iotvpn ~]# cd /etc/yum.repos.d/
[root@iotvpn yum.repos.d]# mv ./CentOS-Base.repo ./CentOS-Base.repo.backup
[root@iotvpn yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@iotvpn yum.repos.d]# yum clean all
[root@iotvpn yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors- base: mirrors.aliyun.com
- extras: mirrors.aliyun.com
- updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/12): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/12): base/7/x86_64/filelists_db | 7.1 MB 00:00:01
(3/12): extras/7/x86_64/filelists_db | 231 kB 00:00:00
(4/12): extras/7/x86_64/prestodelta | 47 kB 00:00:00
(5/12): base/7/x86_64/other_db | 2.6 MB 00:00:00
(6/12): extras/7/x86_64/primary_db | 180 kB 00:00:00
(7/12): updates/7/x86_64/prestodelta | 358 kB 00:00:00
(8/12): extras/7/x86_64/other_db | 118 kB 00:00:00
(9/12): updates/7/x86_64/primary_db | 2.5 MB 00:00:00
(10/12): updates/7/x86_64/filelists_db
(11/12): updates/7/x86_64/other_db
(12/12): base/7/x86_64/primary_db
Metadata Cache Created
</pre>
-
1.2 为了我们在管理进程时方便我们在服务器中可安装htop来对进程进行可视化管理,具体操作如下:
[root@iotvpn ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile- base: mirrors.aliyun.com
- extras: mirrors.aliyun.com
- updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================
Installing:
epel-release noarch 7-11 extras 15 kTransaction Summary
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Retrieving key from http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) security@centos.org"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
From : http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1Installed:
epel-release.noarch 0:7-11Complete!
[root@iotvpn ~]# yum -y install htop
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
epel/x86_64/metalink | 9.0 kB 00:00:00- base: mirrors.aliyun.com
- epel: mirrors.huaweicloud.com
- extras: mirrors.aliyun.com
- updates: mirrors.aliyun.com
epel | 4.7 kB 00:00:00
(1/3): epel/x86_64/group_gz | 88 kB 00:00:00
(2/3): epel/x86_64/updateinfo | 959 kB 00:00:00
(3/3): epel/x86_64/primary_db | 6.6 MB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package htop.x86_64 0:2.2.0-3.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==========================================================================================================================================
Package Arch Version Repository Size
==========================================================================================================================================
Installing:
htop x86_64 2.2.0-3.el7 epel 103 kTransaction Summary
Install 1 Package
Total download size: 103 k
Installed size: 218 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/epel/packages/htop-2.2.0-3.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for htop-2.2.0-3.el7.x86_64.rpm is not installed
htop-2.2.0-3.el7.x86_64.rpm | 103 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) epel@fedoraproject.org"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-11.noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : htop-2.2.0-3.el7.x86_64 1/1
Verifying : htop-2.2.0-3.el7.x86_64 1/1Installed:
htop.x86_64 0:2.2.0-3.el7Complete!
然后htop一下就可以看到可视化得进程界面了</br>
-
1.3 接下来我们安装Java得JDK环境并配置JAVA环境变量
[root@iotvpn home]# mkdir download
[root@iotvpn home]# ls
beitakeji download
[root@iotvpn home]# cd download/这里使用Xftp上传到服务器我们刚刚创建得download文件夹中,关于Xshell和Xftp得下载链接上面给出,可进行下载,若不会使用可百度搜索,非常简单,这里不做赘述</br>
[root@iotvpn download]# ls
jdk-8u201-linux-x64.tar.gz
[root@iotvpn download]# tar -zxvf ./jdk-8u201-linux-x64.tar.gz
[root@iotvpn download]# ls
jdk1.8.0_201 jdk-8u201-linux-x64.tar.gz
[root@iotvpn download]# mkdir /usr/local/java
[root@iotvpn download]# mv ./jdk1.8.0_201/ /usr/local/java/jdk1_8_201
[root@iotvpn download]# cd /usr/local/java/jdk1_8_201/
[root@iotvpn jdk1_8_201]# pwd
/usr/local/java/jdk1_8_201
[root@iotvpn jdk1_8_201]# vim /etc/profile在profile文件得末尾处按键盘a插入如下的内容
JAVA_HOME=/usr/local/java/jdk1_8_201
JRE_HOME=JAVA_HOME/lib/dt.jar:JRE_HOME/lib
PATH=JRE_HOME/bin:$PATH
export JAVA_HOME JRE_HOME CLASSPATH PATH然后按ESC键,按shift+键盘冒号键并输入wq!回车保存:
:wq!
推出VIM文本编辑器后我们刷新下profile配置文件,并确认下是否配置成功
[root@iotvpn jdk1_8_201]# source /etc/profile
[root@iotvpn jdk1_8_201]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)
[root@iotvpn jdk1_8_201]# javac -version
javac 1.8.0_201
[root@iotvpn jdk1_8_201]#
到这里JDK环境变量在Centos中便配置完成了!理论上所有的服务器拿到手需要在上面做JAVA或者数据库的环境搭建都需要做以上的这些操作,阿里的云服yum的源已经在镜像中指定好了我们可以节省掉配置yum源的操作 -
1.4 Tomcat容器的搭建
系统中iotBmp的业务系统是外置服务器容器的项目所以当我们部署整套系统的时候必须要在该项目部署的所在服务器中搭建Tomcat中间件容器
* 首先把下载好得Tomcat中间件tar包使用xftp上传至目标服务器并解压
[root@iotvpn download]# ls
apache-tomcat-7.0.93.tar.gz jdk-8u201-linux-x64.tar.gz
[root@iotvpn download]# tar -zxvf apache-tomcat-7.0.93.tar.gz
[root@iotvpn download]# ls
apache-tomcat-7.0.93 apache-tomcat-7.0.93.tar.gz jdk-8u201-linux-x64.tar.gz
[root@iotvpn download]# mv ./apache-tomcat-7.0.93 /usr/local/tomcat7
[root@iotvpn download]# cd /usr/local/tomcat7/bin
[root@iotvpn bin]# ls
bootstrap.jar commons-daemon.jar daemon.sh setclasspath.sh startup.sh tool-wrapper.sh
catalina.bat commons-daemon-native.tar.gz digest.bat shutdown.bat tomcat-juli.jar version.bat
catalina.sh configtest.bat digest.sh shutdown.sh tomcat-native.tar.gz version.sh
catalina-tasks.xml configtest.sh setclasspath.bat startup.bat tool-wrapper.bat
[root@iotvpn bin]# vim ./catalina.sh
>在红框位置添加 CATALINA_PID="$CATALINA_BASE/tomcat.pid" 然后按ESC再按shift+冒号键 “wq!”保存
[root@iotvpn bin]# vim /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target[Service]
Type=forkingEnvironment="JAVA_HOME=/usr/local/java/jdk1_8_201"
PIDFile=/usr/local/tomcat7/tomcat.pid
ExecStart=/usr/local/tomcat7/bin/startup.sh
ExecReload=/bin/kill -s HUP MAINPID
PrivateTmp=true[Install]
WantedBy=multi-user.target然后按ESC再按shift+冒号键进行"wq!"保存!这里要注意一点 Environment="JAVA_HOME=/usr/local/java/jdk1_8_201" 这里要配置环境变量,在/etc/profile下配置得环境变量这里是无效得!所以要单独进行配置,接下来我们启动下tomcat访问下8080端口访问试试看。另外如果服务器本身防火墙firewalld或者iptables打开得状态,需要把端口访问权限放开这里以firewalld为例
[root@iotvpn bin]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@iotvpn bin]# firewall-cmd --reload
success
[root@iotvpn bin]# firewall-cmd --zone=public --list-ports
8080/tcp
[root@iotvpn bin]# systemctl enable tomcat
[root@iotvpn bin]# systemctl start tomcat这样tomcat就可以访问测试项目了,通常来说我们为了部署文件夹会把项目部署文件夹放在一个上传比较方便得目录下,这个配置在tomcat得conf文件夹得server.xml中,接下来我们进行修改
[root@iotvpn bin]# cd /usr/local/tomcat7
[root@iotvpn tomcat7]# ls
bin BUILDING.txt conf CONTRIBUTING.md lib LICENSE logs NOTICE README.md RELEASE-NOTES RUNNING.txt temp webapps work
[root@iotvpn tomcat7]# cd conf/
[root@iotvpn conf]# ls
Catalina catalina.policy catalina.properties context.xml logging.properties server.xml tomcat-users.xml web.xml
[root@iotvpn conf]# vim ./server.xml
</pre>把<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"></br>
改为<Host name="localhost" appBase="/home/webapps" unpackWARs="true" autoDeploy="true"></br>
然后按ESC再按shift+冒号输入"wq!"保存,接着因为我们设定得tomcat项目部署目录为/home/webapps所以我们要创建出该目录
<pre>
[root@iotvpn conf]# mkdir /home/webapps
[root@iotvpn conf]# cd /home/
[root@iotvpn home]# ls
beitakeji download webapps
[root@iotvpn home]# systemctl stop tomcat
[root@iotvpn home]# systemctl start tomcat
[root@iotvpn home]# systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-03-12 13:20:41 CST; 1min 12s ago
Process: 32402 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
Process: 32541 ExecStart=/usr/local/tomcat7/bin/startup.sh (code=exited, status=0/SUCCESS)
Main PID: 32551 (java)
Tasks: 41
CGroup: /system.slice/tomcat.service
└─32551 /usr/local/java/jdk1_8_201/bin/java -Djava.util.logging.config.file=/usr/local/tomcat7/conf/logging.properties -Djav...Mar 12 13:20:41 iotvpn systemd[1]: Starting Tomcat...
Mar 12 13:20:41 iotvpn startup.sh[32541]: Tomcat started.
Mar 12 13:20:41 iotvpn systemd[1]: Started Tomcat.
[root@iotvpn home]# netstat -apn|grep 8080
tcp6 0 0 :::8080 :::* LISTEN 32551/java
❄到这里为止所有服务器环境部署前所需要做的准备就都做好了