How to Install Jenkins and Deploy a Laravel Website into an Apache Server using Jenkins Pipeline on CentOS
Jenkins is an open-source automation server that helps in continuous integration and continuous deployment (CI/CD) of software projects. It is widely used in software development to automate building, testing, and deploying software. In this article, we will guide you through the installation of Jenkins and the deployment of a Laravel website into an Apache server using a Jenkins pipeline on CentOS.
Prerequisites Before proceeding with the installation of Jenkins and the deployment of Laravel, make sure that you have the following prerequisites installed on your CentOS server:
- Apache web server
- PHP and its extensions
- MySQL or any other database server
- Git
Step 1: Install Jenkins on CentOS The first step is to install Jenkins on your CentOS server. You can install Jenkins using the following commands:
sudo yum install wget -y
sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
sudo yum install jenkins -y
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 2: Install Required Jenkins Plugins Once Jenkins is installed, we need to install the required plugins to deploy a Laravel website. Login to your Jenkins server from your web browser using your server's IP address on port 8080 (e.g., http://your-server-ip-address:8080/). You will be prompted to enter the initial administrator password. The password can be found in the following file:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
After logging in, you need to install the following Jenkins plugins:
- Git Plugin
- SSH Plugin
- Pipeline Plugin
To install these plugins, navigate to Manage Jenkins -> Manage Plugins -> Available
and search for the plugin's name. Select the plugin and click on the "Install without restart" button.
Step 3: Create Jenkins Pipeline Now, we will create a Jenkins pipeline to deploy a Laravel website. Go to the Jenkins dashboard and click on New Item
. Enter the name of the pipeline and select Pipeline
as the project type. Click on the "OK" button to create the pipeline.
Under the Pipeline
section, you will see a Pipeline script
field. In this field, you need to enter the pipeline script. The following is a sample pipeline script to deploy a Laravel website:
pipeline {
agent any
stages {
stage('Git Clone') {
steps {
git branch: 'master', url: 'https://github.com/your-github-repository-url.git'
}
}
stage('Composer Install') {
steps {
sh 'composer install'
}
}
stage('Environment Setup') {
steps {
sh 'cp .env.example .env'
sh 'php artisan key:generate'
}
}
stage('Apache Configuration') {
steps {
sh 'sudo rm -rf /var/www/html/*'
sh 'sudo cp -r * /var/www/html'
}
}
stage('Restart Apache') {
steps {
sh 'sudo systemctl restart httpd'
}
}
}
}
This pipeline script will clone your Laravel website from your Git repository, install the composer dependencies, set up the environment, copy the files to the Apache server, and restart the Apache server.
Step 4: Run the Jenkins Pipeline To run the Jenkins pipeline, click on the Build Now
button in the pipeline's dashboard. The pipeline will start, and you can see the progress in the Jenkins console output.
Conclusion In this article, we have shown you how to install Jenkins on CentOS and deploy a Laravel website into an Apache server using a Jenkins pipeline. With the help of Jenkins, you can automate the deployment process, saving time and effort. By following these steps, you should be able to deploy your Laravel website quickly and efficiently.
It is essential to note that the sample pipeline script we provided is just an example, and you may need to modify it to fit your specific needs. Also, make sure to follow best practices in configuring and securing Jenkins to ensure a smooth deployment process.
Overall, Jenkins is a powerful tool that can streamline your development process and help you deliver your projects quickly and efficiently. With its extensive plugin ecosystem and easy-to-use interface, it is a popular choice for many developers and teams around the world.