Vagrant Up With Lamp Server

###Download and Create Project First of all, download VirtualBox and Vagrant. Also do not forget the VirtualBox Extension Pack

After installing, create a directory for project and initialize the directory.

$ mkdir project
$ cd project
$ vagrant init 			# Creates Vagrantfile

Of course, we can follow the documentation to do vagrant box add hashicorp/precise32 but let’s conclude it into our Vagrantfile. Let’s create new provisioning script- in our project root to install apache, PHP, mysql, XDebug, Composer etc.

sudo apt-get update

sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password password root'
sudo debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password root'

sudo apt-get install -y vim curl python-software-properties
sudo add-apt-repository -y ppa:ondrej/php5
sudo apt-get update

sudo apt-get install -y php5 apache2 libapache2-mod-php5 php5-curl php5-gd php5-mcrypt php5-readline mysql-server-5.5 php5-mysql git-core php5-xdebug

cat << EOF | sudo tee -a /etc/php5/mods-available/xdebug.ini

sudo a2enmod rewrite

sed -i "s/error_reporting = .*/error_reporting = E_ALL/" /etc/php5/apache2/php.ini
sed -i "s/display_errors = .*/display_errors = On/" /etc/php5/apache2/php.ini
sed -i "s/disable_functions = .*/disable_functions = /" /etc/php5/cli/php.ini

sudo service apache2 restart

curl -sS | php
sudo mv composer.phar /usr/local/bin/composer

Next, let’s edit the magic Vagrantfile.

# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| = "precise32"
  config.vm.box_url = "" :private_network, ip: ""
  config.vm.provision :shell, :path => ""
  config.vm.synced_folder ".", "/var/www"

This basically talks to Vagrant like this:

$ vagrant up 	# Start the server
# ...... This might takes a long time for the first time. ......
$ vagrant ssh 	# Log into server

Once you’re in, try to play around by checking all the software installed.

php -v
apache2 -v
mysql -u root -p 	# Both username and password are root

You should be seeing something like It Works! if you browse via your Chrome or Firefox. We’ll be able to see the index.php inside /var/www/html directory. Also, the directory /vagrant in virtual machine is synced to /var/www in host machine. ###A more human like address
Next, instead of typing to access our page, we can set it to a url like address. For instance, dev.local. To do this, we need to configure 2 files. First, inside our virtual machine(after SSHed), let’s open up /etc/apache2/sites-enabled/000-default.conf.

# ServerName
# Uncomment it and change it to the URL you like

 ServerName dev.local

 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html

Here we changed the server name to dev.local. Also don’t forget to reload apache.

sudo service apache2 reload

Next, use ctrl+d or cmd+d to logout to our host machine and customize the /etc/hosts file. 		localhost
... 	dev.local 	# Add this line according to your own private 
							# network ip and server name

Try to go to dev.local/ in your browser. You should see Apache2 default page. The default document root is /var/www/html, so to start using our LAMP server, we can delete the default index.php inside the directory and create our own. Of course if you like you can alter your document root. For instance /var/www/.

So we had successful created our LAMP server on vagrant up. It might be a little bit painful at first but once you get comfortable with it, you can create your next project like a boss.

###Shut down the VM Also don’t forget to shut down your virtual machine when you’re done.

$ vagrant suspend 	# super fast shutdown and start
$ vagrant up        # take some extra time to start 
$ vagrant destory   # take more time to start

All of these 3 commands work to shut down a virtual machine with differenct speed. Of course the one take shorter time to shut down and boot up consumes more disk space and RAM.


© 2019 - 2021 · · Theme Simpleness Powered by Hugo ·