Install Apache, MySQL and PHP may be painful for beginners since they lack all the necessary knowledge of how those stuff work together. But it should not. In this article, I'll guide you on how to install Apache, MySQL and PHP on Windows, Mac OS X and Ubuntu step by step.
First, here are a list of things that you will need to install:
- A PHP processor (or PHP compiler): The PHP compiler is a software program that can be install like many other softwares. PHP complier is used to execute the PHP code. The execution process is actually to translate PHP code (human reader code) into machine code (contain only zero and one numbers) so that the computer (either your local laptop or
remote server) can understand what it needs to do.
- A HTTP web server software: HTTP web server is a software program that is used to handle requests that sent to server via HTTP protocol. In the simplest term, HTTP protocol is a way (method) of communication between client and server to transport
HyperText content (or HTML content). Apache is one of the most common HTTP web servers out there and we'll be using Apache in this tutorial.
- A database software (optional): A database software helps store data in a dynamic web application. All popular websites like Facebook, Twitter have some sort of databases software installed on their servers. In this tutorial we'll be using MySQL as Database software.
- A text editor: A text editor is a helpful tool to edit PHP code. As a beginner, you should use Notepad++ if you're on Windows or Sublime Text if you're on Mac OS X or Ubuntu.
Install XAMPP on Windows
The easiest (and the most popular) way to intall Apache, PHP and MySQL on Windows is to use the XAMPP package:
- Download XAMPP from https://www.apachefriends.org/download.html.
- Double click into the downloaded file and start installation process (The default options is pretty sensible so I recommend you to not change any of them).
- After finishing the installation process, you want to open the XAMPP control panel by press the Windows key and search for XAMPP and click to the icon appeared in the search result. then click the Start button next to Apache and MySQL modules.
Note: If you're having trouble with starting Apache module you may want to stop all the services that is running on port 80. One of the most common applications that uses port 80 is Skype.
Mac OS X
If you're using Mac OS X, you can use brew to install Apache, MySQL and PHP.
Note: There's also the MAMP package just like XAMPP, however I find the best way to manage software applications on Mac is to use Homebrew.
If you don't have Homebrew installed on your Mac, then open terminal and run the following command:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Note: If you're having trouble with the installation process, most common error is missing developer tool with error message Warning: No developer tools installed., then you could run the command
brew doctor on terminal to detect any problems with your system and
follow the instruction to fix them one by one.
Since Mac OS X is shipped with a default version Apache installed on the system, you should run the following command to remove it:
$ sudo apachectl stop
And remove the launch deamon from plist:
$ sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist 2>/dev/null
Then install the Homebrew version of Apache (2.4 in this case):
$ brew install httpd24 --with-privileged-ports --with-http2
After that restart the web server:
$ brew service httpd24 restart
Then install PHP (version 5.6):
$ brew install php56 --with-httpd24
And config Apache to load PHP module:
$ sudo echo "LoadModule php5_module /usr/local/Cellar/php56/5.6.26_3/libexec/apache2/libphp5.so" >> /usr/local/etc/apache2/2.4/httpd.conf
Finally restart Apache to make all change take affect:
$ brew service httpd24 restart
To make sure Apache web server functioning properly, open your web browser and visit
http://localhost you should see a default web page display.
If you're using Ubuntu you may find that PHP is already installed on your computer. Open terminal (Ctrl + Alt + T) and run the following command:
$ php -v
You should see the following information display:
PHP 5.6.29-1+deb.sury.org~xenial+1 (cli) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
In case PHP is not installed on your computer or you want to reinstall you can follow those simple steps:
Run the following command to install Apache web server:
$ sudo apt-get install apache2
Run the following command to install MySQL database server:
sudo apt-get install mysql-server
And if you want to install MySQL database client also (recommend):
$ sudo apt-get install mysql-client
And finally install PHP:
$ sudo apt-get install php5 libapache2-mod-php5
In the command above the
php5 is the actuall PHP compiler that executes PHP code and the
libapache2-mod-php5 is the library that supports running PHP code via Apache web server.
After the process you need to restart Apache:
$ sudo /etc/init.d/apache2 restart
Or using this command:
$ sudo service apache2 restart
To check Apache web server is functioning properly, open browser and visit
http://localhost/. You should see a message It works display.
Apache Document Root
C:\\ then the document root is located at
C:\\xampp\htdocs\ by default.
On Mac OS X, if you install Apache using Homebrew then the document root is located at
/usr/local/var/www/htdocs/ by default. On Ubuntu if you install Apache using Homebrew then the document root is located at
/var/www/html/ by default. When you visit
then Apache will server the content of any
index.htm... files that are currently being stored in this folder.
We can always change the Apache document root location. However, this goes beyond the scope of this tutorial so I skip it and use the default location for now.