How do I change the root directory of an apache se

2019-01-01 06:33发布

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.

17条回答
皆成旧梦
2楼-- · 2019-01-01 06:55

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.).

查看更多
荒废的爱情
3楼-- · 2019-01-01 06:57

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.

查看更多
怪性笑人.
4楼-- · 2019-01-01 06:58

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....

查看更多
永恒的永恒
5楼-- · 2019-01-01 06:59

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.

查看更多
初与友歌
6楼-- · 2019-01-01 07:02

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

查看更多
登录 后发表回答