willcoderwang  翻译中
How to Install Jenkins Automation Server with Apache on Ubuntu 16.04
============================================================


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.

[
 ![Install Java 7 openJDK on Ubuntu 16.04](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/1.png) 
][9]

### 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.

[
 ![Jenkins has been installed on port 8080](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/2.png) 
][10]

### 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

[
 ![Check that Apache and Jenkins are running](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/3.png) 
][11]

### 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

[
 ![Get the Jenkins admin password](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/4.png) 
][12]

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

[
 ![Jenkins Installation and Configuration](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/5.png) 
][13]

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

[
 ![Install jenkins Plugins](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/6.png) 
][14]

Jenkins plugins installations in progress.

[
 ![Jenkins plugins get installed](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/7.png) 
][15]

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**'.

[
 ![Create Jenkins admin account](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/8.png) 
][16]

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

[
 ![Get redirected to the admin dashboard](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/9.png) 
][17]

Jenkins installation and Configuration finished successfully

[
 ![The Jenkins admin dashboard](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/10.png) 
][18]

### 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 Global Security Settings](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/11.png) 
][19]

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**'.

[
 ![Configure Jenkins Permissions](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/12.png) 
][20]

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**'.

[
 ![Create a new Job in Jenkins](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/13.png) 
][21]

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

[
 ![Configure new Jenkins Job](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/14.png) 
][22]

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**'.

[
 ![Start a Jenkins Job](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/15.png) 
][23]

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.

[
 ![Build and run a Jenkins Job](https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/16.png) 
][24]

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

--------------------------------------------------------------------------------

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

作者：[Muhammad Arul ][a]
译者：[译者ID](https://github.com/译者ID)
校对：[校对者ID](https://github.com/校对者ID)

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

[a]:https://twitter.com/howtoforgecom
[1]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#prerequisite
[2]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-install-java-openjdk-
[3]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-install-jenkins
[4]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-install-and-configure-apache-as-reverse-proxy-for-jenkins
[5]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-configure-jenkins
[6]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-jenkins-security
[7]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#step-testing-a-simple-automation-job
[8]:https://www.howtoforge.com/tutorial/how-to-install-jenkins-with-apache-on-ubuntu-16-04/#reference
[9]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/1.png
[10]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/2.png
[11]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/3.png
[12]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/4.png
[13]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/5.png
[14]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/6.png
[15]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/7.png
[16]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/8.png
[17]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/9.png
[18]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/10.png
[19]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/11.png
[20]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/12.png
[21]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/13.png
[22]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/14.png
[23]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/15.png
[24]:https://www.howtoforge.com/images/how-to-install-jenkins-with-apache-on-ubuntu-16-04/big/16.png
