laravel 在 apache docker 容器中运行虚拟主机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55939079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running virtual hosts in apache docker container
提问by Luke.T
I have two php applications in the same apache container and I'm trying to run one of them on a port since it needs to be accessible via a root domain and not a subfolder.
我在同一个 apache 容器中有两个 php 应用程序,我试图在端口上运行其中一个,因为它需要通过根域而不是子文件夹访问。
I want to run the application on port 8060 which I've tried doing using apache virtual hosts but it won't load the page (http://192.168.99.100:8060/) it just says connection refused. However the normal root ip - http://192.168.99.100works fine. My docker file is as follows
我想在端口 8060 上运行应用程序,我已经尝试使用 apache 虚拟主机,但它不会加载页面(http://192.168.99.100:8060/)它只是说连接被拒绝。然而,正常的根 ip - http://192.168.99.100工作正常。我的docker文件如下
version: '3.2'
services:
php-apache:
build:
context: ./apache-php
ports:
- 80:80
- 8060:8060
expose:
- '8060'
volumes:
- ./DocumentRoot:/var/www/html:z
My apache configuration
我的apache配置
<VirtualHost *:60>
DocumentRoot /var/www/html/api
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Any help would be greatly appreciated.
任何帮助将不胜感激。
采纳答案by Luke.T
Thanks to @David MazeI found the problem I added the listen directives to the top of my apache configuration and changed the port numbers.
感谢@David Maze,我发现了问题,我将监听指令添加到我的 apache 配置的顶部并更改了端口号。
docker-compose.yml
docker-compose.yml
version: '3.2'
services:
php-apache:
build:
context: ./apache-php
ports:
- 80:80
- 8060:8060
volumes:
- ./DocumentRoot:/var/www/html:z
Apache config
阿帕奇配置
Listen 80
Listen 8060
<VirtualHost *:8060>
DocumentRoot /var/www/html/api
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Directory structure
目录结构
apache-php
? sqlite3_ext
? 000-default.conf (Apache config)
? Dockerfile
? php.ini
docker-compose.yml
Dockerfile
文件
FROM php:7.2.1-apache
RUN docker-php-ext-install pdo pdo_mysql mysqli
# Enable apache rewrite
COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
#Install spatialite and create symlink for libproj.so.0
COPY sqlite3_ext /etc/sqlite3_ext
RUN apt-get update && apt-get -y install gdal-bin
RUN ln -s /usr/lib/x86_64-linux-gnu/libproj.so.12 /usr/lib/x86_64-linux-gnu/libproj.so.0
#Install gd library for images
RUN apt-get install libpng-dev libfreetype6-dev libjpeg62-turbo-dev -qy \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd
#Copy php ini
COPY php.ini /usr/local/etc/php/php.ini
RUN a2enmod rewrite
回答by Hardik Raval
Here is a solution -- Step by Step:
这是一个解决方案 - 一步一步:
Step 1:
Add/Update extra_hosts
, hostname
, domainname
and in docker-compose.yml
and in my case I'm running it over PORT 80
.
This is how my php
service looks like in docker-compose.yml
file.
第1步:
添加/更新extra_hosts
,hostname
,domainname
并docker-compose.yml
在我的情况下我跑过来PORT 80
。
这就是我的php
服务在docker-compose.yml
文件中的样子。
php:
build: .
image: php:7.2-apache
working_dir: /var/www/html
volumes:
- ./:/var/www/html
- ./php.ini:/usr/local/etc/php/php.ini
extra_hosts:
- "lara.local:127.0.0.1"
hostname: lara.local
domainname: local
ports:
- 80:80
environment:
- "DB_PORT=3306"
- "DB_HOST=database"
Step 2:
Update your hosts
file with following line:
第 2 步:使用以下行
更新您的hosts
文件:
127.0.0.1 lara.local
Step 3:Test our hostname by running this command
第 3 步:通过运行此命令测试我们的主机名
docker exec -it <your-php-container-name> hostname
If you see output lara.local
then you are good to go!
如果您看到输出,lara.local
那么您就可以开始了!
Step 4:Rebuild app
第 4 步:重建应用程序
docker-compose build
Step 5:Start the services and check the app is running at http://lara.local
第 5 步:启动服务并检查应用程序正在运行http://lara.local
docker-compose up -d
Note:If you are using a different port for example 8080
then it would be http://lara.local:8080
注意:例如,如果您使用不同的端口,8080
那么它将是http://lara.local:8080
PS. If you want to change the
DocumentRoot
then ssh to your container andcd /etc/apache2/sites-available/000-default.conf
and change DocumentRoot path as per your needs.
附注。如果您想将
DocumentRoot
then ssh更改为您的容器并cd /etc/apache2/sites-available/000-default.conf
根据您的需要更改 DocumentRoot 路径。