Category Archives: Ubuntu

Apache Landing Page

This code list the directories and files in a table.

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
   <title>Ze Master Server of The Universe</title>
   <style>
     *{
         padding:0;
         margin:0;
     }
     html,body {
         color:#333;
         font-family: "Lucida Console", Courier, monospace;
         font-size:14px;
         text-shadow:1px 1px 1px #cacaca;
         -webkit-text-shadow:1px 1px 1px #cacaca;
         -moz-text-shadow:1px 1px 1px #cacaca;
     }
     a{
         padding: 2px 0 0 24px;
         color:#FE4902;
         text-decoration:none;
     }
     a:hover{
         color:#000;
         cursor:url(hand.gif), progress;
     }
     #container{
         margin:0 auto;
         width:700px;
         margin-top:20px;
         padding-top:10px;
         border:1px solid #EEE;
         border-radius:10px;
         -moz-border-radius:10px;
     }
     .head{
         background-color:#38AF64;
         color:#FFF;
         font-weight:bold;
         padding:7px 0 5px 10px;
         font-size:14px;
         letter-spacing:1px;
         font-family: Verdana, Arial, Helvetica, sans-serif;
     }
     .head:hover{background-color:#FE4902;}
     .head span{font-size:9px; letter-spacing:0;}
     td{
         background-color:#F3F3F3;
         padding:6px;
     }
     td:hover{background-color:#EFEFEF;}
     h1{
         font-size:18px;
         font-weight:bold;
         padding:0 0 10px 10px;
     }

     /*icons for file types (add more to suit your needs - icons by famfamfam.)*/

     /*images*/
     a[href$=".jpg"] {background: url(image.gif) no-repeat left 50%;}
     a[href$=".gif"] {background: url(image.gif) no-repeat left 50%;}
     a[href$=".png"] {background: url(image.gif) no-repeat left 0%;}

     /*pdfs*/
     a[href$=".pdf"] {background: url(pdf.gif) no-repeat left 50%;}

     /*psds*/
     a[href$=".psd"] {background: url(psd.gif) no-repeat left 50%;}

     /*docs*/
     a[href$=".doc"] {background: url(doc.gif) no-repeat left 50%;}
     a[href$=".txt"] {background: url(doc.gif) no-repeat left 50%;}

     /*videos*/
     a[href$=".avi"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".m4a"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".mov"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".mp4"] {background: url(video.gif) no-repeat left 50%;}
     a[href$=".wmv"] {background: url(video.gif) no-repeat left 50%;}

     /*audio*/
     a[href$=".mp3"] {background: url(audio.gif) no-repeat left 50%;}
     a[href$=".wma"] {background: url(audio.gif) no-repeat left 50%;}
     a[href$=".aac"] {background: url(audio.gif) no-repeat left 50%;}

     /*web pages*/
     a[href$=".html"] {background: url(html.gif) no-repeat left 50%;}
     a[href$=".php"] {background: url(html.gif) no-repeat left 50%;}

   </style>

</head>
<body>
   <div id="container">
       <?php
         // opens this directory
         $myDirectory = opendir(".");

         // gets each entry
         while($entryName = readdir($myDirectory)) {
           $dirArray[] = $entryName;
         }

         // finds extention of file
         function findexts ($filename)
         {
           $filename = strtolower($filename) ;
           $exts = split("[/\\.]", $filename) ;
           $n = count($exts)-1;
           $exts = $exts[$n];
           return $exts;
         }

         // closes directory
         closedir($myDirectory);

         //  counts elements in array
         $indexCount   = count($dirArray);

         // sorts files
         sort($dirArray);

         // print 'em
         print("<h1>Directory Contents</h1>");
         print("<table width='100%' cellspacing='10'>
                 <tr>
                   <td class='head'>Directory/File</td>
                   <td class='head'>Type</td>
                   <td class='head'>Size <span>(bytes)</span></td></tr>\n");

         // loops through the array of files and print them all
         for($index=0; $index < $indexCount; $index++) {
               if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
               print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
               print("<td>");
               print(findexts($dirArray[$index]));
               print("</td>");
               print("<td>");
               print(filesize($dirArray[$index]));
               print("</td>");
               print("</tr>\n");
           }
         }
         print("</table>\n");
       ?>
   </div>

</body>
</html>

LAMP on Ubuntu 11.10

Install LAMP and phpMyAdmin on Ubuntu 11.10

The Ubuntu development team has made it very easy to install and set up a web server. Open a terminal window and enter the following command.
sudo apt-get install lamp-server^

Please enter the command exactly as it’s shown above. The carat (^) is not a typo and the command will not work without it.

Command to install LAMP

If prompted, enter your password.

The package manager will now display a list of packages to be installed. Hit to confirm that you want to go ahead with the install.

Installing LAMP packages

Apt will now start downloading and installing the packages on your computer.

After a short wait, you will be prompted to set a password for MySQL’s administrative user. Enter a password at the prompt and make sure it’s something you will remember or make a note of it.

MySQL password

You will then be prompted to confirm your password.

Confirm MySQL password

Type in the same password and hit . The package manager will now continue downloading and installing packages. After a short wait the installation will complete.
Testing Apache

Now we’ll run a quick test to make sure that the Apache web server is working. Open a web browser and enter the address https://localhost/. You should see a page that says “It Works!”

Testing Apache installation
Testing php

Now that we’ve verified that Apache works, we need to verify that php is working properly. We’re going to create a file in the /var/www directory called testing.php. Enter the following command in the terminal to create the file.
echo “” | sudo tee /var/www/testing.php

Enter your password if prompted.

Now you’ll need to restart the Apache web server. Enter into the terminal:
sudo service apache2 restart

Now open your web browser and enter the following address: https://localhost/testing.php

You should see a web page that displays a bunch of information about your php and Apache environment.

Testing php
Configure MySQL

Since this is for a local development environment, the MySQL database needs to be bound to the localhost IP address. This should be 127.0.0.1 by default. You can verify your localhost address with the following terminal command.
cat /etc/hosts | grep localhost

You should see output something like this:

127.0.0.1 localhost
::1 ip6-localhost ip6-loopback

Now you need to verify that this address is the bind address MySQL’s my.cnf file. Use the following terminal command.
cat /etc/mysql/my.cnf | grep bind-address

You should see output like this.

bind-address = 127.0.0.1

If it’s not correct you’ll need to edit /etc/mysql/my.cnf as root to fix it.
Install phpMyAdmin

You now have a functioning LAMP installation. You don’t need to install phpMyAdmin, but it provides a much easier way to administer your MySQL databases if you’re not familiar with MySQL’s commands. You can install phpMyAdmin with:
sudo apt-get install libapache2-mod-auth-mysql phpmyadmin

Enter your password if prompted.

Installing phpMyAdmin

Again the package manger will show you the packages it’s about to install. Hit to move forward with the installation.

The package manager will now begin downloading and installing packages. After a short wait you will be prompted to choose the web server to configure for phpMyAdmin. Hit to mark apache2 with an asterisk (*) as it’s shown in the following picture (click on the image to see it in full size). Then hit .

Select apache2

The next screen will ask about some automatic database configuration with dbconfig-common. Hit to accept the default and move on.

Configure phpMyAdmin with dbconfig-common

Next you’ll be prompted for the MySQL administrator password. Enter the password that your created earlier.

Enter MySQL administrator password

Now you’ll be prompted for a MySQL application password. You can allow the system to generate a random password or choose your own.

Enter MySQL application password

If you choose your own password, you will be prompted to verify it at the next screen.

Verify MySQL application password

Your phpMyAdmin installation and configuration is now complete.
Testing phpMyAdmin

Open your web browser and enter the address https://localhost/phpmyadmin/.

You should see a page like this.

phpMyAdmin login screen

Now log in with the user name root and the password that you created earlier in the tutorial.

phpMyAdmin Home screen

Congratulations, you now have a working web development environment set up on Ubuntu 11.10. You can place the files for your website under /var/www. Note that this location is owned by the root user, so you’ll need to copy your files over as root for it to work. Otherwise, you can do some further Apache configuration so you can place the files for your website in a directory somewhere under your home directory.
Fixing some common problems
Fixing phpMyAdmin

One common error that some people make is that they forget to mark apache2 during the phpMyAdmin configuration. When this happens you’ll get a 404 Not Found error when trying to navigate to https://localhost/phpmyadmin/.

phpMyAdmin Not Found

If this happens, enter the following terminal command.
sudo dpkg-reconfigure phpmyadmin

You will be prompted about reinstalling the database. Accept the default of “No” and hit .

Don’t reinstall phpMyAdmin database

Make sure to then mark apache2 by having the cursor next to apache2 and then hitting to mark it with a *, then hit .

Reconfigure phpMyAdmin for Apache

You will then need to reload Apache.
sudo service apache2 reload

You should now be able to load https://localhost/phpmyadmin/. If you’re still seeing the 404 Not Found error, then you will need to clear your web browser cache and try again.

phpMyAdmin login screen
Fixing the Apache fully qualified domain name

During the above steps you may have seen an error message like this when reloading Apache.

apache2: Could not reliably determine the server’s fully qualified domain name,
using 127.0.1.1 for ServerName

This doesn’t seem to cause any problems for me, but if you don’t like seeing that error, you can fix it with this command.
echo “ServerName localhost” | sudo tee /etc/apache2/conf.d/fqdn

Then reload Apache with
sudo service apache2 reload

Enjoy your new web development environment!