draft
Please do not make any edits to this article without confirmation. Its contents are currently under review and will be replacing https://help.ubuntu.com/community/ApacheMySQLPHP which being merged with the Ubuntu Server Guide. This page is purely for adding LAMP, phpmyadmin and some notes on php.ini and Magic Quotes to Desktop systems.
Parent page: Programming Applications
This is to help people setup and install a LAMP (Linux-Apache-MySQL-PHP) server on a (U)buntu desktop system, using Apache 2, MySQL 5 and PHP 5.
Current Support
This Wiki is for 10.04 LTS, 9.10, 9.04 and 8.04 LTS desktop systems. The instructions will work for any version from 7.04 onwards (although the screen shots may be different). For more detailed information on which versions of PHP, MySQL etc. will be installed (plus installing LAMP on 6.06 LTS) please refer to the server documentation for your version.
Version |
Code name |
Server Page |
Ubuntu 10.04 LTS |
||
Ubuntu 9.10 |
||
Ubuntu 9.04 |
||
Ubuntu 8.04 LTS |
||
Ubuntu 6.06 LTS |
Tasksel
Tasksel is a very useful command that can install in a few clicks and a short question-and-answer session that which would take a very long command line open to misspelling. It also ensures that you pull in all the correct versions of associated programs so that they will work together.
Adding LAMP
Whilst you can call Tasksel just for LAMP, it is a worthwhile exercise here to call it generally and then choose the task you want it to carry out; this will give you some insight into the many jobs that it can do. To use Tasksel, open a terminal session
sudo tasksel
You will get a screen similar to
Arrow down to LAMP, press the space bar to put an asterisk in the box, press the tab bar to get to <OK> and press enter.
From there, do as it says. You will be asked to create a MySQL root password. This password is really important so
make it decent password (e.g. I am completely mad ==> 1_@m_c0mpl3tl3y_m@d).
- don't lose it.
Once that has completed, close the terminal, open up your browser and type the following into your address bar
localhost
You should be greeted with something similar to
It Works !
This is the default web page for this server.
The web server software is running but no content has been added, yet.
Adding phpmyadmin
phpmyadmin is a GUI for accessing, creating and editing the MySQL databases, tables, data and users. It has replaced the older MySQL-Admin and is available now on most (if not all) web-hosting packages that have MySQL as the database system. Whilst phpmyadmin can be added using sudo apt-get install phpmyadmin, I have come across a couple of instances where it has not fully linked into the existing LAMP server (this could well be the person answering the questions incorrectly, but as we have a GUI we may as well take advantage of it).
Launch Synaptic Package Manager. In the search area type phpmyadmin. Right click on the little box to the left of phpmyadmin in the results area, select install, then click on 'Apply'.
Once it starts installing click on Details as you need to enter information such as telling it you are using apache2. NOTE: Click on the box marked Automatically close when the changes have been successfully applied to put a tick in the box. As with navigating Tasksel, use the arrow and tab keys to navigate and the space bar to put the asterisk by apache2 to select it.
As with the installation of LAMP, earlier, follow what it says, such as telling it to use the default database for phpmyadmin.
You will be required to enter the master MySQL password created when you installed LAMP and create one for master account on phpmyadmin (I use the same for both).
Once everything is completed, the installation window will close and you can exit Synaptic Package Manager.
Test phpmyadmin
To check phpmyadmin, go back to your browser and type
localhost/phpmyadmin
Using root as the login and whatever password you set up for phpmyadmin at this screen
you should be greeted with the screen below. Click on the little green exit icon to the top left of phpmyadmin to exit (circled in red).
Learning phpmyadmin
A decent set of notes from a Web provider are available at phpmyadmin tutorial I am not affiliated with that provider; most providers will also have a section on it. A decent Youtube introduction is available at You Tube phpmyadmin. The site that has more links and multi-language support is phpmyadmin wiki page, documents.
Notes
php.ini
10.04
As of 10.04 the ubuntu installation comes with what is called 'production' level security. It is one of the many layers of security designed to assist servers on the bad old internet. One of its jobs is to prevent error messages appearing on the server. This is good for servers on the web; not so good for us when we make mistakes as we learn and test.
/etc/php5/apache2/php.ini
has the current file (production) for configuring up the php security and has comments as to what is turned on and off between 'production' and 'development'. Copies of the production and development versions are over at
/usr/share/doc/php5-common/examples
As they have included the development version there, you simply copy over the one you need from there to
/etc/php5/apache2/php.ini
To switch to development version:
sudo cp /usr/share/doc/php5-common/examples/php.ini-development /etc/php5/apache2/php.ini
If you want to switch back to production security to see the difference:
sudo cp /usr/share/doc/php5-common/examples/php.ini-production /etc/php5/apache2/php.ini
Whenever you change over, you need to restart the apache server:
sudo /etc/init.d/apache2 restart
9.10
This ships with the development version of php.ini by default, with the exception of the E_NOTICE flag (See Variable Undefined Notice, below).
9.04
I have no idea which it ships with.
8.04
I have no idea which it ships with.
Magic Quotes
They were there in the very early days to try and do good things. That was a long time ago, and the people who created them now say it is a bad idea. Magic Quotes where an attempt to get PHP to try and look after MySQL. As they are 2 very different systems, this was never going to be a happy relationship. Magic Quotes are going to be gone completely and are 'off' by default with 10.04. So, when you are setting up a server, or are using an external server - make sure that they have magic-quotes turned 'off'. Else poor Mr O'Reily can quickly become Mr 0/////////////////////////////////'Reily as strip slash and mysql_real_escape_string do not get on. See Why Magic Quotes are going for more details.
10.04
Off by default
Pre 10.04
On by default, as they are going to be gone it is good practice to turn them off.
Edit the php.ini file
Using whatever is your favourite application for editing files with root priveledges, open the file
/etc/php5/apache2/php.ini
e.g.
gksudo gedit /etc/php5/apache2/php.ini
Find the area for magic quotes and turn them Off, e.g.:
; Magic quotes ; ; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = Off ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc. magic_quotes_runtime = Off ; Use Sybase-style magic quotes (escape ' with '' instead of \'). magic_quotes_sybase = Off
then save and quit.
Whenever you change over, you need to restart the apache server:
sudo /etc/init.d/apache2 restart
Use .htaccess
If access to the server configuration is unavailable (some host providers do not allow access to it), then they usually allow you the use of .htaccess to modify their default php.ini
php_flag magic_quotes_gpc Off
Turn them off in your own code
In the interest of writing portable code (code that works in any environment), like if setting at the server level is not possible or it varies, here's an example to disable magic_quotes_gpc at runtime. This method is inefficient so it's preferred to instead set the appropriate directives elsewhere as detailed above.
<?php if (get_magic_quotes_gpc()) { $process = array(&$_GET, &$_POST, &$_COOKIE, &$_REQUEST); while (list($key, $val) = each($process)) { foreach ($val as $k => $v) { unset($process[$key][$k]); if (is_array($v)) { $process[$key][stripslashes($k)] = $v; $process[] = &$process[$key][stripslashes($k)]; } else { $process[$key][stripslashes($k)] = stripslashes($v); } } } unset($process); } ?>
You would need to call this at the start of every routine. If your server provider will not turn them off for you, nor allows you to turn them off, it's time to move to consider moving to a server provider who will.
Trouble Shooting
Apache2
By default when you (re)start Apache, you will get the message apache2: Could not determine the server's fully qualified domain name, using 127.0.0.1 for ServerName This is quite okay, however if you do wish rid of it then:
echo "ServerName localhost" | sudo tee /etc/apache2/conf.d/fqdn sudo /etc/init.d/apache2 restart
will get rid of it.
PHP 5
Browser asks to download the php file
If your browswer asks to download your php file instead of displaying it, it means that Apache is not actually parsing the php after you restarted it. libapache2-mod-php5 is installed when you install the php5 package, but may have been removed inadvertently by packages if you have been adding modules which need to run a different version of php.
You may also need to actually re-enable it, by doing
sudo a2enmod php5 sudo /etc/init.d/apache2 restart
If sudo a2enmod php5 returns
This module does not exist!
then you should purge (not just remove) the libapache2-mod-php5 package and reinstall it.
Be sure to clear your browser's cache before testing your site again.
Variable Undefined
Once you have switched to development mode for php in 10.04, you may find a lot of Variable Undefined Notices this is because whilst php can deal with undeclared variables, it is warning you of a possible problem. It is best to get into the habit of declaring variables and arrays before you first use them as if you ever want to consider Java, C++ etc. in the future you will have to declare them. If you are not sure what variables are declared as you go from module to module then
if (!isset($YourVariable)) $YourVariable='';
will test a variable and only set it if it does not have a value.
If you do need to turn off 'Notice' reporting, and it is not really advised, then you need to edit
/etc/php5/apache2/php.ini
E.g.
gksudo gedit /etc/php5/apache2/php.ini
Look for the line that reads
error_reporting = E_ALL | E_STRICT
and alter it to
error_reporting = E_ALL | E_STRICT & ~E_NOTICE
Then save the file and exit. As always, if you alter php.ini you will need to restart the apache server
sudo /etc/init.d/apache2 restart
MySQL
If you wish to connect to your local system from another computer on your Local Area Network, chances are you will see the error
Can not connect to mysql error 10061
You need to set mysql bind address before you can access the database from other computers in your network. Note that this can be a security problem, because your database can be accessed by others computers than your own and you. You do not need this step if the applications which require mysql are running on the same machine.
Edit
/etc/mysql/my.cnf
fileE.g.
sudo gedit /etc/mysql/my.cnf
and change the line:
bind-address = localhost
to your own internal ip address e.g. 192.168.1.20
bind-address = 192.168.1.20
If your ip address is dynamic you can also comment out the bind-address line and it will default to your current ip.
Permission Denied in /var/www
If you want to make /var/www your own. (Use only for non-production web servers - this is not a secure way to have /var/www).
sudo chown -R $USER:$USER /var/www
Further Reading
The Ubuntu Server Guide.
The Apache Site.
The PHP Site.
The MySQL Site.
General tutorial sites for all things to do with MySQL, PHP and web-sites ((X)HTML, CSS etc.): W3Schools tutorials and Tizag tutorials
A good forum area for programming / web questions / php / MySQL etc. Code Call
sandbox/draft (last edited 2013-04-19 22:56:04 by host-89-242-242-252)