Installing and Securing Jenkins
Earlier this year, I wrote an article about PHP-CI, which you can use as a continuous integration tool for your PHP projects. Within this article I indicated I still liked Jenkins the most as a CI tool. Time to dive into Jenkins and see how we can set this up for our PHP project.
Key Takeaways
- Jenkins is a versatile tool that can be used for continuous integration of many different applications, including web, software, and mobile applications. It offers numerous options and possibilities, making it potentially overwhelming for new users.
- Installing Jenkins on Ubuntu Server 14.04 LTS involves running specific commands to add the key for the Jenkins repository to the system, add Jenkins to the sources.list file, and finally install Jenkins. Once installed, Jenkins can be accessed through a browser by adding port 8080 to the end of the URL.
- Securing Jenkins is crucial to prevent unauthorized access. This involves enabling the security realm, creating a user database, disabling the option for users to sign up without permission, and setting the authorization using either matrix based security or Project-based Matrix Authorization Strategy.
- Preparing Jenkins for PHP projects involves installing several plugins and creating a template. The plugins perform tasks such as analyzing a report and converting it to a graph. The template, created by Sebastian Bergmann, simplifies the process of configuring a new project.
Introduction to Jenkins
The list of things that Jenkins has to offer is huge due to the rich plugin system it has. Basically, Jenkins is just a tool which connects all kinds of different tools and plugins together to create a report for you. For example, it can run PHPUnit and show you the results in a graph over time. It can check your PHP code for errors by running php -l. However, you can also let Jenkins build a project and output a zip file, which you can use to deploy your application. The advantage of this is that you get a complete zip file back to upgrade your production application without having to run tools like Composer or NPM on your production servers.
Jenkins can be used for many different applications. You can use it for web applications written in PHP, but also for software and mobile applications written in Java or any other language. This makes Jenkins a very versatile tool and very interesting for companies handling many different projects.
Because Jenkins has so many options and possibilities, it looks overwhelming for people to start using it. Within this series of articles, we will slowly work our way into Jenkins. We will start with the installation and setup. After that we will continue with analyzing a project. In the end, we will take a close look at numerous other plugins we can use to check the quality of our product.
We will mainly focus on the quality of the PHP code, but in the end we will also take a short look at analyzing our HTML, CSS and JavaScript which work in close harmony with PHP.
Installing Jenkins
You can install Jenkins on any popular operating system. For this article, we are going to install Jenkins on Ubuntu Server 14.04 LTS. If you are using a different operating system, you can check here for the installation instructions per operating system. To install Jenkins on Ubuntu the easiest way would be by running this command.
<span>sudo apt-get install jenkins</span>
However, you are now installing Jenkins which is located in Ubuntu’s repository. Jenkins itself suggests to install directly from the Jenkins repositories. First, we add the key to our system for the repository.
<span>wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -</span>
Now we run the following command to add Jenkins to the sources.list file.
<span>deb http://pkg.jenkins-ci.org/debian binary/</span>
Now we are ready to install Jenkins.
sudo apt-get update
<span>sudo apt-get install jenkins</span>
When finished, Jenkins is successfully installed on your server.
You can now reach Jenkins through your browser by adding port 8080 to the end of the URL. So imagine my server is named ci.myserver.com you can reach Jenkins on ci.myserver.com:8080.
.
Securing Jenkins
The first thing you should notice is that Jenkins is completely open. Anyone now going to this URL can do all kinds of different tasks, so the first thing we are going to do is secure it. We start by clicking on Manage Jenkins in the sidebar menu. where we will be greeted by a notification from Jenkins, recommending to secure the CI server.
Click the Setup Security button to continue. On the next page, mark enable security checkbox as checked. The following steps are crucial to follow correctly, because it’s easy to lock yourself out of Jenkins. If this happens, you can follow these steps to gain access again.
The first thing you need to do, is set your security realm. The easiest one is to have Jenkins have his own user database by marking the checkbox in front of Jenkins' own user database. Make sure the allow users to sign up option is also enabled. Now save your configuration by clicking save.
You should now be able to see a sign up link at the top right corner of the page. Click this link and fill in the form to sign up. When done, log in with your newly created account.
Go back to the security settings by clicking Manage Jenkins and then clicking Configure Global Security. You should now uncheck the allow users to sign up checkbox to make sure no new accounts can be created without your permission.
Next we are going to set the authorization. The best options for you are either matrix based security or Project-based Matrix Authorization Strategy. This allows you to set per user which actions they can and cannot perform. If you choose the latter option, you will be able to even set this per project. For now, I am going to pick the matrix based security.
A table with permissions is now displayed. You will only see one table row in which you can define what an anonymous user can do. However, we want to set permissions for our own user account. So fill in your username in the box below and click Add. To make sure you have access to everything, you can check them all.
In the end, this is what it looks like.
Note: Be careful with capitalized letters. Peter and peter are 2 different user accounts, so make sure you fill in the correct username in both cases else you will be locked out of the system.
When saving, you will automatically get logged out. Note that you don’t see anything anymore except a login form. After logging in, you should have access to everything like before. If you get a permission error, you made a mistake and you either have to change the settings or you got yourself locked out of Jenkins. In the latter case, see the link above.
Preparing Jenkins
So far, we have been busy installing and securing Jenkins. Now it’s time to configure Jenkins so we can start building and analyzing our PHP projects. Sebastian Bergmann created an excellent website showing you how to set up Jenkins for PHP. We are going to do the same steps as documented, but in a slightly different order.
Installing plugins
First we need to install several plugins into Jenkins. Plugins are small extensions to Jenkins which can perform tasks for you like analyzing a report and converting it to a graph. We will need the following list of plugins to get started.
- checkstyle
- cloverphp
- crap4j
- dry
- htmlpublisher
- jdepend
- plot
- pmd
- violations
- xunit
We are going to install these plugins through the interface. If you feel more experienced or comfortable with the command line, you can check out this documentation to see how to install them via the command line. Also, that page will give you a short explanation about what every plugin will do.
Within Jenkins, go to Manage Jenkins and then to Manage Plugins. Click the tab named available. Here you will see a complete list of all available plugins. With the filter at the top right corner, you can quickly search for every plugin. Make sure you install the complete list of the plugins mentioned above by checking the checkboxes.
Check the checkbox in front of Restart Jenkins when installation is complete and no jobs are running to restart Jenkins when everything is done. After the restart, all the required plugins have been installed correctly.
Creating a template
When Jenkins is set up, we normally start by creating a project, sometimes called a job. A project is typically one PHP project you got. Within a project, you will have several builds. You can configure Jenkins to analyze (build) your code at certain moments. This can be scheduled intervals or on pull requests from contributors, for example. So a project contains several builds over time, each being a separate analysis of your code. Thanks to the plugins we installed, you will be able to see the results of those builds in nice graphs. Within these graphs you can quickly spot if a certain build increased or decreased the quality of your project. We can also see where we need to improve our code according to all of our tools.
We could now create a new project. However, we also would need to configure this project. For example, we could configure that each time PHPUnit is run, a code coverage page should be created so we can see which code is tested and which code is untested. There’s a lot of tests we would need to configure on a new project, and this would likely be time consuming. Luckily, Sebastian Bergmann also created a template which we’re going to use.
We need to open up the command line to be able to install this template. The easiest way would be to download the CLI tool Jenkins provided. The problem however is the fact that we secured our Jenkins installation earlier. This would mean we have to open up security back to anonymous or connect public/private key to our account.
In this case, we are going for an alternative method of installing the template. First, log in as user Jenkins on the command line and go to the home directory.
<span>sudo apt-get install jenkins</span>
Within the home directory, go into the jobs directory and create a new directory named php-template.
<span>wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -</span>
Now we are going to enter this directory and add the configuration file.
<span>deb http://pkg.jenkins-ci.org/debian binary/</span>
Now, we need Jenkins to reload its configuration. You can do this by going back to the web interface, click Manage Jenkins and click on Reload Configuration from Disk. Jenkins will now reload its configuration. When done, you should see the php-template project on the main overview page.
Jenkins is now ready. We can create new projects based on this template.
Conclusion
Within this article we installed, secured and prepared Jenkins to start analyzing our PHP project. In the next part, we will add our project, prepare it, and of course analyze it.
Frequently Asked Questions (FAQs) on Installing and Securing Jenkins
What are the best practices for securing Jenkins?
Securing Jenkins involves several steps. First, ensure that Jenkins is running in its own user account with limited privileges to minimize potential damage in case of a security breach. Second, enable security in the Configure Global Security section and choose the security realm for authentication. Third, use the Matrix-based security or Project-based Matrix Authorization Strategy for authorization. Fourth, ensure that all Jenkins plugins and the Jenkins core are always up-to-date. Lastly, regularly backup your Jenkins setup to prevent data loss.
How can I enable security in Jenkins?
To enable security in Jenkins, navigate to Manage Jenkins > Configure Global Security. Check the Enable security box. In the Access Control section, choose the security realm for authentication. Jenkins provides several options, including Jenkins’ own user database, LDAP, and others.
What is the Matrix-based security in Jenkins?
Matrix-based security is an authorization strategy in Jenkins that allows specifying different permissions for each user or group. It provides fine-grained control over what actions a user or a group can perform. To use it, you need to have the Role-Based Authorization Strategy plugin installed.
How can I keep my Jenkins plugins and core up-to-date?
Jenkins provides an easy way to update plugins and the core. Navigate to Manage Jenkins > Manage Plugins > Updates tab. Here, you can see the available updates for your installed plugins. To update the Jenkins core, go to Manage Jenkins > Manage Plugins > Advanced tab and click on the Check now button in the Update Site section.
How can I backup my Jenkins setup?
Regularly backing up your Jenkins setup is crucial to prevent data loss. You can use the ThinBackup plugin to backup your Jenkins setup. It allows scheduling backups and restoring from backups.
How can I limit the privileges of the Jenkins user account?
To limit the privileges of the Jenkins user account, create a new user account specifically for running Jenkins. This user account should not have sudo privileges or be able to log in to the system.
What is the Project-based Matrix Authorization Strategy in Jenkins?
The Project-based Matrix Authorization Strategy is an extension of the Matrix-based security that allows specifying different permissions for each job. It provides even more fine-grained control over what actions a user or a group can perform.
How can I install the Role-Based Authorization Strategy plugin in Jenkins?
To install the Role-Based Authorization Strategy plugin, navigate to Manage Jenkins > Manage Plugins > Available tab. Search for the Role-Based Authorization Strategy plugin, select it, and click on the Install without restart button.
What is the security realm in Jenkins?
The security realm in Jenkins is used for authentication. It determines how users are authenticated. Jenkins provides several options, including Jenkins’ own user database, LDAP, and others.
How can I secure Jenkins against cross-site request forgery (CSRF)?
To secure Jenkins against CSRF, navigate to Manage Jenkins > Configure Global Security. In the CSRF Protection section, check the Prevent Cross Site Request Forgery exploits box and choose the default Crumb Issuer.
The above is the detailed content of Installing and Securing Jenkins. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.
