Ubuntu 16.04 Vagrant PHP 7.0 Nginx

Wed, 18 July 2018

Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.hostname = "Web-Server"
  config.vm.box_url = "https://vagrantcloud.com/ubuntu/boxes/xenial64"
  config.vm.network "forwarded_port", guest: 80, host: 8080
  config.vm.network "forwarded_port", guest: 3306, host: 3306
  config.vm.provision :shell, path: "vagrant/provision.sh"
end
vagrant/nginx.conf
server {
    listen 80;
    index index.php;
    set $root_path '/var/www/public';
    root $root_path;
    try_files $uri $uri/ @rewrite;
    keepalive_timeout 2;
    sendfile off;
    location @rewrite {
        rewrite ^/(.*)$ /index.php?_url=/$1;
    }
    location ~ \\.php {
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index /index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info       ^(.+\\.php)(/.+)$;
        fastcgi_param PATH_INFO       $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }
    location ~ /\\.ht {
        deny all;
    }
    client_max_body_size 100M;
}
vagrant/schema.sql
CREATE DATABASE IF NOT EXISTS `site_db` /*!40100 DEFAULT CHARACTER SET utf8 */;

use site_db;

CREATE TABLE `test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `example_field` varchar(45) NOT NULL
  PRIMARY KEY (`id`),
  UNIQUE KEY `example_field_UNIQUE` (`example_field`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
vagrant/provision.sh
#!/usr/bin/env bash

echo "Beginning provisioning"
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y python-software-properties memcached vim git-core gcc autoconf make zip unzip nginx \\
    build-essential libpcre3-dev php7.0-fpm curl php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick \\
    php7.0-imap php7.0-mcrypt php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl \\
    php7.0-mbstring php-gettext php-memcache php-pear php-gettext php-apcu

mysql --version > /dev/null 2>&1
MYSQL_IS_INSTALLED=$?

if [ ${MYSQL_IS_INSTALLED} -gt 0 ]; then
    sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password password"
    sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password password"
    sudo apt-get install -y mysql-server mysql-client
    sudo service mysql stop
fi

sudo rm /etc/nginx/sites-enabled/default >> /dev/null
sudo cp /vagrant/vagrant/nginx.conf /etc/nginx/sites-enabled/vagrant

sudo sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 1024M/g' /etc/php/7.0/fpm/php.ini
sudo sed -i 's/post_max_size = 8M/post_max_size = 1024M/g' /etc/php/7.0/fpm/php.ini

echo "Configuring nginx"
sudo rm -rf /var/www
sudo ln -s /vagrant /var/www

sudo service nginx restart
sudo service php7.0-fpm restart
sudo service mysql restart

mysql -uroot -ppassword < /vagrant/vagrant/schema.sql
public/index.php
<?php
echo "Yes, it's running";