Issue changing Document Root in Ubuntu 16.04

Solution 1:

First fix the broken Apaches2

You have apparently broken Apache2. You should use the default host settings and create a virtualhost for the changes custom root directory you want. Remove or rename the configuration files you have edited and restore the original files.

Use the default 000-default.conf file as a template. Copy it to a name for your virtual host and make modifications to your custom file.

You can rename the files using these steps:

$ cd /etc/apache2/sites-available
$ sudo mv 000-default.conf mysite.conf
$ cd /etc/apache2
$ sudo mv apache2.conf apache2.conf.test

Now restore the default files with this commandline:

$ sudo apt -o Dpkg::Options::="--force-confmiss" install --reinstall apache2

Now create your virtual host, "mysite.com". You can replace the /home/web/mysite to any directory. In your case you can use /home/{user}/projects/web.

$ cd /etc/apache2/sites-available
$ sudo cp 000-default.conf mysite.conf
$ sudo mkdir -p /home/web/mysite/www
$ sudo mkdir -p /home/web/mysite/log

If the mysite.com host doesn't exist, create it locally with:

$ gksudo gedit /etc/hosts

Add it to the hosts file:

127.0.0.1  mysite.com

Now edit your mysite.conf file to point to your Document Root choice.

$ pksudo gedit /etc/apache2/sites-available/mysite.conf

Make the following changes to this file. This is the difference between the 000-default.conf file and your newly created mysite.conf file. The difference is highlighted in bold type.

&ltVirtualHost *:80&gt
        # 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 mysite.com

        ServerAdmin webmaster@localhost
        DocumentRoot /home/web/mysite/www


        &ltDirectory /&gt
                Options +FollowSymLinks +ExecCGI +Includes
                Require all granted
        &lt/Directory&gt


        # 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 /home/web/mysite/log/error.log
        CustomLog /home/web/mysite//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
&lt/VirtualHost&gt

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

Now enable the new site with:

$ sudo a2ensite mysite.conf

When making a change to the Apache configuration files restart the service with:

$ sudo systemctl restart apache2

Write Permissions to the directory and files of the VirtualHost:

You can use the commands chown and chgrp to change the owner of the files and folders of /home/web/mysite/www. If they are owned by your userID, you'll have read and write access to the files and folders.

You can also create a specific group and add users to the specific group so that user's of that group will have read/write access to the files and folders.

Using the latter consideration may be a better security alternative than giving other users group access to files and folders in your /home space.