How To Setup PHP MYsql on Mac (Apple Laptops) Easily

Are you wondering how to get your web development kit going on Apple’s OS ? Answer that with a yes and I would say you have arrived at the right place. Here we bring for you the complete guide on how to get the environment for Web Development Up and going on Apple OS .

This article answers how to set up-
1. Apache
2. MySQL
3. PHP
on Apple OS X El Capitan.

So here we go.

1. Apache

Step 1. Set Up Apache

Apache is shipped with the OS. The action starts with launching the Apache Open Terminal. Get that in Applications/Utilities/Terminal or just press + Space and type Terminal.

Done with that, you’ll get to see a command prompt. Type or paste commands directly there and hit enter.

Commands with their applications protected in certain folders need to be entered with the prefix ‘sudo.’ You’ll also need to confirm with your admin password in such cases. There won’t be any response to some commands, and that means that the command is done. For others, you will get proper feedback.
Start Apache: sudo apachectl start
Stop Apache: sudo apachectl stop
Restart Apache: sudo apachectl restart
Find the Apache version: httpd -v

Now you know how to start Apache. The first step is about one more thing. You now need to test whether the web server is working in the browser http://localhost/. In your browser, you need to see the “It Works!??? text. The version that accompanies OS X El Capitan is Apache 2.4.16.

In case you don’t want to run the above test; you can rather troubleshoot Apache to detect any issue within it.

apachectl configtest

Step 2. Document Root/Web Root

The next step is to make a document Root or a Web Root. This is the place where the files from the file system are shared. It’s like the public_html and htdocs.

On OS X El Capitan, you have two web roots. One is at the system level and another at the user level. You can set either one of them or both. System level is global for all users while the user level will allow multiple accounts having their own web roots. Using user level is easier as it doesn’t need to authenticate as admin repeatedly.

System level root: You will find the default root at http://localhost/. The files are shared in the file sharing system at /Library/WebServer/Documents/.

User level root: The other root directory that’s missing by default is Sites folder in the User account. You need to make “Sites??? at the root level of your account. After making the folder, you’ll see that it has an unusual icon. Create the folder and proceed to set up user configuration file.

Now add “username.conf??? filed under: /etc/apache2/users/. You can create it with a short username of your account followed by the suffix .conf. The locations and permissions can be undertaken using the Terminal. The best tool for this can be the text editor nano.

Now go the Terminal and enter following commands.

In Apache user directory: cd /etc/apache2/users

username.conf file: sudo nano username.conf

Now add the following content and replace the ‘username’ with the account name.

AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted

Use Ctrl(⌃) + O to save the file, press Ctrl(⌃) + X to exit and run the following command.

sudo chmod 644 username.conf

Step 3. Loading the Modules

The next step is to load the modules.

Open the main httpd.conf: sudo nano /etc/apache2/httpd.conf

and uncomment the following lines

LoadModule authz_core_module libexec/apache2/
LoadModule authz_host_module libexec/apache2/
LoadModule userdir_module libexec/apache2/
LoadModule include_module libexec/apache2/
LoadModule rewrite_module libexec/apache2/
Include /private/etc/apache2/extra/httpd-userdir.conf

Use Ctrl(⌃) + W for searches, Ctrl(⌃) + O for file save and Ctrl(⌃) + X to exit.


Open httpd-userdir.conf file: sudo nano /etc/apache2/extra/httpd-userdir.conf

Uncomment the following

Include /private/etc/apache2/users/*.conf

And now, Restart the Apache: sudo apachectl restart

You now are ready to use your Sites folder that you created in the previous step: http://localhost/~username/

Replace the username with your account name.

Step 4. Allowing URL Rewrites

It would be better to allow .htaccess files used to override the default settings when using system level root.

Go to httpd.conf and Allow Override to All: sudo nano /etc/apache2/httpd.conf

Search with Ctrl(⌃) + W, save file with Ctrl(⌃) + O, and exit with Ctrl(⌃) + X.

Restart Apache again: sudo apachectl restart

2. PHP

Step 1. Setting Up PHP

PHP is shipped with the OS. Here also you start with setting up open Terminal. The PHP open Terminal is found in /Applications/Utilities/Terminal. or just press ⌘ + Space and type Terminal.

Now open httpd.conf: sudo nano /etc/apache2/httpd.conf

and uncomment the following

LoadModule php5_module libexec/apache2/

Search with Ctrl(⌃) + W, save the file with Ctrl(⌃) + O, and exit with Ctrl(⌃) + X.

Restart Apache: sudo apachectl restart

Step 2. Testing the PHP

The next step would be to test the PHP. Do it in following way.

Go to the Sites folder: cd ~/sites

Create a file: sudo nano index.php

Code into the file.

<?php phpinfo() ?>

Save with Ctrl(⌃) + O, and exit Ctrl(⌃) + X to exit.

Next open http://localhost/~username/

and replace the ‘username’ with the account name.

3. MySQL

Step 1. Download MySQL

Here the very first step would be to download MySQL as it’s not shipped with the OS. Select from the list the Mac OS X (x86, 64-bit) DMG archive version. It would ask to sign up or login but you can just click NO thanks and start the download.

Step 2. Installing

After downloading, you need to install the MySQL. Open the .dmg and run the installer

After installation, give it a temporary password and keep the password safe.

Step 3 Auto Start MySQL

Next, go to System Preference where you can start/stop the MySQL. You can also set it to start MySQL on startup automatically.

Step 4. Start/Stop from command line

Start MySQL: sudo launchctl load -F /Library/LaunchDaemons/

Stop MySQL: sudo launchctl unload -F /Library/LaunchDaemons/

Find MySQL Version: /usr/local/mysql/bin/mysql -v -u root -p

Enter the MySQL root password when asked whi9ch opens a shell interactive dialogue with MySQL. Type q to exit that.

Step 5. Change Your Root Password

This is different from the admin password of OS X. This password is exclusive for the MySQL root user.

Change the password: /usr/local/mysql/bin/mysqladmin -u root -p ‘temppassword’ password ‘newpassword’

Replace the ‘temppassword’ with your created temporary password and ‘new password’ with your personal new password.

Always use single quotes for enclosing the passwords.

Step 6 Fix The 2002 MySQL Socket Error

Next, you have to fix the 2002 socket error. There is a clash between where MySQL places the socket and where the OS thinks it should be. MySQL puts it in /tmp and OS X looks for it in /var/mysql. the socket is an important file as it allows mysql client or server communication.

Do this with

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/MySQL/MySQL.sock

4. phpMyAdmin

Step 1. Downloading phpMyAdmin

Again, phpMyAdmin isn’t shipped with the OS and thus you need to download it. Select its English zip package for download. Even before that, you must fix the 2002 MySQL socket error if not already done.

Step 2. Set the phpMyAdmin Up

Next step is setting up the phpMyAdmin. For that, unzip the package and move the folders to document root level and rename the folder to phpmyadmin.

Create the config folder: mkdir ~/Sites/phpmyadmin/config

Change folder permissions: chmod o+w ~/Sites/phpmyadmin/config

And now, run the setup in browser: http://localhost/~username/phpmyadmin/setup/

You will also have to create a new localhost MySQL server connection. Click the new server and switch to the authentication tab. Next, set the local MySQL root username and password. Remember this would not bbe the OS X admin or root opassword but the MySQL root user.

Dont forget to click on the save. Now a will be in /config directory of PHPMyAdmin directory. Move it to the root level of /phpmyadmin and delete /config directory. Then, open http://localhost/~username/phpmyadmin/, enter username as ‘root’ and the MySQL root password. Now you can interact with the MySQL databases.

Step 3. Upgrade

Next, you have to upgrade the PHPMyAdmin. Download the latest version of it and copy the old ‘’ from the existing directory and replace in the new folder. Always keep the backup of the older one.


Step 1. Site permissions

Ste your web root and all its contents writeable by all. This will help you run your website without any permission issues. It’s local development and hence no security issues.

Now change permission: chmod o+w ~/Sites/phpmyadmin/config

If you have security concerns in setting it globally writable, set the owner as Apache_www, but in that case, you’ll have to authenticate more as admin as you’re not the owner.

sudo chown -R _www ~/Sites/testsite

Apache Virtual Hosts

Step 1. Module in httpd.conf

This starts with loading the module in httpd.conf. Setting up virtual hosts is a lot easier done in Terminal using nano with sudo or as root user. Allow the virtual hosts configuration from Apache config file in httpd.conf.

Open the httpd.conf: sudo nano /etc/apache2/httpd.conf

And uncomment following

Include /private/etc/apache2/extra/httpd-vhosts.conf
LoadModule vhost_alias_module libexec/apache2/

Ctrl(⌃) + W to search, Ctrl(⌃) + O to save and Ctrl(⌃) + X to exit.

Step 2. Setup Virtual Hosts

Open virtual host file: sudo nano /etc/apache2/extra/httpd-vhosts.conf

Example domain would be given for reference. Add the below code in virtual host file

<VirtualHost *:80>
ServerAdmin Owner Email
DocumentRoot “/Users/username/Sites/testsite”

Now replace the ‘owner email’ with your own email and ‘username with your account name.

SAVE WITH Ctrl(⌃) + O and exit with Ctrl(⌃) + X.

Step 3. Mapping to localhost

Now you have to map to the localhost.

Open hosts file: sudo nano /etc/hosts

Add following domain

Save with Ctrl(⌃) + O and exit with Ctrl(⌃) + X.

Restart Apache: sudo apachectl restart

Now open

Step 4. Getting back localhost

Once you have set up your virtual hosts, you lose the old document root at /LIbrary/WebServer/Documents or accessed in http://localhost. You get a 403 Forbidden Error.

Now to solve the issue, you have to add a virtual host to localhost and declare it any other in the same file.

Open virtual host file: sudo nano /etc/apache2/extra/httpd-vhosts.conf

Add localhost:

<VirtualHost *:80>
ServerName localhost
DocumentRoot /Library/WebServer/Documents/

Save with Ctrl(⌃) + O and exit with Ctrl(⌃) + X.

Restart Apache: sudo apachectl restart

Step 5. Change default user for Web Server

The ~/Users/username/Sites folder for virtual hosts has permission issues for updating or authentication. This is so as the default webserver user that runs httpd is known as _www. Bur this will not be the user in local account. If the machine is used and webserver is run only by you, you can change the user.

Find Your User and Group: id

Now you can see some user groups. You now need your primary user uid and group gid names.

uid & gid: uid=502(admin) gid=20(staff)

Now open httpd: sudo nano /etc/apache2/httpd.conf

Here replace ‘admin’ with the name you get from id command.

Save with Ctrl(⌃) + O and exit with Ctrl(⌃) + X.

Restart Apache: sudo apachectl restart


That’s it. You’re all set to use your local web development environment on Apple OS. Play smooth.

Share Your Feedback

Related Post

How To Setup PHP MYsql on Mac (Apple Laptops) Easily

Post navigation

You May Also Like