How to enable external access to user web directories in Apache2

botond published 2019/02/12, k - 21:30 time

Content

 

Introductory

User Web Libraries (UserDir) allow us to provide our customers with temporary access to their websites until they have been directed to the purchased domain name, or the right one has not even been selected, etc. During this time, we can make the websites available so that the development work can go smoothly even before the websites are opened.

 

 

Prerequisites

It is expedient to carry out the description in a simple manner LAMP server execute because these servers have Apache web server most of them even have the default settings or their approximate state and run PHP as an Apache module (mod_php SAPI). Accordingly, the configuration here is my Debian 8 (Jessie) on an 1.0 LAMP server I'll make it.

 

Configuring Apache2

Apache2 UserDir module allows users to access users' web directories from outside the server from a single domain name. If a domain name already points to the server, a user's web directory can be accessed from the following URL after configuration:

http(s)://domain.tld/~<felhasználónév>/index.php

And if the server does not have a domain name, the same content can only be accessed with the IP address:

http://<szerver IP címe>/~<felhasználónév>/index.php

(For an IP address, most often only an http connection is available because  SSLis usually claimed only for domain names, but in principle is also issued for IP addresses.)

This way the web directories of the users in the system can be accessed through the existing domain name or the server's IP address. By typing web addresses into the browser address bar this way, Apache loads the content in the appropriate web directory. These web directories can be inside the user's home directory, eg:

/home/<felhasználó>/public_html/

or even outside, for example in the / var structure:

/var/www/<felhasználó>/docs/

Wherever these web directories may be, the point is to include the user name as one of the subdirectories in the path so that Apache can find the appropriate directory based on the user in the requested URL, and to store these web directories in the same structure for each user to be uniform for all.

Enable UserDir module

By default, the UserDir module is not enabled in Apache2, so enable it as root by the following command:

a2enmod userdir

You will be prompted to restart Apache at this point, but you can still wait for it as we review the configuration first.

Configuring UserDir

Open the /etc/apache2/mods-enabled/userdir.conf file for editing, such as dwarf editor:

nano /etc/apache2/mods-enabled/userdir.conf

In this "factory" for me, the newly installed LAMP server has this:

<IfModule mod_userdir.c>
        UserDir public_html
        UserDir disabled root

        <Directory /home/*/public_html>
                #AllowOverride FileInfo AuthConfig Limit Indexes
                AllowOverride All
                Options MultiViews Indexes SymLinksIfOwnerMatch
                <Limit GET POST OPTIONS>
                        Order allow,deny
                        Allow from all
                </Limit>
                <LimitExcept GET POST OPTIONS>
                        Order deny,allow
                        Deny from all
                </LimitExcept>
        </Directory>
</IfModule>

Here, in the first round, comment on the following line according to the section highlighted in green:

#AllowOverride FileInfo AuthConfig Limit Indexes

And let's do this:

AllowOverride All

This allows all Apache settings to be overwritten with .htaccess files in the user web directory.

Also the configuration UserDir directive after the "public_html" value is still important. This tells Apache that the user's web directory is the default / home / / public_html or browse for it here. If you want to use a different subdirectory system, here are some examples of how to use it.

For example, consider the same URL for multiple UserDir options. So our URL is:

http://<szerver domain neve vagy IP-címe>/~felhasznalo/index.html

Loading this address will cause Apache to translate the request into the following subdirectories as a result of the following UserDir settings:

UserDir /var/html

In this case, the request is physically a /var/html/felhasznalo/index.html will arrive at a file. Because we did not use the wildcard "*" here, Apache interprets it to look for the user subdirectory at the very end of the configured path.

UserDir /var/www/*/docs

Then the /var/www/felhasznalo/docs/index.html the file will be loaded.

And so on. For more details see See the Apache documentation for this We can be found.

Of course, according to the set value, the configuration Directory block setting must also be synchronized so that the directory settings apply to the appropriate directory. E.g.:

[...]
<Directory /var/www/*/docs>
[...]

 

 

Create and configure a web directory

Exit root and create a web directory as a Smooth user as described above:

mkdir /home/$USER/public_html

Or where we just set the location of the web directory.

Go back to root and migrate the directory group to www data groups:

chgrp www-data /home/<felhasználónév>/public_html

(replace user name here)

Let's restart Apache:

service apache2 restart

HTML trial

From now on, HTML files are retrieved from user web directories. Let's create a test just finished public_html as a regular user within the library:

nano ~/public_html/index.html

And let's throw in a very basic HTML5 skeleton, just to get the accents working properly with proper character encoding:

1
2
3
4
5
6
7
8
9
10
<!doctype html>
<html lang="hu">
    <head>
        <meta charset="utf-8">
        <title>Próba oldal</title>
    </head>
    <body>
        Működik!
    </body>
</html>

Then load it into your browser:

http://<szerver domain neve vagy IP-címe>/~<felhasználó>/

vagy

http://<szerver domain neve vagy IP-címe>/~<felhasználó>/index.html

Apache2 - Testing HTML content in a user web directory

And a nice HTML page comes in.

Setting up PHP

The same cannot be said for PHP files. If we were to load a PHP file like the one above, its source code would appear as text, as Apache is disabled by default in user cache in user web directories.

To enable this, open /etc/apache2/mods-available/php5.conf file for editing as root:

nano /etc/apache2/mods-available/php5.conf
[...]
# Running PHP scripts in user directories is disabled by default
# 
# To re-enable PHP in user directories comment the following lines
# (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it
# prevents .htaccess files from disabling it.
#<IfModule mod_userdir.c>
#    <Directory /home/*/public_html>
#        php_admin_flag engine Off
#    </Directory>
#</IfModule>

And comment on it at the bottom of the file IfModule part 5 lines, as I highlighted in green.

Let's restart Apache:

service apache2 restart

PHP trial

When you load a php file, you will see the corresponding output:

Apache2 - Test a PHP file in a user web directory

So now both HTML and PHP files work in user web directories.

 

 

Disadvantages

This is all nice and good, but we should not overlook the fact that with this solution we run the web page in a subdirectory instead of the web root. This is not a problem for most HTML + PHP web pages, but it is unique or complex CMS system, running or moving to its final location may cause some headaches.

Examining PHP $ _SERVER array

Let's take a closer look at this by examining the PHP $ _SERVER array.

Modify the contents of our php file created above to:

<?php
echo "<pre>"
    .print_r($_SERVER,true)
    ."</pre>";
?>

Please run the PHP file again:

Examining PHP $ _SERVER array in user web directory

Then copy this php file as root to the default web root directory:

cp /home/<felhasználó>/public_html/index.php /var/www/html/

And let's run it here:

Examine PHP $ _SERVER array in default web root directory

Opening the images and rotating them with the arrow keys will make the difference perfectly visible.

So the point here is that if the PHP logical part of a website doesn't handle these paths properly, these discrepancies can cause problems when, for example, we move the page to its final location, where it will load under its own domain name, etc. Today's modern CMS systems are, of course, prepared for this, but this possibility of error must also be taken into account when, for example, we hand over a newly completed web store to the customer, but when moving to its final location, they throw error messages.

.Htaccess files

.Htaccess files are also quite sensitive to their processing location because they behave differently in the web root directory and differently in a subdirectory. As in the PHP example above, it matters where the .htaccess file is processed.

For example, today’s modern and trendy SEO-friendly URLs can be processed by Apache-based web servers thanks to the Apache rewrite module. These are handled by web pages using settings in .htaccess files. So it doesn't matter here whether the webpage is running in a subdirectory (user web directory) or in the web root.

Some CMS systems create these .htaccess files when they are installed, and include automatic settings that match the conditions at the time of installation, so migrating after installation is a problem when changing the web root when moving a page under a domain name. Other systems offer the possibility to set the parameters of the runtime environment of the website in their admin interface, where we can change them on the fly.

 

Conclusion

We have seen how convenient it is to use user web directories, they allow us to make web content from any user subdirectory without additional domain names, but when using it, it is worthwhile to find out about the running needs of the website or CMS to the final location, or just one Virtual hosting with the newly redirected domain name, after which the web page must run from the web root.

In another description, we review how to create a "domain named", sharp environment without setting up a virtualhost and using client-side hosts files instead of using user web directories, so that the website under development is fully compatible with its final location. we can run.