centos

centos lnmp如何实现自动化部署

小樊
50
2025-10-22 03:22:28
栏目: 智能运维

一、自动化部署前的准备工作

在开始自动化部署前,需确保服务器满足基础环境要求:

二、使用Shell脚本实现自动化部署(手动编写/使用开源脚本)

Shell脚本是CentOS下最常用的自动化部署方式,通过编写脚本自动完成依赖安装→组件下载→配置修改→服务启动全流程。以下是具体步骤:

1. 编写基础Shell脚本(以LNMP 1.4为例)

创建脚本文件(如lnmp_deploy.sh),内容涵盖:

#!/bin/bash
# LNMP自动化部署脚本(基础版)
set -e  # 遇到错误立即退出

# 1. 更新系统并安装依赖
echo "正在更新系统并安装依赖..."
yum update -y && yum install -y epel-release wget vim gcc make pcre-devel openssl-devel libxml2-devel libjpeg-devel libpng-devel freetype-devel zlib-devel mariadb-server mariadb php php-fpm php-mysql php-mbstring php-xml php-gd php-opcache -y

# 2. 安装Nginx
echo "正在安装Nginx..."
yum install -y nginx
systemctl enable nginx --now

# 3. 安装MariaDB(MySQL兼容)
echo "正在安装MariaDB..."
systemctl enable mariadb --now
# 自动执行安全配置(自动输入密码)
DB_ROOT_PASS="your_password"  # 替换为你的数据库密码
mysql_secure_installation <<EOF
y
$DB_ROOT_PASS
$DB_ROOT_PASS
y
y
y
y
EOF

# 4. 安装PHP及扩展
echo "正在安装PHP及扩展..."
yum install -y php php-fpm php-mysql php-mbstring php-xml php-gd php-opcache -y
systemctl enable php-fpm --now

# 5. 配置Nginx支持PHP
echo "正在配置Nginx支持PHP..."
cat > /etc/nginx/conf.d/default.conf <<EOF
server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
    location / {
        try_files \$uri \$uri/ =404;
    }
    location ~ \.php\$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }
}
EOF

# 6. 创建PHP测试页
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/index.php

# 7. 重启服务并验证
systemctl restart nginx php-fpm
echo "LNMP部署完成!请访问 http://服务器IP 查看PHP信息页。"

2. 使用开源LNMP部署脚本(推荐)

若不想手动编写脚本,可使用社区提供的开源LNMP部署脚本,这些脚本已集成常见功能(如交互式选择、错误处理、卸载/重置),更稳定可靠:

3. 脚本优化技巧

三、使用Ansible Playbook实现自动化部署(适合大规模集群)

若需要批量部署(如多台服务器),可使用Ansible(基于SSH的自动化运维工具),无需在被管理节点安装客户端,通过Playbook定义部署流程。

1. 准备Ansible环境

2. 编写Ansible Playbook(lnmp.yml

Playbook使用YAML格式,定义任务(tasks)(如安装组件、配置文件、启动服务):

---
- name: Deploy LNMP on CentOS 7
  hosts: webservers  # 被管理节点组(需在inventory文件中定义)
  become: yes  # 以root权限执行
  tasks:
    # 1. 更新系统
    - name: Update system
      yum:
        name: "*"
        state: latest
        update_cache: yes

    # 2. 安装依赖
    - name: Install dependencies
      yum:
        name:
          - epel-release
          - wget
          - gcc
          - make
          - pcre-devel
          - openssl-devel
          - mariadb-server
          - mariadb
          - php
          - php-fpm
          - php-mysql
          - php-mbstring
          - php-xml
          - php-gd
          - php-opcache
        state: present

    # 3. 启动MariaDB并设置开机自启
    - name: Start and enable MariaDB
      service:
        name: mariadb
        state: started
        enabled: yes

    # 4. 执行MariaDB安全配置
    - name: Secure MariaDB installation
      command: >
        mysql_secure_installation <<EOF
y
your_password
your_password
y
y
y
y
EOF
      args:
        stdin: yes

    # 5. 启动PHP-FPM并设置开机自启
    - name: Start and enable PHP-FPM
      service:
        name: php-fpm
        state: started
        enabled: yes

    # 6. 配置Nginx支持PHP
    - name: Configure Nginx for PHP
      copy:
        dest: /etc/nginx/conf.d/default.conf
        content: |
          server {
              listen 80;
              server_name localhost;
              root /usr/share/nginx/html;
              index index.php index.html index.htm;
              location / {
                  try_files $uri $uri/ =404;
              }
              location ~ \.php$ {
                  fastcgi_pass 127.0.0.1:9000;
                  fastcgi_index index.php;
                  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                  include fastcgi_params;
              }
          }

    # 7. 创建PHP测试页
    - name: Create PHP test page
      copy:
        dest: /usr/share/nginx/html/index.php
        content: "<?php phpinfo(); ?>"

    # 8. 重启Nginx
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted

3. 执行Playbook

Ansible会自动连接到所有被管理节点,依次执行Playbook中的任务,实现批量自动化部署。

四、自动化部署的注意事项

0
看了该问题的人还看了