Skip to content

Instantly share code, notes, and snippets.

@gobinathm
Last active August 29, 2015 14:01
Show Gist options
  • Save gobinathm/1515fc2352494e15dc44 to your computer and use it in GitHub Desktop.
Save gobinathm/1515fc2352494e15dc44 to your computer and use it in GitHub Desktop.
Step By Step Guide to Configure Virtual Host on an Apache in a Ubuntu Machine
#Initial Step is to install / Step Apache
sudo apt-get install apache2
#below steps are done assuming that your website will be hosted under /var/www
#Create a directory that will act as a document root for your site
sudo mkdir -p /var/www/example.dev/public_html
#Create a directory that will used for Log Storage
sudo mkdir -p /var/www/example.dev/log
# Document Root & LOG directory should be writable to USERS, not Just for ROOT
sudo chown -R $USER:$USER /var/www/example.dev/public_html
sudo chown -R $USER:$USER /var/www/example.dev/log
# Ensure all user have read access to WWW directory
sudo chmod -R 755 /var/www
# Create Virtual Host by copying an existing file [Note: Some version of Ubuntu might not have .conf extension.
sudo cp /etc/apache2/sites-available/default-000.conf /etc/apache2/sites-available/example.dev.conf
# Before enabling newly created virtual host. we need to make few config changes. Use your favorite Text editor, here i'm using vi
# 1. Document Root to be update to the one we created above
# 2. Change ServerName & ServerAlias to the One you have created above
# 3. Change the ServerAdmin Email ID
# 4. Change Log path to the one created above
sudo vi /etc/apache2/sites-available/example.dev.conf
# Final conf file for this example would look like the one in comments
# Time to enable new virtual host
sudo a2ensite example.dev
# As we are using a full qualified domain name "example.dev" its good to make appropriate entry in hosts file. Sample given comment
sudo vi /etc/hosts
# Reload / Restart your apache (I have given both option, its fine to use any one)
sudo service apache2 restart
sudo service apache2 reload
@gobinathm
Copy link
Author

example.dev.conf

<VirtualHost *:80>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.

        ServerName example.dev
        ServerAlias www.example.dev
        ServerAdmin serveradmin@gobinath.com
        DocumentRoot /var/www/example.dev/public_html

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog /var/www/example.dev/log/error.log
        CustomLog /var/www/example.dev/log/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

@gobinathm
Copy link
Author

hosts file sample

# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost

#Virtual Hosts - using local IP address assuming that there is no Public Static IP assigned to the server
127.0.0.1    example.dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment