Apache 2 HTTP virtual host


Preparation

Initialize configuration

cd /etc/apache2/sites-available/


Create target directory

mkdir -p /var/www/myServer


Prepare the log files

mkdir -p /var/log/apache2/myServer
touch /var/log/apache2/myServer/access.log
touch /var/log/apache2/myServer/error.log
chmod -R 660 /var/log/apache2/myServer/*
chown -R www-data:www-data /var/log/apache2/myServer/*


Copy default index file

cp /var/www/html/index.html /var/www/myServer
chown -R www-data:www-data /var/log/apache2/myServer/*


Configuration

Init configuration

cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/myServer.conf


Edit configuration

vim /etc/apache2/sites-available/myServer


To begin the virtual host, write the following lines:

  • Adjust the settings to your own configuration
<VirtualHost 192.168.0.100:80>		  → Choose the best options for your needs
<VirtualHost *:80>

	#############################
        # Server main properties
	#############################

	ServerName		myServer
	ServerAlias		www.myServer *.myServer
	ServerAdmin		webmaster@domain
	
	# Logs settings
	LogLevel		Warn
	CustomLog		${APACHE_LOG_DIR}/myServer/access.log combined
	ErrorLog		${APACHE_LOG_DIR}/myServer/error.log


	#############################
        # Root folder properties
	#############################
	DocumentRoot	/var/www/myServer

        # SECURITY: forbid access to .htaccess so no outsider can ever change it
        <Files ~ "^\.ht">
                ## Old Apache2 (before 2.4) syntax
                Order allow,deny
                deny from all

                ## Apache 2.4 syntax
                Require all denied
        </Files>
        # Restrict access to server root
        <Directory />
                Options FollowSymLinks
                AllowOverride None
                Require all denied
        </Directory>


        # Virtual host root directory
	<Directory /var/www/myServer>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None

                ## Old Apache2 (before 2.4) syntax
		Order allow,deny
		allow from all
                
                ## Apache 2.4
                Require all granted  
	</Directory>


	#############################
        # Other configuration
        # Alias, proxy redirections, CGI scripts, Directory, etc.
	#############################



</VirtualHost>


Enable / disable virtual host(s)

Virtual Host desactivation

If you're listening on *:80 then you should probably disable the default virtual host before enabling yours!

a2dissite 000-default



Virtual Host activation

To activate a Virtual Host, just type

a2ensite  myServer

Then, restart your web server

/etc/init.d/apache2 restart


Check your server! You should see your "index.html" page.