Apache multiple DocumentRoot not working on Ubuntu

I have installed Apache version 2.4 on my cumpoter runnig Ubuntu 16.04. To add a new virtual host I took the following steps:

Fist, add a new entry to /etc/hosts: wordpress.dev

Then, create a new file /var/apache2/site-availables/wordpress.dev with the following content:

    ServerName wordpress.dev
    ServerAlias www.wordpress.dev
    DocumentRoot /var/www/html/wordpress/

    # 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}/wordpress_error.log
    CustomLog ${APACHE_LOG_DIR}/wordpress_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

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

And then enable the site:

# service a2ensite wordpress-dev.conf

Finally, retart Apache:

# service apache2 restart

But when I visit http://wordpress.dev Apache serves the content of index.php file located at /var/www/html not the file located at /var/www/html/wordpress.

Then I checked the file /var/apache2/site-availables/000-default.conf and noticed there was also another DocumentRoot directive in this file that points to /var/www/html/ directory. I hadn't made any changes to this file before. Then I tried to comment the DocumentRoot directory in this file and restarted Apache but the Wordpress site still didn't work. I dont' understand why this error happens, could you help me figure it out?

Asked 2 years ago ago

What you missed is adding the `` directive in your configuration. Here's the correct version:

    ServerName wordpress.dev
    ServerAlias www.wordpress.dev
    DocumentRoot /var/www/html/wordpress/

        AllowOverride All
        Options Indexes FollowSymLinks
        Require all granted


You could change the content in the `` to suit your need.

Answered 2 years ago ago

Write Answer: