Linux 如何在 Ubuntu 中创建公共 HTML 文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526742/
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 can I make a public HTML folder in Ubuntu?
提问by RexE
Simple question, but for some reason I couldn't find the exact answer on Google:
简单的问题,但由于某种原因,我在谷歌上找不到确切的答案:
I have a fresh Ubuntu install on Slicehost, and would like to make a public directory in my home dir for a simple website containing a bunch of static HTML files. How do I do this? Is it just a matter of typing mkdir public_html
and setting the permissions, or is there a cleaner way? (I remember in the past I've had issues where every time I copied a file into my public_html directory, I would have to manually set its permissions, which was quite frustrating.)
我在 Slicehost 上安装了一个全新的 Ubuntu,并且想在我的主目录中为一个包含一堆静态 HTML 文件的简单网站创建一个公共目录。我该怎么做呢?只是输入mkdir public_html
和设置权限的问题,还是有更简洁的方法?(我记得过去我遇到过问题,每次我将文件复制到 public_html 目录中时,我都必须手动设置其权限,这很令人沮丧。)
采纳答案by rz.
Assuming you've already installed apache, do the following:
假设您已经安装了 apache,请执行以下操作:
sudo a2enmod userdir
sudo service apache2 reload
The first command enables the userdir apache mod, which does exactly what you want. The second reloads apache configurations so that it starts using the new configuration.
第一个命令启用 userdir apache mod,它完全符合您的要求。第二个重新加载 apache 配置,以便它开始使用新配置。
To install apache2:
安装 apache2:
sudo apt-get install apache2
Of course, you'll also need to make sure that the permissions on your public_html folder allow the www-data user to see the files in there -- 755 usually works well. To do this:
当然,您还需要确保 public_html 文件夹的权限允许 www-data 用户查看其中的文件——755 通常工作良好。去做这个:
mkdir ~/public_html
chmod -R 755 ~/public_html
This will recursively (-R) go through your public_html and set the permissions to 755 (owner rwx, and both group and other r-x, r-x).
这将递归 (-R) 遍历您的 public_html 并将权限设置为 755(所有者 rwx,以及组和其他 rx、rx)。
回答by strager
You need to use mod_userdirfor Apache, otherwise you need to set up symlinks from /var/www/
or wherever.
您需要为 Apache使用mod_userdir,否则您需要从/var/www/
任何地方设置符号链接。
Your permissions issue is because Apache does not have read access to your files. You need to allow read access to www-data
(or whatever the user is; distro-specific).
您的权限问题是因为 Apache 对您的文件没有读取权限。您需要允许读取访问www-data
(或任何用户;特定于发行版)。
回答by David Z
The other answers are on the right track with mod_userdir
, but using that will give your website the base URL http://www.yourdomain.com/~username/
- for instance, a file /home/username/public_html/index.html
would be accessible as http://www.yourdomain.com/~username/index.html
. If you want your files to be accessible under the domain root, as http://www.yourdomain.com/index.html
for example, then you'll need to put the directive
其他的答案是正确的轨道上mod_userdir
,但使用这将使你的网站的基本URL http://www.yourdomain.com/~username/
-例如,文件/home/username/public_html/index.html
将作为访问http://www.yourdomain.com/~username/index.html
。例如,如果您希望您的文件可以在域根目录下访问http://www.yourdomain.com/index.html
,那么您需要放置指令
DocumentRoot /home/username/public_html
in the Apache configuration file.
在 Apache 配置文件中。
By the way, this kind of question is more suited for the Slicehost Forums.
顺便说一句,这种问题更适合 Slicehost 论坛。