jon thompson

Windows 10 LAMP Stack Part 2 : Configuring Apache

Posted on 2019-02-14 14:44:13
Difficulty level: Intermediate

Increase the flexibility of your Apache web server, allowing you to serve multiple websites easily using mod_vhost_alias

Part 2 : Configuring Apache

Now that we have a working installation of an Apache web server running under Debian using the Windows Subsystem For Linux, we can take a look under the hood and set up some more advance configuration

The default location for your website files is via the Debian bash shell, at /var/www/html

This isn't very easy to manage from the Windows desktop and IDE programs, so we'll be taking a look at expanding this to use a normal Windows folder, and have the ability to develop & test multiple web sites without needing to make any changes

Working Example

We've got an external USB drive that we want to use to keep our website files on, and when it's plugged in to the Windows PC, it's assigned the drive letter G

We can see this in Debian if we enter

ls -al /mnt/g

We want to keep all our websites in one place, so let's create a folder to house them

mkdir /mnt/g/websites

Editing the Apache configuration

We need to add a new environment variable to Apache to make things easier, and we need to make sure that Apache can access the new location of our sites

You need the username that you logged in to Debian with. If you're not sure, type

whoami

Now we can start editing the Apache configuration

sudo vi /etc/apache2/envvars

Look for the following lines

export APACHE_RUN_USER=www-data
export APACHE_RUN_GROUP=www-data

and change them to 

export APACHE_RUN_USER=your-user
export APACHE_RUN_GROUP=your-user
export APACHE_DOC_ROOT=/mnt/g/websites

Update the main Apache configuration file, and the default website configuration file to use this new variable

sudo vi /etc/apache2/apache2.conf

Look for the following line

<Directory /var/www/>

and change it to

<Directory ${APACHE_DOC_ROOT}>

We're now going to set the default site configuration to use mod_vhost_alias.

sudo vi /etc/apache2/sites-available/000-default.conf

Look for the line

DocumentRoot /var/www/html

and change it to

VirtualDocumentRoot ${APACHE_DOC_ROOT}/%0/html

Testing the configuration

We've already set up the folder /mnt/g/websites to house our websites

Let's create one for our http://localhost site and add a simple index page that will show us the current PHP values

mkdir -p /mnt/g/websites/localhost/html
echo '<?php phpinfo();' > /mnt/g/websites/localhost/html/index.php
sudo service apache2 restart

Now, when we go to http://localhost, we should see the standard phpinfo() output on-screen

If we want to create more sites, simply create a new folder.  For example: Create a new project for example.com

mkdir -p /mnt/g/websites/example.com/html

All you need to remember is that the html folder is accessible by your web browser, so that's where your website goes. You can create folders at the same level, but they will only be able to be accessed by scripts on your site

example.com -+
             |- html < your website
             | 
             |- private < only accessed by scripts