Linux:记录一次完整的后端项目部署过程

本文记录了一次完整的后端项目部署过程,包括在Linux服务器上安装Java、Git和Maven,从Git拉取代码,使用Maven打包,并编写启动脚本来实现项目的自动化部署。最终,通过一个简单的脚本实现了项目的启动、停止和状态查看等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、背景

公司的CI/CD环境出现了故障,影响项目联调测试,于是协调运维给了台服务器,准备自己来玩

初步设想是通过脚本一键自动从git拉代码、maven自动打包、自动启动项目

二、服务器环境

已装好Centos、有外网

三、环境准备

先建好目录

cd /data
# 在/data目录下建一个project目录,用于存放项目相关文件
mkdir project
# 切换到project,再创建一个gitlab目录,用于存在git clone下载的代码
cd project
mkdir gitlab
1. 安装Java

参考网上资料,jdk要求建议1.8+

我安装的是 jdk-8u341-linux-x64.tar.gz,先把包下载下来,然后上传到服务器(我是在/usr/local目录下建了个java目录,然后把包上传到这个目录),然后执行如下命令解压

tar -xzvf jdk-8u341-linux-x64.tar.gz

然后配置一下环境变量

vim /etc/profile

在最后面加上

#java environment
export JAVA_HOME=/usr/local/java/jdk1.8.0_341
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin

然后执行如下命令让环境变量生效

source /etc/profile

最后验证一下,输入:java -version,看到如下则代表成功

在这里插入图片描述

2. 安装Git

我是先移除了服务器自带的git

yum -y remove git

然后重新安装了下

yum -y install git

执行完检查下,看到如下则ok

在这里插入图片描述

3. 安装Maven

切换到/data/apps目录

先下载maven包

wget http://mirror.bit.edu.cn/apache/maven/maven-3/3.8.2/binaries/apache-maven-3.8.2-bin.tar.gz

然后解压

tar -xzvf apache-maven-3.8.2-bin.tar.gz
cd apache-maven-3.8.2/
# 查看当前目录,复制下来
pwd

获取到目录
在这里插入图片描述
然后去配置maven的环境变量

也是先vim /etc/profile,在最后输入如下

#maven enviroment
export MAVEN_HOME=/data/apps/apache-maven-3.8.2
export PATH=${
   PATH}:${MAVEN_HOME}/bin

然后执行如下命令让环境变量生效

source /etc/profile

验证一下:mvn -v, 看到如下代表成功

在这里插入图片描述
/data目录下建一个目录,名为:mvnrepository 用于存放jar包

在这里插入图片描述

进入到:/data/apps/apache-maven-3.8.2/conf 目录,先将默认的settings.xml文件备份

mv settings.xml settings.xml.bak

然后新建一下包含公司私服的配置文件,取名:settings.xml,内容参考如下:

<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
 | This is the configuration file for Maven. It can be specified at two levels:
 |
 |  1. User Level. This settings.xml file provides configuration for a single user,
 |                 and is normally provided in ${user.home}/.m2/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -s /path/to/user/settings.xml
 |
 |  2. Global Level. This settings.xml file provides configuration for all Maven
 |                 users on a machine (assuming they're all using the same Maven
 |                 installation). It's normally provided in
 |                 ${maven.conf}/settings.xml.
 |
 |                 NOTE: This location can be overridden with the CLI option:
 |
 |                 -gs /path/to/global/settings.xml
 |
 | The sections in this sample file are intended to give you a running start at
 | getting the most out of your Maven installation. Where appropriate, the default
 | values (values used when the setting is not specified) are provided.
 |
 |-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
	<!-- localRepository
     | The path to the local repository maven will use to store artifacts.
     |
     | Default: ${user.home}/.m2/repository
    <localRepository>/path/to/local/repo</localRepository>-->
    
    <!-- 这里改成上一步建好的maven仓库地址-->
	<localRepository>/data/mvnrepository</localRepository>
	<!-- interactiveMode
     | This will determine whether maven prompts you when it needs input. If set to false,
     | maven will use a sensible default value, perhaps based on some other setting, for
     | the parameter in question.
     |
     | Default: true
    <interactiveMode>true</interactiveMode>-->
	<!-- offline
     | Determines whether maven should attempt to connect to the network when executing a build.
     | This will have an effect on artifact downloads, artifact deployment, and others.
     |
     | Default: false
    <offline>false</offline>-->
	<!-- pluginGroups
     | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.
     | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers
     | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.
     |-->
	<pluginGroups>
		<!-- pluginGroup
         | Specifies a further group identifier to use for plugin lookup.
        <pluginGroup>com.your.plugins</pluginGroup>-->
	</pluginGroups>
	<!-- proxies
     | This is a list of proxies which can be used on this machine to connect to the network.
     | Unless otherwise specified (by system property or command-line switch), the first proxy
     | specification in this list marked as active will be used.
     |-->
	<proxies>
		<!-- proxy
         | Specification for one proxy, to be used in connecting to the network.
         |
        <proxy><id>optional</id><active>true</active><protocol>http</protocol><username>proxyuser</username><password>proxypass</password><host>proxy.host.net</host><port>80</port><nonProxyHosts>local.net|some.host.com</nonProxyHosts></proxy>-->
	</proxies>
	<!-- servers
     | This is a list of authentication profiles, keyed by the server-id used within the system.
     | Authentication profiles can be used whenever maven must make a connection to a remote server.
     |-->
	<servers>
		<!-- server
         | Specifies the authentication information to use when connecting to a particular server, identified by
         | a unique name within the system (referred to by the 'id' attribute below).
         |
         | NOTE: You should either specify username/password OR privateKey/passphrase, since these pairings are
         |       used together.
         |
        <server><id>deploymentRepo</id><username>repouser</username><password>repopwd</password></server>-->
		<!-- Another sample, using keys to authenticate.
        <server><id>siteServer</id><privateKey>/path/to/private/key</privateKey><passphrase>optional; leave empty if not used.</passphrase></server>-->
		<server>
			<id>nexus</id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值