Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created June 6, 2012 02:24
Show Gist options
  • Star 72 You must be signed in to star a gist
  • Fork 20 You must be signed in to fork a gist
  • Save fideloper/2879466 to your computer and use it in GitHub Desktop.
Save fideloper/2879466 to your computer and use it in GitHub Desktop.
You should develop in a Virtual Machine

#You should do all your LAMP development in a Virtual Machine

##Here's Why:

Many of us develop on Macintoshes. There are many reasons for this, but one of them is that it's based on a Unix platform of some sort. This allows us to run common server software such as Apache, Ruby, Python and Nodejs on our Macs.

Our computers become powerful develoment machines similar to the servers our apps will eventually live on.

Sometime we start our computer only to find Apache won't start, or MySQL can't create a PID file, or we've updated to Mountain Lion and Apache needs to be reconfigured. Death!

Using a Virtual Machine gives us the full power of a Linux server, and the full ability to completely fuck that server up without repercussion.

##Here's How: This is an easy-to-follow guide on setting up a Linux server, getting a LAMP server setup, and sharing files. This will enable you to:

  1. Edit files in your favorite IDE on your Macintosh
  2. Test off of your Linux server in a real server environment
  3. Copy Virtual Machines in their current state, for backup or re-use
  4. Play with configurations or software without fear of messing anything important up

This assumes some knowledge of Linux and Apache. If you're a total beginner, some things might not make sense. Use google, or ask me.

###Step 1: Get Linux My personal favorite is Ubuntu, and that is what this will teach you about. Ubuntu favors a lot of convention over configuration, making getting up and running very easy.

  1. Download the latest Ubuntu Server and burn that to a CD. 12.04 LTS is what I use as of this writing.

Hint: Download it via an official torrent, it's much quicker. Choose ubuntu-12.04-server-amd64.iso.torrent if you're on a MacBook Pro or iMac.

  1. Create a new VM (I use VMware Fusion) and install Ubuntu from the CD. You can choose the Lamp Server and SSH packages during the install, or not. This article assumes you choose no packages. If you do, still follow these directions. You can't mess this up.

Hint: Install OpenSSH if you want to SSH into your virtual machine from another machine on your network. I always install this. I SSH in from Terminal instead of use the VMWare interface because I like how Terminal lets me scroll and copy and paste.

$ sudo apt-get install openssh-server

###Step 2: Setup File Sharing We'll be using SMB to connect to and share files with our Ubuntu server. VMware sets up its own file sharing if you let it, but it can be problematic. Log into your server and get ready to run some commands (Minus the comments after the # below)

1. Install Samba so we can use SMB. This is directly from this guide.

$ sudo apt-get update  #Update the package manager we'll use to install stuff
$ sudo apt-get install -y samba #Install samba, so we can use SMB
$ sudo apt-get install libpam-smbpass #Allow login via your Ubuntu user

2. Know Your IP Address

Find your IP address by viewing the output of this command:

$ ifconfig

You can create a static IP address so you always know this, but its not strictly necessary.

You'll use this in step 4.

3. Configure Samba

$ sudo nano /etc/samba/smb.conf

Once you're editing smb.conf:

  • Find 'security = user' and uncomment it by removing the #. This will make it so you can log in with a user
  • Go to the bottom of the file and add:
[webshare]
    comment = Ubuntu Web Files Share
    path = /var/www
    browsable = yes
    guest ok = no
    read only = no
    create mask = 0755
  • Save your file and exit (ctrl+x, y, enter)

  • Set up /var/www for sharing:

    $ sudo mkdir /var/www
    $ sudo chown your_user:your_user /var/www  #Change 'your_user' to whatever your Ubuntu username is. Be sure you keep the colon in place and use your username twice
  • Restart SMB server with our settings. Then log out, and log back in.
$ sudo restart smbd
$ sudo restart nmbd
$ exit  #IMPORTANT: log out, then relog back in!

4. Connect To Your Shared Folder

Head back out to your Macintosh. In Finder, go to the Go menu, and choose Connect to Server (or use cmd+k).

Connect to: smb://192.168.1.102 - Replace that with whatever your IP address is from step 2.

If everything is OK, you'll be prompted to enter your credentials. Use the username and password that you use to log into Ubuntu. If that connects, you'll find your sharing files from /var/www in Ubuntu with your macintosh. You can edit those files there as you would any other folder or file. If you followed this from the beginning, this is probably empty right now.

4. Install and Setup a LAMP stack.

$ sudo tasksel install lamp-server #Oh, hey, you're done.

This installs a working LAMP stack.

  • Web root is /var/www (Look familiar? That folder is shared with your Mac).
  • Apache config files are found in /etc/apache2.
  • Virtual hosts can be created in /etc/apache2/sites-available and symlinked to /etc/apache2/sites-enabled to be enabled. Or use this. Or read this and see how a2ensite and a2enmod works.
  • Alternatively, use one of these scripts to create a vhost for you
  • Also, do this to turn on mod_rewrite:
$ sudo a2enmod rewrite
$ sudo service apache2 reload

###Step 3: Up your game. You can now point your web browser to your IP address and see the It Works! page.

Try these:

$ sudo apt-get install -y git-core
$ sudo apt-get install -y git-flow
  • Install phpMyAdmin for easier database management (Altho I like MySQL Bench or SequelPro better). Make sure you follow those steps to include the .conf file in your apache config.
  • Play with NodeJS!
  • Install GD Library
$ sudo apt-get install -y php5-gd
  • Install cURL
$ sudo apt-get install -y php5-curl
@nklatt
Copy link

nklatt commented Nov 5, 2013

Thanks for posting this.

FWIW, I was unable to see the VM from my Mac until I set up a static IP address on a host-only adapter. I basically followed the "Set up the network" section of these instructions for that except, as it was the first time I'd used a host-only adapter, I also had to add one of those in the VirtualBox network preferences.

@DavidStrada
Copy link

If I recall correctly you can use $ sudo chown $USER:$USER instead of $ sudo chown your_user:your_user To set allow you to modify /var/www files.

@xeoncross
Copy link

This sounds like a job for vagrant. There is no reason to do all this by hand. Just install vagrant and then use a simple config like this to setup a nice new box with sharing, ssh, and everything already setup:

https://github.com/Xeoncross/simpleserversetup/blob/master/Vagrantfile-Sample

@justinmurphy
Copy link

This was very helpful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment