如何修改Apache服务器的文档根目录?我基本上希望localhost来自/users/spencer/projects目录,而不是/var/www.


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


当前回答

这是Ubuntu 14.04 (Trusty Tahr):

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

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

在/etc/apache2/sites-available/000-default.conf文件中,你应该包括自定义目录名,即www:

DocumentRoot /home/username/www

如果不是如上所述,它会在加载服务器时给你一个错误:

禁止您没有访问此服务器的权限

其他回答

我已经使/var/www成为一个软链接到所需的目录(例如,/users/username/projects),之后事情就很好了。

但是,很自然地,原始的/var/www需要被删除或重命名。

如果你正在使用Linux Mint(个人观点,从所有的发行版来看,这个版本让我很开心),请遵循以下内容:

进入/etc/apache2/sites-available文件夹,编辑文件000-default.conf。 搜索DocumentRoot,例如DocumentRoot /var/www/html。您更改到您各自的目录; 打开终端,输入:sudo service apache2 restart

在Linux Mint中,您可以访问文件/etc/apache2/apache.conf。用您各自的路径替换/var/www,然后重新启动服务器(步骤3)。

就是这样。

这是Ubuntu 14.04 (Trusty Tahr):

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

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

在/etc/apache2/sites-available/000-default.conf文件中,你应该包括自定义目录名,即www:

DocumentRoot /home/username/www

如果不是如上所述,它会在加载服务器时给你一个错误:

禁止您没有访问此服务器的权限

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

使用你最喜欢的编辑器(我推荐Vim),寻找DocumentRoot并将其更改为/users/spencer/projects。再往下看一点,是这样的设置:

<Directory "/var/www">

您还需要将引号中的内容更改到新目录中。这使Apache能够在用户发出调用该目录的请求时从该目录读取数据。

现在重新启动Apache服务(httpd -k restart),您应该可以正常运行了。

Apache 2站点配置文件现在通常保存在/etc/apache2/sites-available/ (Debian, Ubuntu等)。

Apache 2在不同端口下切换目录或从多个目录运行的正确方法如下:

For Apache 2, the configuration files are located under /etc/apache2 and doesn’t 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 configuration 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.

首先,复制Apache默认安装所使用的默认虚拟主机文件(在localhost上的80端口上运行的虚拟主机文件),创建一个新的虚拟主机文件。进入目录/etc/apache2/sites-available,然后用sudo cp 000-default.conf example.com.conf复制,现在用sudo gedit example.com.conf编辑文件到:

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

为了简洁起见,我从上面的文件中删除了不重要的行。这里DocumentRoot是网站文件的目录路径,例如index.html。

创建存放文件的目录,例如mkdir example.com,并修改目录的属主和默认组,例如登录用户名为ubuntu,则更改权限为sudo chown ubuntu:www-data example.com。这将授予用户ubuntu的完全访问权限,并允许读取和执行组www-data的访问权限。

现在编辑Apache配置文件/etc/apache2/apache2.conf,通过发出命令sudo gedit apache2.conf,找到<Directory /var/www/>和</Directory>结束标记下面的行,添加以下内容:

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

现在有两个命令来启用或禁用虚拟主机配置文件,分别是a2ensite和a2dissite。现在,由于example.com.conf文件使用的端口(80)与默认配置文件(000-default.conf)使用的端口相同,我们必须通过发出sudo a2dissite 000-default.conf命令来禁用默认配置文件,并通过sudo a2ensite example.com.conf启用虚拟主机配置文件

现在使用sudo service apache2 restart命令重新启动或重新加载服务器。现在Apache在默认端口为80的localhost上提供来自example.com目录的文件。

a2ensite命令基本上是在启用站点的目录下创建到配置文件的符号链接。

如本回答中所指出的,不要编辑启用站点(或启用*)目录中的文件。

更改端口并在不同端口的多个目录下运行:

现在如果你需要在不同的端口上运行目录,通过编辑虚拟主机文件将端口号从80更改为8080:

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

编辑/etc/apache2/ports.conf,在“Listen 80”行下面添加“Listen 8080”

现在,我们可以启用运行在端口80上的默认虚拟主机配置文件,因为example.com目录使用端口8080,如sudo a2ensite 000-default.conf。

现在使用sudo service apache2 restart命令重新启动或重新加载服务器。现在可以从localhost和localhost:8080访问这两个目录。