Setup apache2 vhosts

Setting up apache2 vhosts is a continuation from setting up a lamp stack on ubuntu 14.04. Assuming you have apache2 installed, setting up vhosts should be relatively straightforward.

 

By default, apache directory is set to /var/www/html in /etc/apache2/sites-available/000-default.conf

 

Copy the default file. We’ll be editing the copy of that file for this purpose:

sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.conf

 

Open up the file with vi or nano and edit the items as shown to suit your needs. Here is a sample configuration (without the comments) that should help get started. This sample is meant for a LAMP stack with htaccess and mod_rewrite enabled.

<VirtualHost *:80>
	ServerAdmin team@company.com
	DocumentRoot /var/www/dir
	ServerName 52.33.134.43
	ServerAlias www.company.com company.com
	<Directory /var/www/dir>
		Options FollowSymLinks
		AllowOverride All
		Order allow,deny
		Allow from all
		Require all granted
	</Directory>
	ErrorLog ${APACHE_LOG_DIR}/error.log
	CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

Disable the default site

sudo a2dissite 000-default.conf

 

Enable the new site

sudo a2ensite mysite.conf

 

Create your directory if it doesn’t exist

mkdir -p /var/www/dir

 

Restart apache to complete it all

sudo service apache2 restart

 

If apache doesn’t start, you can start troubleshooting by running:

apache2 -t

 

 

Visit your page to make sure it works. Issues? Comment below and we’ll update the post with possible solutions.

Leave a Reply

Your email address will not be published. Required fields are marked *