将Apache文档根目录更改为辅助硬盘驱动器

我在我的服务器电脑上安装了Ubuntu 12.04服务器版。我已经安装了LAMP服务器。我需要将/var/www的位置更改为我的辅助硬盘位置。我已经多次尝试在gedit /etc/apache2/sites-available/default中进行配置,以下是我的代码。
<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    #DocumentRoot /var/www
    DocumentRoot /media/myserver/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    #<Directory /var/www/>
        <Directory /media/myserver/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

而且也使用了

sudo chown -R var/www /media/myserver/

并且

chmod -R 755 /media/myserver/

尽管如此,我仍然无法连接我的 /media/myserver,并且我的浏览器显示以下信息。
Forbidden

You don't have permission to access / on this server.

请告诉任何人如何将我的服务器挂载到我的var/www目录下,提前感谢。

第二个驱动器是否已经格式化并挂载到/media/myserver目录下?您是否为该驱动器添加了fstab条目? - damien
你有检查过文档根目录后面的斜杠是否是问题所在吗?注意到在注释掉的地方没有斜杠,但在你的编辑中却有。 - CodingInTheUK
你试过改变apache2.conf文件吗? - Golden_flash
8个回答

你需要编辑 apache2.conf000-default.conf 来改变 Apache 的文档根目录。
Apache 服务器安装在 /var/www/html。这是 Apache 的默认根目录。
要么改变 Apache 的根目录,要么将项目移动到 /var/www/html
要更改Apache的根目录,请运行以下命令: ``` cd /etc/apache2/sites-available ```
然后使用以下命令打开`000-default.conf`文件: ``` nano 000-default.conf ```
зј–иѕ‘`DocumentRoot`йЂ‰йЎ№пјљ ``` DocumentRoot /path/to/my/project ```
然后重新启动Apache服务器: ``` sudo service apache2 restart ```
如果您在更改Apache的根目录后遇到“Forbidden You don't have permission to access / on this server”错误,请按照以下步骤操作。
找到位于/etc/apache2的apache2.conf文件,并使用以下命令打开它: nano apache2.conf
按Ctrl+W进行搜索,找到Directory(应该在第153行)
它应该是这样的: Options Indexes FollowSymLinks AllowOverride All Require all denied 将其修改为: Options Indexes FollowSymLinks Includes ExecCGI AllowOverride All Require all granted 重新启动Apache:
sudo service apache2 restart
我写了一个脚本,可以通过一条命令来更改Apache的根目录。你可以在我的github上找到它。

这对我来说是一项工作,但我必须在重新启动apache2服务之前,在/etc/apache2/apahce2.conf中添加以下内容: Options Indexes FollowSymLinks AllowOverride All Require all denied - r0ng
这在20.04中有效,谢谢。 - Geppettvs D'Constanzo

也许有点晚了。但还是要说一下。。
你应该在/etc/apache2下的apache.conf中编辑你的目录权限。
搜索以下内容
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

在此代码下方添加以下代码,该代码允许访问您的目录。
 <Directory /media/myserver/>
            Options Indexes FollowSymLinks
            AllowOverride None
            Require all granted
    </Directory>

只需在已激活的配置文件中更改文档根目录,路径为 /etc/apache2/sites-enabled/000-default ,然后确保重新加载 Apache。
那么,请尝试以下步骤:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /media/myserver/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /media/myserver/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

然后应该给予适当的许可,就像这样:
sudo adduser <username> www-data
sudo chown -R www-data:www-data /media/myserver/
sudo chmod -R g+rw /media/myserver/

是的,我的朋友,我已经完成了这个。请看上面的代码,我已经注释掉了默认的文档根目录并插入了新的文档根目录。在重启Apache2后,它没有起作用。出现了403错误,显示权限访问被拒绝,并且我的浏览器也无法访问。 - Moovendra Dhinesh babu
你看到我最后的编辑了吗? - Achu
是的,我看到了。不过显示你没有权限访问/这个服务器。 - Moovendra Dhinesh babu
运行httpd.exe时出现错误:无效命令'Order',可能拼写错误或由服务器配置中未包含的模块定义 - mercury

你需要修改apache2.conf文件。将/var/www/替换为/your/path
```html Options Indexes FollowSymLinks AllowOverride None Require all granted ```
修改000-default.conf文件:
```html DocumentRoot /your/path/ ```
通过文件管理器或终端更改目录的所有权(例如:sudo chown pi: path)。如果不这样做,你将会收到以下错误信息:
```html You don't have permission to access / on this server. ```

作为一个快速的权宜之计(安全和迅速),您可以将外部硬盘驱动器的挂载点设置为默认根目录(默认情况下为 /var/www)。
将挂载点分配给已存在的目录是安全的,但除非您卸载了驱动器,否则无法访问旧内容。
要了解有关如何创建挂载点的更多信息,请参考this

根据那个想法,我会使用bindfs将某个文件夹挂载起来,具体描述请参考此链接:https://askubuntu.com/a/1024308/566421 - pa4080

使用以下命令打开文件:sudo gedit etc/apache2/apache2.conf 在文件中添加以下内容:
Options Indexes FollowSymLinks MultiViews AllowOverride None Order allow,deny allow from all
在虚拟配置文件/etc/apache2/sites-available/site.conf中添加。
ServerAdmin anilrmg@localhost.com
ServerName anilrmg.localhost.com
ServerAlias www.anilrmg.localhost.com
DocumentRoot /home/anilrmg/projects/code/anilrmg

sudo a2dissite 000-default.conf


对于那些使用VirtualBox客户端附加组件并且尽管上述所有操作都已尝试,仍然出现“您没有权限访问/此服务器”的用户:
如果您正在尝试将Apache文档根文件夹设置为VirtualBox共享文件夹,并且您已经尝试了上述所有方法但没有帮助,还有一步需要完成。
简而言之,解决方案是将用户'www-data'添加到组'vboxsf'中。
sudo usermod -a -G vboxsf www-data

你无法更改VirtualBox共享文件夹的所有者和/或组,但上述解决方案对我很有效。

我必须确保整个路径都被授予Apache权限。
chown www-data /media;
chown www-data /media/MNT/;
chown www-data /media/MNT/DISK;
chown www-data /media/MNT/DISK/www-root;