Install Apache with Virtual Host Concept on Ubuntu VPS

Last time, we have talked about securing our Ubuntu VPS and create different users for different websites. Now, we are going to see, how to actually setup those websites with Apache and Virtual Host Concept. This tutorial deals with the following concepts from the series:

  1. Installing Apache2 on your Ubuntu VPS.
  2. Using Apache Virtual Host Concept to host different sites from the same VPS.
  3. Enabling/Disabling Apache Modules.

At the end of this you should be able to:

  • Update/Upgrade your software on your Ubuntu VPS.
  • Install Apache2 and any other software from terminal.
  • Understand and use Apache2 as Virtual Host to have more than one website on your VPS.

#1: Upgrading your VPS first:

It is recommended to upgrade your VPS before installing any of the software. To do this simply run the two commands:

sudo apt-get update
sudo apt-get upgrade

And your box should be running everything latest.

#2: Install Apache2:

Simply run as root or use sudo to

apt-get install apache2

This will install and start apache server. What it means is, you can now simply navigate to the server address and it should show some message to say that apache is installed and working correctly.

At this point, all your web pages are hosted under /var/www. We will be changing them shortly.

#3: Virtual Host:

#3.1: Concept first:

Virtual Host is a way of telling apache to host different websites at different places of your server. For simplicity, let us assume:

  • Our VPS has got only IP address.
  • Now we have two domains (mainsite.com and mywordpress.com), each pointing to the same IP Address.
  • We would like apache to host different sets of web pages for different domain.
  • We will also put phpmyadmin.mainsite.com to another place.

The rest of the tutorial will follow this example, but if you want to give a thorough read, then I recommend reading this tutorial at thegeekstuff.

#3.2: Setup your Virtual Hosts:

First copy your existing sites to a new file:

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/mysites

Now edit the file

sudo nano /etc/apache2/sites-available/mysites

And put your configuration. For this tutorial, we are going to create three virtual hosts:

  1. mainsite.com, which will be hosted at /home/localhost/public_html
  2. mywordpress.com which will be hosted at /home/wordpress/public/html
  3. phpmyadmin.mainsite.com, which will be hosted at /home/phpmyadmin/public_html
#Mainsite.com
<VirtualHost *:80>
	ServerAdmin webmaster@mainsite.com
	ServerName mainsite.com
	ServerAlias www.mainsite.com

	DocumentRoot /home/localhost/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/localhost/public_html>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /home/localhost/logs/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /home/localhost/logs/access.log combined
</VirtualHost>

#PHPMyAdmin
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName phpmyadmin.mainsite.com
	ServerAlias www.phpmyadmin.mainsite.com

	DocumentRoot /home/phpmyadmin/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/phpmyadmin/public_html>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /home/phpmyadmin/logs/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /home/phpmyadmin/logs/access.log combined
</VirtualHost>

#MyWordPress.com
<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName mywordpress.com
	ServerAlias www.mywordpress.com

	DocumentRoot /home/wordpress/public_html
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/wordpress/public_html>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog /home/wordpress/logs/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /home/wordpress/logs/access.log combined
</VirtualHost>

Now you are getting the idea why we created all those directories at the first place. (Check part #1 of this series).

Now, we will need to enabled the site using some commands:

sudo a2dissite default

The above will disable the default site configuration.

sudo a2ensite mysites

This will enable the one we created.

Now restart apache server

sudo apache2 restart

And you should be able to access the sites.

For further testing, create a file named index.html inside the public_html directory of all the sites and see if you are able to open them through the browser.

#4: Enabling/Disabling Apache Modules:

Apache comes with many modules, say for example, rewrite, which we will need for proper functionality of our web server. These commands are handy to set/unset/view all apache modules:

#4.1: View all available modules:

ls /etc/apache2/mods-available

#4.2: Enable a module:

sudo a2enmod rewrite

Replace rewrite with the mod name.

#4.3: Disable a module:

sudo a2dismod mod_name

In practice, we will need to restart the apache2 server in order to reflect the changes.

Well that’s it. At this point, you are now probably familiar with all the apache thing you’ll need to do to keep your server running.

Next on this series, we are going to install PHP and suPHP with all modules we will need.