Using MAMP is probably the easiest way to have PHP, Apache, and MySQL up and running in OS X. You simply put the application in the Application folder, launch the app, and hit the Start Server button and off you go.
But, OS X has actually been shipped with PHP, and Apache built in so why not use them to develop and run websites locally instead of using third-party apps like MAMP or the likes? That way, we will occupy less space for apps, and we will also learn a few things in the process of getting them to work.
Recommended Reading :Beginner’s Guide To WordPress Multisite With MAMP
Step 1: Enable Sublime Text Command Line
First, since I’m using Sublime Text, I would like to enable the Sublime Text command line so I will be able to open files and folders through Terminal, and edit them within Sublime Text. Here’s how to do this.
1. Create a symlink of Sublime Text
subl binary in the ~/bin folder with this command:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl
2. Then run
subl --help command.
It will return the Usage and the list of Arguments that can be used with
subl, as you can see from the screenshot below. In addition, this also ensures that
subl is registered and working.
Step 2: Configure PHP
As mentioned, PHP has already been installed in OS X. You can check the PHP version that is installed in Terminal with the following command:
The PHP module, however, is disabled by default. We have to enable it in order to use and execute PHP scripts in Apache server:
1. Type the following command to open httpd.conf in Sublime Text.
sudo subl /etc/apache2/httpd.conf
2. Uncomment the following line by removing the hash sign
LoadModule php5_module libexec/apache2/libphp5.so
See the following screenshot for more detail:
PHP configurations such as Increasing Memory Limit, File Upload Allowance, and Set the Maximum Upload File Size are done through php.ini file, which also is disabled by default. Let’s enable it.
Go to Terminal and run the following command line. This command copies the php.ini.default and rename it to php.ini.
sudo cp /private/etc/php.ini.default /private/etc/php.ini
Step 3: Configure Apache
OS X is also shipped with Apache. You can type the following command in Terminal to start Apache server.
sudo apachectl start
To stop Apache type:
sudo apachectl stop
http://localhost:8888 in the Browser to verify that the Apache server is working. If it does, it should show “It Works!”.
The default Apache DocumentRoot, where we should put our website documents, is located at /Library/WebServer/Documents. But all my existing web projects reside in /Users/username/Sites. Let’s change the DocumentRoot to that folder:
1. Run the following command lines consecutively to create a user-level configuration file. Change the
username with your OS X shortname account.
cd /etc/apache2/users/ touch username.conf
2. Open this file in Sublime Text
sudo subl username.conf
3. Then, add the following lines in.
DocumentRoot "/Users/username/Sites" <Directory "/Users/username/Sites"> Options All AllowOverride All Order allow,deny Allow from all </Directory>
Change Apache Port Number
Apache, by default, run on port 80. But, since I was using MAMP that uses port
8888, I’m making Apache point to this port number instead. That way, I will not need to change the site urls for all my exisiting websites.
1. Open httpd.conf and comment out the following line by adding a hash sign, so it won’t listen to two ports at the same time.
2. Then open username.conf and add:
Listen 8888 ServerName localhost:8888
3. After all the above configurations, save httpd.conf and username.conf, and type the command below to restart Apache:
sudo apachectl restart
4. Lastly, access
http://localhost:8888 in the browser. It will show all the directories stored within the ~/Sites folder, as shown below.
Run Apache at Startup
You might not want to launch Terminal and type
sudo apachectl start repeatedly to start the Apache server so let’s make it run automatically at startup. To do this, run the following command:
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist
In this tutorial, we have configured PHP and Apache. Technically, we can run a PHP website already. You can test it out by creating a PHP file named phpinfo.php in ~/Sites and put this code
phpinfo() in. Launch the file in the browser:
http://localhost:8888/phpinfo.php. It will show information of the current PHP configuration.
In the next part of this series, we will show you how to install MySQL and make it all work together. So, stay tuned.