Ansible-playbook实战:基于LNMP安装wordpress

本文详细描述了如何使用Ansible脚本在Linux系统上部署LAMP(Linux,Nginx,MariaDB,PHP)环境,并配置WordPress。涉及的角色包括安装和配置Nginx,MariaDB数据库,PHP,以及创建WordPress数据库和权限设置。
摘要由CSDN通过智能技术生成

1. 目录结构:

roles/
├── lnmp.yaml
├── mariadb
│   └── tasks
│       └── main.yaml
├── nginx
│   └── tasks
│       └── main.yaml
└── php
    └── tasks
        └── main.yaml
​
6 directories, 4 files

2. 配置nginx:

- name: install nginx
  yum:
    name: nginx
​
- name: copy nginx file
  copy:
    src: /opt/nginx.conf
    dest: /etc/nginx/
​
​
- name: Download WordPress
  get_url:
    url: https://cn.wordpress.org/latest-zh_CN.tar.gz
    dest: /opt/latest.tar.gz
 
- name: Extract WordPress
  command: "tar -xf /opt/latest.tar.gz -C /usr/share/nginx/html/"
 
- name: Set permissions for WordPress
  command: "chmod -R 777 /usr/share/nginx/html/wordpress"
​
- name: start nginx
  service:
    name: nginx
    state: started
​

3. 配置mariadb:

- name: Install MariaDB
  yum:
    name:
      - mariadb
      - mariadb-server
​
- name: Start MariaDB service
  service:
    name: mariadb
    state: started
​
- name: Ensure the MariaDB service starts on boot
  service:
    name: mariadb
    enabled: yes
​
- name: Create WordPress database
  mysql_db:
    name: wordpress
    state: present
​
- name: Grant all privileges on wordpress.* to 'wordpress' user
  mysql_user:
    name: wordpress
    password: '123456'
    priv: 'wordpress.*:ALL'
    state: present
​
- name: Grant all privileges to root user from any host
  mysql_user:
    name: root
    password: '123456'
    priv: '*.*:ALL'
    host: '%'
    state: present
​
- name: Flush privileges
  command: mysql -uroot -p123456 -e "FLUSH PRIVILEGES;"

4. 配置php:

- name: remi install
  yum:
    name: https://rpms.remirepo.net/enterprise/remi-release-7.rpm
    validate_certs: no
​
- name: remi start
  command:
    cmd: yum-config-manager --enable remi-php71
​
- name: php install
  yum:
    name:
      - php
      - php-fpm
      - php-mysqlnd.x86_64
​
- name: php start
  service:
    name: php-fpm
    state: started

5. 配置nginx.conf

将nginx.conf放到/opt/nginx.conf

#user  nobody;
worker_processes  1;
​
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
​
#pid        logs/nginx.pid;
​
​
events {
    worker_connections  1024;
}
​
​
http {
    include       mime.types;
    default_type  application/octet-stream;
​
    #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  logs/access.log  main;
​
    sendfile        on;
    #tcp_nopush     on;
​
    #keepalive_timeout  0;
    keepalive_timeout  65;
​
    #gzip  on;
​
    server {
        listen       80;
        server_name  localhost;
​
        #charset koi8-r;
​
        #access_log  logs/host.access.log  main;
​
location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
}
location / {
            root   html;
            index  index.html index.htm index.php;
        }
        #error_page  404              /404.html;
​
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
​
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
​
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
​
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
​
​
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
​
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
​
​
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
​
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
​
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
​
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
​
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
​
}
​

将提前准备好的nginx.conf 配置文件复制到各个主机中:

ansible all -m copy -a 'src=/opt/nginx.conf dest=/opt/'

6. 配置lnmp.yaml调用:

- hosts: all
  remote_user: root
  roles:
   - nginx
   - mariadb
   - php
​

7.启动ansible-playbook:

ansible-playbook roles/lnmp.yaml

8.浏览器访问wordpress

http://xxxxx/wordpress/index.php

  • 8
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值