您的位置:首页 >LNMP如何实现自动化
发布于2026-04-24 阅读(0)
扫一扫,手机访问

想把LNMP环境部署从繁琐的手工操作中解放出来?核心思路其实很清晰:用一套标准化的自动化流程,彻底替代那些重复且容易出错的人工步骤。整个过程可以拆解为几个关键阶段:从系统准备开始,到组件安装、配置生成,再到服务启动,最后别忘了健康检查、安全加固以及建立长效的监控与备份机制。
那么,具体该选择哪种自动化方式呢?这得看你的场景和规模:
理论说完了,来看看能立刻上手的几种方案。它们各有侧重,总有一款适合你当下的需求。
#!/usr/bin/env bash
set -e
exec > >(tee lnmp_install.log) 2>&1
# 0) 参数
DB_ROOT_PASS="${DB_ROOT_PASS:-YourStrongDBPass!}"
NGINX_CONF="/etc/nginx/conf.d/default.conf"
PHP_TEST="/usr/share/nginx/html/info.php"
# 1) 基础准备
yum update -y
yum install -y 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
# 2) Nginx
systemctl enable --now nginx
# 3) MariaDB
systemctl enable --now mariadb
mysql_secure_installation < "$NGINX_CONF" <<'EOF'
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.php index.html;
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) 测试页
echo "" > "$PHP_TEST"
# 7) 重载与验证
systemctl reload nginx php-fpm
echo "Done. Check: http://$(curl -s ifconfig.me)/info.php"
---
- name: Deploy LNMP on CentOS
hosts: webservers
become: yes
vars:
db_root_pass: "YourStrongDBPass!"
tasks:
- name: Update cache
yum:
name: "*"
state: latest
update_cache: yes
- name: Install packages
yum:
name:
- epel-release
- nginx
- mariadb-server
- mariadb
- php
- php-fpm
- php-mysql
- php-mbstring
- php-xml
- php-gd
- php-opcache
state: present
- name: Enable and start services
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- nginx
- mariadb
- php-fpm
- name: Secure MariaDB
command: >
mysql_secure_installation
args:
stdin: "y\n{{ db_root_pass }}\n{{ db_root_pass }}\ny\ny\ny\ny\n"
- 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;
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;
}
}
- name: Create PHP info
copy:
dest: /usr/share/nginx/html/info.php
content: ""
- name: Reload Nginx
service:
name: nginx
state: reloaded
环境部署好了只是第一步。要让这套系统真正稳定、可靠地跑起来,并且能应对未来的变化,还得把自动化的思维延伸到上线和日常运维中去。
上一篇:如何正确使用deluser命令
下一篇:LNMP如何扩展功能
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9