- Enable Apache mod_rewrite:
sudo a2enmod rewrite - Set up some site folders in the Document root, for Ubuntu it is located at /var/www:
Create directory: mysite.com
mkdir /var/www/mysite.comNOTE: Create an index.html file in each of these folders just for testing purposes.
If we open http://localhost/mysite.com from browser, we should be able to see the index.html pages.
Our goal here is to be able to load sites by just typing mysite.com in browser and for this we need to set up virtual hosts. - Add your sites to the hosts file:
In Ubuntu open your hosts file.
It should be located in the /etc folder.
To make changes to this file you will need the proper admin permissions,
so you may need to open it as root with the editor of your choice
or open from command line.
sudo vi /etc/hostsYou will have at least one line in your hosts file, maybe more.
127.0.0.1 localhost
127.0.1.1 chipmunk-thinkpadT500
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allroutersHere our interest is on first line because it points local web server (localhost) to local IP address.
We need to replicate that so that other sites do the same.
So the hosts file will now look like this:
127.0.0.1 localhost
127.0.1.1 chipmunk-thinkpadT500
127.0.0.1 mysite.com
127.0.0.1 mysite1.com
127.0.0.1 mysite2.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allroutersWhat this basically means is that when we type mysite.com in browser, it’s going to resolve back to local webserver. Same with mysite1.com etc.
You can do as many of these as you want for as many local sites as you’re developing locally. - Configure Apache:
We now need to make changes to our Apache configuration so that Apache knows what to do to our new sites.
If you navigate to /etc/apache2/sites-available you will see two files in there: default, and default-ssl.
We need to add files to correspond to our sites in this folder.
We create new files for editing, one for each site.
Remember you need to do this with the correct permissions so you can save the files.
From the terminal type: sudo vi /etc/apache2/sites-available/mysite.comIn this new file, type the following code.
DocumentRoot /var/www/mysite.com
ServerName www.mysite.com
ServerAlias mysite.com
Explanation:
We open the virtual host tag and tell Apache to listen to port 80.
The DocumentRoot directive tells Apache where our site files are stored.
ServerName is the server name of our site, the URL that we will use to navigate to the site.
ServerAlias is any other name or URL that we may use to access the site, the most common being excluding the WWW.
So basically we’re saying mysite.com is equivalent to www.mysite.com.
You can add other aliases as you see fit, but this one is the most common.
There are other directives that you can add to this file, such as error log information, admin email etc.
Go ahead and create one of these files for each site. - The next step is to enable these sites.
From terminal run: sudo a2ensite mylocalsite1.com
You should get a message that the site is being enabled, and a prompt to restart Apache.
If you have more than one site to enable, you can go ahead and enable them all before reloading Apache.
Once you’re all done, you can then restart apache to allow the configuration changes to take.
sudo /etc/init.d/apache2 reloadOnce Apache successfully restarts, our site is successfully set up.
Now we are able to open the site from browser by just typing mylsite.com.
Notes:
If you navigate to /etc/apache2/sites-enabled, you will see a symlink to each of your sites (in terminal they just look like files.
But if you browse to the folder using your GUI file manager you will notice that they are links).
They are placed there by the a2ensite command we execute in the last step.
If you need to remove any sites, the cleanest way in my opinion is to reverse through the steps.
- First disable them using the a2dissite command:
sudo a2dissite mysite.com - Then delete the mysite.com file from /etc/apache2/sites-available folder.
- Then remove the reference to mysite.com from /etc/hosts
- Then finally restart the apache server