Vagrant+AnsibleでPHP、MySQL、Nginxのローカル環境構築

スポンサーリンク
スポンサーリンク

Vagrant がインストールされている状態を前提で進めていきます。
※ついでにLaravelのインストールも同時に行っていきたいと思います。

今回のディレクトリ構成

project/
 ├ server/ (※Laravelインストールディレクトリ)
 ├ Vagrantfile
 └ playbook.yml


$ mkdir project
$ mkdir project/server

Vagrantfile作成


$ cd project
$ vagrant init

Vagrantfile修正


$ vi Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "localhost"
  config.vm.network "private_network", ip: "192.168.33.11"
  config.ssh.insert_key = false
  config.vm.network "public_network"
  config.vm.synced_folder ".", "/vagrant", owner: "vagrant", group: "vagrant", type: "virtualbox"

  # Run Ansible from the Vagrant VM
  config.vm.provision "ansible_local" do |ansible|
    ansible.playbook = "playbook.yml"
  end
end

playbook.yml作成

Ansibleで必要となる構成ファイルとなります。


$ vi playbook.yml

- hosts: all
become: yes
user: vagrant
tasks:
- name: add yum repository
  yum: name="{{ item }}" state=latest
  with_items:
    - epel-release
    - http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

- name: yum basic install
  yum: name="{{ item }}" state=latest
  with_items:
    - zip
    - unzip
    - git
    - mysql
    - nginx

- name: yum php install
  yum: name="{{ item }}" enablerepo=remi-php74 state=latest
  with_items:
    - php
    - php-devel
    - php-mbstring
    - php-pdo
    - php-gd
    - php-mysql
    - php-xml
    - php-fpm

- name: modify php.ini
  replace:
    dest: /etc/php.ini
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  with_items:
    - { regexp: "^;date.timezone =", replace: "date.timezone = Asia/Tokyo" }
    - { regexp: "^expose_php = On", replace: "expose_php = Off" }
    
- name: modify php-fpm config file
  replace:
    dest: /etc/php-fpm.d/www.conf
    regexp: "{{ item.regexp }}"
    replace: "{{ item.replace }}"
  with_items:
    - { regexp: "^user = apache", replace: "user = nginx" }
    - { regexp: "^group = apache", replace: "group = nginx" }
    - { regexp: "^listen = 127.0.0.1:9000", replace: "listen = /var/run/php-fpm/php-fpm.sock" }
    - { regexp: "^;listen.owner = nobody", replace: "listen.owner = nginx" }
    - { regexp: "^;listen.group = nobody", replace: "listen.group = nginx" }        
    
- name: start php-fpm
  systemd:
    name: php-fpm.service
    state: restarted
    daemon_reload: yes
    enabled: yes

- name: install mysql repository
  yum:
    name: http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
    state: present

- name: install mysql
  yum:
    name: mysql-community-server
    state: present

- name: make nginx config file for laravel
  copy:
      dest: /etc/nginx/conf.d/default.conf
      mode: 0644
      content: |
        server {
          listen 80;
          server_name localhost;
          root /vagrant/server/public;

          add_header X-Frame-Options "SAMEORIGIN";
          add_header X-XSS-Protection "1; mode=block";
          add_header X-Content-Type-Options "nosniff";

          index index.html index.htm index.php;

          charset utf-8;

          location / {
            try_files $uri $uri/ /index.php?$query_string;
          }

          location = /favicon.ico { access_log off; log_not_found off; }
          location = /robots.txt  { access_log off; log_not_found off; }

          access_log /var/log/nginx/project-access.log main;
          error_log /var/log/nginx/project-error.log error;

          error_page 404 /index.php;

          location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
          }

          location ~ /\.(?!well-known).* {
            deny all;
          }
        }

- name: start nginx
  systemd:
    name: nginx.service
    state: restarted
    daemon_reload: yes
    enabled: yes

- name: install composer
  command: "{{ item }}"
  with_items:
    - php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
    - php composer-setup.php
    - php -r "unlink('composer-setup.php');"
    - mv composer.phar /usr/local/bin/composer

- name: create laravel project 
  command: /usr/local/bin/composer create-project --prefer-dist laravel/laravel /vagrant/server

- name: change the owner of laravel project
  file:
    path: /vagrant/server
    owner: nginx
    group: nginx
    recurse: yes

- name: change the permission of laravel project
  file:
    path: /vagrant/server/{{ item }}
    mode: 0755
    recurse: yes
  with_items:
    - storage
    - bootstrap/cache

- name: disable selinux
  selinux:
    state: disabled
  register: selinux

- name: reboot
  shell: "sleep 2 && reboot"
  async: 1
  poll: 0
  when: selinux.reboot_required

- name: wait
  wait_for_connection:
    connect_timeout: 20
    sleep: 5
    delay: 5
    timeout: 60
  when: selinux.reboot_required

vagrantの起動


$ vagrant up

仮想環境へ接続し、PHP、Nginx、MySQLがインストールされていることを確認


$ vagrant ssh

PHPのバージョン確認


$ php -v
PHP 7.4.8 (cli) (built: Jul  7 2020 09:27:45) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Nginxのバージョン確認


$ nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017

MySQLのバージョン確認


$ mysql --version
mysql  Ver 8.0.20 for Linux on x86_64 (MySQL Community Server - GPL)

PHP、Nginx、MySQL がインストールされていることが確認できました!

今回、playbook.yml の記述の部分が結構ハマりました。。。
書き方一つでエラーになってしまったりするので、vagrant 起動時にエラーになる場合には、記述を見直してみるの一つかもしれません!

コメント

タイトルとURLをコピーしました