今天我们继续DevOps内容Jenkins自动化部署PHP项目

1、实验环境

需要准备三台服务器充当:git、jenkins、web

2、准备git仓库

bash 复制代码
[root@git ~]# useradd git
[root@git ~]# echo "123" | passwd --stdin git
[root@git ~]# su - git
上一次登录:三 8月 23 20:41:53 CST 2023pts/0 上
[git@git ~]$ mkdir php.git
[git@git ~]$ cd php.git
#--bare:创建一个裸仓库。裸仓库是一种特殊的Git仓库,它不包含工作目录(没有文件的检出和编辑空间),主要用于共享,是作为远程仓#库被其他仓库推送(push)和拉取(pull)的中心存储库。
[git@git php.git]$ git --bare init
#初始化空的 Git 版本库于 /home/git/php.git/
[git@git php.git]$ exit
#登出

3、上传代码到仓库

bash 复制代码
[root@git ~]# git clone git@192.168.166.9:/home/git/php.git
正克隆到 'php'...
git@192.168.200.111's password: 
warning: 您似乎克隆了一个空版本库。
[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"
[master(根提交) 4ec0ba3] all
 1 file changed, 3 insertions(+)
 create mode 100644 index.php
[root@git php]# git push origin master
git@192.168.166.9's password: 
Counting objects: 3, done.
Writing objects: 100% (3/3), 218 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.166.9:/home/git/php.git
* [new branch]      master -> master

4、部署web主机环境

bash 复制代码
yum install -y nginx php php-mysqlnd mysql-server php-fpm
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;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
...
}
}
systemctl start  php-fpm mysqld nginx

6、基于rsync部署

创建一个Freestyle project

7、 基于ansible部署

bash 复制代码
[root@jenkins ~]# rpm -ivh epel-release-latest-7.noarch.rpm 
[root@jenkins ~]# yum -y install ansible
[root@jenkins ~]# vim /etc/ansible/hosts
[webserver]
192.168.166.6

修改jenkins运行用户

bash 复制代码
[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

添加Ansible插件

bash 复制代码
[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;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf; 
      	.....
[root@web ~]# systemctl restart nginx
相关推荐
科技研学社3 分钟前
中小服装工装厂自动化改造:产线节拍平衡与工位减负实施方案
运维·自动化
ZHOUPUYU18 分钟前
PHP 9.0 前瞻:下一代 PHP 会带来哪些新功能?
android·开发语言·php
人生百态,人生如梦21 分钟前
Ubuntu在低版本下使用ccswitch+claudecode(CCSwitch+Docker+Claude)连调教程
linux·ubuntu·docker·claudecode·ccswitch
byte轻骑兵39 分钟前
【BlueZ】hciconfig 工具:蓝牙适配器管理实操 + 源码实现基础
linux·bluez·电脑蓝牙·嵌入式蓝牙
SKH.1 小时前
Linux驱动-系统移植
linux·服务器
根目录下的猫1 小时前
虚拟机复制过来运行时:“虚拟机使用的此版本,VMware Workstation 不支持的硬件版本。”错误解决办法,亲测可用
linux·运维·服务器·后端
初雪云1 小时前
UniApp 云打包拿到 IPA 后,真正的发布工作才刚开始
ios·自动化·编辑器·产品经理·iphone
yunwei371 小时前
eBPF 入门实践教程十七:编写 eBPF 程序统计随机/顺序磁盘 I/O
linux·后端·性能优化
ZHOUPUYU1 小时前
PHP 8.2 的核心高级特性,包括只读类、类型系统增强
android·性能优化·php
帷幕落秋1 小时前
内存、虚拟内存与 OOM
linux·运维