Search code examples
apacheservershared-hosting

Set root directory on shared hosting for multiple projects


Locally, I can change the root directory of apache following these instructions. What I really want is to set up a way to have multiple projects with a different root directory for each project. I did this with help from this question.

I have a shared hosting plan (Apache running on Linux Ubuntu) where I would like to test these projects in a more production-like environment. The problem I run into, is that I cannot configure a different root directory for each project because I cannot configure Apache on this shared hosting.

My question is, is there any way to 'tell' Apache I have different projects with different document roots? I'm hoping there is a solution like having a .htaccess file per project with this setting, but I am not sure if this is possible.


Solution

  • There are a few of "it depends", so I will address the most common setups.

    1) How do you define "multiple projects"? Usually that means 1 project == 1 domain.

    • Your provider should allow you to setup multiple domains on your web space. Each domain is then linked to 1 directory in your web space. This incurs fees, to pay for each domain.
    • Ex. for 1and1 (not a publicity, it just happens have a sites on their setup): www.example1.com points to /example1, www.example2.com points to /example2, and so on. This is setup in their administration interface.
    • Doing it like this is equivalent to setting up multiple VirtualHost sections.

    2) 1 project == 1 domain prefix

    • Depending how your hoster does it, you could setup prefix1.example.com points to /site1 and prefix2.example.com points to /site2.
    • The user never sees /site1 or /site2 in their address bar.
    • Again that is done in the administration interface of your provider.

    3) 1 project == 1 sub-directory of one domain, you cannot edit .htaccess

    • If you do not mind that, each project could be a sub-directory to the 1 domain you have.
    • www.example.com/project1, www.example.com/project2, ...
    • The user of your site must type in www.example.com/projectX to get access to the project. It is not hidden in the configuration, the user must explicitly input the project he wants in the browser address bar.
    • This does not require any configuration in Apache, or at the hoster level. Simply put the different directories under the DocumentRoot directory of the entire domain.
    • This does not provide a "clean" separation of the sites, but it might be enough for your needs.

    4) like 3), but you can edit .htaccess files

    • In the DocumentRoot directory create a .htaccess file
    • In there, use Alias to tell Apache that requests to a sub-directory in the URL corresponds to another directory on disk, somewhere OTHER than under DocumentRoot like in 3)
    • This scenario might be less probable since your hoster will probably only allow you to put content under the DocumentRoot anyway.
    • See https://httpd.apache.org/docs/2.4/mod/mod_alias.html

    Have fun!