news 2026/4/15 16:55:59

Jenkins 自动化部署 PHP 项目

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Jenkins 自动化部署 PHP 项目

1. 准备实验环境

搭建包含 Git 仓库主机、Jenkins 主机和 Web 主机的环境。

2. 准备 Git 仓库

  • 在 Git 主机上创建 git 用户并设置密码:
[root@git ~]# useradd git [root@git ~]# echo "123" | passwd --stdin git
  • 切换到 git 用户,创建并初始化裸仓库:
[root@git ~]# su - git [git@git ~]$ mkdir php.git [git@git ~]$ cd php.git [git@git php.git]$ git --bare init [git@git php.git]$ exit

3. 上传代码到仓库

  • 克隆仓库到本地:
[root@git ~]# git clone git@192.168.166.9:/home/git/php.git
  • 创建 PHP 测试文件并提交推送:
[root@git ~]# cd php/ [root@git php]# cat << EOF > index.php <?php phpinfo(); ?> EOF [root@git php]# git add . [root@git php]# git commit -m "all" [root@git php]# git push origin master

4. 部署 Web 主机环境

nginx

  • 安装相关软件:
yum install -y nginx php php-mysqlnd mysql-server php-fpm
  • 配置 nginx.conf 文件,添加 PHP 解析相关配置:
cd /etc/nginx/ mv nginx.conf nginx.conf.back cp nginx.conf.default nginx.conf vim nginx.conf http { ... include conf.d/*.conf; ... server { ... location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; root html; fastcgi_pass php-fpm; fastcgi_index index.php; include fastcgi.conf; } ... } }
  • 启动服务:
systemctl start php-fpm mysqld nginx

5. Jenkins 主机配置密钥认证

  • 切换到 jenkins 用户,生成密钥并分发到 Web 主机和 Git 仓库主机:
[root@jenkins ~]# su -s /bin/bash jenkins bash-4.2$ ssh-keygen bash-4.2$ ssh-copy-id root@192.168.158.5 bash-4.2$ ssh-copy-id git@192.168.158.4

6. 基于 rsync 部署

  1. 在 Jenkins 中创建一个 Freestyle project。
  2. 进行相关配置(具体配置参考相关图示)。
  3. 点击 “build Now” 执行部署。

7. 基于 ansible 部署

  • 在 Jenkins 主机上安装 ansible:
[root@jenkins ~]# rpm -ivh epel-release-latest-7.noarch.rpm [root@jenkins ~]# yum -y install ansible
  • 配置 ansible 主机清单:
[root@jenkins ~]# vim /etc/ansible/hosts [webserver] 192.168.166.6
  • 修改 Jenkins 运行用户:
[root@jenkins ~]# vim /usr/lib/systemd/system/jenkins.service User=root Group=root [root@jenkins ~]# systemctl daemon-reload [root@jenkins ~]# systemctl restart jenkins
  • 设置 SSH 免密登录:
[root@jenkins ~]# ssh-keygen -N '' -f ~/.ssh/id_rsa [root@jenkins ~]# ssh-copy-id root@192.168.158.5 [root@jenkins ~]# ssh-copy-id git@192.168.158.4
  1. 在 Jenkins 中添加 Ansible 插件。
  2. 配置 Web 主机的 nginx.conf 文件,设置网站根目录等信息并重启 nginx:
[root@web ~]# cat /etc/nginx/nginx.conf server { listen 80; listen [::]:80; server_name _; root /usr/share/nginx/html/php-ansible; index index.html index.php; include /etc/nginx/default.d/*.conf; ..... [root@web ~]# systemctl restart nginx

8. 使用 pipeline 部署

  • 创建 Pipeline 项目,编写 Pipeline 脚本:
pipeline { agent any stages { stage('Checkout Code') { steps { // 使用 SSH 方式拉取 Git 代码 git branch: 'master', // 替换为你的分支名称 url: 'git@192.168.158.4:/home/git/discuz.git' // 替换为你的 Git 仓库地址 } } stage('Deploy to Server') { steps { sh 'scp -r ** root@192.168.158.5:/usr/share/nginx/html/ ;' } } } post { success { echo 'Deployment successful!' } failure { echo 'Deployment failed!' } } }
  • 执行部署。

9. Discuz 论坛部署

1. Git 主机准备 Discuz 代码
  1. 克隆 Discuz 代码到本地(以官方仓库为例,或上传本地 Discuz 源码):

    [root@git ~]# cd /home/git/ [root@git git]# git clone https://gitee.com/ComsenzDiscuz/DiscuzX.git discuz [root@git git]# cd discuz
  2. 初始化并推送到私有 Git 仓库

    [root@git discuz]# git init [root@git discuz]# git add . [root@git discuz]# git commit -m "initial discuz code" [root@git discuz]# git remote add origin git@192.168.166.9:/home/git/discuz.git # 关联私有仓库 [root@git discuz]# git push -u origin master # 推送代码
2. Web 主机环境配置
  1. 创建网站根目录并授权(以 Nginx 为例):

    [root@web ~]# mkdir -p /usr/share/nginx/html/discuz [root@web ~]# chown -R nginx:nginx /usr/share/nginx/html/discuz # 确保 Web 服务有权限访问
  2. 配置数据库(创建 Discuz 专用数据库和用户):

    [root@web ~]# mysql -u root -p Enter password: # 输入数据库密码 mysql> CREATE DATABASE discuz_db CHARACTER SET utf8mb4; # 创建数据库 mysql> CREATE USER 'discuz_user'@'localhost' IDENTIFIED BY '123456'; # 创建用户 mysql> GRANT ALL PRIVILEGES ON discuz_db.* TO 'discuz_user'@'localhost'; # 授权 mysql> FLUSH PRIVILEGES; mysql> exit
  3. 调整 PHP 配置(确保满足 Discuz 需求):

    [root@web ~]# vim /etc/php.ini # 修改以下参数(根据实际环境调整) upload_max_filesize = 20M post_max_size = 20M max_execution_time = 300 [root@web ~]# systemctl restart php-fpm # 重启 PHP 服务
3. Jenkins 部署配置(基于 Pipeline)
  1. 修改 Pipeline 脚本(适配 Discuz 部署):

    pipeline { agent any stages { stage('Checkout Code') { steps { // 拉取 Discuz 代码 git branch: 'master', url: 'git@192.168.158.4:/home/git/discuz.git' # 私有仓库地址 } } stage('Deploy to Web Server') { steps { // 同步代码到 Web 主机目录 sh 'scp -r ./* root@192.168.158.5:/usr/share/nginx/html/discuz/' // 远程授权(确保文件权限正确) sh 'ssh root@192.168.158.5 "chown -R nginx:nginx /usr/share/nginx/html/discuz"' } } } post { success { echo 'Discuz deployment successful! Please complete installation via browser.' } failure { echo 'Discuz deployment failed!' } } }
  2. 执行部署:在 Jenkins 中运行 Pipeline 项目,等待代码同步完成。

4. 浏览器完成 Discuz 安装
  1. 访问 Web 主机地址(如http://192.168.158.5/discuz),进入 Discuz 安装向导。
  2. 按照提示完成以下步骤:
    • 同意许可协议,点击 “下一步”。
    • 检查环境(确保所有项均为 “符合”),点击 “下一步”。
    • 选择 “全新安装”,输入数据库信息:
      • 数据库名:discuz_db
      • 数据库用户:discuz_user
      • 数据库密码:123456
    • 设置管理员账号和密码,完成安装。
5. 验证部署
  1. 安装完成后,访问论坛首页确认正常显示。
  2. 登录管理员账号,检查功能是否正常(如发帖、上传附件等)。
  3. 后续代码更新可直接提交到 Git 仓库,Jenkins 会自动触发部署(需配置 Git 钩子或定时构建)。
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/10 21:39:51

Accelerated C++ 终极指南:快速掌握C++核心编程技巧

Accelerated C 终极指南&#xff1a;快速掌握C核心编程技巧 【免费下载链接】AcceleratedC中文英文两版高清下载介绍 Accelerated C 是一本备受推崇的编程书籍&#xff0c;专为具备C或C基础的读者设计&#xff0c;旨在快速提升编程水平。通过高效的讲解方式&#xff0c;本书深入…

作者头像 李华
网站建设 2026/4/9 12:02:09

Langchain-Chatchat保险产品比对:为家庭选择最优保障组合

Langchain-Chatchat保险产品比对&#xff1a;为家庭选择最优保障组合 在当今信息爆炸的时代&#xff0c;一个普通家庭面对市面上琳琅满目的保险产品时&#xff0c;常常陷入“选择困难”——上百页的条款、专业术语堆砌、细微差异难辨。更令人担忧的是&#xff0c;当试图通过AI助…

作者头像 李华
网站建设 2026/4/15 14:45:08

2、Windows XP 电脑个性化设置全攻略

Windows XP 电脑个性化设置全攻略 在使用 Windows XP 系统的电脑时,个性化设置是让电脑更符合自己使用习惯和需求的重要步骤。它不仅能让你拥有专属的使用体验,还能提高工作效率。下面将详细介绍各种个性化设置的方法。 桌面设置 更改背景和屏幕保护程序 :Windows XP 提…

作者头像 李华
网站建设 2026/4/12 16:36:15

2023中国渔业统计年鉴:最完整的渔业数据分析指南

2023中国渔业统计年鉴&#xff1a;最完整的渔业数据分析指南 【免费下载链接】中国渔业统计年鉴2023下载仓库分享 中国渔业统计年鉴2023 下载仓库 项目地址: https://gitcode.com/Open-source-documentation-tutorial/5c539 &#x1f3af; 资源亮点 这份《中国渔业统计…

作者头像 李华
网站建设 2026/4/3 6:34:08

Nextest:革命性的Rust测试性能优化工具

Nextest&#xff1a;革命性的Rust测试性能优化工具 【免费下载链接】nextest A next-generation test runner for Rust. 项目地址: https://gitcode.com/gh_mirrors/ne/nextest 在当今软件开发领域&#xff0c;测试效率直接影响着项目交付速度和质量。Nextest作为专为Ru…

作者头像 李华
网站建设 2026/4/1 14:15:04

OpenCode环境变量终极配置指南:5分钟搞定AI密钥与性能调优

OpenCode环境变量终极配置指南&#xff1a;5分钟搞定AI密钥与性能调优 【免费下载链接】termai 项目地址: https://gitcode.com/gh_mirrors/te/termai 还在为OpenCode连接AI服务失败而困扰&#xff1f;配置文件反复修改却始终无法正常调用&#xff1f;本文将为你提供一…

作者头像 李华