February 06, 2015

Web Server & File Permissions in Vagrant

Start using a real web server and set file permissions for development in Vagrant.

^ Ad space to help offset hosting costs :D

Start using a real web server and set file permissions for development in Vagrant.We'll install a web server and see how it interacts with Vagrant.

# Startup and log into Vagrant server
vagrant up
vagrant ssh

# From within the Sagrant server,
Install Apache & PHP
sudo apt-get install -y apache2 libapache2-mod-php5 php5-cli

Edit the PHP configuration to turn on error reporting:

sudo vim /etc/php5/apache2/php.ini

Make the following directives uncommented and to the following values:

error_reporting = E_ALL
display_errors = on
date.timezone = UTC

Save that and then reload Apache:

sudo service apache2 reload

Add an index.php file for Apache to serve out of its default location /var/www/html:

sudo vim /var/www/html/index.php

Add the following:

<?php

file_put_contents('/vagrant/writeout.txt', 'Hello, World!');

Save that and head to https://localhost:8080/index.php in your browser. You should see an error stating that the file /vagrant/writeout.txt could not be written to.

To fix that, we need Apache to run PHP as the "vagrant" user, so it has permission to write to the /vagrant location.

sudo vim /etc/apache2/envvars

Change Apache runs as user/group "vagrant" instead of "www-data":

export APACHE_RUN_USER=vagrant
export APACHE_RUN_GROUP=vagrant

Then restart (not reload) Apache:

sudo service apache2 restart

Run the https://localhost:8080/index.php URL again and see that we don't get an error and it writes to the /vagrant/writeout.txt file!

All Topics