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