Linux 如何更改apache服务器的根目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5891802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:48:42  来源:igfitidea点击:

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

linuxapachelamp

提问by Spencer Cooley

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

有谁知道如何更改Apache服务器的文档根目录?我基本上想localhost来自/users/spencer/projects目录而不是/var/www.

Edit

编辑

I ended up figuring it out. Some suggested I change the httpd.conffile, but I ended up finding a file in /etc/apache2/sites-available/defaultand changed the root directory from /var/wwwto /home/myusername/projects_folderand that worked.

我终于弄清楚了。有人建议我更改httpd.conf文件,但我最终在其中找到了一个文件/etc/apache2/sites-available/default并将根目录从 更改/var/www/home/myusername/projects_folder并且有效。

采纳答案by RDL

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

您需要更改文件中的DocumentRoot设置httpd.conf。可能会在类似的情况下/etc/apache2/conf/httpd.conf

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

使用您最喜欢的编辑器(我推荐Vim)并查找DocumentRoot并将其更改为/users/spencer/projects. 再往下看一些看起来像这样的设置:

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

您还需要将引号中的内容更改为新目录。当用户发出调用该目录的请求时,这使 Apache 可以从该目录读取。

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

现在重新启动您的 apache 服务 ( httpd -k restart),您应该一切顺利。

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

编辑:Apache2 站点配置文件现在通常保存在/etc/apache2/sites-available/(Debian、Ubuntu 等)中。

回答by Nick

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

我不得不编辑/etc/apache2/sites-available/default. 这些行与 RDL 提到的相同。

回答by Nitin

If you couldn't find http.confand followed Nick's way.

如果你找不到http.conf并遵循尼克的方式。

Restart Apache using sudo service apache2 restart

使用重新启动Apache sudo service apache2 restart

回答by Shashidhara

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

我正在使用 LAMP,为了更改文档根文件夹,我编辑了/etc/apache2/sites-available文件夹中的默认文件 。如果你想做同样的编辑如下

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.

在此之后,如果您在浏览器中键入 localhost,它将加载*/home/username/new_root_folder*内容。

回答by Yogesh Gupta

If someone has installed LAMPin the /optfolder then the /etc/apache2is not what you are looking for.

如果有人已安装LAMP在该/opt文件夹中,则该文件夹/etc/apache2不是您要查找的内容。

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

查找httpd.conf文件/opt/lampp/etc/httpd.conf

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

更改此文件夹中的行并从终端保存。

回答by mozzbozz

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

请注意,这仅适用于 Ubuntu 14.04 LTS 和更新版本。

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

在我的 Ubuntu 14.04 LTS 中,文档根目录设置为/var/www/html. 它在以下文件中配置:

/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/htmlto your preferred directory

并更改/var/www/html到您的首选目录

and save it.

并保存它。

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

保存更改后,只需重新启动 apache2 网络服务器即可完成:)

sudo service apache2 restart



如果您更喜欢图形文本编辑器,则只需将 替换sudo nanosudo nanogksu geditgksu gedit.

回答by ehacinom

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

在红帽 7.0 中: /etc/httpd/conf/httpd.conf

回答by Borkee

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

如果你(像我一样)通过谷歌找到这篇文章:

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

我在 /etc/apache2/sites-available/000-default.conf 找到它

回答by androsfat

This is for Ubunutu 14.04:

这适用于 Ubunutu 14.04:

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

在文件中,/etc/apache2/apache2.conf它应该如下所示,没有目录名:

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

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

在文件中,/etc/apache2/sites-available/000-default.conf您应该包含自定义目录名称,即 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

如果不是上面的,它会在加载服务器时给你一个错误: Forbidden You don't have permission to access / on this server

回答by stackato

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

对于Linux Mint 17.3Cinnamon 64 位上的apache2,以下工作:

  1. In /etc/apache2/sites-available/open the 000-default.conffile, and change the Document Rootto 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:

  1. /etc/apache2/sites-available/打开000-default.conf文件中,将文档根目录更改为目录的绝对路径。

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

  2. /etc/apache2/open 中httpd.conf,添加一个<Directory>引用您的目录并包含与var/www.

    sudo vim /etc/apache2/apache2.conf

    在我的机器上它看起来像这样:



<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 Rootin the default-ssl.conffile as well for SSL purposes. But as far as I can tell this isn't required to get a general development environment running.

注意:在第一步中,您可能还想更改文件中的Document Rootdefault-ssl.conf用于 SSL。但据我所知,这不是运行一般开发环境所必需的。