Create Vagrant Box for Laravel (Using Nginx)

Set Up Ubuntu Vagrant Box

Find out what boxes are available

vagrant box list

Add the new newest Ubuntu box (currently xenial/16.04 LTS).

vagrant box add envimation/ubuntu-xenial-docker https://atlas.hashicorp.com/envimation/boxes/ubuntu-xenial-docker

Create a directory for the box and init the ubuntu box.

cd ~/

mkdir testlar

cd testlar

vagrant init envimation/ubuntu-xenial-docker

By default, Vagrant syncs the folder with the Vagrantfile in the host machine with the /vagrant folder in the virtual machine.

Create a file called bootstrap.sh in the same folder as the Vagrant file and add the following.

#!/usr/bin/env bash

apt-get update

Include the bootstrap.sh file in the Vagrantfile.

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/xenial64"
  config.vm.provision :shell, path: "bootstrap.sh"
end

It is a good idea to allocate more memory to the virtual box. I got an error when trying to set up the Laravel project. Add this to the Vagrantfile.

  config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
    vb.memory = "2000"
  end

Allow port forwarding to view web pages using the server we just set up.

config.vm.network :forwarded_port, guest: 80, host: 4567

Also, allow the guest machine to be accessed by a static IP address.

config.vm.network “private_network”, ip: “192.168.50.4”

Now, you can visit 192.168.50.4 in your browser. If you set up Nginx, then you can view your Laravel site via that IP.

If you add the following line to your host machine’s host file, you can visit your Laravel site by visiting testlar.test.

cd ~/etc

sudo vi hosts

192.168.50.4 testlar.test

Create a folder called sites in the root folder (~/testlar).

Add the following to the Vagrantfile to change the permissions of folders in the VM, so that www-data can modify them.

  config.vm.synced_folder "./sites", "/var/www",
      :owner => 'vagrant',
      :group => 'www-data',
      :mount_options => ['dmode=775', 'fmode=775']

Start up vagrant or reload it.

vagrant up

vagrant reload –provision

Install Nginx

Enter the vagrant machine and install nginx.

cd ~/testlar && vagrant ssh

sudo apt-get install nginx

Updating PHP

sudo add-apt-repository ppa:ondrej/php

sudo apt-get update

sudo apt-get install php7.2

sudo apt-get install php7.2-cli php7.2-fpm php7.2-curl php7.2-gd php7.2-mysql php7.2-mbstring zip unzip php7.2-xml

If you don’t install php7.2-mbstring, you will get an error when install Laravel. Also, Nginx depends on some of the libraries above.

Install MySQL

sudo apt-get install mysql-server php7.2-mysql

(The php7.2-mysql package is possibly part of the ppa:ondrej/php repo because it could not be found before enabling that repo.)

Now, we can try connecting to MySQL using SequelPRO.

Here is the location of the vagrant box’s SSH private key for SequelPRO if the username/password vagrant/vagrant doesn’t work: /Users/davidsr/testlar/.vagrant/machines/default/virtualbox/private_key

https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04

To connect to MySQL the first time, use the following command.

sudo mysql

When the MySQL prompt appears, run the following command to check to see if the root user is using the auth_socket plugin. If it is, we will change it to a password.

SELECT user,authentication_string,plugin,host FROM mysql.user;

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

FLUSH PRIVILEGES;

Now, I can use the following info to connect to mysql with SequelPRO.

MySQL Host: 127.0.0.1

Username: root

Password: password

Port: 3306

SSH Host: 192.168.50.4

SSH User: vagrant

SSH Key: /Users/davidsr/testlar/.vagrant/machines/default/virtualbox/private_key

Install Composer

Now, I am going to start following this tutorial: https://www.howtoforge.com/tutorial/install-laravel-on-ubuntu-for-apache/

In Vagrant, run the two commands

sudo apt-get update

sudo apt-get upgrade

curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer

To install Laravel, run the following commands.

cd /var/www

mkdir testlar.test

cd testlar.test

Install Laravel

composer create-project laravel/laravel testlar –prefer-dist

It will take a couple seconds for this command to take affect (no loading)

Configure Nginx

Replace the contents of the file named default with the code below.

cd /etc/nginx/sites-available

sudo vi default

server {
        listen 80;
        listen [::]:80;
        root /var/www/testlar/public;
        index index.php index.html index.htm;

        # Make site accessible from http://localhost/
        server_name testlar.test;

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

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_intercept_errors on;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }

        location ~ /\.ht {
            deny all;
        }
}

Make sure nginx setup is okay.

sudo nginx -t

sudo service nginx restart

If ubuntu is using Apache instead of nginx, you can switch to nginx by disabling apache.

sudo update-rc.d apache2 disable

Now, you should be able to visit dox.test.

Change the permissions for the storage directory. You have to do this on your host machine (not the virtual machine).

Open a new tab in your terminal or exit vagrant and go to the location of this vagrant installation.

exit

cd ~/testlar

sudo chmod -R 777 html/test/storage

Now, visit the site.

http://localhost:4568/

That’s it! Hopefully it will work! I noticed Firefox cached the Nginx welcome page. Try a different browser if you keep seeing that message.

Set Up Node and NPM

http://docs.webrichmedia.com/notes/install-node-nvm-prevents-permission-errors-installing-npm-packages-globally/

Set Up VueJS

If you get the error “SyntaxError: Invalid or unexpected token” after compiling the JS, it may be an error with Nginx. Bullets would be injected at the end of my JavaScript file; the JavaScript file would just end for no reason; and the strange this is that it would work if I renamed the file, but if I made any changes, the error would happen, again.

https://stackoverflow.com/questions/34718498/errors-with-vagrant-and-javascript-possibly-due-to-line-endings

https://stackoverflow.com/questions/12719859/no-visible-cause-for-unexpected-token-illegal

I updated Nginx, and it started working.

cd /etc/nginx

sudo vi nginx.conf

Change sendfile on; to sendfile off;

Troubleshooting

I’ve encountered countless errors when working with Laravel. Here are some references for your benefit if you run into any of the errors that I did.

https://github.com/thomaszbz/native-dockerfiles-typo3/issues/19

http://stackoverflow.com/questions/23540083/failed-to-open-stream-permission-denied-error-laravel

https://laracasts.com/discuss/channels/servers/homesteadvagrant-cant-change-storage-file-permissions-issue

I got a 500 internal server error. If you get a 500 error, the php versions might not be matching up. Particularly important is to change any references to php 7.0 to php 7.2. Laravel 5.6 only works with PHP versions greater than 7.1.3.

You can also echo out PHP details by editing the index.php file in the public folder.

cd /vagrant/html/test/public

sudo vi index.php

Add the following the very top of the file.

phpinfo();

die();

If the version is 7.0, you need to update your Nginx blocks.

cd /etc/nginx/sites-available

sudo vi default

Old: fastcgi_pass unix:/run/php/php7.0-fpm.sock;

New: fastcgi_pass unix:/run/php/php7.2-fpm.sock;

http://stackoverflow.com/questions/31543175/getting-a-500-internal-server-error-on-laravel-5-ubuntu-14-04

My Laravel installation would timeout. Running the following command fixed it.

export COMPOSER_PROCESS_TIMEOUT=600

https://stackoverflow.com/questions/18917768/why-composer-install-timeouts-after-300-seconds

https://laracasts.com/discuss/channels/laravel/laravel-56-parse-error-syntax-error-unexpected-expecting-variable-t-variable?page=1

Upgrade to php 7.2

https://gist.github.com/GhazanfarMir/03bd1f1f770a3834d47274586d46ea62

If routes aren’t working and return a 404, change try_files $uri $uri/ =404; to try_files $uri $uri/ /index.php;

 

 

 

I can visit testlar.test, but it goes to the default NGINX page (/usr/share/nginx/html).

I had to change the server_name from localhost to server_name testlar.test.

Here is the simple configuration for my Vagrantfile.

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

Vagrant.configure("2") do |config|

  config.vm.box = "ubuntu/trusty64"

  config.vm.provision :shell, path: "bootstrap.sh"

  config.vm.network :forwarded_port, guest: 8080, host: 80

  config.vm.network "private_network", ip: "192.168.50.4"

  config.vm.provider "virtualbox" do |vb|

    vb.memory = "2000"
  end

end

I was then able to visit http://testlar.test/ in the browser.

Leave a Reply

Your email address will not be published. Required fields are marked *