Setup localhost site in apache?

Solution 1:

You can set up your vhosts in

/etc/apache2/sites-available

For every vhost you like to have make a conf file in this directory. In the vhost configuration you can set your document root. The name of the file must end with .conf

So for example: /etc/apache2/sites-available/projectname.conf:

<VirtualHost *:80>
  ServerName projectname.pl

  DocumentRoot "/home/Sites/projectname/public"

</VirtualHost>

Now enable your site with:

sudo a2ensite projectname

And restart apache:

sudo /etc/init.d/apache2 restart

Then edit your /etc/hosts file.

Just paste the line:

127.0.0.1 projectname.pl

on the end.

Now you can access your site with http://projectname.pl

Solution 2:

Just to add to the other answers here, the way I have configured such behaviour is with symlinks... Seems to work fine. It is very easy to do, and does not require any configuration files.

sudo ln -s ~/path/to/yoursite/ /var/www/html/yoursite

The last portion of the above may be different for you. The above is what I use in Linux Mint 17. The '/var/www/html/' directory is the default apache "site" folder.

So yeah, for anyone who is looking for a clean, easy method to map a site without actually placing their site's directory in the default apache folder, there ya go.

Note: I am not sure if the above is appropriate for production environments, but for local development, it seems to be okay. The reason I am unsure is because the symlink carries the permissions of the folder in the home directory, which is (most likely) "owned" by the user. If you need to, you can obviously change that to the more secure 'root' like the apache directory, but I leave mine alone as I am just using this in a development environment.