Installing Apache, PHP, and MySQL on Mac OS X

Apache and PHP come packaged with OS X. To create a local web server, all you need to do is configure Apache and install MySQL.

1. Apache

Enable Apache on Mac OS X

To check what version of apache you have currently installed on your mac just open you terminal and type  httpd -v

Verify if apache is running by accessing http://localhost:

If you get “This site can’t be reached”, you need to start apache, just type,sudo apachectl start and press enter:

Verify localhost again:

if you see “It works!” apache is running.

Apache Commands

You can start, stop, and restart your server by typing:

sudo apachectl start 
sudo apachectl stop  
sudo apachectl restart

Setting up root directory

Apache’s default directory is  /Library/WebServer/Documents that is where `It work’s!` file is located at.

Move into apache users folder:

Type ls and check if you have a config file named after your yourusername.conf  username, if you can’t find one, that’s ok, then just type  sudo nano yourusername.conf

Add this configuration to your file:

Press “Ctrl + x” to exit, “y” to save changes and hit enter.

Then to give apache permissions to read we type sudo chmod 644 YourUserName.conf and press enter, lastly we type sudo apachectl restart

Done!

2. PHP

Mac OSX 10.8 comes with PHP 5.2 by default, in order to check what version you currently have just type php -v.

Whatever is the current version of your PHP, go ahead and update it here.

Enable PHP

cd into Documents:

cd /Library/WebServer/Documents/

cd into apache directory:

cd /etc/apache2/

then edit your config file:

sudo nano httpd.conf
Password:

then search “PHP” by using ctrl + w” :

Uncomment the following line (remove #):

LoadModule php5_module libexec/apache2/libphp5.so

Now apache can serve PHP.

Configure PHP .ini

cd into /usr/local/php5/php.d

The configuration file (php.ini) is read when PHP starts up. For the server module versions of PHP, this happens only once when the web server is started. For the CGI and CLI versions, it happens on every invocation.

Here you can configure your time zone and other settings, just type:

sudo nano 99-liip-developer.ini

Done!.

3. MySQL

Download it from here

You don’t have to log in, just click “No thanks, just start my download”.

Run the installer.

Finish installation and set it up to be running on startup by going into your system preferences bottom line and clicking on MySQL icon, you will get a default password, save it, you will need it later.

 

Configure MySQL

Add MySQL to PATH

The path is a list of directories that Unix uses in order to locate programs on the machine that it can run.

If you type which php in your terminal you will  get back its location:

$ which php
/usr/bin/php <= location

Up until this point, MySQL has not yet been added to the path, we have to add the directory where MySQL resides to that list.

You can take a look at that list by typing:

$ echo $PATH

MySQL is located at:

$ ls /usr/local/mysql/bin

Now we need to add this path to PATH, just type:

$ cd
$ sudo nano .bash_profile
Password:

We need to add this to .bash_profile:

# MySQL_PATH
export PATH="/usr/local/mysql/bin:$PATH"

Ctrl + x to exit then press “y” to save changes and enter to confirm file name.

Set root password

To set a new password:

$ mysqladmin -u root password

Press Enter, then enter a password.

$ mysqladmin -u root password
New password: _

If you already have a password and you want to change it, then type:

$ mysqladmin -u root -p password

Press Enter, then enter a new password.

$ mysqladmin -u root -p password
Enter password:
New password: _

All ready!

Request Respond Cycle

Request–response, or request–reply, is one of the basic methods computers use to communicate with each other.

The browser sends a request for some data and apache responds to the request. Usually, there is a series of such interchanges until the complete response is sent.

This image illustrates how it works.

You can check if everything works by creating a .php file inside your /Sites directory, launch your browser and type in the address bar http://localhost/~yourusername/filename.php

Now your development environment is ready. Enjoy!

You may also like