Virtual machines

Tutorial: Setting up a LAMP Development Environment

In production (the actual public facing website, the ‘product’), a LAMP stack (Linux/Apache/MySQL/PHP) is a common back-end webhosting package. As the abbreviation suggests, it uses a distribution of Linux as the operating system, Apache as the webserver, MySQL as a database and PHP as the back-end programming language for dynamic websites. When I first started learning how to program for the web in PHP, I needed an environment to practice, explore and develop in. I could have just paid a webhosting service, but I didn’t want to upload all of my practice material online, be dependent on an internet service and, frankly, pay for something I wasn’t familiar with. I wanted to develop on my local machine.

Plug-and-play solutions

Setting up a local development machine is difficult for those who are just getting started, especially when one is used to Windows. However, there are some plug-and-play solutions available to get started with your own local WAMP-stack (W for Windows). Popular variants are: EasyPHPXAMPP and WampServer. These packages also include phpMyAdmin, a php-based web interface for easily maintaining and monitoring MySQL databases. Just install the package and the software creates the environment automatically. To get started, put your files in the designated directory, run the WAMP software and go to localhost in your browser. Software settings can be changed via the WAMP software or by editing the relevant ini-files. Don’t forget to restart your WAMP box.

Plug-and-play WAMP software is a great way to get familiar with webdevelopment using Apache, MySQL and PHP. But what if we don’t want to use Apache, but nginx or lighttpd as a webserver? What if we wanted to try a different SQL-database, like PostgreSQL, or a NoSQL variant, like MongoDB? What if we would like to mess around with Linux or use a different programming language?

More advanced solutions

One solution would be to (dual) boot Linux on our machine or on a different machine. We could also put some cash down for an online solution like a virtual private server (VPS) or a dedicated server. If the latter solution seems expensive and the first seems like a lot of work, we could use a virtual Linux environment on our current machine. Of course, we could do this using Windows as the (virtual) OS, but there may be some licensing issues involved. Also, some experience with Linux would not be a bad thing.

Running a virtual machine is an easy way to get an advanced environment. VirtualBox (GPLv2) and VMWare player (free for personal use) are two examples of virtualization software. I often use VMWare player. The great thing about virtual machines is that we can create several on the same physical machine. This way we could create seperate environments that don’t interfere with eachother if we wanted to. It’s pretty much like a contained operating system inside your operating system. The downside to this is that you have to install and configure everything by yourself. The upside is that you learn a lot more about back-end software than just using a plug-and-play solution.

When you instantiate a virtual machine, you need to choose and install an operating system. If you would like to learn the Linux command line, I would recommend a non-commercial distribution without a graphical user interface, such as Ubuntu Server. Download the latest .iso from their website and install it via your virtualization software. Also, make sure the virtual machine is emulating a connection on your network, so you can reach it via your non-virtual machine and – when you’ve set up a webserver correctly – its webbrowser.

By the way, if you are not familiar with the Linux command line, be prepared to google a lot. My tip would be to take note of everything you do and every useful resource you come across. For instance, you could start by setting up a LAMP or LEMP stack (replace Apache with nginx, pronounced engine-x). Just search for ‘Ubuntu server LAMP’ or ‘Ubuntu server LEMP’ and you will be on your way. Or you could get started by checking out my post on installing a LEMP stack on Ubuntu Server.

Another clever way of setting up your development environment is by using Vagrant. The primary advantage of Vagrant is that you can define not only what OS is to be installed but also what other software should be installed using shell or a third-party provisioner like Ansible, Chef, Docker or Puppet. That makes Vagrant a viable solution for when you develop with a team or when you want installable images of your environment. Also, to get a preconfigured PHP Vagrant box, you could check out Phansible or PuPHPet.