How to run PHP at home: WAMP, MAMP and LAMP

Introduction

You can easily install the Apache web server (A), MySQL (M) and PHP (P) on your laptop or home PC. The three come bundled together, referred to as either the WAMP, MAMP or LAMP according to whether you are installing them on Windows (W), a Mac (M), or Linux (L).

Below you'll find instructions for WAMP, MAMP and LAMP. Feel free to send me corrections or improvements to these instructions. (Thanks to Sean Cormican and Conor Roche, whose comments have already been incorporated, and special thanks to Paul McGarry, who supplied all of the instructions for LAMP, based on instructions found here: http://tuxtweaks.com/2009/10/install-lamp-on-ubuntu-9-10-karmic-koala/.)

Warning: There will be many, many small differences between your personal installation and the one we have here in UCC. A PHP script that you write might run at home but produce an error message on the CS1 server, or vice versa. Ensure that you test all your scripts on our server, since this is what I will use when grading your work.

Download and installation

Windows users: WAMP

  1. Download WampServer 2.2. The usual place to get this from is http://www.wampserver.com/en/.
  2. Double-click the downloaded file to install.
  3. Follow the instructions on the setup wizard.
    • I will assume that you accept its recommendation to use C:/wamp as the destination folder.
    • I will assume that you check the boxes to allow it to set up Quick Launch and Desktop icons.
    • I will assume that it detects Firefox and that you agree to use this as the default browser. (But later I will explain how to change this to Chrome.)
    • You can leave the default values for the PHP mail parameters.
  4. Tidy up by deleting the downloaded file.
  5. If you are running the Windows firewall and you have blocked most external access, you may get a Windows security alert in a pop-up window. In this pop-up, choose Keep Blocking. (I am assuming that you want to use the WAMP for your private use, and that you don't want external users to be able to access the Apache web server that you have just installed.)

Mac users: MAMP

The MAMP is available if you are running Apple Mac OS X version 10.4.0 or later.

  1. Download the MAMP (which is free), not MAMP PRO (which is a configuration tool for MAMP and is not free) from http://www.mamp.info/en/.
  2. Double-click the downloaded file. Drag the MAMP folder into your Applications folder.
  3. Optionally, drag a copy of the MAMP icon from your Applications folder to the Dock at the foot of your screen.
  4. Tidy up by deleting the downloaded DMG file.

Linux users: LAMP

Installing LAMP:

  1. Open a terminal window (Applications/Accessories/Terminal)
  2. Type the command 'sudo apt-get install lamp-server^' (without the quotes but including the ^ )
  3. During the install you will be prompted to create a root password for the MySQL database, type the password, hit enter, retype it to confirm it, hit enter again and the install will continue

Testing Apache:

Open Firefox and browse to this address: http://localhost/ You should see a web page that says "It Works!"

Testing PHP:

  1. Open a terminal and type this command: 'gksudo gedit /var/www/testing.php' (gedit being the default text editor, others should work too)
  2. Enter this text in the file and save it: <?php phpinfo(); ?>
  3. Restart the php server using this command: 'sudo /etc/init.d/apache2 restart'
  4. Open Firefox and browse to this address: http://localhost/testing.php/

You should see a page displaying version information for your php installation.

Installing phpMyAdmin(Optional):

  1. Open a terminal and type the command 'sudo apt-get install libapache2-mod-auth-mysql phpmyadmin'
  2. The installation will prompt you to select a web server for automatic configuration. Use the space bar on your keyboard to select apache2 and then hit enter.
  3. A prompt will come up asking you if you want to configure a database. Select yes and hit enter.
  4. Enter the MySQL root password you created earlier and create a new password for phpMyAdmin. (You can use the same as the MySQL one.)
  5. Test phpMyAdmin by browsing to http://localhost/phpmyadmin/

You should see a login page similar the one shown in CS1106 lectures.

Launching the servers

Windows users: WAMP

Double-click the Desktop icon (or choose WampServer from your Quick Launch tray if you have one, or choose Start > All Programs > WampServer > start WampServer). An icon will appear in your System Tray (rightmost in the Windows Taskbar at the bottom of your screen.) Hover over this icon and a tooltip should say WAMPSERVER - Server Offline. This is good. (Again I'm assuming that you do not want to allow external access to this server.)

Note that the McAfee firewall, if you are running it, may interfere with the WAMP servers, preventing them from successfully launching. You can either disconnect from the Internet (you don't need to be connected whilst using the WAMP) and temporarily disable the firewall, or uninstall the McAfee firewall and seek alternative protection software that does not interfere with the WAMP.

Mac users: MAMP

Click the MAMP icon (which is in Applications > MAMP but it may also be in the Dock if you dragged it here earlier). A window should pop up and, if all is well, Apache Server and MySQL server will both have green radio buttons alongside them.

If either of the buttons is ever red, then click Start Servers.

Linux users: LAMP

If installed as above, the servers should be started automatically.

Configuring the servers

Windows users: WAMP

php.ini

Left click on the WAMP icon in your System Tray. From the menu, choose Stop All Services. The icon should turn part red. Left click it again, and choose PHP and then php.ini. This should open the php.ini file in Notepad.

Edit the file to make your installation a little closer to the one you use on our machines in UCC. In other words, for each of the following, find the left-hand side in your file and, if necessary, change the right-hand side to what you see below.

  • allow_call_time_pass_references = On
  • allow_url_fopen = Off
  • error_reporting = E_ALL & ~E_STRICT
  • log_errors = Off
  • magic_quotes_gpc = On
  • precision = 12
  • register_argc_argv = On
  • safe_mode = On
  • short_open_tag = On

Save the file and exit Notepad. Then start the servers again by left-clicking the System Tray icon and choosing Start All Services. Wait for the icon to turn from red to yellow and then to green.

MySql

We will add a password to your MySQL database:

  1. Left click the WAMP icon in your System Tray and choose phpMyAdmin from the menu. This opens a web page in your browser.
  2. On this page, click on the Users tab.
  3. On this User Overview page, click on the Edit Privileges action for the user called root and the host called localhost. This takes you to another new web page.
  4. On this Edit Privileges page, scroll down to the Change password box. Enter a password twice, and then click Go.
  5. With your favourite editor (Notepad++, I presume), open C:\wamp\apps\phpmyadmin\config.inc.php.
  6. Find the following line:
    $cfg['Servers'][$i]['password'] = '';
    and change it thus:
    $cfg['Servers'][$i]['password'] = 'yournewpassword';
    but replacing yournewpassword with the password that you just created with phpMyAdmin.
  7. Repeat steps 2-5 above for all other users called root. (There are probably two others.)
  8. Left click the WAMP icon in your System Tray and, from the menu, choose Restart All Services. As usual, wait for the red/yellow to disappear.

N.B. The above is adequate for an installation on your personal laptop or PC, especially since I recommended that you keep the server offline, so that external users cannot access it. If you are installing on a shared machine, or running online, then consult an authoratative source for information about secure configuration.

Changing the browser

At the moment, if you left click the WAMP icon in your System Tray and, from the menu, choose Localhost (or phpMyAdmin), I imagine that it is the Firefox browser that is launched. Let's suppose you want to change this to Chrome.

You must use a text editor such as Notepad++ to open and edit two files: C:/wamp/wampmanager.conf and C:/wamp/wampmanager.ini. In these two files, you must replace all occurrences of this:

C:/Program Files/Mozilla Firefox/firefox.exe

with the location of your installation of Chrome, most likely the following:

C:/Users/userid/AppData/Local/Google/Chrome/Application/chrome.exe --enable-ipv6

— but with the user id that you use when logging in to your PC/laptop in place of userid.

Also, in Chrome, choose Settings > Show advanced settings > Change proxy settings > LAN Settings. Make sure that Automatically detect settings is not checked and that Bypass proxy server for local addresses is checked.

With all that done, I found it necessary to restart my machine.

Hopefully, the next time you left-click the WAMP icon in your System Tray and choose Localhost from the menu, it will launch Chrome. But be warned: I found this to be very temperamental!

Mac users: MAMP

php.ini

Your php.ini file can be found in Applications/MAMP/conf/php5.

Use an editor to change this file, as per WAMP above.

MySql

The password for MySQL for the root user will be 'root'. If you work out how to (successfully) change this, send me instructions. Leaving it as 'root' is OK. With the free version of MAMP, there is no option anyway of allowing external access. (For MAMP PRO, the story is different.)

Linux users: LAMP

Open a terminal and type the command 'sudo gedit /etc/php5/apache2/php.ini'

Edit the file to make your installation closer to the one you use on our machines in UCC. In other words, for each of the following, find the left-hand side in your file and, if necessary, change the right-hand side to what you see below.

There is a lot of commented out information. In gedit, the lines you need to look at and possibly change should be highlighted in green. Make sure to save after you make these changes, then restart the php server using this command: 'sudo /etc/init.d/apache2 restart'

Running scripts

Windows users: WAMP

I will assume that you have launched the servers. (See instructions above.)

To be runnable, your scripts (and HTML, images and stylesheets) must be placed in the following folder: C:\wamp\www. (You can reach this folder more quickly by left-clicking on the WAMP icon in your System Tray and choosing www directory from the menu.)

Suppose you have placed a script called greetings1.php into the folder. Simply double-clicking it will not run the script.

Instead, you must open your browser and type in the following URL:
http://localhost/greetings1.php

Obviously, if you had placed greetings1.php in a subfolder of C:\wamp\www, then you would need to include that subfolder's name in the URL.

Mac users: MAMP

I will assume that you have launched the servers. (See instructions above.)

To be runnable, your scripts (and HTML, images and stylesheets) must be placed in the following directory: Applications/MAMP/htdocs.

Suppose you have placed a script called greetings1.php into the directory. Simply double-clicking it will not run the script.

Instead, you must open your browser and type in the following URL:
http://localhost:8888/greetings1.php
8888 is the port, although this can be changed.

Obviously, if you had placed greetings1.php in a subdirectory of Applications/MAMP/htdocs, then you would need to include that subdirectory's name in the URL.

Linux users: LAMP

To run scripts they must be in the folder /var/www/ If you have a script called greetings1.php you will have to browse to http://localhost/greetings1.php to view it.