使用playbook实现一键安装nginx+PHP

环境

主机名 wanIP lanIP 服务 角色
web01 10.0.0.7 172.16.1.7 nginx服务端 被控端
web02 10.0.0.8 172.16.1.8 nginx服务端 被控端

流程分析

1.安装ansible
2.优化ansible
3.推送公钥
4.开启防火墙
5.开启80 443 873 nfs等端口和服务白名单
6.关闭selinux
7.创建同一的用户
	1.安装nginx
	2.拷贝nginx配置文件
	3.拷贝nginx server
	4.创建站点目录
	5.编辑默认页面
	6.启动nginx

主机清单

mkdir /root/ansible/nginx/ -p && vim /root/ansible/nginx/hosts

[web_group]
web01 ansible_ssh_host=172.16.1.7 asible_ssh_user=root ansible_ssh_port=22
web02 ansible_ssh_host=172.16.1.8 asible_ssh_user=root ansible_ssh_port=22

上传做好的nginx_php(rpm)包

cd / && rz

nginx配置文件

vim /root/ansible/nginx/nginx.conf 

user  www;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

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

站点目录

vim /root/ansible/nginx/html
123

nginx server

mkdir /root/ansible/nginx/conf.d/ -p && vim /root/ansible/nginx/conf.d/wp.conf

server {
	listen 80;
	server_name cs.wp.com;
	root /code;
	index index.html;
 
	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;
		}
}

yml

vim /root/ansible/nginx/nginx.yml

- hosts: all
  tasks:
    - name: Install Nginx Server
      yum:
        name: nginx
        state: present

    - name: Configure Nginx Conf
      copy:
        src: /root/ansible/nginx/nginx.conf
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: 0644
      when: ansible_hostname is match "web*" 

    - name: Nginx Server
      copy:
        src: /root/ansible/nginx/conf.d/wp.conf
        dest: /etc/nginx/conf.d/wp.conf
        owner: root
        group: root
        mode: 0644
      when: ansible_hostname is match "web*" 


    - name: Create web index.html
      copy:
        src: /root/ansible/nginx/html
        dest: /code/index.html
        owner: www
        group: www
        mode: 0644
      when: ansible_hostname is match "web*" 

    - name: Create HTML Directory
      file:
        path: /code/wordpress
        owner: www
        group: www
        mode: 0755
        state: directory
        recurse: yes
      when: ansible_hostname is match "web*" 

    - name: Start Nginx Server
      service:
        name: nginx
        state: started
        enabled: true

    - name: jieya nginx_php.tar.gz
      unarchive:
        src: /root/nginx_php.tar.gz
        dest: /root

    - name: install php
      shell: "{{ item }}"
      with_items:
        - "yum remove -y php-common.x86_64"
        - "yum localinstall -y /root/rpm/*rpm"
      when: ansible_hostname is match "web*"
      
    - name: Configure php Conf
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        owner: root
        group: root
        mode: 0644
      with_items:
        - { src: "/root/ansible/nginx/php.ini",dest: "/etc" }
        - { src: "/root/ansible/nginx/www.conf",dest: "/etc/php-fpm.d/"}
      when: ansible_hostname is match "web*"  

    - name: Start php Server
      service:
        name: php-fpm
        state: started
        enabled: true
      when: ansible_hostname is match "web*"  

执行

1.执行base.yml
[root@m01 ~]# ansible-playbook ansible/base.yml 

2.执行rsync.yml
[root@m01 ~]# ansible-playbook ansible/nginx/nginx.yml -i /root/ansible/nginx/hosts

3.域名解析

4.客户端重启nginx PHP即可访问cs.wp.com网站

使用playbook实现一键安装nginx+PHP

上一篇:屌炸天实战 MySQL 系列教程(二) 史上最屌、你不知道的数据库操作


下一篇:PHP配置文件www.conf