ubuntu安装Jenkins及修改端口

先决条件

安装Java SDK

sudo apt-get install openjdk-8-jdk
# sudo apt-get install openjdk-7-jdk  早些系统可以安装
第1步 - 安装Jenkins

包含在默认Ubuntu软件包中的Jenkins版本往往落后于项目本身的最新版本。 为了利用最新的修复和功能,我们将使用项目维护的软件包来安装Jenkins。

首先,我们将存储库密钥添加到系统。

wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

添加密钥后,系统将返回OK 。 接下来,我们将Debian包存储库地址附加到服务器的sources.list :

echo deb http://pkg.jenkins.io/debian-stable binary/ | sudo tee /etc/apt/sources.list.d/jenkins.list

当这两个都到位时,我们将运行update ,以便apt-get将使用新的存储库:

sudo apt-get update

最后,我们将安装Jenkins及其依赖项,包括Java:

sudo apt-get install jenkins

指定早期版本可以安装。

sudo apt-get install jenkins=2.138.1

如果太慢的话,可以去官网下载后,再安装。
官网:http://pkg.jenkins-ci.org/debian/

第2步 - 更改默认端口号

由于Jenkins默认的8080端口经常容易被占用,我们可以修改它的默认端口号。

  1. 修改/etc/init.d/jenkins文件do_start函数的check_tcp_port命令,端口号从8080换成8088。
sudo vim /etc/init.d/jenkins
#                                                                                                                                                                                                                   
# Function that starts the daemon/service                                                                                                                                                                           
#                                                                                                                                                                                                                   
do_start()
{
    # the default location is /var/run/jenkins/jenkins.pid but the parent directory needs to be created                                                                                                             
    mkdir `dirname $PIDFILE` > /dev/null 2>&1 || true
    chown $JENKINS_USER `dirname $PIDFILE`
    # Return                                                                                                                                                                                                       
    #  0 if daemon has been started                                                                                                                                                                               
    #  1 if daemon was already running                                                                                                                                                                             
    #  2 if daemon could not be started                                                                                                                                                                           
    $DAEMON $DAEMON_ARGS --running && return 1

    # Verify that the jenkins port is not already in use, winstone does not exit                                                                                                                                   
    # even for BindException                                                                                                                                                                                       
    check_tcp_port "http" "$HTTP_PORT" "8088" || return 1

    # If the var MAXOPENFILES is enabled in /etc/default/jenkins then set the max open files to the                                                                                                                 
    # proper value                                                                                                                                                                                                 
    if [ -n "$MAXOPENFILES" ]; then
        [ "$VERBOSE" != no ] && echo Setting up max open files limit to $MAXOPENFILES
        ulimit -n $MAXOPENFILES
    fi

    # --user in daemon doesn't prepare environment variables like HOME, USER, LOGNAME or USERNAME,                                                                                                                 
    # so we let su do so for us now                                                                                                                                                                                 
    $SU -l $JENKINS_USER --shell=/bin/bash -c "$DAEMON $DAEMON_ARGS -- $[Java](https://www.linuxidc.com/Java "https://www.linuxidc.com/topicnews.aspx?tid=19") $JAVA_ARGS -jar $JENKINS_WAR $JENKINS_ARGS" || return 2
}

2.修改/etc/default/jenkins文件,将HTTP_PORT从8080改成8088。

sudo vim /etc/default/jenkins
# defaults for Jenkins automation server

# pulled in from the init script; makes things easier.
NAME=jenkins

# arguments to pass to java

# Allow graphs etc. to work even when an X server is present
JAVA_ARGS="-Djava.awt.headless=true"

#JAVA_ARGS="-Xmx256m"

# make jenkins listen on IPv4 address
#JAVA_ARGS="-Djava.net.preferIPv4Stack=true"

PIDFILE=/var/run/$NAME/$NAME.pid

# user and group to be invoked as (default to jenkins)
JENKINS_USER=$NAME
JENKINS_GROUP=$NAME

# location of the jenkins war file
JENKINS_WAR=/usr/share/$NAME/$NAME.war

# jenkins home location
JENKINS_HOME=/var/lib/$NAME

# set this to false if you don't want Jenkins to run by itself
# in this set up, you are expected to provide a servlet container
# to host jenkins.
RUN_STANDALONE=true

# log location.  this may be a syslog facility.priority
JENKINS_LOG=/var/log/$NAME/$NAME.log
#JENKINS_LOG=daemon.info

# Whether to enable web access logging or not.
# Set to "yes" to enable logging to /var/log/$NAME/access_log
JENKINS_ENABLE_ACCESS_LOG="no"

# OS LIMITS SETUP
#   comment this out to observe /etc/security/limits.conf
#   this is on by default because http://github.com/jenkinsci/jenkins/commit/2fb288474e980d0e7ff9c4a3b768874835a3e92e
#   reported that Ubuntu's PAM configuration doesn't include pam_limits.so, and as a result the # of file
#   descriptors are forced to 1024 regardless of /etc/security/limits.conf
MAXOPENFILES=8192

# set the umask to control permission bits of files that Jenkins creates.
#   027 makes files read-only for group and inaccessible for others, which some security sensitive users
#   might consider benefitial, especially if Jenkins runs in a box that's used for multiple purposes.
#   Beware that 027 permission would interfere with sudo scripts that run on the master (JENKINS-25065.)
#
#   Note also that the particularly sensitive part of $JENKINS_HOME (such as credentials) are always
#   written without 'others' access. So the umask values only affect job configuration, build records,
#   that sort of things.
#
#   If commented out, the value from the OS is inherited,  which is normally 022 (as of Ubuntu 12.04,
#   by default umask comes from pam_umask(8) and /etc/login.defs

# UMASK=027

# port for HTTP connector (default 8080; disable with -1)
HTTP_PORT=8088


# servlet context, important if you want to use apache proxying
PREFIX=/$NAME

# arguments to pass to jenkins.
# --javahome=$JAVA_HOME
# --httpListenAddress=$HTTP_HOST (default 0.0.0.0)
# --httpPort=$HTTP_PORT (default 8080; disable with -1)
# --httpsPort=$HTTP_PORT
# --argumentsRealm.passwd.$ADMIN_USER=[password]
# --argumentsRealm.roles.$ADMIN_USER=admin
# --webroot=~/.jenkins/war
# --prefix=$PREFIX

JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"
  1. 执行以下命令启动Jenkins即可。
sudo systemctl daemon-reload
sudo systemctl start jenkins
sudo systemctl status jenkins

可以使用其status命令来验证它是否成功启动。

sudo systemctl status jenkins

如果一切顺利,输出的开始应显示服务处于活动状态,成功启动会有如下显示:

● jenkins.service - LSB: Start Jenkins at boot time
  Loaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)
  Active:active (exited) since Thu 2017-04-20 16:51:13 UTC; 2min 7s ago
    Docs: man:systemd-sysv-generator(8)

之后就可以浏览器访问Jenkins了。

你可能感兴趣的:(ubuntu安装Jenkins及修改端口)