Dabeliuteef Dabeliuteef - 3 months ago 14
Apache Configuration Question

Apache same domain multi virtual host

I'm currently trying to set my Apache configuration.
I have one IP (ex:

10.10.10.10
) and one domain (ex:
www.mydomain.com
).

This server is currently hosting 2 sites and each site has his own Apache Instance with his own conf and php.ini :

/appli/projects/project1/apache_2.4/conf/project1.conf
=> Site 1

/appli/projects/project2/apache_2.4/conf/project2.conf
=> Site 2

project1.conf is set like that (same with 443 for SSL) :

<VirtualHost 10.10.10.10:80>
DocumentRoot /appli/projects/project1/apache_2.4/htdocs/project1folder/
ServerName mydomain.com
</virtualHost>


project2.conf is set like that (same with 453 for SSL) :

<VirtualHost 10.10.10.10:90>
DocumentRoot /appli/projects/project2/apache_2.4/htdocs/project2folder/
ServerName mydomain.com
</virtualHost>


So when I am connecting to "mydomain.com" I am going to "Site 1" but I have to hit mydomain.com:453 for accessing to "Site 2" which is not really good...

I would like to have something like that :

mydomain.com/site1
=> "Site 1" (or
site1.mydomain.com
)

mydomain.com/site2
=> "Site 2" (or
site2.mydomain.com
)

And I would like to keep the
mydomain.com
hitting the "Site 1".

Unfortunatly, because each site has his own instance of Apache, I have no idea how to set them.

Last but not the Least I would like to know if it's possible to share PHP (Apache)
SESSION
accross the multi Apache Instance or if I have to use
COOKIES
accross the domain to share informations between sites.

I don't know if it's really clear, just ask if you need more informations.

Thanks you in advance.

Answer

My recommendation is this:

File structure:

htdocs/
    site1
        ...files...
    site2
        ...files...

Apache vhost:

<VirtualHost  10.10.10.10:80>
    DocumentRoot /appli/projects/project1/apache_2.4/htdocs/
    ServerName  example.com
</virtualHost>

...and then do the same thing for the SSL-vhost.

Now you can use http://example.com/site1 and http://example.com/site2 and the same for https.

If you start using different ports (anything other than the defaults 80 and 443, you need to explicitly write the port names to be able to access the sites, or set up a reverse proxy (adds extra complexity with no real gain).

You can even define specific rules for site1 and site2 in your vhost.

Config per site:

<VirtualHost  10.10.10.10:80>
    DocumentRoot /appli/projects/project1/apache_2.4/htdocs/
    ServerName  example.com

    <Directory /appli/projects/project1/apache_2.4/htdocs/site1/>
        # Some specific vhost config...
    </Directory>

    <Directory /appli/projects/project1/apache_2.4/htdocs/site2/>
        # Some specific vhost config...
    </Directory>

</virtualHost>

If you want different sub domains like: site1.example.com and site2.example.com, then just do different vhosts that point to the folders directly, still using port 80 for http and port 443 for https.

Just remember that you then need to have a multi-domain-certificate or a wildcard certificate for https (since they are two different hostnames). Or you could install Let's Encrypt.