Thursday, April 23

Custom Virtual Host in Apache2 in Ubuntu

 
cd ~
mkdir public_html
 
Now for each domain create a folder with following template
 
mkdir -p public_html/domain1.com/{public,private,log,cgi-bin,backup}


 
NameVirtualHost *:80
Listen 80
 
Make sure this setting is set either in ports.conf or apache2.conf or httpd.conf or in sites-available/default
 
Now create the vhost file for domain1.com in /etc/apache2/sites-available/domain1.com

with the following settings

<VirtualHost *:80>
  
  ServerAdmin webmaster@domain1.com
  ServerName  domain1.com
  ServerAlias www.domain1.com


  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html
  DocumentRoot /home/username/public_html/domain1.com/public


  # Custom log file locations
  LogLevel warn
  ErrorLog  /home/username/public_html/domain1.com/log/error.log
  CustomLog /home/username/public_html/domain1.com/log/access.log combined

</VirtualHost>
 
 
Now to enable the site in sites-enabled
sudo a2ensite domain1.com
 
and then restart the apache2 server
sudo service apache2 restart
 
Modify the host file '/etc/hosts' 
 
127.0.0.1    localhost 
127.0.0.1    domain1.com
 
 
 Now set the owner permissions

~/public_html/domain1.com$ sudo chown -R username:www-data cgi-bin log public
~/public_html/domain1.com$ sudo find ./public -type d -exec chmod 775 {} \;
~/public_html/domain1.com$ sudo find ./public -type f -exec chmod 774 {} \;

likewise for log and cgi-bin
 
 

References

1. Apache Virtual Hosts
2. Permissions for /var/www/html 
3. Chmod only to folders and its subfolders