Ubuntu操作系统使用和开发环境部署指南

1简介

最近由于自己要在Ubuntu环境下部署一个通过Java调用.so动态链接库文件,并且把这个工具部署成war包,运行在Linux环境的Tomcat容器下,因此需要自己搭建了Ubuntu 16.04的操作系统和开发环境。在此简要技术Ubuntu系统的使用流程,以及开发环境的搭建过程总结。本文的主要内容如下图所示:
在这里插入图片描述

2Ubuntu安装

Ubuntu系统的安装参见博客使用VMWare安装Ubentu指南

3必要的软件安装

注意: 以下系统的安装需要网络的支持,如果是在内网离线的环境下,请首先在外网有网络的情况下把虚拟机安装好,然后通过完整的虚拟机压缩拷贝的方式在内网使用。

3.1 vim、lrzsz、tree、openssh

在这里插入图片描述

3.1.1配置apt阿里源

root@sqh-virtual-machine:~# cd /etc/apt
root@sqh-virtual-machine:~# cp sources.list sources.list.bak 
root@sqh-virtual-machine:/etc/apt# ls
apt.conf.d   preferences.d  sources.list.bak  trusted.gpg
auth.conf.d  sources.list   sources.list.d    trusted.gpg.d
root@sqh-virtual-machine:/etc/apt# 

备份原有的sources.list文件。
以root权限修改文件/etc/apt/sources.list,文件内容如下:

deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

执行如下命令使配置生效:

apt-get update 更新源  
apt-get upgrade 更新软件 

这样就使用了阿里源来下载安装包,速度会快很多。

3.1.2安装常用软件包

安装如上的包使用命令如下:
使用

apt-get install -y vim tree lrzsz openssh-server

关于apt-get命令,可以使用man apt-get查看详情

NAME
       apt-get - APT package handling utility -- command-line interface

SYNOPSIS
       apt-get [-asqdyfmubV] [-o=config_string] [-c=config_file]
               [-t=target_release] [-a=architecture] {update | upgrade |
               dselect-upgrade | dist-upgrade |
               install pkg [{=pkg_version_number | /target_release}]...  |
               remove pkg...  | purge pkg...  |
               source pkg [{=pkg_version_number | /target_release}]...  |
               build-dep pkg [{=pkg_version_number | /target_release}]...  |
               download pkg [{=pkg_version_number | /target_release}]...  |
               check | clean | autoclean | autoremove | {-v | --version} |
               {-h | --help}}

DESCRIPTION
       apt-get is the command-line tool for handling packages, and may be
       considered the user's "back-end" to other tools using the APT library.
       Several "front-end" interfaces exist, such as aptitude(8), synaptic(8)
       and wajig(1).

       Unless the -h, or --help option is given, one of the commands below
       must be present.

等待命令执行完成即可。

3.2 搜狗输入法

对于程序员开发时,由于中文作为母语,必然要在代码中写入注释之类的描述性文字,因此安装中文输入法必不可少。
由于安装的为桌面版,因此可以直接去搜狗输入法的Linux版下载安装按照提示即可。

https://pinyin.sogou.com/linux/?r=pinyin

若在命令行下安装,可使用wget命令:

wget "http://cdn2.ime.sogou.com/dl/index/1524572264/sogoupinyin_2.2.0.0108_amd64.deb?st=tQdHKEaxh6PcyFVf4QfT1A&e=1539855246&fn=sogoupinyin_2.2.0.0108_amd64.deb"
dpkg -i sogoupinyin_2.2.0.0108_amd64.deb\?st\=tQdHKEaxh6PcyFVf4QfT1A\&e\=1539855246\&fn\=sogoupinyin_2.2.0.0108_amd64.deb

因详细安装比较复杂,在此不赘述,可以参见Ubuntu16.04系统安装搜狗输入法详细教程

4开发环境安装

开发环境的安装,遵循同样的模式,如下图所示:
在这里插入图片描述

4.1JDK安装

环境变量的配置可以以root权限修改/etc/profile文件中

export JAVA_HOME=/usr/local/java/jdk1.8.0_131
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

配置完成执行命令

source /etc/profile

使得命令生效
JDK的安装具体参见博客Ubentu安装JDK和idea过程指南
关于/etc/profile和~/.bashrc,不详细描述,参见Linux环境变量配置的三个方法–/etc/profile,~/.bashrc,shell

4.2IDEA安装

IDEA的安装参见Ubentu安装JDK和idea过程指南

4.3Maven安装

Maven安装的前提需要先安装JDK,JDK的安装参见Ubentu安装JDK和idea过程指南
Maven安装时可直接在Maven3版本下载页选择相应的版本下载即可。
安装和配置,以3.5.4进行。首先下载apache-maven-3.5.4-bin.tar.gz。
关于Linux压缩和解压缩命令可以参考linux压缩和解压缩命令

4.3.1解压拷贝

tar包的压缩和解压缩命令如下:

    tar
      解包:tar zxvf filename.tar
      打包:tar czvf filename.tar dirname
sqh@sqh-virtual-machine:~/Desktop$ tar zxvf apache-maven-3.5.4-bin.tar.gz 
apache-maven-3.5.4/README.txt
apache-maven-3.5.4/LICENSE
apache-maven-3.5.4/NOTICE
apache-maven-3.5.4/lib/
apache-maven-3.5.4/lib/cdi-api.license
apache-maven-3.5.4/lib/commons-cli.license
apache-maven-3.5.4/lib/commons-io.license
apache-maven-3.5.4/lib/commons-lang3.license
apache-maven-3.5.4/lib/jcl-over-slf4j.license
此处省略很多字
apache-maven-3.5.4/lib/maven-resolver-connector-basic-1.1.1.jar
apache-maven-3.5.4/lib/maven-resolver-transport-wagon-1.1.1.jar
apache-maven-3.5.4/lib/maven-slf4j-provider-3.5.4.jar
apache-maven-3.5.4/lib/jansi-1.17.1.jar

sqh@sqh-virtual-machine:~/Desktop$ mkdir /usr/local/maven
mkdir: cannot create directory ‘/usr/local/maven’: Permission denied

切换成root用户,创建目录/usr/local/maven,把解压之后的文件夹拷贝到拷贝到该目录

sqh@sqh-virtual-machine:~/Desktop$ su
Password: 
root@sqh-virtual-machine:/home/sqh/Desktop# mkdir /usr/local/
bin/     games/   java/    man/     share/   
etc/     include/ lib/     sbin/    src/     
root@sqh-virtual-machine:/home/sqh/Desktop# mkdir /usr/local/maven
root@sqh-virtual-machine:/home/sqh/Desktop# cp -r apache-maven-3.5.4 /usr/local/maven/

4.3.2环境变量配置

vim /etc/profile

在尾部追加如下内容:

注意: 在vim命令行模式下跳转到尾部可以通过Shift + G,直接跳转

export MAVEN_HOME=/usr/local/maven/apache-maven-3.5.4
export CLASSPATH=$CLASSPATH:$MAVEN_HOME/lib
export PATH=$PATH:$MAVEN_HOME/bin

让环境变量配置生效

root@sqh-virtual-machine:/usr/local/maven# source /etc/profile

验证配置生效,可以通过mvn -v命令

root@sqh-virtual-machine:/usr/local/maven# mvn -v
Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-18T02:33:14+08:00)
Maven home: /usr/local/maven/apache-maven-3.5.4
Java version: 1.8.0_131, vendor: Oracle Corporation, runtime: /usr/local/java/jdk1.8.0_131/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.15.0-46-generic", arch: "amd64", family: "unix"

这样即完成了Maven环境变量的配置

4.3.3配置settings.xml使用阿里maven库

在IDEA使用maven下载jar包时,由于自带的中心仓库下载很慢,因此,改用阿里的maven库

<?xml version="1.0" encoding="UTF-8"?>
<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>/home/sqh/.m2/repository</localRepository>
  <mirrors>
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
  </mirrors>

</settings>

这样下载时速度会快很多。

4.4Tomcat安装

4.4.1下载

Tomcat官网下载相应的版本即可

4.4.2安装

安装过程与Maven安装一模一样,不再赘述。
以apache-tomcat-8.5.38为例,在配置环境变量的关键步骤,修改/etc/profile
在尾部追加内容,如下:

TOMCAT_HOME=/usr/local/tomcat/apache-tomcat-8.5.38

确保JDK正确的安装,参见Ubentu安装JDK和idea过程指南

5版本管理工具安装

对于程序开发来说,代码管理是日常生活的重中之重,谁也不想劳心劳力写出来的代码结果因为没有进行版本管理而无法回到一个预期的状态,这是非常糟糕的状态。因此很多职场新人在刚开始步入职场时,第一步就是要进行代码的版本管理。当前流行的版本管理工具中有两个较为优秀的工具,即集中式版本管理的SVN和分布式管理工具Git,由于出差的原因,而且搭建GIt服务器的流程并不熟悉,因此在这里简单演示搭建SVN,之后有时间会演示使用docker安装GitLab服务器。
在这里插入图片描述

5.1SVN安装

在有网环境下进行SVN服务器的安装较为简单,可以通过apt-get命令进行。

sudo apt-get update
sudo apt-get install subversion

5.2SVN的使用

Ubuntu给出的手册如下:

NAME
       svn - Subversion command line client tool

SYNOPSIS
       svn command [options] [args]

OVERVIEW
       Subversion  is  a  version control system, which allows you to keep old
       versions of files and directories (usually source code), keep a log  of
       who, when, and why changes occurred, etc., like CVS, RCS or SCCS.  Sub‐
       version keeps a single copy of the master sources.  This copy is called
       the  source  ``repository'';  it contains all the information to permit
       extracting previous versions of those files at any time.

       For more information about the Subversion project, visit http://subver‐
       sion.apache.org.

       Documentation  for  Subversion  and its tools, including detailed usage
       explanations of the svn, svnadmin, svnserve and svnlook programs,  his‐
       torical  background, philosophical approaches and reasonings, etc., can
       be found at http://svnbook.red-bean.com/.

       Run `svn help' to access the built-in tool documentation.

在使用中关注点如下:
在这里插入图片描述

5.2.1 建立仓库

以思维导图阐述过程
在这里插入图片描述

5.2.2 配置

在这里插入图片描述
注意1:在修改配置文件svnserve.conf文件时,如果指定了

anon-access=read

会发现客户端无法读取仓库的日志。
注意2:在修改权限管理文件authz时要按照格式指定那些用户组可以访问什么仓库
在部署SVN时,笔者修改的文件如下:

### This file is an example authorization file for svnserve.
### Its format is identical to that of mod_authz_svn authorization
### files.
### As shown below each section defines authorizations for the path and
### (optional) repository specified by the section name.
### The authorizations follow. An authorization line can refer to:
###  - a single user,
###  - a group of users defined in a special [groups] section,
###  - an alias defined in a special [aliases] section,
###  - all authenticated users, using the '$authenticated' token,
###  - only anonymous users, using the '$anonymous' token,
###  - anyone, using the '*' wildcard.
###
### A match can be inverted by prefixing the rule with '~'. Rules can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').

[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average

[groups]
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe
admin = songquanheng, gaochao
# [/foo/bar]
# harry = rw
# &joe = r
# * =

# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r

[/]
@admin = rw

在[groups]使用键值对配置完用户组和用户的映射关系之后,一定要继续配置用户组对仓库的访问的映射关系。

[/]
@admin = rw

不然会报错误“由于目标计算机积极拒绝,无法连接”

5.2.3 启动和停止服务

在这里插入图片描述

注意:在启动服务时确保路径的正确性,由于在演示中repository为仓库,因此在使用svnserve -d -r path开启服务时,要指定的路径为仓库的父目录。以下方式是错误的。

~~svnserve -d -r /home/svn/repository~~  

5.2.4 访问和代码提交

5.2.4.1Windows下拉代码

在这里插入图片描述
确保conf/svnserve.conf中项

anon-access=none

使用TortoiseSVN查看日志如下:
在这里插入图片描述

5.2.4.2 Ubuntu系统

通过apt-get 安装了Subervison之后,只要用户权限足够,在本系统自然也可以克隆仓库中的代码。

sqh@sqh-virtual-machine:~/SVN/test$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:41:0c:d3 brd ff:ff:ff:ff:ff:ff
    inet 172.20.10.4/28 brd 172.20.10.15 scope global ens33
       valid_lft forever preferred_lft forever
sqh@sqh-virtual-machine:~/SVN/test$ svn co svn://172.20.10.4/repository
Authentication realm: <svn://172.20.10.4:3690> 53be99f7-cac1-4a79-8685-86cd244d6ffe
Password for 'sqh': #默认尝试以当前用户为用户名访问svn服务,需要输入密码验证,不适用直接enter即可

Authentication realm: <svn://172.20.10.4:3690> 53be99f7-cac1-4a79-8685-86cd244d6ffe
Username: songquanheng
Password for 'songquanheng': ******

A    repository/Readme.txt
A    repository/commands
Checked out revision 2.
sqh@sqh-virtual-machine:~/SVN/test$ 

这样就完成了使用windows和Linux克隆仓库中的代码的问题。
至于代码提交到版本库中,对于Windows和Linux虽然有区别,但遵循相同的逻辑。

5.2.4.3提交代码

参见svn 命令行使用总结

6搬入内网

当这些都在外网正常工作了之后,如果读者在工作时有内外网区分,则可以把虚拟机整体拷贝进入内网,这样就有了一个比较高的起点了。

注意:在整体搬迁时一定要保证VMWare工具的版本在内外网保持一致,这样才可以正常的在内网工作而不引入风险。

6总结

文档主要详细的介绍了使用Ubuntu操作系统的整体流程,按照该文档的叙述去做,可以高效的搭建Linux下的Java开发环境。通过不断的总结和使用锤炼出关键可复用的部分,不断的往脚下搬砖,大概这样才能越来越高远吧。
通过本文档的实践,希望读者能够熟悉Ubuntu操作系统,尽快进入开发的敏杰状态。

										2019-03-23 15:56于南京大学鼓楼校区
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章