Score:0

How to use environment variable in apache config condition?

ke flag

In my apache config file I'm trying to set a virtual host on port 443 IF my environment variable is not dev

I have a docker container running via docker-compose which specifies the environment variables.

To check the env variables are properly set, I remote into my container:

root@4f212c1c7e23:/var/www# echo ${APP_ENV}
prod

Below is the apache config file that is apart of the volume mapped to the container

000-default.conf:

<VirtualHost *:80>

  ServerName ${DOMAIN}
  ServerAdmin ${ADMIN_EMAIL}
  DocumentRoot /var/www/public

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<IfModule "%{APP_ENV} != 'dev'">
  <IfModule mod_ssl.c>
    <VirtualHost *:443>

      ServerName ${DOMAIN}
      ServerAdmin ${ADMIN_EMAIL}
      DocumentRoot /var/www/public
            
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined
        
      SSLCertificateFile /var/imported/ssl/main.crt
      SSLCertificateKeyFile /var/imported/ssl/privkey.pem
      SSLEngine on

    </VirtualHost>
  </IfModule>
</IfModule>

But when starting the container on production, the result in browser is "ERR_SSL_PROTOCOL_ERROR" when trying to access port 443.

My error log only reads:

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.26.0.2. Set the 'ServerName' directive globally to suppress this message

Similarly the expression "%{APP_ENV} == 'prod'", gives the same result

When deleting the outer IfModule in the apache config, the connection is accepted and everything works as expected.

So I'm wondering how to get the outer IfModule condition to behave as intended?

Please let me know if there is more info I can provide

David avatar
ke flag
I realize that `IfModule` is not doing what I think it does. I have instead opted to use `IfFile` which checks if main.crt exists (which is only copied in my production docker-compose file): `<IfFile "/var/imported/ssl/main.crt">` and this seems to work as I planned
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.