Friday, March 9, 2018

How to run Jenkins behind Caddy ?

https://caddyserver.com/ : Caddy is the HTTP/2 web server with automatic HTTPS

https://jenkins.io/ : The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

I have installed Jenkins in Ubuntu.
If you want to install in Ubuntu please follow the tutorial below.
http://www.fullstacktechnos.com/2018/03/how-to-install-jenkins-in-ubuntu.html

Install Caddy

1)
praja@ip-****:~/jenkins$ wget https://github.com/mholt/caddy/releases/download/v0.10.10/caddy_v0.10.10_linux_amd64.tar.gz
Saving to: ‘caddy_v0.10.10_linux_amd64.tar.gz’
caddy_v0.10.10_linux_amd64.tar.gz    100%[===================================================================>]   5.36M  1.95MB/s    in 2.7s    
2017-12-28 09:01:36 (1.95 MB/s) - ‘caddy_v0.10.10_linux_amd64.tar.gz’ saved [5617625/5617625]


2)
sudo cp caddy /usr/local/bin/

praja@ip-****:~/jenkins$ ls -lart /usr/local/bin/
total 17168
drwxr-xr-x 10 root root     4096 Nov 21 10:30 ..
drwxr-xr-x  2 root root     4096 Dec 28 09:07 .
-rwxr-xr-x  1 root root 17568553 Dec 28 09:07 caddy

3)
Once the installation is complete, please add the cap_net_bind_service capability to the Caddy binary. This capability will allow the Caddy executable to bind to a port less than 1024.

praja@ip-****:~/jenkins$ sudo setcap 'cap_net_bind_service=+ep' /usr/local/bin/caddy

4)
Create the directories where you will store the Caddy configuration file Caddyfile and SSL certificates.
The term “Caddyfile” is a plaintext configuration file used to configure how Caddy works.
This configuration file is very similar in purpose to httpd.conf in Apache or nginx.conf in Nginx.

Run the following commands:

sudo mkdir /etc/caddy
sudo chown -R root:www-data /etc/caddy
sudo mkdir /etc/ssl/caddy
sudo chown -R www-data:root /etc/ssl/caddy
sudo chmod 0770 /etc/ssl/caddy
sudo touch /etc/caddy/Caddyfile
sudo mkdir /var/www
sudo chown www-data: /var/www

5)
caddy file

This will proxy all of the root traffic from port 80 to port 8080 where our Jenkins application is running – while passing the host information that most backend applications would expect when we specify transparent.

praja@ip-****:/etc/caddy$ cat Caddyfile 
jenkins.fullstacktechnos.com {
  gzip
  proxy / :8080 {
    transparent
  }
}

sudo chown www-data:www-data Caddyfile
sudo chmod 444 Caddyfile

praja@ip-****:/etc/caddy$ ls -lart Caddyfile 
-r--r--r-- 1 www-data www-data 72 Dec 28 09:31 Caddyfile

** Change the hostname as per your hosting domain or use localhost if you are testing locally.

6)
Caddy Service File

Run the following commands:

praja@ip-****:~/jenkins/init/linux-systemd$ pwd
/home/praja/jenkins/init/linux-systemd

praja@ip-****:~/jenkins/init/linux-systemd$ ls
caddy.service  README.md

praja@ip-****:~/jenkins/init/linux-systemd$ sudo cp caddy.service /etc/systemd/system/

praja@ip-****:~/jenkins/init/linux-systemd$ sudo chown root:root /etc/systemd/system/caddy.service

praja@ip-****:~/jenkins/init/linux-systemd$ sudo chmod 744 /etc/systemd/system/caddy.service

praja@ip-****:~/jenkins/init/linux-systemd$ sudo systemctl daemon-reload

praja@ip-****:/etc/systemd/system$ sudo systemctl enable caddy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/caddy.service to /etc/systemd/system/caddy.service.


praja@ip-****:/etc/systemd/system$ sudo systemctl status caddy.service
● caddy.service - Caddy HTTP/2 web server
   Loaded: loaded (/etc/systemd/system/caddy.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2017-12-28 09:50:04 UTC; 8s ago
     Docs: https://caddyserver.com/docs
 Main PID: 18390 (caddy)
    Tasks: 7
   Memory: 9.5M
      CPU: 163ms
   CGroup: /system.slice/caddy.service
           └─18390 /usr/local/bin/caddy -log stdout -agree=true -conf=/etc/caddy/Caddyfile -root=/var/tmp

Dec 28 09:50:04 ip-**** caddy[18390]: Your sites will be served over HTTPS automatically using Let's Encrypt.
...

** Now visit jenkins.<your-domain>.com. Jenkins will be served over https.

Caddy is the HTTP/2 web server with automatic HTTPS

How to install Jenkins in Ubuntu ?

https://jenkins.io/ : The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.

Inside Your Ubuntu box :


root@ip-****:~# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04

Codename: xenial

Setup: =>

You need to have a JDK and JRE installed.
openjdk-7-jre and openjdk-7-jdk are suggested.

Install Java 8 in Ubuntu:

1)
praja@ip-****:/etc/apt/sources.list.d$ sudo add-apt-repository ppa:webupd8team/java
 Oracle Java (JDK) Installer (automatically downloads and installs Oracle JDK7 / JDK8 / JDK9). There are no actual Java files in this PPA.
Ubuntu 16.10 Yakkety Yak is no longer supported by Canonical (and thus, Launchpad and this PPA). The PPA supports Ubuntu 17.10, 17.04, 16.04, 14.04 and 12.04.
Oracle JDK 9 is now considered stable. There are currently only 64bit builds (no other builds are available for download: http://www.oracle.com/technetwork/java/javase/downloads/index.html )
 More info: https://launchpad.net/~webupd8team/+archive/ubuntu/java
Press [ENTER] to continue or ctrl-c to cancel adding it

OK

praja@ip-****:/etc/apt/sources.list.d$ ls
webupd8team-ubuntu-java-xenial.list
praja@ip-172-31-17-211:/etc/apt/sources.list.d$ cat webupd8team-ubuntu-java-xenial.list 
deb http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main
# deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu xenial main


2)
praja@ip-****:~/jenkins$ sudo apt-get update
Get:1 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease [17.5 kB]
Hit:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial InRelease          
Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]                   
Hit:4 http://security.ubuntu.com/ubuntu xenial-security InRelease                   
Get:5 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main amd64 Packages [2,912 B]                   
Get:6 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial/main Translation-en [1,260 B]                
Get:7 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Fetched 226 kB in 0s (239 kB/s)
Reading package lists... Done

3)

praja@ip-****:~/jenkins$ sudo apt-get install oracle-java8-installer
Reading package lists... Done
Building dependency tree       

Reading state information... Done 

4)


praja@ip-****:~/jenkins$ javac -version
javac 1.8.0_151


Oracle JDK 8 installed

Optional:

set JAVA_HOME

praja@ip-****:/usr/lib$ sudo update-alternatives --config java
There is 1 choice for the alternative java (providing /usr/bin/java).

  Selection    Path                                     Priority   Status
------------------------------------------------------------
  0            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      auto mode
* 1            /usr/lib/jvm/java-8-oracle/jre/bin/java   1081      manual mode

Open => <sudo nano /etc/environment>
Update java home path.

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
JAVA_HOME="/usr/lib/jvm/java-8-oracle"

praja@ip-****:/usr/lib$ source /etc/environment
praja@ip-****:/usr/lib$ echo $JAVA_HOME

/usr/lib/jvm/java-8-oracle


Install Jenkins :


1)
praja@****:~/jenkins$ wget -q -O - https://pkg.jenkins.io/debian/jenkins-ci.org.key | sudo apt-key add -

OK

2)
praja@ip-****:~/jenkins$ sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'


3)
praja@ip-****:~/jenkins$ sudo apt-get update
Hit:1 http://ppa.launchpad.net/webupd8team/java/ubuntu xenial InRelease
Hit:2 http://security.ubuntu.com/ubuntu xenial-security InRelease        
Hit:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial InRelease            
Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]           
Ign:5 http://pkg.jenkins.io/debian-stable binary/ InRelease                                                 
Get:6 http://pkg.jenkins.io/debian-stable binary/ Release [2,042 B]               
Get:7 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:8 http://pkg.jenkins.io/debian-stable binary/ Release.gpg [181 B]
Get:9 http://pkg.jenkins.io/debian-stable binary/ Packages [12.5 kB]
Fetched 219 kB in 1s (181 kB/s)
Reading package lists... Done


4)
praja@ip-****:~/jenkins$ sudo apt-get install jenkins
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Processing triggers for ureadahead (0.100.0-19) ...



Check Service -

praja@ip-****:~/jenkins$ sudo /etc/init.d/jenkins status
● jenkins.service - LSB: Start Jenkins at boot time
   Loaded: loaded (/etc/init.d/jenkins; bad; vendor preset: enabled)
   Active: active (exited) since Thu 2017-12-28 06:48:39 UTC; 3min 14s ago
     Docs: man:systemd-sysv-generator(8)
    Tasks: 0
   Memory: 0B
      CPU: 0

Dec 28 06:48:38 ip-172-31-17-211 systemd[1]: Starting LSB: Start Jenkins at boot time...
Dec 28 06:48:38 ip-172-31-17-211 jenkins[17388]:  * Starting Jenkins Automation Server jenkins
Dec 28 06:48:38 ip-172-31-17-211 su[17409]: Successful su for jenkins by root
Dec 28 06:48:38 ip-172-31-17-211 su[17409]: + ??? root:jenkins
Dec 28 06:48:38 ip-172-31-17-211 su[17409]: pam_unix(su:session): session opened for user jenkins by (uid=0)
Dec 28 06:48:39 ip-172-31-17-211 jenkins[17388]:    ...done.

Dec 28 06:48:39 ip-172-31-17-211 systemd[1]: Started LSB: Start Jenkins at boot time.

** jenkins user is created to run the service


praja@ip-****:~/jenkins$ id jenkins
uid=112(jenkins) gid=116(jenkins) groups=116(jenkins)

Log file location -

praja@ip-172-31-17-211:~/jenkins$ tail /var/log/jenkins/jenkins.log 
Dec 28, 2017 6:48:59 AM hudson.model.DownloadService$Downloadable load
INFO: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller


Config file -

praja@ip-172-31-17-211:~/jenkins$ cat /etc/default/jenkins


For More Info: