Wednesday, December 26, 2012

Setting up a Virtual Host in Linux (Ubuntu)

There are times when you use a framework such as Symfony or Laravel, there are configurations that needs to be done. One of that configuration is pointing your path to web folder (Symfony) or public folder (Laravel) when you are preparing a hostname for your site. The advantage of this is protecting your files what can be seen publicly and what's not. So, today, I'll be setting up virtual host in Linux Ubuntu.

  1. First of all, Go to /etc/apache2/apache2.conf. Open the file and uncomment the Include /etc/apache2/sites-enabled as shown below:
    # Include the virtual host configurations:
    Include /etc/apache2/sites-enabled/

  3. Restart apache using the command:
    apache restart

  5. Create your config file with the filename of your choice. For this example, we will call it filename-site.conf. This is my sample config file.
         DocumentRoot "/home/public_html/my_app/web"
         DirectoryIndex index.php
              Options FollowSymLinks
              AllowOverride All
              Options Indexes FollowSymLinks MultiViews
              AllowOverride All
              allow from all
         ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
              AllowOverride None
              Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
              Order allow,deny
              Allow from all
         ErrorLog "/var/log/apache2/error.log"
         LogLevel warn
         CustomLog "/var/log/apache2/access.log" combined
  7. We will now create a copy of filename-site.conf by using the command: a2ensite <filename-site.conf>. This command is a script that enables the specified site within the apache2 configuration. It creates a symlink to /etc/apaches2/sites-enabled. So if you want to disable the site, just type the command a2dissite which in effect will remove the symlink.

  8. Now, your site should be accessible using your hostname. If you encounter some problems, try setting your browser settings to no proxy. It happens that in our office, we use proxy so my browser is confused. Also, you may trying adding a host to /etc/hosts. Try adding a line there:

No comments:

Post a Comment