How to Change the Root Directory of an Apache Server

How do I change the root directory of an Apache server?

You need to change the DocumentRoot setting in your httpd.conf file. Chances are it will be under something like /etc/apache2/conf/httpd.conf.

Use your favourite editor (I recommend Vim) and look for the DocumentRoot and change it to /users/spencer/projects. Also look a little further down for a setting that looks like this:

<Directory "/var/www">

You will also want to change what is in the quotes to your new directory. This gives Apache access to read from that directory when a user makes a request that call on it.

Now restart your Apache service (httpd -k restart) and you should be good to go.

Apache 2 site configuration files are now typically kept in /etc/apache2/sites-available/ (Debian, Ubuntu, etc.).

Set root directory on shared hosting for multiple projects

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!



Related Topics



Leave a reply



Submit