ansible部署lnmp

2021/12/12 6:17:15

本文主要是介绍ansible部署lnmp,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

创建项目目录

[root@m01 /server/scripts]# mkdir -p /server/scripts/project-d

创建主机清单hosts文件

[root@m01 /server/scripts/project-d]# vim hosts 
[lb]
172.16.1.5
[web]
172.16.1.7
[db]
172.16.1.51
[nfs]
172.16.1.31
[backup]
172.16.1.41

[data:children]
db
nfs
backup

#[all:vars]
#ansible_ssh_port=22
#ansible_ssh_user=root
#ansible_ssh_pass='123456'

基础环境准备

  •   优化ssh
  •   关闭防火墙
  •    配置yum源

创建目录剧本目录

[root@m01 /server/scripts/project-d]# mkdir -p {basic,ngx-web,php,nfs,db}/{files,tasks,templates,handlers} group_vars/all/
[root@m01 /server/scripts/project-d]# tree
.
├── basic
│   ├── files
│   ├── handlers
│   ├── tasks
│   └── templates
├── db
│   ├── files
    ├── handlers
│   ├── tasks
│   └── templates
├── group_vars
│   └── all
├── hosts
├── nfs
│   ├── files
│   ├── handlers
│   ├── tasks
│   └── templates
├── ngx-web
│   ├── files
│   ├── handlers
│   ├── tasks
│   └── templates
└── php
    ├── files
    ├── handlers
    ├── tasks
    └── templates

27 directories, 1 file

变量:

[root@m01 /server/scripts/project-d/group_vars]# vim all/main.yml 
web_user: nginx

传送的文件

[root@m01 /server/scripts/project-d/ngx-web/templates]# vim nginx.conf.j2 
user {{ web_user }};
worker_processes auto;
#access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

 

ngx-web 任务编辑

[root@m01 /server/scripts/project-d]# vim ngx-web/tasks/mian.yml
- name: add ngx repo
  yum_repository:
    name: "nginx-stable"
    description: "ngx yum repo"
    baseurl: "http://nginx.org/packages/centos/$releasever/$basearch/"
    enabled: yes
    gpgcheck: no
    file: nginx
  tags:
    - add_ngx_repo
- name: install ngx
  yum:
    name: nginx
    state: present
  tags:
    - install_ngx
 
- name: config nginx.conf
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
    backup: yes
  tags:
    - ngx.conf
 
- name: start ngx
  systemd:
    name: nginx
    enabled: yes
    state: restarted
  tags:
    - start_ngx

"ngx-web/tasks/mian.yml" [New] 33L, 576C written       

入口配置文件(主配置文件)

[root@m01 /server/scripts/project-d]# vim top.yml
- hosts: web
  gather_facts: false
  roles:
    - role: ngx-web

测试

[root@m01 /server/scripts/project-d]# ansible-playbook -i hosts -C top.yml 

 

部署php

配置文件

[root@m01 /server/scripts/project-d]# vim php/templates/www.conf.j2
[www]
user = {{ web_user }}
group = {{ web_user }}
  sten = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache
[root@m01 /server/scripts/project-d]# cat php/tasks/main.yml 
#- name: fenfa php.rpm
#  copy:
#    src: php72.tar.gz
#    dest: /server/tools/

- name: mkdir dir
  file:
    path: /server/tools
    state: directory
  tags:
    - mkdir dir

- name: unzip
  unarchive:
    src: php72.tar.gz
    dest: /server/tools/
  tags:
    - copy_untar

- name: install php
  yum:
    name: "{{ item }}"
    state: present
  with_items:
    - /server/tools/php72/GeoIP-devel-1.5.0-14.el7.x86_64.rpm
    - /server/tools/php72/httpd-tools-2.4.6-97.el7.centos.1.x86_64.rpm
    - /server/tools/php72/keyutils-libs-devel-1.5.8-3.el7.x86_64.rpm
    - /server/tools/php72/krb5-devel-1.15.1-50.el7.x86_64.rpm
    - /server/tools/php72/libargon2-20161029-3.el7.x86_64.rpm
    - /server/tools/php72/libcom_err-devel-1.42.9-19.el7.x86_64.rpm
    - /server/tools/php72/libevent-2.0.21-4.el7.x86_64.rpm
    - /server/tools/php72/libkadm5-1.15.1-50.el7.x86_64.rpm
    - /server/tools/php72/libmaxminddb-1.2.0-6.el7.x86_64.rpm
    - /server/tools/php72/libmaxminddb-devel-1.2.0-6.el7.x86_64.rpm
    - /server/tools/php72/libmemcached-1.0.16-5.el7.x86_64.rpm
    - /server/tools/php72/libselinux-devel-2.5-15.el7.x86_64.rpm
    - /server/tools/php72/libsepol-devel-2.5-10.el7.x86_64.rpm
    - /server/tools/php72/libverto-devel-0.2.5-4.el7.x86_64.rpm
    - /server/tools/php72/libwebp-0.3.0-10.el7_9.x86_64.rpm
    - /server/tools/php72/libX11-1.6.7-4.el7_9.x86_64.rpm
    - /server/tools/php72/libX11-common-1.6.7-4.el7_9.noarch.rpm
    - /server/tools/php72/libXau-1.0.8-2.1.el7.x86_64.rpm
    - /server/tools/php72/libxcb-1.13-1.el7.x86_64.rpm
    - /server/tools/php72/libXpm-3.5.12-1.el7.x86_64.rpm
    - /server/tools/php72/lrzsz-0.12.20-36.el7.x86_64.rpm
    - /server/tools/php72/mariadb-5.5.68-1.el7.x86_64.rpm
    - /server/tools/php72/mariadb-server-5.5.68-1.el7.x86_64.rpm
    - /server/tools/php72/mod_php72w-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm
    - /server/tools/php72/nmap-6.40-19.el7.x86_64.rpm
    - /server/tools/php72/nmap-ncat-6.40-19.el7.x86_64.rpm
    - /server/tools/php72/openssl-1.0.2k-22.el7_9.x86_64.rpm
    - /server/tools/php72/openssl-devel-1.0.2k-22.el7_9.x86_64.rpm
    - /server/tools/php72/openssl-libs-1.0.2k-22.el7_9.x86_64.rpm
    - /server/tools/php72/pcre-devel-8.32-17.el7.x86_64.rpm
    - /server/tools/php72/perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm
    - /server/tools/php72/perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm
    - /server/tools/php72/perl-DBD-MySQL-4.023-6.el7.x86_64.rpm
    - /server/tools/php72/perl-DBI-1.627-4.el7.x86_64.rpm
    - /server/tools/php72/perl-IO-Compress-2.061-2.el7.noarch.rpm
    - /server/tools/php72/perl-Net-Daemon-0.48-5.el7.noarch.rpm
    - /server/tools/php72/perl-PlRPC-0.2020-14.el7.noarch.rpm
    - /server/tools/php72/php72w-cli-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-common-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-devel-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-embedded-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-fpm-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-gd-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-mbstring-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-mysqlnd-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-opcache-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-pdo-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-pear-1.10.12-1.w7.noarch.rpm
    - /server/tools/php72/php72w-pecl-igbinary-3.1.2-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-pecl-memcached-3.1.5-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-pecl-mongodb-1.7.4-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-pecl-redis-3.1.6-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-process-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/php72w-xml-7.2.34-1.w7.x86_64.rpm
    - /server/tools/php72/tree-1.6.0-10.el7.x86_64.rpm
    - /server/tools/php72/zlib-1.2.7-19.el7_9.x86_64.rpm
    - /server/tools/php72/zlib-devel-1.2.7-19.el7_9.x86_64.rpm
    
  tags:
    - install_php

- name: fenfa www.conf
  template:
    src: www.conf.j2
    dest: /etc/php-fpm.d/www.conf
    backup: yes
  tags:
    - php_cfg

- name: start php
  systemd:
    name: php-fpm
    enabled: yes
    state: restarted
  tags:
    - start_php

安装数据库

[root@m01 /server/scripts/project-d]# vim db/tasks/main.yml
- name: start db
  systemd:
    name: mariadb
    enabled: yes
    state: restarted

- name: create database blognew
  mysql_db:
    login_user: root
    login_password: 123456
    name: blognew
    state: present

- name: create user lidao007
  mysql_user:
    name: lidao007
    login_user: root
    login_password: 123456
    password: '1'
    host: '{{ item }}'
    priv: '".":All'
    state: present
  with_items:
    - 'localhost'
    - '172.16.1.%'
~                                                                                                                                             
~                                                                                                                                             
"db/tasks/main.yml" [New] 25L, 457C written    

提示缺少模块(把模块安装就行了

 

 

[root@db01 ~]# yum install -y MySQL-python

 



这篇关于ansible部署lnmp的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程