在开始自动化部署前,需确保服务器满足基础环境要求:
root用户或具有sudo权限的账户操作;wget、vim、gcc、make等编译工具(部分脚本会自动处理)。Shell脚本是CentOS下最常用的自动化部署方式,通过编写脚本自动完成依赖安装→组件下载→配置修改→服务启动全流程。以下是具体步骤:
创建脚本文件(如lnmp_deploy.sh),内容涵盖:
yum仓库自动下载并安装Nginx、MariaDB、PHP;default.conf)、优化PHP-FPM参数;mysql_secure_installation设置数据库root密码、移除匿名用户;#!/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信息页。"
若不想手动编写脚本,可使用社区提供的开源LNMP部署脚本,这些脚本已集成常见功能(如交互式选择、错误处理、卸载/重置),更稳定可靠:
lnmp(Nginx+MySQL+PHP)、lnmpa(Nginx+Apache+MySQL+PHP)等模式选择。wget -c http://soft.vpser.net/lnmp/lnmp1.4.tar.gz && tar zxf lnmp1.4.tar.gz && cd lnmp1.4 && ./install.sh lnmp
curl -O https://github.com/fire-dog/shell-fire/blob/main/lnmp_install_auto.sh
chmod +x lnmp_install_auto.sh
bash lnmp_install_auto.sh # 无人值守安装
bash lnmp_install_auto.sh --uninstall # 卸载
bash lnmp_install_auto.sh --reset # 重置
config.sh文件中,通过source config.sh引入脚本,提高灵活性;set -e让脚本在遇到错误时立即退出,避免继续执行导致更严重的问题;./lnmp_deploy.sh > lnmp_deploy.log 2>&1),便于后续排查问题;若需要批量部署(如多台服务器),可使用Ansible(基于SSH的自动化运维工具),无需在被管理节点安装客户端,通过Playbook定义部署流程。
yum install -y ansible),配置SSH免密登录(被管理节点);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
hosts):指定被管理节点的IP地址。[webservers]
192.168.1.100
192.168.1.101
ansible-playbook -i hosts lnmp.yml
Ansible会自动连接到所有被管理节点,依次执行Playbook中的任务,实现批量自动化部署。
/etc、/var/www),避免脚本误操作导致数据丢失;vault(Ansible)或环境变量加密密码;