How to set a variable in the configuration of a Virtual Host, to avoid repetition?

Asked

Viewed 133 times

2

I always get upset when I see that my virtualhost ends up being configured this way:

<VirtualHost :80>

    ServerName meusite.local
    ServerAlias www.meusite.local


    DocumentRoot /var/www/meusite/public

    ErrorLog /var/www/meusite/__apache__.log

</VirtualHost>

Note that I repeated the /var/www/meusite/ twice there and, considering I could have more settings using that same root folder, it might be a pain in the ass to do that.

I wonder if there’s any way to save this /var/www/meusite into a variable and use it, concatenating with /public and __apache__.log, for example.

Is there any way to do that?

2 answers

3


In Apache Core there is the Define, as documented: https://httpd.apache.org/docs/2.4/mod/core.html#Define

It is used to define variables

Define root_dir /var/www/meusite

<VirtualHost :80>    
    ServerName meusite.local
    ServerAlias www.meusite.local

    DocumentRoot ${root_dir}/public

    ErrorLog ${root_dir}/__apache__.log
</VirtualHost>

On multiple hosts:

Define root_dir1 /var/www/meusite1
Define root_dir2 /var/www/meusite2

<VirtualHost meusite1.local:80>
    ServerAlias www.meusite2.local

    DocumentRoot ${root_dir1}/public

    ErrorLog ${root_dir1}/__apache__.log
</VirtualHost>

<VirtualHost meusite2.local:80>
    ServerAlias www.meusite2.local

    DocumentRoot ${root_dir2}/public

    ErrorLog ${root_dir2}/__apache__.log
</VirtualHost>

It is worth noting that if you use many "includes" maybe back and a half may accidentally end up conflicting, and include something twice, in this case you can use the <IfDefined> to check whether a file or a variable has already been defined

<IfDefine !FOO>
Include Foo.config
</IfDefine>

In this clear example that within Foo.config there must be something like:

Define FOO valor

In addition to setting it is possible to remove a variable, thus:

<IfDefine FOO>
UnDefine FOO
</IfDefine>

It is good to highlight this excerpt from the documentation:

Virtual Host Scope and pitfalls

While this Directive is supported in virtual host context, the changes it makes Visible to any later Configuration Directives, Beyond any enclosing virtual host.

Translating:

The scope of the Virtual Host and its pitfalls

Although this directive is supported in the context of the Virtual Host, the changes made are visible to any other later configuration directive in addition to any other included Virtual Host.

  • Curiosity: these variables are defined only in the scope of the virtual host file or it is global?

  • 1

    @Wallacemaxters unfortunately gets in the global scope. Updated answer.

2

You can use the Define directive

See document on https://httpd.apache.org/docs/2.4/mod/core.html#define

Define pasta /var/www/meusite/


<VirtualHost :80>

    ServerName meusite.local
    ServerAlias www.meusite.local


    DocumentRoot ${pasta}public

    ErrorLog ${pasta}__apache__.log

</VirtualHost>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.