Search code examples
apacheftpsystem-administrationubuntu-server

Set different data/folders for different domains on apache


So I have Ubuntu 16 installed with LAMP and couple of other things on it (like FTP server...).

IP of my VPS is 1.2.3.4.

I have 2 domains, example.com and mydomain.com - both of these domains have their A record pointed at 1.2.3.4

If I access 1.2.3.4 and example.com and mydomain.com in my browser, all those three are showing the SAME data, specifically the contents of /var/www/html - if I change content of this directory it affects example.com and mydomain.com and the 1.2.3.4.

Now how can I set additional folders for domains to read from? I want different data on example.com and on mydomain.com - I want to make them read from different folder on server. How can i setup this please?


Solution

  • What you want to achieve is technically defined as setting virtual hosts which could be set using the following steps:

    Under your Apache configurations directory, usually at /etc/apache2/, you will find a directory named sites-enabled.


    For each of your domains, you will need to configure a special configuration file in order to point them to the right direction. The name of the file is typically your-domain.conf.

    Here's an example of the file:

    <VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port
        # that the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        ServerName your_domain
        ServerAlias www.your_domain
        ServerAdmin webmaster@your_domain
        DocumentRoot website_directory
    
        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn
    
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    
        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
        <Directory website_directory>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
    </VirtualHost>
    

    After saving the file you will need to restart the Apache server
    /etc/init.d/apache2 restart

    Repeat the above steps for each of your domains.
    If everything goes right, your site will show when you access your domain.