How do I change the root directory of an apache se

2019-01-01 06:12发布

问题:

Does anyone know how to change the document root of the Apache server? I basically want localhost to come from /users/spencer/projects directory instead of /var/www.

Edit

I ended up figuring it out. Some suggested I change the httpd.conf file, but I ended up finding a file in /etc/apache2/sites-available/default and changed the root directory from /var/www to /home/myusername/projects_folder and that worked.

回答1:

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.

Edit: Apache2 site config files are now typically kept in /etc/apache2/sites-available/ (Debian, Ubuntu, etc.).



回答2:

Please note, that this only applies for Ubuntu 14.04 LTS and newer releases.

In my Ubuntu 14.04 LTS, the document root was set to /var/www/html. It was configured in the following file:

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

So just do a

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

and change the following line to what you want:

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 change /var/www/html to your preferred directory

and save it.

After you saved your changes, just restart the apache2 webserver and you\'ll be done :)

sudo service apache2 restart


If you prefer a graphical text editor, you can just replace the sudo nano by a gksu gedit.



回答3:

I had to edit /etc/apache2/sites-available/default. The lines are the same as mentioned by RDL.



回答4:

This is for Ubunutu 14.04:

In file /etc/apache2/apache2.conf it should be as below without the directory name:

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

and in file /etc/apache2/sites-available/000-default.conf you should include the custom directory name i.e. www:

DocumentRoot /home/username/www

If not as above it will give you an error when loading the server: Forbidden You don\'t have permission to access / on this server



回答5:

The right way to change directory or run from multiple directories under different port for apache2 is as follows:

For apache2 the configuration files are located under /etc/apache2 and doesnt use a single configuration file as in older versions but is split into smaller configuration files, with /etc/apache2/apache2.conf being the main configuration file. To serve files from a different directory we need a new virtualhost conf file. The virtualhost conf files are located in /etc/apache2/sites-available (do not edit files within sites-enabled). The default apache installation uses virtualhost conf file 000-default.conf.

Start by creating a new virtualhost file by copying the default virtualhost file used by the default installation of apache(the one that runs at localhost on port 80). Change into directory /etc/apache2/sites-available and then make copy by sudo cp 000-default.conf example.com.conf, now edit the file by sudo gedit example.com.conf to:

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

I have deleted the non important lines from the above file for brevity. Here DocumentRoot is the path to the directory from which the website files are to be served such as index.html.

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

Now edit the apache conf file /etc/apache2/apache2.conf by issuing command sudo gedit apache2.conf and find the line <Directory /var/www/> and below the closing tag </Directory>, add the following below:

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

Now there are two commands to enable or disable the virtualhost configuration files, which are a2ensite and a2dissite respectively. Now since our example.com.conf file uses the same port(80) as used by the default configuration file(000-default.conf), we have to disable the default configuration file by issuing the command sudo a2dissite 000-default.conf and enable our virtualhost conf file by sudo a2ensite example.com.conf

Now restart or reload the server with command sudo service apache2 restart. Now apache serves files from directory example.com at localhost on default port of 80.

The a2ensite command basically creates a symbolic link to the conf file under the site-enabled directory

Do not edit files within sites-enabled(or *-enabled) directoy, as pointed out in this answer https://stackoverflow.com/a/41568701/2532763

To change the port and run from multiple directories on different ports:

Now if you need to run the directory on a different port, change the port number from 80 to 8080 by editing the virtualhost file as:

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

and editing /etc/apache2/ports.conf and adding Listen 8080 just below the line Listen 80

Now we can enable the default virtualhost conf file that runs on port 80 since example.com directory uses port 8080, as sudo a2ensite 000-default.conf

Now restart or reload the server with command sudo service apache2 restart. Now both the directories can be accessed from localhost and localhost:8080



回答6:

I was working with LAMP and To change the Document Root folder i have edited default file which is there in /etc/apache2/sites-available folder. If you want to do the same just edit as follows

DocumentRoot /home/username/new_root_folder
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/username/new_root_folder>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

After this if you type localhost in browser it will load */home/username/new_root_folder* content.



回答7:

For apache2 on Linux Mint 17.3 Cinnamon 64-bit the following works:

  1. In /etc/apache2/sites-available/ open the 000-default.conf file, and change the Document Root to the absolute path of your directory.

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

  2. In /etc/apache2/ open httpd.conf, and add a <Directory> tag referencing your directory and containing the exact same settings as the tag for var/www.

    sudo vim /etc/apache2/apache2.conf

    On my machine it looked like this:


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

Note: In the first step you probably want to change Document Root in the default-ssl.conf file as well for SSL purposes. But as far as I can tell this isn\'t required to get a general development environment running.



回答8:

If you couldn\'t find http.conf and followed Nick\'s way.

Restart Apache using sudo service apache2 restart



回答9:

Incase you are using Ubuntu 16.04. Please update the 000-default.conf in the directory /etc/apache2/sites-available.

here--> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/YourFolder



回答10:

If someone has installed LAMP in the /opt folder then the /etc/apache2 is not what you are looking for.

Look for httpd.conf file in /opt/lampp/etc/httpd.conf.

Change the line in this folder and save it from terminal.



回答11:

In apache version 2.4.18 (Ubuntu).

1.go to this file /etc/apache2/apache2.conf search for <Directory /var/www/> and replace to your directory ......

2.go to /etc/apache2/sites-available/000-default.conf search for DocumentRoot /var/www/html and replace to your DocumentRoot....



回答12:

I had made the /var/www to be a soft link to the required directory ( eg. /users/username/projects) and things were fine after that.

However, naturally, the original /var/www needs to be deleted -- or renamed.



回答13:

In RedHat 7.0: /etc/httpd/conf/httpd.conf



回答14:

If you\'r using Linux Mint (personal opinion, from all distros this one is making me happy), follow this:

1- Go to /etc/apache2/sites-available and edit 000-default.conf 2- Search for DocumentRoot, example DocumentRoot /var/www/html you change to your respective directory; 3- Open terminal and type: sudo service apache2 restart

EDITED----- I realize that in Mint you go for /etc/apache2/apache.conf, replace /var/www to your respective path, than restart server (step 3).

That\'s it.



回答15:

Applies to Ubuntu 14.04 and later releases. Make sure to backup following files before making any changes.

1.Open /etc/apache2/apache2.conf and search for <Directory /var/www/> directive and replace path with /home/<USERNAME>/public_html. You can use * instead of .

2.Open /etc/apache2/sites-available/000-default.conf and replace DocumentRoot value property from /var/www/html to /home/<USERNAME>/public_html. Also <Directory /var/www/html> to <Directory /home/<USERNAME>/public_html.

3.Open /etc/mods-available/php7.1.conf. Find and comment following code

<IfModule mod_userdir.c>
    <Directory /home/*/public_html>
        php_admin_flag engine Off
    </Directory>
</IfModule>

Do not turn ON php_admin_flag engine OFF flag as reason is mentioned in comment above Directive code. Also php version can be 5.0, 7.0 or anything which you have installed.

Create public_html directory in home/<USERNAME>.

Restart apache service by executing command sudo service apache2 restart.

Test by running sample script on server.



回答16:

Instead of changing the default directory of your web server, you can create an Alias.

Therefore you have to create two files:

  • one in /etc/apache2/sites-available/ and
  • one in /etc/apache2/sites-enabled/

Call them both something like example.conf and insert in both files Alias <where you want to access (eg. /example)> \"<your files directory (eg. /home/user/host/example)>\"

Then you should be able to access it with //localhost/example.

EDIT:

Maybe I forgot to mention some commands because I forget them :P
or you have to edit the authorizations of the folder/file you try to access.



回答17:

If you are (like me) finding this post via Google:

I found it at /etc/apache2/sites-available/000-default.conf