我如何更改Apache服务器的文档根目录?基本上,我希望localhost
的根目录是/users/spencer/projects
目录,而不是/var/www
。
最终我找到了解决方法。有些人建议我更改httpd.conf
文件,但我最终在/etc/apache2/sites-available/default
文件中找到了并将根目录从/var/www
更改为/home/myusername/projects_folder
,这样就可以了。
我如何更改Apache服务器的文档根目录?基本上,我希望localhost
的根目录是/users/spencer/projects
目录,而不是/var/www
。
最终我找到了解决方法。有些人建议我更改httpd.conf
文件,但我最终在/etc/apache2/sites-available/default
文件中找到了并将根目录从/var/www
更改为/home/myusername/projects_folder
,这样就可以了。
/var/www/html
。它在以下文件中配置:/etc/apache2/sites-available/000-default.conf
那就只需执行
sudo nano /etc/apache2/sites-available/000-default.conf
DocumentRoot /var/www/html
也执行一个
sudo nano /etc/apache2/apache2.conf
并找到这个:
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
将/var/www/html
更改为您喜欢的目录并保存。
保存更改后,只需重新启动Apache 2 Web服务器即可完成:)
sudo service apache2 restart
sudo nano
替换为gksu gedit
。var/www/html
作为根目录,这真是一个奇怪的选择。他们难道就是想浪费人们的时间吗? :) - davidkonrad你需要在你的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等系统)。我必须编辑/etc/apache2/sites-available/default
文件。这些行的内容与RDL提到的相同。
.conf
,并且默认情况下前缀为000-
。”结果应该是/etc/apache2/sites-available/000-default.conf
。我没有测试过,但这可能对某些人有所帮助。 - Nick这是针对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
如果不是如上所述,加载服务器时会出现错误:
禁止访问 您没有权限访问此服务器上的 /
在Apache 2下更改目录或从不同端口的多个目录运行的正确方法如下:
对于Apache 2,配置文件位于/etc/apache2
,不像旧版本那样使用单个配置文件,而是拆分为较小的配置文件,其中/etc/apache2/apache2.conf
是主要的配置文件。为了从其他目录提供文件,我们需要一个新的虚拟主机(conf)文件。虚拟主机配置文件位于/etc/apache2/sites-available
(不要编辑sites-enabled中的文件)。默认的Apache安装使用虚拟主机配置文件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
具有读取和执行访问权限。
现在,通过发出命令sudo gedit apache2.conf
编辑Apache配置文件/etc/apache2/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
文件使用与默认配置文件000-default.conf
相同的端口(80
),因此我们必须通过输入命令sudo a2dissite 000-default.conf
禁用默认配置文件,并通过sudo a2ensite example.com.conf
启用我们的虚拟主机配置文件。
现在使用命令sudo service apache2 restart
重新启动或重新加载服务器。现在Apache会在默认端口80
上的localhost
上从example.com
目录中提供文件。
a2ensite
命令基本上创建了一个符号链接到站点启用目录下的配置文件。
请勿编辑 sites-enabled(或 *-enabled) 目录中的文件,如此答案所述。
如果您需要在不同的端口上运行目录,请将端口号从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
访问。
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>
000-default.conf
? - Peter Mortensen对于在Linux Mint 17.3 Cinnamon 64位上的Apache 2,以下操作可行:
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
In folder /etc/apache2/
open file 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>
注意: 在第一步中,您可能需要为了SSL目的更改default-ssl.conf
文件中的文档根目录。但据我所知,这不是运行一般开发环境所必需的。
<Directory /var/www/>
,将其替换为您的目录。
2. 打开文件/etc/apache2/sites-available/000-default.conf,搜索 DocumentRoot /var/www/html
,将其替换为您的 DocumentRoot。打开 /etc/apache2/apache2.conf
文件,搜索 <Directory /var/www/>
指令,并将路径更改为 /home/<USERNAME>/public_html
。您可以使用 * 代替 .。
打开 /etc/apache2/sites-available/000-default.conf
文件,将 DocumentRoot
值属性从 /var/www/html
更改为 /home/<USERNAME>/public_html
。还要将 <Directory /var/www/html>
更改为 <Directory /home/<USERNAME>/public_html
。
打开 /etc/mods-available/php7.1.conf
文件。找到并注释掉以下代码:
php_admin_flag engine OFF
标志,因为指令代码上方的注释中已经提到了原因。此外,PHP 版本可以是 5.0、7.0 或您安装的任何版本。home/<USERNAME>
中创建 public_html
目录。sudo service apache2 restart
重新启动 Apache 服务。