【问题标题】:how to modify existing apache configuration to change document root for new application如何修改现有的 apache 配置以更改新应用程序的文档根目录
【发布时间】:2016-08-11 16:54:30
【问题描述】:

我对 apache 不是很有经验,但我已经设法在我的测试盒上使用以下设置运行了几个月:

me@mydev:/etc/apache2/sites-enabled$ vim 000-default.conf 
<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html

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

所以基本上,只要我在 /var/www/html 下创建一个新文件夹,它就会在我的浏览器中可用。 但现在我想尝试执行以下操作:

  1. 我创建了一个新应用程序,其结构如下:

    /var/www/html/testrestapp/src/public/index.php

它有效,但我必须导航到:

http://localhost/testrestapp/src/public/index.php/hello/sometestname

然后应用程序返回“sometestname”

但我想知道如何配置 apache 以便我可以像这样导航:

http://localhost/testrestapp/hello/sometestname

并获得相同的结果。

有人能指出我正确的方向吗?不知道用什么术语来谷歌这个。

谢谢。

编辑 1

我做了以下事情:

me@mydev:/etc/apache2/sites-enabled$ sudo a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  service apache2 restart

然后尝试重启:

me@mydev:/etc/apache2/sites-enabled$ service apache2 restart
Job for apache2.service failed because the control process exited with error code. See "systemctl status apache2.service" and "journalctl -xe" for details.

Apr 19 10:14:39 mydev apache2[9010]: * The apache2 configtest failed.
Apr 19 10:14:39 mydev apache2[9010]: Output of config test was:
Apr 19 10:14:39 mydev apache2[9010]: AH00526: Syntax error on line 14 of /etc/apache2/sites-enabled/000-default.conf:
Apr 19 10:14:39 mydev apache2[9010]: RewriteBase: only valid in per-directory config files
Apr 19 10:14:39 mydev apache2[9010]: Action 'configtest' failed.
Apr 19 10:14:39 mydev apache2[9010]: The Apache error log may have more information.
Apr 19 10:14:39 mydev systemd[1]: apache2.service: Control process exited, code=exited status=1
Apr 19 10:14:39 mydev systemd[1]: Failed to start LSB: Apache2 web server.
Apr 19 10:14:39 mydev systemd[1]: apache2.service: Unit entered failed state.
Apr 19 10:14:39 mydev systemd[1]: apache2.service: Failed with result 'exit-code'.

这是我的 /etc/apache2/sites-enabled/000-default.conf 文件的样子:

 11         ServerAdmin webmaster@localhost
 12         DocumentRoot /var/www/html
 13         RewriteEngine  on
 14         RewriteBase    /testrestapp/
 15         RewriteRule    ^src/public/index.php/hello$  hello/  [R]

编辑 2:

我的 000-default.conf 文件现在看起来像这样:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>
<Directory "/var/www/html/testrestapp/">
    RewriteEngine  on
    RewriteBase    /testrestapp/
    RewriteRule    ^src/public/index.php/hello$  hello/  [R]
</Directory>

Apache 重启成功:

me@mydev:/etc/apache2/sites-enabled$ sudo /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.

但是当我导航到 http://localhost/testrestapp/hello/jojo 时,我收到 404 错误。

这是我在 access.log 中看到的:

127.0.0.1 - - [19/Apr/2016:13:50:04 -0400] "GET /testrestapp/hello/jojo HTTP/1.1" 404 507 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36"

【问题讨论】:

  • webmaster@localhost 没用。
  • @tripleee 感谢您告诉我。我只是在首次设置时将所有内容保留为默认值
  • @Happydevdays 抱歉昨天误导了你,你的编辑和时间安排是不必要的,正如你在我的回答中看到的那样。

标签: apache


【解决方案1】:

在您的“/etc/apache2/sites-enabled/000-default.conf”中,使用mod_alias 并添加以下内容:

alias "/testrestapp/hello" "/var/www/html/testrestapp/src/public/index.php/hello"

【讨论】:

    猜你喜欢
    • 2016-06-21
    • 2014-09-03
    • 2016-04-17
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多