架构一、ansible-playbook搭建LNMP与Nginx反向代理

ansible-playbook

1、所需软件包都需要放在root目录下直接执行剧本即可,执行完成后可直接安装论坛
剧本后缀   .yml  .yaml
ansible-playbook test.yml --syntax-check  检测剧本语法
ansible-playbook -C test.myl            检测执行过程
ansible-playbook test.myl              开始执行剧本

Nginx剧本

- hosts: web
  remote_user: root
  tasks:
    - name: unarchive package
      unarchive: src=/root/nginx-1.15.4.tar.gz dest=/root
    - name: yum rely on
      yum: name=gcc,gcc-c++,pcre-devel,openssl-devel,zlib-devel state=installed
    - name: useradd
      user: name=nginx shell=/sbin/nologin
    - name: shell configure
      shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module  && make && make install
      args:
        chdir: /root/nginx-1.15.4
    - name: file
      file: src=/usr/local/nginx/sbin/nginx dest=/usr/sbin/nginx state=link
    - name: shell  echo  nginx
      shell: echo "#!/bin/bash" > /etc/init.d/nginx && chmod +x /etc/init.d/nginx
    - name: lineinfile add nginx line1
      lineinfile: dest=/etc/init.d/nginx line="#chkconfig:2345 88 88"
    - name: lineinfile add nginx line2
      lineinfile: dest=/etc/init.d/nginx line='a="/usr/local/nginx/sbin/nginx"'
    - name: lineinfile add nginx line3
      lineinfile: dest=/etc/init.d/nginx line='b="/usr/local/nginx/logs/nginx.pid"'
    - name: lineinfile add nginx line4
      lineinfile: dest=/etc/init.d/nginx line='case "$1" in'
    - name: lineinfile add nginx line5
      lineinfile: dest=/etc/init.d/nginx line='start)'
    - name: lineinfile add nginx line6
      lineinfile: dest=/etc/init.d/nginx line='$a'
    - name: lineinfile add nginx line7
      lineinfile: dest=/etc/init.d/nginx line='echo "starting...       ok";;'
    - name: lineinfile add nginx line8
      lineinfile: dest=/etc/init.d/nginx line='stop)'
    - name: lineinfile add nginx line9
      lineinfile: dest=/etc/init.d/nginx line='kill -s QUIT $(cat $b)'
    - name: lineinfile add nginx line10
      lineinfile: dest=/etc/init.d/nginx line='echo "stoping...        ok";;'
    - name: lineinfile add nginx line11
      lineinfile: dest=/etc/init.d/nginx line='reload)'
    - name: lineinfile add nginx line12
      lineinfile: dest=/etc/init.d/nginx line='kill -s HUP $(cat $b)'
    - name: lineinfile add nginx line13
      lineinfile: dest=/etc/init.d/nginx line='echo "reloading...      ok";;'
    - name: lineinfile add nginx line14
      lineinfile: dest=/etc/init.d/nginx line=' restart)'
    - name: lineinfile add nginx line15
      lineinfile: dest=/etc/init.d/nginx line='$0 stop'
    - name: lineinfile add nginx line16
      lineinfile: dest=/etc/init.d/nginx line='$0 start ;;'
    - name: lineinfile add nginx line17
      lineinfile: dest=/etc/init.d/nginx line='esac'
    - name: lineinfile add nginx line18
      lineinfile: dest=/etc/init.d/nginx line='exit 0'
    - name: shell chkconfig
      shell: chkconfig --add nginx
    - name: service
      service: name=nginx state=restarted

MySQL剧本

- hosts: web
  remote_user: root
  tasks:
    - name: unarchive package
      unarchive: src=/root/mysql-5.5.22.tar.gz dest=/root/
    - name: yum rely on
      yum: name=gcc,gcc-c++,bison,cmake,ncurses-devel  state=installed
    - name: useradd
      user: name=mysql shell=/sbin/nologin
    - name: shell cmake
      shell: cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1 -DSYSCONFDIR=/etc && make && make install
      args:
        chdir: /root/mysql-5.5.22
    - name: file link libmysqlclient
      file: src=/usr/local/mysql/lib/libmysqlclient.so.18 dest=/usr/local/lib/libmysqlclient.so.18 state=link
    - name: file link mysql
      file: src=/usr/local/mysql/bin/mysql dest=/usr/bin/mysql state=link
    - name: file link mysql
      file: src=/usr/local/mysql/bin/mysqladmin dest=/usr/bin/mysqladmin state=link
    - name: copy my.cnf
      copy: src=/root/mysql-5.5.22/support-files/my-large.cnf dest=/etc/my.cnf remote_src=yes
    - name: copy mysqld
      copy: src=/root/mysql-5.5.22/support-files/mysql.server dest=/etc/init.d/mysqld remote_src=yes
    - name: lineinfile basedir
      lineinfile: dest=/etc/init.d/mysqld line='basedir=/usr/local/mysql'
    - name: lineinfile datadir
      lineinfile: dest=/etc/init.d/mysqld line='datadir=/usr/local/mysql/data'
    - name: file Modify the properties
      file: path=/etc/init.d/mysqld mode=0755 state=file
    - name: shell defaults user
      shell: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
      args:
        chdir: /root/mysql-5.5.22/support-files
    - name: shell chkconfig
      shell: chkconfig --add mysqld
    - name: service
      service: name=mysqld state=started
    - name: shell mysqladmin
      shell: mysqladmin -uroot password 123.com

PHP剧本

- hosts: web
  remote_user: root
  tasks:
    - name: yum
      yum: name=gcc,gcc-c++,gd,libjpeg-devel,libpng-devel,zlib-devel,openssl-devel,pcre-devel,libxml2-devel
    - name: unarchive
      unarchive: src=/root/php-7.2.0.tar.gz dest=/root/
    - name: shell configure
      shell: ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-mysql=/usr/local/mysql --enable-mbstring --with-gd --with-zlib --with-jpeg-dir=/usr/lib --enable-fpm && make && make install
      args:
        chdir: /root/php-7.2.0
    - name: copy php.ini
      copy: src=/root/php-7.2.0/php.ini-development dest=/usr/local/php/php.ini remote_src=yes
    - name: replace
      replace: path=/usr/local/php/php.ini regexp='short_open_tag = Off' replace='short_open_tag = On'
    - name: copy php-fpm
      copy: src=/root/php-7.2.0/sapi/fpm/init.d.php-fpm dest=/etc/init.d/php-fpm remote_src=yes
    - name: file modify the properties
      file: path=/etc/init.d/php-fpm mode=0755 state=file
    - name: copy php-fpm.conf
      copy: src=/usr/local/php/etc/php-fpm.conf.default dest=/usr/local/php/etc/php-fpm.conf remote_src=yes
    - name: replace 
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';pid = run/php-fpm.pid' replace='pid = run/php-fpm.pid'
    - name: replace 
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';emergency_restart_interval = 10' replace='emergency_restart_interval = 20s'
    - name: replace
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';error_log = log/php-fpm.log' replace='error_log = log/php-fpm.log'
    - name: replace
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';emergency_restart_threshold = 0' replace='emergency_restart_threshold = 10'
    - name: replace
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';process.max = 128' replace='process.max = 128'
    - name: replace
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';rlimit_files = 1024' replace='rlimit_files = 1024'
    - name: replace
      replace: path=/usr/local/php/etc/php-fpm.conf regexp=';events.mechanism = epoll' replace='events.mechanism = epoll'
    - name: copy www.conf
      copy: src=/usr/local/php/etc/php-fpm.d/www.conf.default dest=/usr/local/php/etc/php-fpm.d/www.conf remote_src=yes
    - name: replace
      replace: path=/usr/local/nginx/conf/nginx.conf regexp='            index  index.html index.htm;' replace='          index index.php index.html index.htm;'
    - name: shell sed
      shell: sed -i '65,71 s/#/ /' /usr/local/nginx/conf/nginx.conf
    - name: replace
      replace: path=/usr/local/nginx/conf/nginx.conf regexp='             include        fastcgi_params;' replace='             include        fastcgi.conf;'
    - name: shell
      shell: /etc/init.d/php-fpm restart
    - name: service
      service: name=nginx state=restarted
    - name: unarchive
      unarchive: src=/root/ComsenzDiscuz-DiscuzX-master.zip dest=/root
    - name: delete
      shell: rm -rf /usr/local/nginx/html/*
    - name: shell upload
      shell: cp -r /root/DiscuzX/upload  /usr/local/nginx/html
    - name: file
      file: path=/usr/local/nginx/html/upload  mode=0777 recurse=yes

Nginx反向代理

代理两台后端web

- hosts: nginx
  remote_user: root
  tasks:
    - name: unarchive package
      unarchive: src=/root/nginx-1.15.4.tar.gz dest=/root
    - name: yum rely on
      yum: name=gcc,gcc-c++,pcre-devel,openssl-devel,zlib-devel state=installed
    - name: useradd
      user: name=nginx shell=/sbin/nologin
    - name: shell configure
      shell: ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module  && make && make install
      args:
        chdir: /root/nginx-1.15.4
    - name: file
      file: src=/usr/local/nginx/sbin/nginx dest=/usr/sbin/nginx state=link
    - name: shell  echo  nginx
      shell: echo "#!/bin/bash" > /etc/init.d/nginx && chmod +x /etc/init.d/nginx
    - name: lineinfile add nginx line1
      lineinfile: dest=/etc/init.d/nginx line="#chkconfig:2345 88 88"
    - name: lineinfile add nginx line2
      lineinfile: dest=/etc/init.d/nginx line='a="/usr/local/nginx/sbin/nginx"'
    - name: lineinfile add nginx line3
      lineinfile: dest=/etc/init.d/nginx line='b="/usr/local/nginx/logs/nginx.pid"'
    - name: lineinfile add nginx line4
      lineinfile: dest=/etc/init.d/nginx line='case "$1" in'
    - name: lineinfile add nginx line5
      lineinfile: dest=/etc/init.d/nginx line='start)'
    - name: lineinfile add nginx line6
      lineinfile: dest=/etc/init.d/nginx line='$a'
    - name: lineinfile add nginx line7
      lineinfile: dest=/etc/init.d/nginx line='echo "starting...       ok";;'
    - name: lineinfile add nginx line8
      lineinfile: dest=/etc/init.d/nginx line='stop)'
    - name: lineinfile add nginx line9
      lineinfile: dest=/etc/init.d/nginx line='kill -s QUIT $(cat $b)'
    - name: lineinfile add nginx line10
      lineinfile: dest=/etc/init.d/nginx line='echo "stoping...        ok";;'
    - name: lineinfile add nginx line11
      lineinfile: dest=/etc/init.d/nginx line='reload)'
    - name: lineinfile add nginx line12
      lineinfile: dest=/etc/init.d/nginx line='kill -s HUP $(cat $b)'
    - name: lineinfile add nginx line13
      lineinfile: dest=/etc/init.d/nginx line='echo "reloading...      ok";;'
    - name: lineinfile add nginx line14
      lineinfile: dest=/etc/init.d/nginx line=' restart)'
    - name: lineinfile add nginx line15
      lineinfile: dest=/etc/init.d/nginx line='$0 stop'
    - name: lineinfile add nginx line16
      lineinfile: dest=/etc/init.d/nginx line='$0 start ;;'
    - name: lineinfile add nginx line17
      lineinfile: dest=/etc/init.d/nginx line='esac'
    - name: lineinfile add nginx line18
      lineinfile: dest=/etc/init.d/nginx line='exit 0'
    - name: sed i
      shell: sed -i '35i/     upstream webserver {' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '36i/         server 192.168.1.9:80 weight=1 max_fails=3 fail_timeout=10s;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '37i/         server 192.168.1.10:80 weight=1 max_fails=3 fail_timeout=10s;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '38i/         }' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '48i/             index  index.php index.htm;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '49i/             proxy_connect_timeout       30s;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '50i/             proxy_send_timeout          60s;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '51i/             proxy_read_timeout          60s;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '52i/             proxy_buffer_size           32k;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '53i/             proxy_buffers           4   32k;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '54i/             proxy_busy_buffers_size     64k;' /usr/local/nginx/conf/nginx.conf
    - name: sed i
      shell: sed -i '55i/             proxy_pass  http://webserver;' /usr/local/nginx/conf/nginx.conf
    - name: shell chkconfig
      shell: chkconfig --add nginx
    - name: service
      service: name=nginx state=restarted
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值