最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Ubuntu PHP怎样实现持续集成与持续部署
时间:2026-07-08 10:22:51 编辑:袖梨 来源:一聚教程网
Implementing Continuous Integration and Continuous Deployment (CI/CD) for PHP on Ubuntu

Continuous Integration (CI) and Continuous Deployment (CD) are essential for streamlining PHP development workflows, reducing manual errors, and accelerating delivery. Below is a structured guide to setting up CI/CD for PHP on Ubuntu, covering tools, configurations, and best practices.
1. Prerequisites
Before configuring CI/CD, ensure your Ubuntu environment has:
- PHP installed (e.g.,
sudo apt install php php-cli php-mbstring php-gd php-xmlfor common extensions). - Composer (PHP dependency manager) installed globally (
curl -sS https://getcomposer.org/installer | php && sudo mv composer.phar /usr/local/bin/composer). - A version control system (e.g., Git) initialized in your project (
git init). - A web server (Nginx/Apache) configured to serve your PHP project (e.g., Nginx virtual host pointing to
/var/www/myproject).
2. Choose a CI/CD Tool
Select a tool that aligns with your team’s needs:
- GitHub Actions: Native to GitHub, easy to set up for PHP projects with pre-built actions.
- GitLab CI: Integrated with GitLab, supports complex pipelines with auto-devops.
- Jenkins: Open-source and highly customizable, requires manual setup but offers flexibility.
- Travis CI: Cloud-hosted, simple for small projects but limited to GitHub repositories.
3. Configure CI/CD Pipelines
Option A: GitHub Actions (Recommended for GitHub Projects)
Create a .github/workflows/php-ci.yml file in your project root. This workflow automates testing and deployment on every push to the master branch:
name: PHP CI/CDon:push:branches: [ master ]jobs:build-test-deploy:runs-on: ubuntu-lateststeps:# Checkout code- uses: actions/checkout@v3# Set up PHP (specify version and extensions)- uses: shivammathur/setup-php@v2with:php-version: '8.0'extensions: mbstring, gd, curl# Install dependencies- run: composer install --no-interaction --prefer-dist --optimize-autoloader# Run tests (PHPUnit required)- run: vendor/bin/phpunit# Deploy to server (via SSH)- name: Deploy to Serverrun: scp -r . user@your-server-ip:/var/www/myprojectenv:SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}- Key Steps:
shivammathur/setup-php: Configures PHP and extensions.composer install: Installs project dependencies.vendor/bin/phpunit: Runs unit tests (ensurephpunit.xmlexists).scp: Deploys files to the server via SSH (store the private key in GitHub Secrets).
Option B: Jenkins (For Customizable Pipelines)
Install Jenkins on Ubuntu (sudo apt install jenkins) and create a Jenkinsfile in your project:
pipeline {agent anystages {stage('Build') {steps {checkout scmsh 'composer install'sh 'php artisan key:generate' // If using Laravel}}stage('Test') {steps {sh 'vendor/bin/phpunit'}}stage('Deploy') {when {branch 'master'}steps {sh 'ssh deploy@your-server-ip "cd /var/www/myproject && git pull && composer install && systemctl restart nginx"'}}}}- Key Steps:
checkout scm: Pulls code from Git.composer install: Installs dependencies.ssh: Executes deployment commands on the server (e.g.,git pull,composer install).
Option C: GitLab CI (For GitLab Repositories)
Create a .gitlab-ci.yml file:
stages:- build- test- deploybuild_job:stage: buildscript:- composer installartifacts:paths:- vendor/test_job:stage: testscript:- vendor/bin/phpunitdeploy_job:stage: deployonly:- masterscript:- ssh deploy@your-server-ip "cd /var/www/myproject && git pull && composer install && systemctl restart nginx"4. Key Practices for Effective CI/CD
- Automate Testing: Use PHPUnit for unit/integration tests—fail the pipeline if tests fail.
- Use Environment Variables: Store sensitive data (e.g., database credentials, SSH keys) in CI/CD secrets (GitHub Secrets, GitLab CI/CD Variables).
- Version Control Configuration: Track CI/CD pipeline files (
.github/workflows/,Jenkinsfile) in Git to ensure reproducibility. - Optimize Dependency Management: Use
composer install --prefer-dist --optimize-autoloaderto speed up deployments. - Monitor Pipeline Performance: Regularly review pipeline logs to identify bottlenecks (e.g., slow tests, dependency issues).
5. Post-Deployment Steps
After successful deployment, automate post-deployment tasks:
- Restart Services: Restart PHP-FPM (
systemctl restart php8.0-fpm) and Nginx/Apache to apply changes. - Clear Caches: Run
php artisan cache:clear(Laravel) or equivalent for your framework. - Notify Team: Send alerts (e.g., Slack, email) via CI/CD tools to confirm deployment success.
By following these steps, you can establish a robust CI/CD pipeline for PHP on Ubuntu, enabling faster, more reliable deployments while maintaining code quality.
相关文章
- 异环收集冷气怎么操作 异环收集冷气全流程图文攻略 07-08
- 班级小管家怎么进行收款 07-08
- WooCommerce Shipping Tracking v42.2 汉化版 物流跟踪 07-08
- 《背包英雄》艰难时刻成就解锁指南 07-08
- APK 瘦身与混淆:让安装包更小 更安全 更易维护 07-08
- WorkBuddy 实战教程:用 AI 让日常办公任务自动化的完整指南 07-08