Coder Perfect

How can I update the Apache server’s root directory? [closed]

Problem

How can I update the Apache server’s document root? Instead of /var/www, I want localhost to originate from the /users/spencer/projects directory.

I eventually figured it out. Some advised changing the httpd.conf file, but I found a file in /etc/apache2/sites-available/default and changed the root directory from /var/www to /home/myusername/projects folder, which solved the problem.

Asked by Spencer Cooley

Solution #1

Please keep in mind that this applies only to Ubuntu 14.04 LTS (Trusty Tahr) and newer editions.

The document root in Ubuntu 14.04 LTS was set to /var/www/html. The following file was used to set it up:

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

So simply do a

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

and replace the following line with your desired text:

DocumentRoot /var/www/html

Also do a

sudo nano /etc/apache2/apache2.conf

and find this:

<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>

And save it by changing /var/www/html to your selected directory.

After you’ve saved your modifications, all you have to do now is restart the Apache 2 web server:)

sudo service apache2 restart

You may just replace sudo nano with gksu gedit if you prefer a graphical text editor.

Answered by 5 revs, 3 users 47%

Solution #2

The DocumentRoot setting in your httpd.conf file must be changed. Chances are it will be under something like /etc/apache2/conf/httpd.conf.

Look for the DocumentRoot and update it to /users/spencer/projects in your favorite editor (I recommend Vim). Look for a setting that looks like this a little farther down:

<Directory "/var/www">

You’ll also want to update the quotations to reflect your new directory. When a user makes a request that calls on that directory, Apache will be able to read from it.

After that, restart your Apache service (httpd -k restart) and you should be fine.

Site configuration files for Apache 2 are now usually kept at /etc/apache2/sites-available/. (Debian, Ubuntu, etc.).

Answered by RDL

Solution #3

I had to change the default site in /etc/apache2/sites-available/default. RDL stated the lines, and they are the same.

Answered by Nick

Solution #4

The following instructions are for Ubuntu 14.04 (Trusty Tahr):

Without the directory name, it should be as follows in the file /etc/apache2/apache2.conf:

<Directory /home/username>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

You should also specify the custom directory name in the file /etc/apache2/sites-available/000-default.conf, i.e. www:

DocumentRoot /home/username/www

If it is not as described above, you will receive the following error while trying to load the server:

Answered by androsfat

Solution #5

For Apache 2, the correct approach to change directory or run from various folders on different ports is as follows:

The configuration files for Apache 2 are located at /etc/apache2 and are split into smaller configuration files, with /etc/apache2/apache2.conf serving as the main configuration file. A new virtualhost conf file is required to serve files from a different directory. /etc/apache2/sites-available contains the virtualhost configuration files (do not edit files within sites-enabled). The virtualhost conf file 000-default.conf is used by default in Apache installations.

Begin by copying the default virtualhost file used by Apache’s default installation into a new virtualhost file (the one that runs at localhost on port 80). Change to the /etc/apache2/sites-available directory, then sudo cp 000-default.conf example.com.conf to copy the file, then sudo gedit example.com.conf to edit the file to:

<VirtualHost *:80>
    ServerAdmin example@localhost
    DocumentRoot /home/ubuntu/example.com
</VirtualHost>

For the sake of brevity, I’ve removed the non-essential lines from the above file. DocumentRoot is the path to the directory from which the website files, such as index.html, will be served.

Create the directory from which you want to serve the files, for example, mkdir example.com, and change the owner and default group of the directory, for example, sudo chown ubuntu:www-data example.com if your logged in user name is ubuntu. This gives the user ubuntu full access and the group www-data read and execute permissions.

Now, using the command sudo gedit apache2.conf, edit the Apache configuration file /etc/apache2/apache2.conf and look for the line Directory /var/www/>. and add the following below the closing tag /Directory>:

<Directory /home/ubuntu/example.com>
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
</Directory>

The virtualhost configuration files can now be enabled or disabled using the a2ensite and a2dissite commands, respectively. We must now disable the default configuration file (000-default.conf) and enable our virtualhost configuration file (sudo a2ensite example.com.conf) because our example.com.conf file uses the same port (80) as the default configuration file (000-default.conf).

Now use the sudo service apache2 restart command to restart or reload the server. On the default port of 80, Apache now delivers files from the directory example.com at localhost.

The a2ensite command generates a symbolic link to the configuration file located in the site-enabled directory.

As mentioned in this answer, do not alter files in the sites-enabled (or *-enabled) directory.

If you need to execute the directory on a different port, update the virtualhost file to change the port number from 80 to 8080:

<VirtualHost *:8080>
    ServerAdmin user@localhost
    DocumentRoot /home/ubuntu/work
</VirtualHost>

Then putting Listen 8080 immediately below the line in /etc/apache2/ports.conf 80 minutes of listening

We can now use sudo a2ensite 000-default.conf to enable the default virtualhost configuration file, which runs on port 80 because the example.com directory uses port 8080.

Now use the sudo service apache2 restart command to restart or reload the server. Both directories can now be reached using localhost and localhost:8080.

Answered by lordvcs

Post is based on https://stackoverflow.com/questions/5891802/how-do-i-change-the-root-directory-of-an-apache-server