如何在 Ubuntu16.04 中用 Apache 部署 Jenkins 自动化服务器
Jenkins
Jenkins 是从 Hudson 项目衍生出来的自动化服务器。Jenkins 是一个基于服务器的应用程序，运行在 Java servlet 容器中，它支持包括 Git、SVN 以及 Mercurial 在内的多种 SCM（<ruby> 源码控制工具 <rp>  （ </rp> <rt>  Source Control Management </rt> <rp>  ） </rp></ruby>）。Jenkins 提供了上百种插件帮助你的项目实现自动化。Jenkins 由 Kohsuke Kawaguchi 开发，在 2011 年使用 MIT 协议发布了第一个发行版，它是个自由软件。


![](/data/attachment/album/201705/08/082705awkyywiddmllikiz.jpg)


在这篇指南中，我会向你介绍如何在 Ubuntu 16.04 中安装最新版本的 Jenkins。我们会用自己的域名运行 Jenkins，在 apache web 服务器中安装和配置 Jenkins，而且支持反向代理。


### 前提


* Ubuntu 16.04 服务器 - 64 位
* Root 权限


### 第一步 - 安装 Java OpenJDK 7


Jenkins 基于 Java，因此我们需要在服务器上安装 Java OpenJDK 7。在这里，我们会从一个 PPA 仓库安装 Java 7，首先我们需要添加这个仓库。


默认情况下，Ubuntu 16.04 没有安装用于管理 PPA 仓库的 python-software-properties 软件包，因此我们首先需要安装这个软件。使用 apt 命令安装 python-software-properties。



```
apt-get install python-software-properties

```

下一步，添加 Java PPA 仓库到服务器中。



```
add-apt-repository ppa:openjdk-r/ppa

```

用 apt 命令更新 Ubuntu 仓库并安装 Java OpenJDK。



```
apt-get update
apt-get install openjdk-7-jdk

```

输入下面的命令验证安装：



```
java -version

```

你会看到安装到服务器上的 Java 版本。


![在 Ubuntu 16.04 上安装 Java OpenJDK 7](/data/attachment/album/201705/08/082935ycvptkobpap1kczp.png)


### 第二步 - 安装 Jenkins


Jenkins 给软件安装包提供了一个 Ubuntu 仓库，我们会从这个仓库中安装 Jenkins。


用下面的命令添加 Jenkins 密钥和仓库到系统中。



```
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo 'deb https://pkg.jenkins.io/debian-stable binary/' | tee -a /etc/apt/sources.list

```

更新仓库并安装 Jenkins。



```
apt-get update
apt-get install jenkins

```

安装完成后，用下面的命令启动 Jenkins。



```
systemctl start jenkins

```

通过检查 Jenkins 默认使用的端口（端口 8080）验证 Jenkins 正在运行。我会像下面这样用 `netstat` 命令检测：



```
netstat -plntu

```

Jenkins 已经安装好了并运行在 8080 端口。


![已经将 Jenkins 安装到 8080 端口](/data/attachment/album/201705/08/082936nz9x3tp3t5lt3tlp.png)


### 第三步 - 为 Jenkins 安装和配置 Apache 作为反向代理


在这篇指南中，我们会在一个 Apache web 服务器中运行 Jenkins，我们会为 Jenkins 配置 apache 作为反向代理。首先我会安装 apache 并启用一些需要的模块，然后我会为 Jenkins 用域名 my.jenkins.id 创建虚拟主机文件。请在这里使用你自己的域名并在所有配置文件中出现的地方替换。


从 Ubuntu 仓库安装 apache2 web 服务器。



```
apt-get install apache2

```

安装完成后，启用 proxy 和 proxy\_http 模块以便将 apache 配置为 Jenkins 的前端服务器/反向代理。



```
a2enmod proxy
a2enmod proxy_http

```

下一步，在 `sites-available` 目录创建新的虚拟主机文件。



```
cd /etc/apache2/sites-available/
vim jenkins.conf

```

粘贴下面的虚拟主机配置。



```
<Virtualhost *:80>
    ServerName        my.jenkins.id
    ProxyRequests     Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode

    <Proxy http://localhost:8080/*>
      Order deny,allow
      Allow from all
    </Proxy>

    ProxyPass         /  http://localhost:8080/ nocanon
    ProxyPassReverse  /  http://localhost:8080/
    ProxyPassReverse  /  http://my.jenkins.id/
</Virtualhost>
```

保存文件。然后用 `a2ensite` 命令激活 Jenkins 虚拟主机。



```
a2ensite jenkins

```

重启 Apache 和 Jenkins。



```
systemctl restart apache2
systemctl restart jenkins

```

检查 Jenkins 和 Apache 正在使用 80 和 8080 端口。



```
netstat -plntu

```

![检查 Apache 和 Jenkins 是否在运行](/data/attachment/album/201705/08/082936kflul7x2apz24fyg.png)


### 第四步 - 配置 Jenkins


Jenkins 用域名 'my.jenkins.id' 运行。打开你的 web 浏览器然后输入 URL。你会看到要求你输入初始管理员密码的页面。Jenkins 已经生成了一个密码，因此我们只需要显示并把结果复制到密码框。


用 `cat` 命令显示 Jenkins 初始管理员密码。



```
cat /var/lib/jenkins/secrets/initialAdminPassword
a1789d1561bf413c938122c599cf65c9

```

![获取 Jenkins 管理员密码](/data/attachment/album/201705/08/082937w15csssj5irjas1k.png)


将结果粘贴到密码框然后点击 Continue。


![安装和配置 Jenkins](/data/attachment/album/201705/08/082937kqa85rqfq8g057m4.png)


现在为了后面能比较好的使用，我们需要在 Jenkins 中安装一些插件。选择 Install Suggested Plugin，点击它。


![安装 Jenkins 插件](/data/attachment/album/201705/08/082937suee44ve39nugzxz.png)


Jenkins 插件安装过程：


![Jenkins 安装完插件](/data/attachment/album/201705/08/082938w8tuwj49vjvjhwjw.png)


安装完插件后，我们需要创建一个新的管理员密码。输入你的管理员用户名、密码、电子邮件等，然后点击 ‘Save and Finish’。


![创建 Jenkins 管理员账户](/data/attachment/album/201705/08/082938xgup9kw27zdrrgku.png)


点击 start 开始使用 Jenkins。你会被重定向到 Jenkins 管理员面板。


![重定向到管理员面板](/data/attachment/album/201705/08/082938i2pwpd5m715t1kdt.png)


成功完成 Jenkins 安装和配置。


![Jenkins 管理员面板](/data/attachment/album/201705/08/082939lllbnfo6q8zimdiq.png)


### 第五步 - Jenkins 安全


在 Jenkins 管理员面板，我们需要为 Jenkins 配置标准的安全，点击 ‘Manage Jenkins’ 和 ‘Configure Global Security’。


![Jenkins 全局安全设置](/data/attachment/album/201705/08/082939i0pfgt28v9opu3zg.png)


Jenkins 在 ‘Access Control’ 部分提供了多种认证方法。为了能够控制所有的用户权限，我选择了 ‘Matrix-based Security’。在复选框 ‘User/Group’ 中启用 admin 用户。通过勾选所有选项给 admin 所有权限，给 anonymous 只读权限。现在点击 ‘Save’。


![配置 Jenkins 权限](/data/attachment/album/201705/08/082940imizr93mamzqqy5f.png)


你会被重定向到面板，如果出现了登录选项，只需输入你的管理员账户和密码。


### 第六步 - 测试一个简单的自动化任务


在这一部分，我想为 Jenkins 服务测试一个简单的任务。为了测试 Jenkins 我会创建一个简单的任务，并用 top 命令查看服务器的负载。


在 Jenkins 管理员面板上，点击 ‘Create New Job’。


![在 Jenkins 中创建新的任务](/data/attachment/album/201705/08/082940cuvm06ut2mcv4t7s.png)


输入任务的名称，在这里我输入 ‘Checking System’，选择 Freestyle Project 然后点击 OK。


![配置 Jenkins 任务](/data/attachment/album/201705/08/082940qzb6ggxb33686tt1.png)


进入 Build 标签页。在 Add build step，选择选项 Execute shell。


在输入框输入下面的命令。



```
top -b -n 1 | head -n 5

```

点击 Save。


![启动 Jenkins 任务](/data/attachment/album/201705/08/082941lbtzihp8cfnfjxpu.png)


现在你是在任务 ‘Project checking system’ 的任务页。点击 Build Now 执行任务 ‘checking system’。


任务执行完成后，你会看到 Build History，点击第一个任务查看结果。


下面是 Jenkins 任务执行的结果。


![构建和运行 Jenkins 任务](/data/attachment/album/201705/08/082941f32l7yefw35yyooj.png)


到这里就介绍完了在 Ubuntu 16.04 中用 Apache web 服务器安装 Jenkins 的内容。




---


via: <https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/>


作者：[Muhammad Arul](https://twitter.com/howtoforgecom) 译者：[ictlyh](https://github.com/ictlyh) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 组织编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to Install Jenkins Automation Server with Apache on Ubuntu 16.04

### This tutorial exists for these OS versions

[Ubuntu 22.04 (Jammy Jellyfish)](/tutorial/ubuntu-jenkins-automation-server/)[Ubuntu 20.04 (Focal Fossa)](/tutorial/how-to-install-jenkins-automation-server-with-apache-on-ubuntu-20-04/)[Ubuntu 18.04 (Bionic Beaver)](/tutorial/how-to-install-jenkins-automation-server-with-apache-on-ubuntu-1804/)**Ubuntu 16.04 (Xenial Xerus)**

### On this page

[Step 1 - Install Java OpenJDK 7](#step-install-java-openjdk-)

[Step 2 - Install Jenkins](#step-install-jenkins)

[Step 3 - Install and Configure Apache as Reverse Proxy for Jenkins](#step-install-and-configure-apache-as-reverse-proxy-for-jenkins)

[Step 4 - Configure Jenkins](#step-configure-jenkins)

[Step 5 - Jenkins Security](#step-jenkins-security)

[Step 6 - Testing a simple automation job](#step-testing-a-simple-automation-job)

[Reference](#reference)

Jenkins is an automation server forked from the Hudson project. Jenkins is a server based application running in a Java servlet container, it has support for many SCM (Source Control Management) software systems including Git, SVN, and Mercurial. Jenkins provides hundreds of plugins to automate your project. Jenkins created by Kohsuke Kawaguchi, first released in 2011 under MIT License, and it's free software.

In this tutorial, I will show you how to install the latest Jenkins version on Ubuntu Server 16.04. We will run Jenkins on our own domain name, and we will to install and configure Jenkins to run under the apache web server with the reverse proxy for Jenkins.

### Prerequisite

- Ubuntu Server 16.04 - 64bit
- Root Privileges

## Step 1 - Install Java OpenJDK 7

Jenkins is based on Java, so we need to install Java OpenJDK version 7 on the server. In this step, we will install Java 7 from a PPA repository which we will add first.

By default, Ubuntu 16.04 ships without the python-software-properties package for managing PPA repositories, so we must install this package first. Install python-software-properties with apt command.

apt-get install python-software-properties

Next, add Java PPA repository to the server.

add-apt-repository ppa:openjdk-r/ppa

Just Press ENTER

Update the Ubuntu repository and install the Java OpenJDK with apt command.

apt-get update

apt-get install openjdk-7-jdk

Verify the installation by typing the command below:

java -version

and you will get the Java version that is installed on the server.

## Step 2 - Install Jenkins

Jenkins provides an Ubuntu repository for the installation packages and we will install Jenkins from this repository.

Add Jenkins key and repository to the system with the command below.

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

echo 'deb https://pkg.jenkins.io/debian-stable binary/' | tee -a /etc/apt/sources.list

Update the repository and install Jenkins.

apt-get update

apt-get install jenkins

When the installation is done, start Jenkins with this systemctl command.

systemctl start jenkins

Verify that Jenkins is running by checking the default port used by Jenkins (port 8080). I will check it with the netstat command below:

netstat -plntu

Jenkins is installed and running on port 8080.

## Step 3 - Install and Configure Apache as Reverse Proxy for Jenkins

In this tutorial we will run Jenkins behind an apache web server, we will configure apache as the reverse proxy for Jenkins. First I will install apache and enable some require modules, and then I'll create the virtual host file with domain name my.jenkins.id for Jenkins. Please use your own domain name here and replace it in all config files wherever it appears.

Install apache2 web server from Ubuntu repository.

apt-get install apache2

When the installation is done, enable the proxy and proxy_http modules so we can configure apache as frontend server/reverse proxy for Jenkins.

a2enmod proxy

a2enmod proxy_http

Next, create a new virtual host file in the sites-available directory.

cd /etc/apache2/sites-available/

vim jenkins.conf

Paste virtual host configuration below.

<Virtualhost *:80>

ServerName my.jenkins.id

ProxyRequests Off

ProxyPreserveHost On

AllowEncodedSlashes NoDecode


<Proxy http://localhost:8080/*>

Order deny,allow

Allow from all

</Proxy>


ProxyPass / http://localhost:8080/ nocanon

ProxyPassReverse / http://localhost:8080/

ProxyPassReverse / http://my.jenkins.id/

</Virtualhost>

Save the file. Then activate the Jenkins virtual host with the a2ensite command.

a2ensite jenkins

Restart Apache and Jenkins.

systemctl restart apache2

systemctl restart jenkins

Check that port 80 and 8000 are in use by Jenkins and Apache.

netstat -plntu

## Step 4 - Configure Jenkins

Jenkins is running on the domain name 'my.jenkins.id'. Open your web browser and type in the URL. You will get the screen that requests you to enter the initial admin password. A password has been generated by Jenkins already, so we just need to show and copy the results to the password box.

Show initial admin password Jenkins with cat command.

cat /var/lib/jenkins/secrets/initialAdminPassword


a1789d1561bf413c938122c599cf65c9

Paste the results to the screen and click '**Continue**'.

Now we should install some plugins in Jenkins to get a good foundation for later use. Choose '**Install Suggested Plugins**', click on it.

Jenkins plugins installations in progress.

After plugin installation, we have to create a new admin password. Type in your admin username, password, email etc. and click on '**Save and Finish**'.

Click start and start using Jenkins. You will be redirected to the Jenkins admin dashboard.

Jenkins installation and Configuration finished successfully

## Step 5 - Jenkins Security

From the Jenkins admin dashboard, we need to configure the standard security settings for Jenkins, click on '**Manage Jenkins**' and then '**Configure Global Security**'.

Jenkins provides several authorization methods in the '**Access Control**' section. I select '**Matrix-based Security**' to be able to control all user privileges. Enable the admin user at the box '**User/Group**' and click **add**. Give the admin all privileges by **checking all options**, and give the anonymous just read permissions. Now Click '**Save**'.

You will be redirected to the dashboard, and if there is login option, just type your admin user and password.

## Step 6 - Testing a simple automation job

In this section, I just want to test a simple job for the Jenkins server. I will create a simple job for testing Jenkins and to find out the server load with the top command.

From the Jenkins admin dashboard, click '**Create New Job**'.

Enter the job name, I'll use 'Checking System' here, select '**Freestyle Project**' and click '**OK**'.

Go to the '**Build**' tab. On the '**Add build step**', select the option '**Execute shell**'.

Type in the command below into the box.

top -b -n 1 | head -n 5

Click '**Save**'.

Now you are on the job page of the job 'Project checking system'. Click '**Build Now**' to execute the job 'checking system'.

After the job has been executed, you will see the '**Build History**', click on the first job to see the results.

Here are the results from the job executed by Jenkins.

Jenkins installation with Apache web server on Ubuntu 16.04 completed successfully.