1

I have recently shifted to Ubuntu 16.04 from windows and I am trying to setup web development environment. I successfully installed apache, mysql and php and both html and php (created a test script through sudo nano) is rendered perfectly from /var/www.

Then I tried to create a new file through sublime text in the directory /var/www just to realize that saving file gave an access denied error. So this led me to a new article on permissions etc and I realized that its probably not a good idea to make amend the permissions on that directory.

I looked up articles on how to move apache document root to somewhere else. Then I created a new projects/web directory under /home/user and changed the DocumentRoot in /etc/apache2/sites-available/000-default.conf (I am using apache 2.4.18). I made the relevant changes in the /etc/apache2/apache2.conf to change the <Directory> path too. Restarted apache and the DocuemntRoot was changed but it is not rendering any php. Even phpmyadmin which was earlier working through url http://localhost/phpmyadmin is not working any more.

  • changed in /etc/apache2/sites-available/000-default.conf:
    DocumentRoot /var/www/html
    to
    DocumentRoot /home/{user}/projects/web

  • Changed in /etc/apache2/apache2.conf:
    <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
    to
    <Directory /home/{user}/projects/web/> Options Indexes FollowSymLinks AllowOverride None Require all granted</Directory>

Can someone help me how can I change DocumentRoot and still have php and phpmyadmin working?

  • - changed in /etc/apache2/sites-available/000-default.conf: DocumentRoot /var/www/html to DocumentRoot /home/{user}/projects/web - Changed in /etc/apache2/apache2.conf: <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> to <Directory /home/{user}/projects/web/> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> – A. Munir Feb 18 '17 at 9:23
  • changed above. Of course after all changes i have restarted apache several times through "sudo systemctl restart apache2" but no avail – A. Munir Feb 18 '17 at 9:28
  • You mistake is to change the default settings. Restore the files into their original content and the follow the procedure mentioned here in order to introduce a new site to your apache. – Mostafa Ahangarha Feb 18 '17 at 9:38
  • The link presents the solution in context of hosted websites which is difficult for me to grasp at the moment. I am just doing some development on my local machine. Again the issue is that article is suggesting to setup new sites in /var/www. Is this a good idea to setup websites in that folder and give users permissions to read and write files? I am also planning to start using git. I was thinking of having the main development work in /home/user/projects/web and from there I will later push it to /var/www and remote website too. What do you think about that? – A. Munir Feb 18 '17 at 9:47
  • You can introduce any folder for this purpose. That is the procedure to tell apache to look for what folder if some specific request comes. It is up to you to where to put the folder. But also there is no problem of giving access. Standard permission for directories is 755 and for files is 644. – Mostafa Ahangarha Feb 18 '17 at 9:51
2

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.

<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 mysite.com

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


        <Directory />
                Options +FollowSymLinks +ExecCGI +Includes
                Require all granted
        </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 /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
</VirtualHost>

# 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.