featured computer 850

Apache2: Activating User Directories & Virtual Hosts

Updated: 2017-02. Ubuntu 16.04 LTS. Apache 2.4.18 and PHP 7.0.13.

NOTE: In the default PHP Apache2 module configuration, you must enable PHP for user directories.

Summary: Let the hacker beware (Disclaimer)

These pages describes how I activated the public_html user directories to be served up by the Apache web server and set up virtual directories, meaning fake domains, for a private LAN. The audience is developers and home users who want to work on their websites at their desktop computers. I have no idea if this is applicable to the GUI-less server software. These are experiences I had using computers in my home and they may or may not work for you.

Step 1: Installing Apache2 and php

In a terminal (Dashboard serach “Terminal”), type everything after “$”

me@myhost$ sudo apt-get install apache2 php

You will be told you’re getting some extra software packages. The software php is not necessary, but at some point it will probably be useful to you if you are developing your own website. You can also use Synaptic Package Manager to install the software (from Ubuntu Software Center).

Test Step 1: When you install apache2 in Ubuntu linux, it is set to start automatically on each boot. You should be able to open a web browser and type in “http://localhost” and see the default “It Works!” message. If you can’t do this, don’t proceed. One thing to check is that you have the IP associated with “localhost” in the /etc/hosts file.

Possible Error / Gotcha if Any Reinstalls Done

Subtitled “Don’t do what I have done”. If you have a problem and decide to reinstall apache… just using “mark for reinstallation” in Synaptic or “reinstall” on the command line did NOT get me a fresh /etc/apache2 setup. Make sure you check the timestamp on the files in the /etc/apache2 directory to confirm you have a new installation. If not, delete the directory before reinstalling again.

Step 2: Activating the User’s public_html Directory with userdir

Create a directory (folder) called “public_html” in your home directory, with your file browser or the command below. Do NOT use the sudo command.

me@myhost$ mkdir public_html

I have found that on a simple development machine, it is easier to have your work all within your own home directory and not have to worry about permissions and folders owned by the root user. What it gets you is the ability to access your websites in your home directory by a URL in the form http://localhost/~USER_NAME. We can improve upon this with fake domains, but first things first.

There are canned Debian script for Enabling modules. It creates shortcuts from the “mods-enabled” to the “mods-available” without having to worry about typos. The name stands for Apache2 Enable Module. To disable a module, use a2dismod.

me@myhost$ sudo a2enmod userdir
me@myhost$ sudo service apache2 restart

Test Step 2: Open up a browser and type http://localhost/~USER_NAME, where “USER_NAME” is your actual username. For Debian and Ubuntu, your home directory is located within the “/home” directory. Apache should show the folders and files you have in your directory, or even display a web page if you have an “index.html” file in there. If you get sent to the internet or the default apache2 directory, something is wrong and you shouldn’t go to the next step.

Step 3: Virtual Hosts + Domains for PRIVATE LAN DEVELOPMENT SERVER

Background: There is a HUGE benefit to using virtual hosts (fake local domains) for the do-it-yourself website developer. You can create all of your development sites exactly the same as for your live web host server. For example, if you have a site on a shared hosting environment that you do not have full control over, then the path “/” is the “top of the world” to your instance of the Apache sever. You cannot duplicate these paths using the “~USER_NAME” address.

Part A – Networking: I am describing a method that uses a fixed IP for my server. You CAN do this even if your router is set to DHCP. It makes it much easier when you want to have other machines in your house access your server using the fake domain.

Part A – For ONE COMPUTER. If you are only interested in development on one machine, then the rest of the notes on this page will work without fixed IP addresses. You can simply add the fake, local domains (see parts B-C), to the “localhost” line of the /etc/hosts file. In the example below, “lapcom” and “lapfarmdens” are the fake domains. Otherwise, set up fixed IPs if multiple machines are used.

# A sample /etc/hosts file for SINGLE COMPUTER USE   localhost lapcom lapfarmdens   chair

[The rest of the IPv6 stuff is not shown]

Part A – For SERVER + CLIENTS SETUP. To give a network card (NIC) a fixed (static) IP address, follow the expanded explanation on the Linux Fixed IP Addresses page.

To associate your fake local domains to your local computer, modify the /etc/hosts file to include ALL the aliases (fake domain) names you want to associate with each computer: one line of the file per entry. List the IP and then the aliases, separated by spaces. This is your local DNS. Here is an example.

# A sample /etc/hosts file for SERVER + CLIENT SETUP
# Localhost has fixed IP (, so the entry is not needed   localhost
# KB Section
# samsung chair lapcom lapfarmdens table tree com farmdens shrub

[The rest of the IPv6 stuff is not shown]

WARNING: Like I said at the top, these notes are for usage on a PRIVATE LAN, i.e., NOT the internet. If you try to use these notes on the internet, then you are an idiot and I cannot help you. The main thing to remember is that for your development machine and/or LAN, your /etc/hosts file is your DNS.

Part B – Create Apache Virtual Directory FILES: This will be a physical text file in the “sites-available” section. It has as its bare minimum requirements, the values of ServerName and DocumentRoot in it. Lots more can go in, but this is enough to get started.

# Contents of text file in /etc/apache2/sites-available
<VirtualHost *:80>
ServerName com
DocumentRoot /home/USER_NAME/public_html/com

The pre-formatted code above is the content of a plain text file I can call anything I want, but will name it “com.conf” so as to keep my sanity. “ServerName” is the name of the fake domain that will allow you to enter “http://com” in your browser to see the site. The “DocumentRoot” is the absolute path to a directory called “com”. Within my home directory, I have created a file called “public_html” where all my websites are. Each website has a directory within public_html, like “com”, to keep all of the files for that website. I happen to keep the names consistent throughout, but that is not necessary. You need to substitute “USER_NAME” with your actual user name. So the next file we create would be /etc/apache2/sites-available/farmdens.conf (you can use $sudo gedit to make it simple) and would look like this:

# Contents of text file in /etc/apache2/sites-available
<VirtualHost *:80>
ServerName farmdens
DocumentRoot /home/USER_NAME/public_html/com/farmdens

Follow this pattern for all of your virtual hosts (fake local domains). NOTE: NEW FOR Apache2 2.4.7 all of these text files MUST have the extension “.conf” or it will not work.

Part C – Make Apache Virtual Directories Available: The Debian-based recommended method for this is again an Apache script to enable virtual sites called “a2ensite”. It works in the same was described above as a2endmod. Disable a virtual host with “a2dissite”.

me@myhost$ sudo a2ensite com.conf
me@myhost$ sudo a2ensite farmdens.conf
me@myhost$ sudo service apache2 restart

REMINDER: The most common mistake I make is to create a text file in “sites-available” and execute the “a2ensite” command without editing the /etc/hosts file. Remember you need to add the ServerName for each new vitural host in the /etc/hosts file (see above).

Test Step 3: Okay, by now I’m assuming you actually have some kind of a website you are working with and that the contents of it are within a directory, like “com”, in your public_html folder. Open up a browser and try a local-only address, like “http://com”. Apache should serve up the index file in that directory. The index.html file should not be a problem. If you are using PHP, however, you would have to enable PHP for user directories.

I have gotten “stuck” on this step a number of times, thinking it had not worked when it really was done. If you don’t get your local site, you need to clear the cache of your browser and maybe make it stop sending you to a search engine or domain containing your words. Here are two settings I need to change in Firefox in the about:config window:

  • browser.urlbar.autofill = OFF
  • keyword.fixup.alternate-enabled = OFF

NOTES: Apache official docs – Cryptic for beginner, as it’s hard to tell where the text in all their boxes actually goes. They also will not be specifying Debian-specific scripts. Ubuntu Apache2 README File – Found in file:///usr/share/doc/apache2. Ubuntu’s default Archive Manager for the compressed README.Debian.gz file WILL uncompress it, as long as you do it in a folder you own, not the existing one.

Step 4:  Configure Clients Computers If Needed

Similar Posts