Log in Register
Home Search Categories About
Bilel Tr
read article

How to Install Jenkins and Deploy a Laravel Website into an Apache Server Using Jenkins Pipeline on CentOS

laravel with jenkins

Continuous Integration and Continuous Deployment (CI/CD) are essential practices for modern web development, enabling teams to deliver features faster and more reliably. Jenkins, a popular open-source automation server, pairs well with Laravel—a powerful PHP framework—to automate testing, building, and deploying applications. In this tutorial, we’ll install Jenkins on CentOS, prepare an Apache server with PHP and Composer, and configure a Jenkins Pipeline that pulls your Laravel code from Git, runs build steps, and deploys to your Apache webroot.

Prerequisites

Before you begin, ensure you have the following in place:

  • A CentOS 7 or 8 server with sudo privileges.

  • Java (OpenJDK 8 or 11) installed, required by Jenkins.

  • Apache HTTP Server (httpd) installed and running.

  • PHP 7.4 or higher, along with Composer and required PHP extensions for Laravel.

  • Git installed on both the Jenkins server and the deployment server.

  • A Laravel application hosted in a Git repository (e.g., GitHub).

Installing Jenkins on CentOS

1. Install Java

Jenkins runs on the Java Virtual Machine, so first install OpenJDK:

sudo yum install java-1.8.0-openjdk-devel -y

This command installs Java 8 on CentOS, which is fully compatible with Jenkins LTS releases. (chathura-siriwardhana.medium.com)

2. Add Jenkins Repository

Import the Jenkins repository and its GPG 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

This enables the stable Jenkins repository on your CentOS server. (utho.com)

3. Install Jenkins

With the repo enabled, install Jenkins:

sudo yum install jenkins -y
sudo systemctl enable --now jenkins

This installs Jenkins and starts it immediately, configuring it to start on boot. (utho.com)

4. Adjust Firewall

Open port 8080 (Jenkins) and 80 (Apache) in the firewall:

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reload

This ensures Jenkins and Apache are accessible externally. (chathura-siriwardhana.medium.com)

5. Initial Jenkins Setup

Visit http://your_server_ip:8080, then unlock Jenkins with the generated password:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Follow the on-screen wizard to install suggested plugins and create the first admin user. (jenkins.io)

Configuring Apache and PHP for Laravel

1. Install Apache, PHP, and Composer

Enable the EPEL and Remi repositories for up-to-date packages:

sudo yum install epel-release yum-utils -y
sudo yum-config-manager --enable remi-php74
sudo yum install httpd php php-cli php-fpm php-mysqlnd php-zip php-gd php-mbstring php-xml composer -y

This installs Apache HTTP Server, PHP 7.4, and Composer. (github.com, sinelogix.com)

Start Apache and enable on boot:

sudo systemctl enable --now httpd

2. Configure Apache Virtual Host

Create an Apache config for your Laravel site (e.g., /etc/httpd/conf.d/laravel.conf):

<VirtualHost *:80>
    ServerName your-domain.com
    DocumentRoot /var/www/laravel/public

    <Directory /var/www/laravel>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/laravel-error.log
    CustomLog /var/log/httpd/laravel-access.log combined
</VirtualHost>

Enable mod_rewrite:

sudo sed -i 's/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf

sudo systemctl restart httpd

This sets up Apache to serve your Laravel public directory. (sinelogix.com)

Creating the Jenkins Pipeline

1. Install Required Jenkins Plugins

In Jenkins, navigate to Manage Jenkins » Manage Plugins and install:

  • Git Plugin

  • Pipeline Plugin

  • SSH Agent Plugin (for deployments)

These plugins allow Jenkins to fetch code, define pipelines, and SSH to your server. (jenkins.io)

2. Define a Jenkinsfile

Add a Jenkinsfile to the root of your Laravel project:

pipeline {
    agent any
    environment {
        DEPLOY_SERVER = 'user@your_server_ip'
        APP_DIR = '/var/www/laravel'
    }
    stages {
        stage('Checkout') {
            steps { git url: 'https://github.com/youruser/your-laravel-repo.git' }
        }
        stage('Install Dependencies') {
            steps {
                sh 'composer install --no-interaction --prefer-dist'
            }
        }
        stage('Run Tests') {
            steps {
                sh 'php artisan test --env=testing'
            }
        }
        stage('Build') {
            steps {
                sh 'npm ci && npm run prod'
            }
        }
        stage('Deploy') {
            steps {
                sshagent (credentials: ['ssh-credentials-id']) {
                    sh "ssh ${DEPLOY_SERVER} 'cd ${APP_DIR} && git pull && composer install --no-dev && php artisan migrate --force'"
                }
            }
        }
    }
    post {
        failure { mail to: 'dev-team@example.com', subject: 'Build Failed', body: 'Check Jenkins logs.' }
    }
}

This pipeline checks out code, installs dependencies, runs tests, builds assets, and deploys via SSH. (dev.to, faun.pub)

3. Create Jenkins Pipeline Job

  1. New Item » Pipeline

  2. Select Pipeline script from SCM

  3. Choose Git, enter your repo URL, and credentials.

  4. Set Script Path to Jenkinsfile.

  5. Save and Build Now.

Jenkins will now automate your full CI/CD flow. (faun.pub)

Post-Deployment Testing and Maintenance

  • Monitor Logs: Check /var/log/jenkins/jenkins.log and your Laravel storage/logs for errors.

  • Automated Rollbacks: Consider adding a rollback stage or using deployment tools like Envoy or Ansible.

  • Backup: Schedule database backups before migrations.

  • Security: Regularly update Jenkins plugins and your server packages.

Conclusion

You’ve successfully installed Jenkins on CentOS, configured Apache and PHP for Laravel, and created a robust Jenkins Pipeline for CI/CD. This setup automates code testing, building, and deployment, accelerating your development cycle and minimizing manual errors. Customize further with notifications, containerization, or blue-green deployments to enhance reliability.

Happy coding and continuous deployment!