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!

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),