I stole this from another website:
Install MacPorts
After installing Xcode you can download and install MacPorts. The best way to install it is to use the DMG installer on the MacPorts site. On the other hand if you want to compile it from source then follow these steps: Open up the TerminalI recommend whichever way you install macports you should run this command to make sure everything is up to date:svn co http://svn.macports.org/repository/macports/trunk/base/ cd base ./configure make sudo make install
If nothing as gone wrong so far then you should have MacPorts fully setup. Only one more thing to do before installing PHP/MySQL and Apachesudo /opt/local/bin/port -v selfupdate
Update your system paths
Just so I don’t have to keep on typing /opt/local/bin all the time i’m going add that directory to my PATH. This information can be stored in your .profile which runs every time you start a new terminal. If you don’t have a .profile file use these steps to create one:and that’s that. If you open a new terminal window (yes do that now) then those directories will be searched for terminal commands before the ones in the default path.cd ~ echo 'export PATH=/opt/local/bin:/opt/local/sbin:$PATH' >> .profile
Setting up the LAMP stack
Enable SSL – if you want to!
Before installing PHP, i’m going to make sure that the version of curl installed is compiled with SSL enabled. If you don’t need or care about SSL then you can safely skip this step.You should note that regardless if you run the command above first or one of the ones below then MacPorts will download a whole bunch or ports that are required to install that port or one of its dependencies. Also these commands may take sometime, so give it a while or go make a cup a tea.sudo port install curl +ssl
Install MySQL
This one is fairly simple, well kind of…Note: a lot of blogs recommend installing just the mysql5 port, but if you want the option to have MySQL start automatically at startup you’ll want to install the server. The mysql5-server port installs the mysql5 port anyway, so you can’t lose! If you want to run MySQL at startup then run this command after the port finishes installingsudo port install mysql5-server
Finally to make sure that the database if fully setup you must run this command (before starting up any mysql process!)sudo port load mysql5-server
And make sure you pay attention to its instructions! Personally I didn’t do anything here as this is a development setup for me, so I prefer to keep my settings loose. Although if you want MySQL commands in your terminals path (like port) then run these commands and start a new terminal:sudo -u _mysql mysql_install_db5
Create /opt/local/etc/mysql5/my.cnf, add the following to it and savecd ~ echo 'export PATH=/opt/local/lib/mysql5/bin:$PATH' >> .profile
Also to remain compatible with other programs that may have been coded to look for the socket file in its original location then add this symbolic link:[mysqld_safe] socket = /tmp/mysql.sock
sudo ln -s /tmp/mysql.sock /opt/local/var/run/mysql5/mysqld.sock
Install PHP & Apache
Ok we’re finally installing PHP with MySQL support and Apache (FastCGI is just being installed separately for use with some debugging and profiling tools).Like MySQL if you want Apache to start automatically when you start your computer then run this command after the port installs (as shown while the ports are installing)sudo port install php5 +apache2 +pear +fastcgi php5-mysql +mysqlnd
sudo port load apache2
Configure PHP
To customize php, copy /opt/local/etc/php5/php.ini-development (if this is a development server) or /opt/local/etc/php5/php.ini-production (if this is a production server) to /opt/local/etc/php5/php.ini and then make changes (see below). i.e. for development:i.e. for production:cd /opt/local/etc/php5/ sudo cp php.ini-development php.ini
To customize php edit (using your favourite editor) /opt/local/etc/php5/php.ini to configure it properly, make sure you set the correct timezone (for myself it’s this):cd /opt/local/etc/php5/ sudo cp php.ini-production php.ini
date.timezone = Europe/London
Configure Apache
If this is your first install, you need to activate PHP in your web server.To enable PHP in Apache, runOpen /opt/local/apache2/conf/httpd.conf in your favourite text editor (it’s vi for me, but feel free to use pico, emacs or whatever!).cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
If you want to change the default MacPorts Apache document root to match default Apple’s Apache (personally I didn’t, but I found this information on another blog – so it might be useful to some), change:sudo vi /opt/local/apache2/conf/httpd.conf
to:DocumentRoot "/opt/local/apache2/htdocs"
If you changed the DocumentRoot, change the Directory directive from:DocumentRoot "/Library/WebServer/Documents"
to<Directory "/opt/local/apache2/htdocs">
For PHP to work correctly you must add index.php to the dir_module directive:<Directory "/Library/WebServer/Documents">
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
Add a new mimetype so that Apache will direct files ending in .php to the PHP module for processing. Add the following within the <IfModule mime_module> block. Without this, all you’ll see is the text of your PHP scripts
And finally, to enable user directories, uncomment:AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps
But if you are going to use virtual hosts (as described below) then uncomment this line in addition to (or instead of) the line above:Include conf/extra/httpd-userdir.conf
Save and close the httpd.conf file.Include conf/extra/httpd-vhosts.conf
Advanced Post-Configuration
Configure Apache Virtual Hosts
Now unless you just want to access your new web server using http://localhost you might want to set up some virtual hosts for Apache. I personally only need one virtual host atm i.e. http://testing.example.com. Virtual hosts need to be configured in two places, the hosts file (/etc/hosts) and the Apache virtual hosts file (/opt/local/apache2/conf/extra/httpd-vhosts.conf). Firstly add this line to /etc/hosts after the localhost has been defined i.e.Then you must edit the virtual hosts file /opt/local/apache2/conf/extra/httpd-vhosts.conf and add this configuration127.0.0.1 localhost 127.0.0.1 testing.example.com
The document root can be anything you want as long as apache has permissions to read that directory i.e. in this case you can:<VirtualHost *:80> ServerAdmin webmaster@testing.example.com DocumentRoot "/opt/local/apache2/vhosts/testing.example.com" ServerName testing.example.com ServerAlias www.testing.example.com ErrorLog "logs/testing.example.com-error_log" CustomLog "logs/testing.example.com-access_log" common </VirtualHost>
You can do this process for as many virtual hosts as you want. Also I personally keep all my websites stored in my user directory – yes bad on a server, but this is my personal development machine. Finally don’t forget to comment out or remove the example vhosts from http-vhost.conf or you’ll get warnings while starting up Apache, they are harmless to leave in, but it’s probably for the best to remove any potential errorssudo mkdir -p /opt/local/apache2/vhosts/testing.example.com cd /opt/local/apache2/vhosts/ sudo chmod -R 755 testing.example.com
Installing extra PHP modules
There are plenty of extra PHP modules available from MacPorts, you can get a list of them by using this command. Although you won’t need any of the MySQL ones as you’ve already compiled it into PHP.Here is the list of PHP5 ports I installedport search php5-
sudo port install php5-openssl php5-curl php5-gd php5-iconv php5-http php5-mcrypt php5-xdebug
Updating MacPorts
MacPorts is simple to update, every few weeks I just run the command below to check for any outdated ports (i’ll also update this post with any changes to this process I discover)to update all installed ports to the latests and greatestsudo port outdated
sudo port upgrade outdated