简体   繁体   English

如何在 Docker Z2FEC392304A5C73AC138DA228C 容器中使用 Apache vhost URL

[英]How to use Apache vhost URL in Docker PHP container

I need to perform a cURL request from within PHP.我需要从 PHP 中执行cURL请求。

I don't know to access my project from inside the PHP container.我不知道从 PHP 容器内部访问我的项目。 myproject.local of course doesn't work as that configuration is set in the /etc/hosts file on my machine. myproject.local当然不起作用,因为该配置是在我机器上的/etc/hosts文件中设置的。

Apache and PHP-FPM are separate containers. Apache 和 PHP-FPM 是独立的容器。 Configuration below.配置如下。

Apache virtualhosts Apache 虚拟主机

<VirtualHost *:80>
    ServerName myproject.local
    ServerAlias www.myproject.local
    
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.0.33:9000/usr/local/apache2/my/project$1
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    DocumentRoot /usr/local/apache2/my/project
    <Directory /usr/local/apache2/my/project>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:443>
    ServerName myproject.local
    ServerAlias www.myproject.local

    SSLEngine on
    SSLCertificateFile /usr/local/apache2/conf/server.crt
    SSLCertificateKeyFile /usr/local/apache2/conf/server.key
    
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.0.33:9000/usr/local/apache2/my/project$1
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    DocumentRoot /usr/local/apache2/my/project
    <Directory /usr/local/apache2/my/project>
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

docker-compose.yml docker-compose.yml

version: "3.7"
services:
    apache:
        container_name: my-project-web
        ports:
            - "80:80"
            - "443:443"
        networks:
            public_net:
                ipv4_address: 192.168.0.11
    php:
        container_name: my-project-php
        depends_on:
            - apache
        networks:
            public_net:
                ipv4_address: 192.168.0.33
networks:
    public_net:
        driver: bridge
        ipam:
            driver: default
            config:
                - subnet: 192.168.0.0/24

By default, you can connect between containers within one Docker-Compose-Environment by their names.默认情况下,您可以通过名称在一个 Docker-Compose-Environment 中的容器之间进行连接。 No need to assign static IP addresses.无需分配 static IP 地址。

For your example from the PHP-Container to the Apache-Container with http://apache/ .对于您的示例,从 PHP-Container 到带有http://apache/的 Apache-Container 。 Or from you Apache config to the PHP-FPM with fcgi://php:9000 .或者从你 Apache 配置到 PHP-FPM 与fcgi://php:9000

But keep in mind, making requests on your own webserver is bad practise!但请记住,在您自己的网络服务器上发出请求是不好的做法!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM