How to Install Apache on Ubuntu

This post was migrated from my Techblog.

When learning web development, it isn’t long before you need something more than pointing your browser at a local file. You want to learn PHP, AJAX, or Wordpress … and that means a web server. Fortunately, it’s not too hard to get one installed on your local machine.

In this post, I’m going to show you the simplest way to get an Apache server up and running on Ubuntu, plus some helpful notes that you can either read now or come back to later.

I’m by no means an Apache expert … but then again, that’s why I’m writing this. Most of what I know was learned by prowing forums, digging through obscure documentation, and banging my head against a brick wall over and over again (as well as purging Apache several times). My hope is that these notes will be helpful to some other beginner and perhaps prevent a concussion or two. :)

Installation | Where Is Everything? | Restart Apache | Permissions | Modules | Purge Apache


To install Apache, open a terminal and run

$ sudo aptitude install apache2

Yep, that’s it! Now, point your browser at http://localhost/. You should see the default index.html page.

Where is everything?

Now that you’ve got Apache, you’re no doubt wondering how to do anything with it. Where is everything and how can you add content?

Browser address and content

First things first. In the browser, your apache server address is http://localhost/. You should also be able to use the name of your machine: http://myubuntubox/.

The content you’re seeing is in /var/www/. You could work directly in there, but you’d have to sudo everything, which is cumbersome and not very safe. My advice is to set up a folder in your home directory where you put all of your content. Then symlink it into /var/www/.

For example:

$ cd
$ mkdir my-web-content
$ cd /var/www/
$ ln -s ~/my-web-content ./

FYI, the way I do it is I keep a folder in my home directory called public\_html that is symlinked into /var/www/. I leave it empty by default and create symlinks to any projects I’m working on; when I’m done, I remove them.

If you’re wondering, Apache is by default configured to follow symlinks, so that’s not a problem.

Configuration files

Now for the less-visible but all-important back end. All of your Apache configuration files are located in /etc/apache2/. Try to leave them alone if you don’t need to touch them. If you’re going to change anything, make copies of the originals before doing so. Why all the paranoia? If you mess up Apache, purging and reinstalling is not always a cakewalk.

Now, I’m about to say something very important: httpd.conf is now deprecated!. If you go to /etc/apache2/ and open httpd.conf, it will be empty!!!.

Okay, let me back up and explain myself here, lest you think me crazy. In older versions of Apache, httpd.conf was the master configuration file with all the important settings. If you needed to change anything, chances were that it was in httpd.conf. In recent versions, they’ve decentralized all these settings. Anyway, the reason I say this with such urgency is that in just about any Apache forum, the first thing that the well-meaning respondents will tell you to do is to go into httpd.conf and change something. You will follow their instructions … only to be faced with a blank file. You will then think that (1) something is wrong with your installation, or (2) you are insane. Neither is in fact the case.

Unfortunately, up-to-date advice on Apache configuration is scarce and far between. I’ve found only a handful of websites that actually acknowledge the emptiness of httpd.conf (which has become a bit of a litmus test in my book).

Okay, moving on …

Restart Apache

You’ll periodically need to restart your Apache server after you make some configuration change. To do that, run

$ sudo /etc/init.d/apache2 restart

Usually, Apache will tell you when you need to restart: Run '/etc/init.d/apache2 restart' to activate new configuration!

Note: There are several different commands you can use to “restart” Apache (reload is another one). Honestly, I never entirely figured out the difference between them; I’ve always just used restart. It’s the sledgehammer of the bunch, kind of like rebooting your machine as opposed to logging out and back in. I’m sure it’s not a good idea for big commerical setups, but for a small local server on which nothing critical depends, I don’t know that there’s any harm. It’s fast and most importantly, it always works; whereas in some situations the other commands aren’t enough.


Apache runs as a user called www-data. To load your files, Apache needs read acccess. This can be tricky if you’re keeping the files in your home directory and symlinking into /var/www/.

My way of simplifying this is to add www-data to my user group, and me to www-data’s usergroup:

$ sudo adduser username www-data
$ sudo adduser www-data username

You will need to restart Apache at this point. You may also need to log out and back in.

Now, you can own all of your web content. Just make sure your group has read access. (However avoid the executable bit on files: According to the hubby, it can cause problems, besides being a general safety risk.)

$ chown -R username:username public_html
$ chmod -R a+r public_html

If Apache is having trouble accessing your files, check that it has read access to your files and all folders in their path.


Apache modules are like plugins that you can turn on and off. They can be very helpful and some, like mod_rewrite, are required by a lot of software to achieve full functionality (e.g. Wordpress pretty permalinks).

Find Modules

Apache comes with a bunch of modules. You can find the list of available modules in /etc/apache2/mods-available/. If it’s in there, you already have that module; however, it may not be enabled. The list of enabled modules is in /etc/apache2/mods-enabled/.

Enable a module

To enable a module, first find its name — that’s the part before the .conf or .load in the file name. For example, if you see the files userdir.conf and userdir.load in your mods-available folder, the module’s name is userdir. (See example below.)

Once you know what it’s called, you simply run:

$ sudo a2enmod <module-name>

You’ll need to restart Apache for it to take effect.

Disable a module

To disable a module, you use the companion command

$ sudo a2dismod <module-name>

Check that a module is running

To get a list of modules that are running, use this command:

$ sudo apache2ctl -M

Per-User Directories

The module userdir lets you have per-user directories, which be nice if if you have more than one user that wants to work with the webserver. Since this will also double as a good example for the above commands, I’ll go ahead and walk you through it.

First, enable the userdir module and restart apache.

$ sudo a2enmod userdir
$ sudo /etc/init.d/apache2 restart

Then go to your home directory and create a folder called public\_html (it can be a symlink). This folder houses the contents of your per-user directory.

The browser address of your per-user directory is http://localhost/~username/. For example, if your username were janesmith, your per-user directory would be in http://localhost/~janesmith/. Once there, you should see the contents of your public\_html folder.

One major drawback of using a per-user directory is that by default, PHP is not enabled. That means that if you point your browser at a PHP file, you’ll end up downloading it instead of executing the code. You can of course configure Apache to enable PHP, but my brief investigation into the subject showed that it was by no means straightforward. If you want PHP support, my advice is to symlink into /var/www/.

[Update: I did another search and it turns out that it’s not hard at all to enable PHP in userdirs. Check out this post for details.]

Purge Apache

Hopefully, you’ll be careful with your installation and you’ll never need this section. But if you do, well, I feel your pain. It happens. :)

Purging Apache can be a pill. The first time I tried it, I spent hours hunting down all of its tentacles, ended up with a bunch of broken packages, and finally came out with a fresh copy. I’ve been more careful ever since.

If you have to purge, the best list I’ve ever found is the one on the Ubuntu ApacheMySQLPHP page:

apache2 apache2-mpm-prefork apache2-utils apache2.2-common libapache2-mod-php5 libapr1 libaprutil1 libdbd-mysql-perl libdbi-perl libmysqlclient15off libnet-daemon-perl libplrpc-perl libpq5 mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 php5-common php5-mysql

Notice that this purges the entire LAMP stack, including MySQL and PHP. If you don’t have those installed, then it’s not a problem; aptitude is pretty smart about figuring that out. However, if you do have them installed and would like to keep them, you’ll have to try singling out only the packages that are related to Apache. The following list worked for me:

apache2 apache2-mpm-prefork apache2-utils apache2.2-common libapache2-mod-php5 libapr1 libaprutil1

If neither works very well for you, you could try “reversing” the install. This is the list of packages that aptitude installed when I installed Apache:

apache2 apache2-mpm-worker{a} apache2-utils{a} apache2.2-bin{a} apache2.2-common{a} libapr1{a} libaprutil1{a} libaprutil1-dbd-sqlite3{a} libaprutil1-ldap{a}

Whichever list you choose, the syntax you want is

$ sudo aptitude purge <list of packages>

Then remove its etc folder if it’s still there:

$ sudo rm -rf /etc/apache2/

Now, try to reinstall. Hopefully, you won’t have broken packages. If you do, follow the package manager’s instructions for fixing them.

Well that’s all! I hope that was helpful. Stay tuned for posts on MySQL and Wordpress.