본문 바로가기
클라우드 기초/Ansible

<nginx + wordpress + php-fpm + mysql > yml 파일

by 라라쇼퍼 2023. 6. 14.
반응형

nginx + wordpress + php-fpm + mysql 를 설치 및 설정까지 끝내서 웹사이트 접속까지 확인해보겠습니다. 

이번엔 php-fpm 설치가 새롭게 추가되었습니다.! 

---
- name: nginx, wordpress, php-fpm, mysql
  hosts: nginx1
  tasks:
    - name: nginx install
      yum:
        name:
        - wget
        - yum-utils
        - epel-release
        state: present
      ignore_errors: yes

    - name: nginx package
      yum:
        name: nginx
        state: present

    - name: wordress tar download
      get_url:
        url: https://ko.wordpress.org/wordpress-5.8.6-ko_KR.tar.gz
        dest: ./

    - name: arcive & unzip wordpress tar
      unarchive:
        src: wordpress-5.8.6-ko_KR.tar.gz
        dest: ./
        remote_src: yes

    - name: copy wordpress file to /usr/share/nginx/html/
      copy:
        src: "{{ item.src }}"
        dest: "{{ item.dest }}"
        remote_src: yes
      loop:
        - {src: './wordpress/', dest: '/usr/share/nginx/html/'}
        - {src: '/usr/share/nginx/html/wp-config-sample.php', dest: '/usr/share/nginx/html/wp-config.php'}
    - name: php-repository
      yum:
        name: http://rpms.remirepo.net/enterprise/remi-release-7.rpm
        state: present
      ignore_errors: yes

    - name: yum-config-manager
      shell:
        yum-config-manager --enable remi-php74

    - name: install php , php-module
      yum:
        name: "{{ packages }}"
      vars:
        packages:
          - php
          - php-cli
          - php-curl
          - php-mcrypt
          - php-gd
          - php-opcache
          - php-common
          - php-mysqlnd
          - php-fpm

    - name: change /etc/php-fpm.d/www.conf
      lineinfile:
        path: /etc/php-fpm.d/www.conf
        regexp: "{{ item.src }}"
        line: "{{ item.dest }}"
      loop:
        - {src: 'user = apache', dest: 'user = nginx'}
        - {src: 'group = apache', dest: 'group = nginx'}
        - {src: 'listen = 127.0.0.1:9000', dest: 'listen = /run/php-fpm/www.sock'}
        - {src: ';listen.owner = nobody', dest: 'listen.owner = nginx'}
        - {src: ';listen.group = nobody', dest: 'listen.group = nginx'}
    
    - name: nginx.conf fixed
      blockinfile:
        path: /etc/nginx/nginx.conf
        insertbefore: '^(\s+error_page+\s)404 /404.html;'
        block: |
          #babo
                 index    index.php;
                 location ~ \.php$ {
                     try_files $uri =404;
                     fastcgi_pass unix:/run/php-fpm/www.sock;
                     fastcgi_index   index.php;
                     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                     include fastcgi_params;
                 }

    - name: wp-config setting
      replace:
        path: /usr/share/nginx/html/wp-config.php
        regexp: "{{ item.src }}"
        replace: "{{ item.dest }}"
      loop:
        - {src: "database_name_here", dest: "wordpress" }
        - {src: "username_here", dest: "root" }
        - {src: "password_here", dest: "It12345@" }
        - {src: "localhost", dest: "10.0.0.4" }

    - name: start php-fpm nginx
      systemd:
        name: "{{ item }}"
        state: started
        enabled: yes
      loop:
        - php-fpm
        - nginx

    - name: firewall open 80/tcp
      firewalld:
        port: 80/tcp
        state: enabled
        permanent: yes

           /etc/nginx/nginx.conf  에 아래의 내용을 추가할 때 지속적으로 에러가 났었습니다.

해결 포인트는 아래 내용이 server { ~ } ~ 위치에 들어가야 한다는 점입니다.

단, 해당 설정파일 밑에도 주석처리된 똑같은 server { ~ } 가 있으므로, blockinfile이 주석처리된 문단으로 들어가지 않도록 

정규표현식을 이용해야합니다.

주석처리된 동일 내용

                index    index.php;
                 location ~ \.php$ {
                     try_files $uri =404;
                     fastcgi_pass unix:/run/php-fpm/www.sock;
                     fastcgi_index   index.php;
                     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                     include fastcgi_params;

 

또한 index ~ 와 location~ 부분이 들여쓰기가 되도록 기준을 정해주기 위해 block: |  밑에 #주석을 일부러 넣었습니다. 

 

정상적으로 작동되었음을 확인하였습니다. 

반응형

댓글