Featured Snippet: Setting up a LAMP stack using MacPorts requires installing Apache, PHP, and MySQL directly via the terminal. By bypassing pre-packaged tools like MAMP, developers gain full control over standard ports and system configurations. Begin by updating MacPorts with sudo port -v selfupdate, then sequentially install Apache2, PHP, MySQL-server, and phpMyAdmin. Configure the document root in httpd.conf to point to your local development directory.
Why Use MacPorts Instead of MAMP?
I wanted to move away from using MAMP since everything works out of the box with the default Mac installations (although Apache is the only one truly active as part of "Web Sharing" under the older systems). Since Mountain Lion (10.8), Web Sharing has been removed and you have to dig into the terminal to run it.
Even before things got a bit more complicated with the removal of simple buttons, I always found MAMP a little bit annoying since it ran on completely different ports than standard ones used by the vast majority of web hosts across the world. Sure, I could configure the ports...but then things got messy, required password entry upon every load...so on and so forth.
MacPorts has been absolutely incredible to me and a joy to use. Setting up Apache, PHP, and MySQL seems a bit intimidating, but hopefully this comprehensive guide will keep that process smooth.
Prerequisites: Developer Tools
Make sure your command line tools are installed! I am doing this on a fresh install of Mavericks (10.9) and quickly discovered I need to make sure I have Xcode and its developer tools. It usually just works without jumping through hoops, but if something fails regarding the compiler, check Xcode.
The Installation Process
If you haven't yet, install MacPorts. You can use their package installer from the site to keep things simple.
Once that's completed, open up your terminal application and execute:
sudo port -v selfupdate
That will simply make sure you have the absolute newest package lists from their servers.
We will begin installing each piece of our stack. Feel free to execute each of these separately or string them all together as one line. Note that if you want to use the newest packages available right now (Apache 2.4 and PHP 5.5...then please use those instead of these listed, replacing the numbers. These are the configurations that are fully tested and active on my system, which I need to mirror heavily customized production servers. I highly advise you match the specific versions of your host! I will try to update this to the newest versions very soon!)
sudo port install apache2
sudo port install php54 +apache2 +mysql55 +pear
sudo port install mysql55-server
sudo port install phpmyadmin +php54
That basically downloaded and installed everything you would need to run a high-performance web server on your computer! Now we have to link everything together nicely so it turns on automatically on reboot and functions with the correct directory configurations.
The Configuration Details
1. Connecting Apache and PHP
We need to make sure Apache is loading PHP. Usually when installing PHP as done above, it will spit out instructions at the end of the script giving you exactly what you need to do...but if you cleared your terminal, here it is:
cd /opt/local/apache2/modules
sudo /opt/local/apache2/bin/apxs -a -e -n php5 mod_php54.so
2. Adjusting Apache Configuration
We need to adjust apache's configuration file. Use whatever editor you like (I'm fond of `nano`):
sudo nano /opt/local/apache2/conf/httpd.conf
Search for your DocumentRoot. It should be "/opt/local/apache2/htdocs". Change this line and the line directly beneath it to a directory you want to hold your websites in. I made a "Sites" folder in my main user's home directory. Let's make it look like this (Change the "rob" to whatever your username is...):
DocumentRoot "/Users/rob/Sites/"
<Directory "/Users/rob/Sites/">
Scroll down a bit from there and we want to change "AllowOverride None" to "AllowOverride All" inside that same <Directory> block.
Scroll down a bit more, and find "DirectoryIndex index.html". We want to add index.php to the end of that.
Search for `#Include conf/extra/httpd-vhosts.conf` and uncomment it.
Now save and exit that file! We can set up MySQL now.
Setting Up MySQL
sudo -u _mysql mysql_install_db55
And we will start the engines:
sudo port load apache2
sudo port load mysql55-server
Note: If you don't want them to automatically load when your system boots up, don't use the "load" command. Instead, just simply type:
sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
sudo /opt/local/etc/LaunchDaemons/org.macports.mysql55-server/mysql55-server.wrapper start
(You will need to manually start them every single time you reboot! You can use "stop" or "restart" instead to achieve other tasks...)
Set your root password for the MySQL server you just started:
/opt/local/lib/mysql55/bin/mysqladmin -u root password 'new-password'
Configuring phpMyAdmin
Now if you want, you can configure your phpmyadmin installation. Since this is simply a local development environment, I don't set up fancy alias directives in my apache configuration file for it or anything. I just dive into `/opt/local/www/phpmyadmin` and copy/paste all the files directly into my DocumentRoot directory `/Users/rob/Sites/phpmyadmin/`.
I do have to configure the software though, so copy `config.sample.inc.php` to `config.inc.php` inside your shiny new `phpmyadmin` directory. You might need to generate a blowfish secret key and drop it into that file along with filling out your root password that you just made a few minutes ago. Other than that, everything should work nicely.
As for virtual hosts, I am currently drafting up another guide focused entirely on my specific local workflow, as that requires a fair amount of configuration with MacPorts/Apache, the Mac Hosts file, and some creative thinking.
Frequently Asked Questions
What is MacPorts and why use it for a LAMP stack?
MacPorts is a package management system for macOS that simplifies the installation of software. Using it for a LAMP stack provides a high-performance, robust development environment without the port conflicts common with pre-packaged solutions like MAMP.
Do I need command line tools for MacPorts?
Yes, command line tools and Xcode developer tools are required to compile and install packages through MacPorts successfully on macOS.
Good luck! If you run into troubles, leave me a comment!
