使用 Apache 在目录中加载 Symfony 项目

使用 Apache 在目录中加载 Symfony 项目

我有一个 Symfony PHP 应用程序,我想在装有 Apache 2.4 的 Windows 服务器中运行

其文件存储在D:\Apache24\htdocs\project1\public

它的切入点是D:\Apache24\htdocs\project1\public\index.php

我按照以下说明进行操作https://symfony.com/doc/current/setup/web_server_configuration.html并将其添加到我的\Apache24\conf\extra\httpd-vhosts.conf

<VirtualHost *:80>
    ServerName localhost
    ServerAlias localhost

    DocumentRoot /Apache24/htdocs/project1/public
    <Directory /Apache24/htdocs/project1/public>
        AllowOverride All
        Require all granted
    </Directory>

    # uncomment the following lines if you install assets as symlinks
    # or run into problems when compiling LESS/Sass/CoffeeScript assets
    # <Directory /var/www/project>
    #     Options FollowSymlinks
    # </Directory>

    ErrorLog /Apache24/logs/project1_error.log
    CustomLog /Apache24/logs/project1_access.log combined
</VirtualHost>

但是然后我的项目接管了服务器并在根目录加载,我不想要这个。

我希望这个项目的应用程序能够http://localhost/project1

我如何才能实现这一点并满足 Symfony 的路由需求?

这是我的http -S输出:

Included configuration files:
  (*) D:/Apache24/conf/httpd.conf
    (514) D:/Apache24/conf/extra/httpd-vhosts.conf
VirtualHost configuration:
*:80                   localhost (D:/Apache24/conf/extra/httpd-vhosts.conf:41)
ServerRoot: "D:/Apache24"
Main DocumentRoot: "D:/Apache24/htdocs"
Main ErrorLog: "D:/Apache24/logs/error.log"
Mutex default: dir="D:/Apache24/logs/" mechanism=default
PidFile: "D:/Apache24/logs/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
Define: DUMP_MODULES
Define: DUMP_INCLUDES
Define: SRVROOT=D:\Apache24
Loaded Modules:
 core_module (static)
 win32_module (static)
 mpm_winnt_module (static)
 http_module (static)
 so_module (static)
 actions_module (shared)
 alias_module (shared)
 allowmethods_module (shared)
 asis_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_groupfile_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 cgi_module (shared)
 dir_module (shared)
 env_module (shared)
 include_module (shared)
 isapi_module (shared)
 log_config_module (shared)
 mime_module (shared)
 negotiation_module (shared)
 setenvif_module (shared)
 php7_module (shared)

答案1

您需要在 vhost 配置中使用以下内容: 别名 /project1 /Apache24/htdocs/project1/public

更多信息请访问: https://httpd.apache.org/docs/2.4/mod/mod_alias.html

另一种方法是将 project1.local 之类的域名添加到本地主机文件,为托管在本地主机上的站点使用自定义域名。

(您需要管理员权限才能修改此文件)c:\windows\system32\drivers\etc\hosts 或者在 *nix (Mac) 上是 /etc/hosts

添加的内容如下所示,但不要删除任何未添加的内容:127.0.0.1 project1.local www.project1.local

我更喜欢第二种开发方法,因为我可以让本地网站主机名与生产主机名相似。它还可以使从开发环境到生产环境的转换更加顺畅,因为应用程序仅依赖于不同的主机名,而不是主机名和路径。

相关内容