Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I install mod_rewrite module for PHP in Docker container?

Tags:

php

docker

apache

docker-compose.yml

version: "3.7"
services:

  php:
    build: './php/'
    volumes:
      - ./public_html/www/:/var/www/html/

  apache:
    build: './apache/'
    depends_on:
      - php
      - mysql
    ports:
      - "8080:80"
    volumes:
      - ./public_html/www/:/var/www/html/
      #- ./log:/var/log/apache2

  mysql:
    image: mysql:5.6.40
    ports:
      - "3306:3306"
    environment:
      - MYSQL_DATABASE=el-cigarette
      - MYSQL_ROOT_PASSWORD=root

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - '8081:80'

php/Dockerfile

FROM php:7.0-apache
MAINTAINER Webgriffe Srl <[email protected]>
RUN docker-php-ext-install mysqli && a2enmod rewrite

apache/demo.apache.conf

ServerName localhost

LoadModule deflate_module /usr/local/apache2/modules/mod_deflate.so
LoadModule proxy_module /usr/local/apache2/modules/mod_proxy.so
LoadModule proxy_fcgi_module /usr/local/apache2/modules/mod_proxy_fcgi.so



<VirtualHost *:80>
    # Proxy .php requests to port 9000 of the php-fpm container
    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://php:9000/var/www/html/$1
    DocumentRoot /var/www/html/

    DirectoryIndex index.php

    <Directory /var/www/html/>
        Options All
            AllowOverride All
        Order allow,deny
        Allow from all
            Require all granted
    </Directory>
    
    # Send apache logs to stdout and stderr
    CustomLog /var/www/1 common
    ErrorLog /var/www/2
</VirtualHost>

I inputted next commands

docker-compose build
docker-compose up -d

And I get that output

Creating docker-local2_mysql_1 ... done
Creating docker-local2_php_1        ... done
Creating docker-local2_phpmyadmin_1 ... done
Creating docker-local2_apache_1     ... done

Farther I write this

docker exec -it docker-local2_php_1 sh
# php -m

But I didn't see mod_rewrite module

[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

My .htaccess file doesn't work Where did I go wrong? And what I need do to fix this trouble?

like image 460
Тимур Сафаров Avatar asked Nov 28 '25 16:11

Тимур Сафаров


1 Answers

As mentioned in the comment, mod_rewrite is not a PHP module, it an apache module. You have already enabled it in your php/Dockerfile at:

a2enmod rewrite

No additional package installation is required. Verify that the module is enabled by running apachectl -M to list all enabled modules, you should see something like:

...
rewrite_module (shared)
...

With regards to your docker compose file, it appears you are using two separate containers to run your web service, where in actual fact you only need one (unless you are using a second apache container to reverse-proxy requests to the PHP-apache container). In the simple case, PHP and apache have been preconfigured to work together (e.g. mod_php pre-installed) in the php:<version>-apache image and you would just need to copy your demo.apache.conf as part of your php build, for example:

version: "3.7"
services:
  php:
    build: './php/'
    volumes:
      - ./public_html/www/:/var/www/html/
      - ./apache/demo.apache.conf:/etc/apache2/sites-enabled/demo.conf
  mysql:
    image: mysql:5.6.40
    ports:
      - "3306:3306"
    environment:
      - MYSQL_DATABASE=el-cigarette
      - MYSQL_ROOT_PASSWORD=root
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mysql
    environment:
      PMA_HOST: mysql
      PMA_PORT: 3306
    ports:
      - '8081:80'

Final tip, avoid using the links keyword, they are deprecated. Docker compose will by default create a network for you in which all containers can resolve eachother using service names as hostnames.

like image 197
danialk Avatar answered Dec 01 '25 05:12

danialk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!