Install web applications locally on Ubuntu

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”! So let’s walk through installing WordPress.

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-server“. Since Ubuntu 10.10, you need to install tasksel to install by task either way – “sudo apt-get install tasksel”.

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 goto 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?

You need to consider permissions, this is the casue of “Forbidden/You don’t have permission to access / on this server” error messages . There are a couple of ways but I recommend using the www-data group. You could use a folder in your home folder, however I have never got this will not work with encrypted home folders. I would leave /var/www in root’s ownership and add each subsite as belonging to www-data then give that group write access:

sudo mkdir /var/www/wordpress; sudo chown :www-data wordpress; sudo chmod g+w wordpress

The first user will have been added to this group during installation (you may need to logout) but further users can be added using System→Administration→Users and Groups or by:

sudo useradd -G www-data username

Apache uses virtual hosts – 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 add a ServerName, change the VirtualHostDocumentRoot and Directory to point to the folder your site is going to be in, changes are in red.
<VirtualHost wordpress:80>
ServerAdmin webmaster@localhost
ServerName wordpress
DocumentRoot /var/www/wordpress
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/wordpress>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>

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.

Apache needs to know about the site:

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

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 /var/www/wordpress – remember the wordpress folder already exists so put the contents in there. 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 summarise:

  1. Install a LAMP stack – sudo tasksel install lamp-server
  2. Create a folder and set the correct permissions – sudo mkdir /var/www/wordpress; sudo chown :www-data wordpress; sudo chmod g+w wordpress
  3. Download WordPress and put the contents in /var/www/wordpress
  4. Open mysql:mysql -u root -p
  5. Set up database: create database wordpress; grant usage on wordpress.* to wordpress@localhost identified by 'wordpress'; grant all privileges on wordpress.* to wordpress@localhost; q
  6. Run http://wordpress/install.php

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.

This entry was posted in Computing, Linux, Ubuntu and tagged , , . Bookmark the permalink.

4 Responses to Install web applications locally on Ubuntu

  1. Pingback: Tweets that mention Install web applications locally on Ubuntu | Lynx Blog -- Topsy.com

  2. Pingback: Install web applications locally on Ubuntu | Ubuntu-News - Your one stop for news about Ubuntu

  3. Flimm says:

    You seem to switch from

    DocumentRoot /home/USERNAME/wordpress

    to

    DocumentRoot /var/www/wordpress

    As well as switching from

    http://wordpress

    to

    http://localhost/wordpress

Comments are closed.