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
How do I change the root directory of an apache server?
提问by Spencer Cooley
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
.
有谁知道如何更改Apache服务器的文档根目录?我基本上想localhost
来自/users/spencer/projects
目录而不是/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.
我终于弄清楚了。有人建议我更改httpd.conf
文件,但我最终在其中找到了一个文件/etc/apache2/sites-available/default
并将根目录从 更改/var/www
为/home/myusername/projects_folder
并且有效。
采纳答案by RDL
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
您需要更改文件中的DocumentRoot
设置httpd.conf
。可能会在类似的情况下/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:
使用您最喜欢的编辑器(我推荐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.conf
and 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 LAMP
in the /opt
folder then the /etc/apache2
is not what you are looking for.
如果有人已安装LAMP
在该/opt
文件夹中,则该文件夹/etc/apache2
不是您要查找的内容。
Look for httpd.conf
file 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/html
to 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 nano
sudo nano
为gksu gedit
gksu 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.conf
it 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.conf
you 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,以下工作:
In
/etc/apache2/sites-available/
open the000-default.conf
file, and change the Document Rootto the absolute path of your directory.sudo vim /etc/apache2/sites-available/000-default.conf
In
/etc/apache2/
openhttpd.conf
, and add a<Directory>
tag referencing your directory and containing the exact same settings as the tag forvar/www
.sudo vim /etc/apache2/apache2.conf
On my machine it looked like this:
在
/etc/apache2/sites-available/
打开000-default.conf
文件中,将文档根目录更改为目录的绝对路径。sudo vim /etc/apache2/sites-available/000-default.conf
在
/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.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.
注意:在第一步中,您可能还想更改文件中的Document Root以default-ssl.conf
用于 SSL。但据我所知,这不是运行一般开发环境所必需的。