上個月寫過一篇如何設定 Nginx + Jenkins 文章,可以參考: Jenkins + Nginx User Auth,這次筆記 Jenkins + Apache 設定方式
安裝 Jenkins
在 Ubuntu 環境,可以直接參考 Installing Jenkins on Ubuntu
$ wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
$ sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
$ sudo apt-get update
$ sudo apt-get install jenkins
$ sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian binary/ > /etc/apt/sources.list.d/jenkins.list'
$ sudo apt-get update
$ sudo apt-get install jenkins
設定 Apache
如果您是需要用一個特定 domain 也就是 jenkein.example.com,沒有 sub folder,你可以透過底下設定
啟用 Apache proxy 模組
$ a2enmod proxy
$ a2enmod proxy_http
$ a2enmod proxy_http
Apache virtual host 部份加入底下設定
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080
設定到這邊,重新啟動 Apache 即可,如果你是使用 sub folder 的方式,有就是透過 www.example.com/jenkins 方式瀏覽,請換成底下設定
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
ProxyPass /jenkins http://127.0.0.1:8080/jenkins
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
ProxyPass /jenkins http://127.0.0.1:8080/jenkins
但是你會發現 css, js, image 都讀取錯誤。這時候請修改 /etc/default/jenkins 將 JENKINS_ARGS 加入 –prefix 設定
JENKINS_ARGS=”–prefix=$PREFIX –httpListenAddress=127.0.0.1 –webroot=/var/cache/jenkins/war –httpPort=$HTTP_PORT –ajp13Port=$AJP_PORT”
重新啟動 Jenkins 及 Apache 即可。最後透過 htpasswd 將 jenkins web 目錄鎖起來
<Location /jenkins/>
AuthType basic
AuthName "jenkins"
AuthUserFile "/etc/apache2/conf/.htpasswd"
Require valid-user
</Location>
AuthType basic
AuthName "jenkins"
AuthUserFile "/etc/apache2/conf/.htpasswd"
Require valid-user
</Location>