Setup Apache, MySQL, PHP and phpMyAdmin On MAC OS X El Capitan

Objective

Setup Apache, MySQL, PHP and phpMyAdmin On MAC OS X.

Setting Stuff Up

  1. Apache
    1. Setup Apache
    2. Make Web Root
    3. Load Modules
    4. Allow URL Rewrites
  2. PHP
    1. Setup PHP
    2. Test PHP
  3. MySQL
    1. Download MySql
    2. Install MySql
    3. Auto Start MySql
    4. Starting/Stopping from the command line
    5. Change The MySQL Root Password
    6. Fix The 2002 MySQL Socket Error
  4. phpMyAdmin
    1. Download phpMyAdmin
    2. Setup phpMyAdmin
    3. Upgrade phpMyAdmin
  5. Permission
    1. Set Permission for Sites
  6. Virtual Hosts In Apache
    1. Load Module in httpd.conf
    2. Setup Virtual Hosts
    3. Map to localhost
    4. Get back localhost
    5. Change Web Server Default User

With Apple’s new OS X El Capitan here ; how to get the Web Development stack up and running on the OS X El Capitan. This tutorial will go through the process on getting Apache, MySQL, PHP and phpMyAdmin running on the MAC OS X.

This tutorial Setup your Local Web Development Environment Up & Running on OS X El Capitan.

Apache


Step 1 Setup Apache


Apache is shipped with OS X El Capitan. To setup Apache open Terminal which is found in /Applications/Utilities/Terminal. OR press + Space and type Terminal.

For those not familiar with the Terminal, it really isn’t as hard as you may think, once Terminal launched you see with a command prompt waiting for your commands. just type/paste in a command and hit enter, some commands give you no response, it just means the command is done, other commands give you feedback.

Using the prefix of sudo is required for commands that have their applications protected in certain folders. When using sudo you will need to confirm with your administrator password.

Start Apache

sudo apachectl start

Stop Apache

sudo apachectl stop

Restart Apache

sudo apachectl restart

Find the Apache version

httpd -v

The Apache version that comes in OS X El Capitan is Apache 2.4.16

After starting Apache, test to see if the web server is working in the browser, http://localhost/ you should see the “It Works!??? text.

localhost-osx-apache

If you don’t get the localhost test, you can try troubleshooting Apache to see if there is anything wrong in its config file by running.

This will give you an indication of what might be wrong

apachectl configtest

Step 2 Make Web Root


Document root / web root is the location where the files are shared from the file system and is similar to the traditional names of public_html and htdocs.

OS X El Capitan has 2 web roots one at a system level and one at a user level. You can set both up or just run with one, the user level one allows multiple accounts to have their own web root whilst the system one is global for all users.

It is easier to use the user level one as you don’t have to keep on authenticating as an admin user.

System Level Web Root – the default system document root is still found at http://localhost/. The files are shared in the filing system at /Library/WebServer/Documents/.

User Level Root

The other web root directory which is missing by default is the Sites folder in the User account.

You need to make a “Sites??? the folder at the root level of your account and then it will work. Once you make the Sites folder you will notice that it has a unique icon which is a throwback from a few versions older. Make that folder before you set up the user configuration file described next.

site-folder in mac os x

Add a “username.conf??? filed under: /etc/apache2/users/

If you don’t already have one (very likely), then create one named by the short username of the account with the suffix .conf.

Its location and permissions/ownership is best tackled by using the Terminal, the text editor nano would be the best tool to deal with this.

Open terminal and fire the below commands:

Go to user directory in apache

cd /etc/apache2/users

Create username.conf file

sudo nano username.conf

Add this content and replace ‘username’ with your account name

<Directory “/Users/username/Sites/”>
AllowOverride All
Options Indexes MultiViews FollowSymLinks
Require all granted
</Directory>

Press Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

After that run this command

sudo chmod 644 username.conf

Step 3 Load Modules


Open the main httpd.conf

sudo nano /etc/apache2/httpd.conf

Uncomment these lines

LoadModule authz_core_module libexec/apache2/mod_authz_core.so
LoadModule authz_host_module libexec/apache2/mod_authz_host.so
LoadModule userdir_module libexec/apache2/mod_userdir.so
LoadModule include_module libexec/apache2/mod_include.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Include /private/etc/apache2/extra/httpd-userdir.conf

Press Ctrl(⌃) + W for search.
Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

Open httpd-userdir.conf file

sudo nano /etc/apache2/extra/httpd-userdir.conf

Uncomment this line

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

Restart Apache

sudo apachectl restart

Now you can use your Sites folder (Created in Step 2).

http://localhost/~username/

Replace ‘username’ with your account name

if the folder is empty, its look like this

userlevel


Step 4 Allow URL Rewrites


if you are going to use the System Level Web Root it is a good idea to allow any .htaccess files used to override the default settings.

Open httpd.conf and Set AllowOverride to All

sudo nano /etc/apache2/httpd.conf

Press Ctrl(⌃) + W for search.
Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

rewrite url

Restart Apache

sudo apachectl restart

This is already taken care of at the Sites level webroot in step 2.

PHP


Step 1 Setup PHP


PHP is shipped with OS X El Capitan. To setup PHP open Terminal which is found in /Applications/Utilities/Terminal. OR press + Space and type Terminal.

Open httpd.conf

sudo nano /etc/apache2/httpd.conf

Uncomment this line

LoadModule php5_module libexec/apache2/libphp5.so

Press Ctrl(⌃) + W for search.
Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

Restart Apache

sudo apachectl restart

Step 2 Test PHP


Go to Sites folder

cd ~/sites

Create file

sudo nano index.php

Write code in file

<?php  phpinfo() ?>

Press Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

open http://localhost/~username/

Replace ‘username’ with your account name

MySQL


Step 1 Download MySQL


MySQL is not shipped with OS X. So you needs to be dowloaded MySQL. Select the Mac OS X (x86, 64-bit), DMG Archive version from the download list.

it’s asking you to login or signup for download. just click on No thanks, just start my download.


Step 2 Install MySQL


Once download completes open the .dmg and runs the installer.
mysql-osx-install

After the installation is done MySQL give a temporary password, Remember/copy that password.

mysql-root-password


Step 3 Auto Start MySQL


Go to System Preference and you will see MySQL Preference, which you can start/stop MySQL and also you can set to automatically start MySQL on startup.

mysql-auto-start


Step 4 Starting/Stopping from the command line


To Start MySQL

sudo launchctl load -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

To Stop MySQL

sudo launchctl unload -F /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist

Find MySQL Version

/usr/local/mysql/bin/mysql -v -u root -p

Enter your MySQL root password when prompted, the temporary password one you copied earlier (step 2), this also puts you into a shell interactive dialogue with MySQL, type q to exit.


Step 5 Change The MySQL Root Password


This is not the same as the root or admin password of OS X. This is a unique password for the mysql root user.
Change the MySQL root password

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

Replace ‘temppassword’ with temporary password (step 2) and ‘newpassword’ with your new password.
Use the single ‘quotes’ surrounding the password.


Step 6 Fix The 2002 MySQL Socket Error


Fix the 2002 socket error which is linking where MySQL places the socket and where OS X thinks it should be, MySQL puts it in /tmp and OS X looks for it in /var/mysql the socket is a type of file that allows mysql client/server communication.

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

phpMyAdmin


Step 1 Download phpMyAdmin


First fix the 2002 MySQL socket error if you have not done first fix that.

phpMyAdmin is not shipped with OS X. So you need to be download phpMyAdmin. Select the phpMyAdmin zip English package will suit a lot of users.


Step 2 Setup phpMyAdmin


Unzip it and move the folder with its contents into the document root level renaming folder to phpmyadmin.
Create the config folder

mkdir ~/Sites/phpmyadmin/config

Change the folder permissions

chmod o+w ~/Sites/phpmyadmin/config

Run the setup in the browser : http://localhost/~username/phpmyadmin/setup/

phpmyadmin-set-new-server

You need to create a new localhost MySQL server connection,for that click on a new server.

Now switch to the Authentication tab and set the local MySQL root user and the password (MySQL Step 5).This is not the OS X Admin or root password – it is the MySQL root user.

phpmyadmin-set-new-server-mysql-password

phpmyadmin-save-config

Make sure you click on save.

Now a config.inc.php is now in the /config directory of PHPMyAdmin directory.

Now move this file to the root level of /phpmyadmin and then delete the /config directory.

Now open the http://localhost/~username/phpmyadmin/.

Enter username ‘root’ and password (MySQL root password), after that it will allow you to interact with your MySQL databases.

phpmyadmin-4-no-frames


Step 3 Upgrade phpMyAdmin


To upgrade PHPMyAdmin just download the latest version and copy, the older ‘config.inc.php‘ from the existing directory copy into the new folder and replace.

Backup the older one just in case.

Permission


Step 1 Set Permission For Sites


To run a website with no permission issues it is best to set the web root and its contents to be writeable by all since it’s a local development it shouldn’t be a security issue.

Let’s say that you have a site in the Sites folder at the following location ~/Sites/testsite you would set it to be writeable like so:

Change Permission

chmod o+w ~/Sites/phpmyadmin/config

If you are concerned about security then instead of making it world writeable you can set the owner to be Apache _www but when working on files you would have to authenticate more as admin you are not the owner.

sudo chown -R _www ~/Sites/testsite

Virtual Hosts In Apache


Step 1 Load Module in httpd.conf


The process of setting up Virtual Hosts is done easier in the Terminal using nano with sudo or as a root user.

Allow the virtual hosts configuration from the Apache configuration file in httpd.conf

Open the httpd.conf

sudo nano /etc/apache2/httpd.conf

Uncomment these lines

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

Press Ctrl(⌃) + W for search.
Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.


Step 2 Setup Virtual Hosts


Open virtual host file

sudo nano /etc/apache2/extra/httpd-vhosts.conf

In this file example domain already given for our reference.
Add the following code in your virtual host file

<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
ServerAdmin Owner Email
DocumentRoot “/Users/username/Sites/testsite”
</VirtualHost>

Replace ‘Owner Email’ with your email and ‘username’ with your account name.

Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

In the above example, we create test.com virtual host and its document root is in the ~Sites/testsite folder.


Step 3 Map to localhost


Open hosts file

sudo nano /etc/hosts

Add the Domain

127.0.0.1 test.com www.test.com

Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

Restart Apache

sudo apachectl restart

Now open http://test.com


Step 4 Get back localhost


problem in virtual hosts is that once setup you lose your older document root previously at /LIbrary/WebServer/Documents or accessed in the browser at http://localhost what happens is that you get a 403 Forbidden Error.

To solve this, you need to add in a virtual host for localhost and declare this virtual host before any of the others, 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/
</VirtualHost>

Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

Restart Apache

sudo apachectl restart

Step 5 Change Web Server Default User


One of the problem of using the ~/Users/username/Sites folder for virtual host is the permissions issues with things like updates and authentication.

This is because the default webserver user which runs httpd is known as _www, which will not be the user in your local account. If your machine is only in use by you and the web server will run only under your account then you can change the user.

Find Your User and Group :


Command to see your username and group

id

You will get a bunch of user groups, you need your primary user uid and group gid names.

uid & gid like this

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

Open httpd

sudo nano /etc/apache2/httpd.conf

osx-webserver-user

Replace ‘admin‘ with your name that you get from id command.

Ctrl(⌃) + O for save file and then press Ctrl(⌃) + X to exit.

Restart Apache

sudo apachectl restart

That’s it. Now your local web development environment is ready to use on you mac os x.

 

I hope you will find this post very useful regarding  ways in Which Mobile Tech will get hotter in 2016 . Let me know if you have any suggestions/questions  regarding it . I will reply to you ASAP.

Have you got a cool idea about iPhone App Development or Web application Development ? Contact us Now to get a free consultation on your idea. Alphanso Tech is rated as one of the best iPhone App Development Company in India.

Post by Milan Sanathara

I am iOS Application Developer with an aspiration of learning new technology and creating a bright future in Information Technology.

12 Responses to Setup Apache, MySQL, PHP and phpMyAdmin On MAC OS X El Capitan

  1. Pingback: php tutorials with examples | Step by Step tutorial guide

  2. David A. Ryan

    I have been following your tutorial so that I can install PHP on my Mac. There is a program that I need to be able to run. Unfortunately I have reached a deadend after one of the steps. I have created the config.inc.php file and placed it into the phpmyadmin directory. When I then go to http://http://localhost/~username/phpmyadmin/ and it requests the username and password, I get the following error “#2002 – No such file or directoryThe server is not responding (or the local server’s socket is not correctly configured).” The password worked up to this point and I have used root as the username. Any hints as to what to look for?

  3. JP Verm

    ok, working fine ; one thing missing is the IP address used. under el capitan no web sharing anymore under sharing . so no ip address given. where to find?

    • That option seemed to be removed. But what you do is you can allow anyone from the same network to access website’s in your Mac’s Apache.

      Suppose your MAC has ip: 192.168.1.5 and your user name is: jon and the direcotry under apache is demowebsite which holds web-pages.

      Your team mates need to fire up 192.168.1.5/~jon/demowebsite This will allow anyone from the same network to navigate entire website.

      If you want this directory to be accessed via internet. You need to open port 80 on your router and forward it to your MAC ip-address. You can find more info from your router vendor.
      Say your static live ip from ISP is 120.125.30.12 then anyone with this IP address can open demowebsite using 120.125.30.12/demowebsite

      Hope it helps.

  4. Nisaa

    Hi there,

    I am stuck after the last point of the ‘Document Root’ section.

    When I try to view the user level document using http://localhost/~ryanboundy/ I get: safari can’t open the page “localhost/~ryanboundy/” because safari cant connect to the server “localhost”.

    To confirm:
    – I have a ‘Sites’ folder in the root level of my account.
    – I added the comments:

    AllowOverride All
    Options Indexes MultiViews FollowSymLinks
    Require all granted

    – I changed permissions
    – I uncommented the modules listed and restarted apache
    – Apache is running as I get the ‘It works’ message when I type http://localhost into my browser.

    Followed instructions to the letter so no idea why it is not working.
    Not sure if there should be anything in the ‘Sites’ folder? There isn’t in mine.

    Hope someone can help.

    • Nisaa

      Also to add that when I check permissions it says:
      -rw-r–r– 1 root wheel 133

      I believe it should be:
      -rw-r–r– root wheel 298

      using:
      sudo chmod 644 ryanboundy.conf doesn’t seem to change anything

  5. Odhiambo Victor

    All is well until myphpadmin set up, running set up on browser it brings an error, “Forbidden You don’t have permission to access /phpmyadmin/setup/ on this server.”
    Pliiiiz somebody help!!!

Leave a Reply

Your email address will not be published. Required fields are marked *