Installing and Maintaining the Application

In general this is a 4 step process:

  1. Package our application so that it is easily deployable.

  2. Finish to prepare the system so that our application can run. Here we will simply create a MySQL user for our application to use.

  3. Install the application. Here we will just add a single file in /opt/sample-app/ and configure Apache to access it.

  4. Setup the system so that it will be automatically updated everyday.

Package the Application

Two option are available to us:

  • The recommended method to do so is to make a Debian package. Since this is outside of the scope of this tutorial, we will not perform this here and invite the reader to read the documentation on how to do this in the Ubuntu Packaging Guide. In this case it is also a good idea to setup a repository for your package so that updates can be conveniently pulled from it. See the Debian Administration article for a tutorial on this.

  • Manually install the application under /opt as recommended by the FHS guidelines. This is the approach we will be using here for more simplicity even though this is not the one we would recommend for any serious application as it does mean more complexity in maintaining it. We cover this in the section called “How to Get Your Application Updated”.

Setting Up a MySQL User

Our application requires to access the MySQL database. For security reasons, we do not want this user to be the root MySQL user, so we define a user named www-data that has read access to the local databases and can only connect locally with some password.

  • First we need to connect to the MySQL monitor using the default password we specified earlier:

user@JeOS:$ mysql -p --user=root Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.0.45-Debian_1ubuntu3-log Debian etch distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql>
  • Then we can create our user and exit the MySQL monitor:

mysql> GRANT SELECT ON *.* TO 'www-data'@'localhost' IDENTIFIED BY 'password'; Query OK, 0 rows affected (0.00 sec) mysql> exit Bye user@JeOS:$

Installing our Application

In this example our application is a very simple PHP page that lists the databases available in MySQL:

<?php session_start(); ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="description" content="Database List"> <title>Database List>/title> </head> <body> <h1>Database list</h1> <ul> <?php error_reporting(E_ALL); $link = mysql_connect('localhost', 'www-data', 'password'); $db_list = mysql_list_dbs($link); $i = 0; $cnt = mysql_num_rows($db_list); while ($i < $cnt) { echo "<li>" . mysql_db_name($db_list, $i) . "</li>\n"; $i++; } ?> </ul> </body> </html>

To install it:

  • sudo mkdir /opt/sample-app/

  • Open the file /opt/sample-app/index.php using sudo with your text editor of choice.

  • Paste the above page content in the text editor, save and exit.

  • sudo chown -R www-data:www-data /opt/sample-app to allow Apache to read it.

Then we just need to modify Apache default configuration to point to our application directory:

  • Open the file /etc/apache2/sites-enabled/000-default using sudo with your text editor of choice.

  • Change all instances of /var/www/ to /opt/sample-app

  • Save and exit.

  • sudo /etc/init.d/apache2 reload

You can now test your application by pointing your browser to the IP address of the virtual appliance.

Configuring Automatic Updates

To have your system be configured to update itself on a regular basis, we will just install unattended-upgrades:

sudo aptitude install unattended-upgrades

How to Get Your Application Updated

If you decided to package your application as a Debian package and did setup your own repository, all you need to do is to add your repository to /etc/apt/source.list.d/. The above auto-update cron will then pull the update each time you provide it in your repository. Another advantage of using a packaged approach is that, if your dependencies evolve during the life of your application, all updated dependencies will be automatically installed in the same process.

If you decided to install your application manually, it is really up to you on how you will be automating the updates. A possible solution would be to:

  • Place a tar file with your app on some HTTP server along with a version file.

  • Create a script that is placed in /etc/cron.daily that regularly compares the version file on the server with the version file that you would have stored in /opt/sample-app/

  • Download and apply the new tar file if the version available on-line is greater.

  • Reload Apache.