CentOS7.2安装Jenkins

云服务器中已经安装了Java 和Tomcat 环境
如未安装请参考 CentOS7.2安装JDK + Tomcat

一、通过下载jenkins.war的方式

下载比较慢

wget http://mirrors.jenkins-ci.org/war/latest/jenkins.war /usr/local/tomcat8/webapps/

下载完成执行

jar -xvf /usr/local/tomcat8/webapps/jenkins.war

通过访问 http://ip:8080/jenkins/ 进行配置
初始密码 initialAdminPassword 中复制

vi /root/.jenkins/secrets/initialAdminPassword

二、通过yum源方式安装

使用 Jenkins 仓库,我们要执行以下命令
如果Jenkins 导入过 key了, 就可以跳过这步

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

安装

yum -y install jenkins

查看安装位置

rpm -qc jenkins

修改Jenkins 端口
默认是8080端口

vi /etc/sysconfig/jenkins

#修改监听端口
JENKINS_PORT="8081"

修改Jenkins 的java路径
因为Jenkins默认的java可选路径不包含我们部署的jdk路径,所以这里要配置一下,不然Jenkins服务会启动失败

自己java地址的查看方式
which java
sudo vi /etc/init.d/jenkins

修改candidates增加java可选路径:/usr/local/jdk8/bin/java

candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-1.7.0/bin/java
/usr/lib/jvm/jre-1.7.0/bin/java
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/bin/java
/usr/local/jdk8/bin/java
"

启动Jenkins并设置Jenkins开机启动

#重载服务(由于前面修改了Jenkins启动脚本)
sudo systemctl daemon-reload

#启动Jenkins服务
sudo systemctl start jenkins   或  sudo service jenkins start

#将Jenkins服务设置为开机启动
#由于Jenkins不是Native Service,所以需要用chkconfig命令而不是systemctl命令
sudo /sbin/chkconfig jenkins on

配置防火墙
firewall防火墙配置

firewall-cmd --permanent --zone=public --add-port=8081/tcp
firewall-cmd --reload

iptables防火墙配置

vim /etc/sysconfig/iptables

添加
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8081 -j ACCEPT

重启防火墙
systemctl enable iptables.service
systemctl start iptables.service

配置Nginx反向代理Jenkins

#新增Jenkins专用Nginx配置文件
sudo vi /etc/nginx/conf.d/jenkins.conf

#输入以下内容并保存
upstream jenkins {
       server localhost:8081;
 }
 
server {
        listen       80;
        server_name  jenkins.xx.com;
        access_log  /var/log/nginx/jenkins.access.log main;
    	error_log  /var/log/nginx/jenkins.error.log error;
        client_max_body_size 60M;
        client_body_buffer_size 512k;

        location / {
                proxy_pass http://jenkins/;
                proxy_redirect  off;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;
        }
}

重载Nginx配置

sudo nginx -s reload

配置SELinux
如果不配置SELinux,通过Nginx反向代理访问,可能会产如下错误

#查看错误信息
/var/log/nginx/jenkins.error.log

#错误信息示例
connect() to 127.0.0.1:8001 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1
方法一
setsebool -P httpd_can_network_connect 1

方法二
#关闭SELinux: 
sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config 
#重启:
reboot

卸载

service jenkins stop
yum clean all
yum -y remove jenkins
rm -rf /var/cache/jenkins
rm -rf /var/lib/jenkins/

你可能感兴趣的:(CentOS,Linux)