The acronym LAMP stands for Linux Apache MySQL PHP and refers to a solution of free and open source software used to run dynamic web sites or servers.
I was in need of a new development server that matched my production server that is already online and is hosting my clients websites and that has the most recent versions of the software (Apache version 2.2.11 (Unix), PHP version 5.2.6, MySQL version 5.0.67-community)
So in this particular server I’ll have:
- Ubuntu Linux 9.04 (Jaunty Jackalope) that just came out as the operating system – I’m using wubi in Windows Vista tbh, it’s not really important;
- Apache2 as the Web server;
- MySQL, for the time being as the database management system or database server (PostgreSQL will replace it in the future as Oracle bought Sun)
- PHP5 as the preferred scripting language
To install everything in Ubuntu I just launched the Terminal (I really enjoy using it).
First we are going to install apache2 meta package.
sudo apt-get install apache2
After that you’ll get a “It works!” page if you go to http://localhost actually that are the html contents of index.html file in /var/www/
To start/stop/restart apache2
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
sudo /etc/init.d/apache2 restart
Next we’ll have php5 meta package installed
sudo apt-get install php5 libapache2-mod-php5
You may need to restart apache2 daemon so let’s just do it anyway as we are just in a development server, np.
sudo /etc/init.d/apache2 restart
Let’s test php5 installation.
sudo gedit /var/www/test.php &
The & is to run gedit in the background so we can still use the terminal.
To test it we gonna create a php file with the following code:
<?php echo "It works! apache2 + php5"; ?>
Save it. Now let’s run it in Firefox the following URL http://localhost/test.php
Finally MySQL
sudo apt-get install mysql-server php5-mysql mysql-client
You’ll be asked to set a root password for mysql root user.
When it finishes downloading and installing MySQL we need to link the extension mysql.so to php5 in order to make mysql run with it.
Edit php.ini
sudo gedit /etc/php5/apache2/php.ini
and add to it this line in Dynamic Extensions part just before Module Settings and restart apache2:
extensions=mysql.so
Let’s just try running it.
mysql -uroot -p
Enter the password you just defined for MySQL root. If you get a mysql> prompt it’s done.
A final test just to see all working.
Let’s create a db.php file
sudo gedit /var/www/db.php
and type:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$connection = mysql_connect($dbhost, $dbuser, $dbpass)
or die ('Error connecting to mysql: '.mysql_error());
$dbname = 'mysql';
mysql_select_db($dbname)
or die('Error selecting database: '.mysql_error());
$result = mysql_query("select user, host, password from user")
or die('Error querying database: '.mysql_error());
?>
<table>
<tr><th>user</th><th>host</th><th>password</th></tr>
<?php while($row = mysql_fetch_array( $result )) { ?>
<tr><td><?=$row['user'] ?></td>
<td><?=$row['host'] ?></td>
<td><?=$row['password']?></td></tr>
<?php } ?>
</table>
Save it and run it in Firefox http://localhost/db.php
Don’t forget to the change password on line 4 in db.php
That’s it.
Some useful commands to test MySQL
mysql> quit
mysql> select version(), user(), now();
mysql> show databases;
mysql> use mysql;
mysql> show tables;
mysql> select user, host, password from user;