Quantcast
Channel: Planet Ubuntu
Viewing all articles
Browse latest Browse all 17727

Dougie Richardson: Install web applications locally on Ubuntu

$
0
0

I was talking with someone yesterday who is hacking a WordPress theme together. If you work with web sites, being able to run a site locally allows testing, experimentation, developing new themes and even just checking that a software update isn’t going to break your site. You might want to keep a web application on a local network and away from the Internet – such as StatusNet, a Wiki or a project management application. All we need is to install a LAMP stack – Linux Apache MySQL and PHP. We’ve already got the “L”!

Synaptic (System->Administration->Synaptic Package Manager) lets you install common groups of packages (Edit->Mark packages by task…) in this case a LAMP server. You can do the same from a command line using “sudo tasksel install lamp“. Once the components are installed, you’ll be asked for a root password – this is used by MySQL and is not the system’s root password.

The default web root is /var/www – if you check it now there is an index.html. Open a browser and enter the system’s address (usually http://localhost/), you’ll be greeted with the contents of that file so we know the system is working. So how do we get our own files up?

Apache uses virtual hosts – we can have multiple sites on the same server. Each site is defined by a configuration file in /etc/apache2/sites-available. If you look there now, you’ll see the default site, we can use this as a template for a new site:

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/wordpress
gksu gedit /etc/apache2/sites-available/wordpress

You need to change DocumentRoot and Directory to point to the folder your site is going to be in. So lets say you want to use a folder called “wordpress” in your home folder, change “DocumentRoot /var/www” to “DocumentRoot /home/USERNAME/wordpress” and “<Directory /var/www/>” to “<Directory /home/USERNAME/wordpress/>“. We can also give an alias, so we access individual sites by name, to do this add the following “ServerAlias wordpress“.

You can then add “wordpress” to /etc/hosts (change the line that reads 127.0.0.1 localhost to 127.0.0.1 localhost wordpress) and use the address “http://wordpress” to access your site.

Ubuntu has a utility to add the site to Apache which will also need restarted:

a2ensite wordpress; sudo /etc/init.d/apache2 reload

If you’re only going to be using one site it would be easier to use the default site (which is already configured). Ubuntu adds the first user to the www-data group, so you can either change the ownership of /var/www or add a sub-folder with either the user’s ownership or membership of www-data (note you’ll need to change the group permission too). I’m adding a folder I own:

sudo mkdir /var/www/dougie;
sudo chown dougie:dougie /var/www/dougie

This can be accessed by http://localhost/dougie. I’ve seen a few forum posts saying that people have issues with folder permissions and there are some misconceptions. I’ve even seen it suggested to edit everything with “sudo nautilus”!

Now with the letter “A” out of the way we can deal with the “M”. Most web applications need at least one database. Remember that MySQL password?

mysql -u root -p

A good example is installing WordPress. Download and extract the contents to your web folder (in my case /var/www/dougie). Create a new database called “wordpress“, with a user called “wordpress” and a password of “wordpress” by entering each command at the prompt:

create database wordpress;
grant usage on wordpress.* to wordpress@localhost identified by 'wordpress';
grant all privileges on wordpress.* to wordpress@localhost;

Type “\q” to exit then open a browser and go to your site, for me that’s http://localhost/dougie.

So lastly we get on to the “P” – PHP. Apache will recognise and run PHP, however be aware of a caveat I’ve noticed in Ubuntu. If you try to use your system hostname instead of localhost in Firefox, it will try to download rather than run PHP files. I believe this is due to the system hostname resolving to 127.0.1.1, a solution to Debian bug #316099.

PHP applications often have their own installation scripts, which WordPress does. Enter the database details we just created on the WordPress install screen:

Once the rest of the screens are complete, you’ll have WordPress installed.

So to recap, all we need to do to add an application is create a virtual host so Apache can serve it up; create a database for it to store data; and configure the application – often via a browser interface. Now you can hack away at those WordPress themes to your heart’s content.

More information is available from the Ubuntu Server Guide.

Related posts:

  1. Blogging platforms Has anyone else noticed a large amount of ping backs...
  2. What do you identify as? I’ve been looking at my site stats and it seems...
  3. Samsung NC10 – a pleasant Ubuntu experience It’s another year and I’m deploying next week. One of...

Viewing all articles
Browse latest Browse all 17727