DEV Community

Cover image for Habilitando a sobrescrita do apache no Docker
Jhonatan Henkel
Jhonatan Henkel

Posted on

Habilitando a sobrescrita do apache no Docker

Olá pessoal, hoje vamos bater um papo sobre como configurar o apache no seu container docker para aceitar a sobrescrita de regras no .htaccess do se projeto.

Tenho o seguinte .htaccess:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php [QSA,L]
Enter fullscreen mode Exit fullscreen mode

Nesse .htaccess estou alterando a regra para mandar tudo após o diretório home “/” para o meu index.php.

Como no apache do docker está desabilitado a regra de sobrescrita, acaba que sempre que eu ia acessar a minha rota pelo insomnia retornava server error 500.

Como estamos falando de docker, vamos começar pelo Dockerfile, estou usando a imagem php:8.1.11-apache.

Adicionei dois comando run, como pode-se verificar abaixo:

RUN a2enmod rewrite
RUN addgroup --gid 1000 appuser; \
    adduser --uid 1000 --gid 1000 --disabled-password appuser; \
    adduser www-data appuser; \
    sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf; \
    service apache2 restart;
Enter fullscreen mode Exit fullscreen mode

Nessas dois comandos run, estou habilitando a sobrscrita do apache no container, adicionando um usuário para alterar o apache2.conf e reiniciando o serviço do apache.

Sendo assim, quando eu quando eu subir esse container, o apache já estará configurado e pronto para aceitar as regras do .htaccess do seu projeto.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay