See a much cleaner version of this post here: https://gist.github.com/2879466
You should do all your LAMP development in a Virtual Machine
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.
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:
- Edit files in your favorite IDE on your Macintosh
- Test off of your Linux server in a real server environment
- Copy Virtual Machines in their current state, for backup or re-use
- 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.
- Download the latest Ubuntu Server and burn that to a CD.
12.04 LTSis what I use as of this writing.
Hint: Download it via an official torrent: http://releases.ubuntu.com/12.04/, it's much quicker. Choose ubuntu-12.04-server-amd64.iso.torrent: http://releases.ubuntu.com/12.04/ubuntu-12.04-server-amd64.iso.torrent if you're on a MacBook Pro or iMac.
- 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 https://help.ubuntu.com/12.04/serverguide/samba-fileserver.html.
$ 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:
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).
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
Virtual hosts can be created in
/etc/apache2/sites-available and symlinked to
/etc/apache2/sites-enabled to be enabled. Or use this: https://gist.github.com/2710970. Or read this: https://help.ubuntu.com/12.04/serverguide/httpd.html and see how
Alternatively, use one of these scripts: https://gist.github.com/2710970 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.
Edit your Mac's hosts file: http://www.switchingtomac.com/tutorials/how-to-edit-the-hosts-file-in-os-x/ to create fake domains that point to your web server. You'll also need to create virtual hosts: https://help.ubuntu.com/12.04/serverguide/httpd.html#http-basic-settings to make this work.
Install Git: http://evgeny-goldin.com/blog/3-ways-install-git-linux-ubuntu/ on your Ubuntu Server and use it religiously.
$ sudo apt-get install -y git-core
Do you use git-flow? https://github.com/nvie/gitflow
$ sudo apt-get install -y git-flow
Install phpMyAdmin: https://help.ubuntu.com/community/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: https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager !
Install GD Library:
$ sudo apt-get install -y php5-gd
$ sudo apt-get install -y php5-curl