Set up multiple local hosts for testing
Testing websites on a live webserver just sucks. It can be good for some things, but not most things. This is why I, like many people, set up locally hosted web servers to develop and test sites (like this one) using Apache.
It's not too hard to set one up, but I've written this tutorial to help you all on your way.
Install Apache and friends
If you've already installed Apache, PHP and a MySQL server you can skip this step
If you haven't, run the following command and your server programs will be installed locally. I'll be using Ubuntu for this tutorial, but any other Debian derivative will work just as well.
sudo apt-get install apache2 php5 php5-mysql mysql-server
If you're installing MySQL, it will ask you to enter and then confirm a login password for the server. This can be anything you want, just as long as you can remember it for when you need to use MySQL.
Note: MySQL is entirely optional here, as is PHP, but are included in examples as they're very commonly used services.
When the install process has finished, go to http://localhost in your browser. If you haven't got any strange /etc/hosts entries and Apache is working
fine, you should see an It works! page.
Other tutorials will also ask to install PHPMyAdmin, but I don't like it; it's too heavy and needs a server (service) of it's own. I prefer Adminer, so I'll be using that for MySQL management. It's a single PHP file and very easy to use.
Note: this is a minimal installation; PHP and Apache have a bunch more stuff you can install. Take a look through Synaptic for examples.
Configure Apache
This is actually quite simple. First things first, we need a directory to keep all your websites in. I personally use ~/Projects/Web/[project name]/, but
it's up to you entirely. For simplicity's sake, we'll be sticking to my structure for now.
If you haven't already done so, make some folders for your projects in ~/Projects/. These folders will be the roots of each of your sites.
Apache has a very nice feature called virtual hosts. These are, as the name implies, not real machines but different document roots that share the same physical
server. We will now create a virtual host for each site you have. To do so, we need to edit /etc/apache2/apache2.conf as root. I prefer GEdit, so:
sudo gedit /etc/apache2/apache2.conf
This is the Apache config file. It's full of comments and other stuff, but ignore that and scroll to the bottom. Now, for each site, add the following to
the bottom of apache2.conf:
<VirtualHost *:80>
DocumentRoot /home/username/Projects/WebsiteName
ServerName website
</VirtualHost>
You will need to replace /home/username/Projects/WebsiteName with the path to your website directory. Leave the trailing / off the path; it confuses Apache.
The ServerName line will be what you type after http:// in the URL bar. In this case, the address would be http://website.
Once you've added all your sites, the bottom of your apache2.conf might look a bit like this:
<VirtualHost *:80>
DocumentRoot /home/james/Projects/JamWaffles
ServerName jamwaffles
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/james/Projects/PurpleMoose
ServerName purplemoose
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/james/Projects/HelloWorld
ServerName testing
</VirtualHost>
All that's left to do is restart Apache by typing:
sudo service apache2 restart
Read into the next section; we're not finished yet!
Configuring hosts
Now we have Apache serving files from the correct place, it's time to make the browser look in the right place. We do this by editing /etc/hosts and
adding a few bits and pieces of our own.
First open up /etc/hosts in your favourite editor (mine is GEdit)
sudo gedit /etc/hosts
You should see lines such as
127.0.0.1 localhost.localdomain localhost
::1 james-desktop localhost6.localdomain6 localhost6
At the top of the file. Underneath these entries, but above the comment line
# The following lines are desirable for IPv6 capable hosts
Add a line for each host in apache2.conf, formatted as 127.0.0.1 [server name]. Ensure that each server name in /etc/hosts relates to one in apache2.conf.
For our example above, hosts would look like this:
127.0.0.1 localhost.localdomain localhost
::1 james-desktop localhost6.localdomain6 localhost6
127.0.0.1 jamwaffles
127.0.0.1 purplemoose
127.0.0.1 testing
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
You can ignore everything else in this file, but take note of the 3rd, 4th and 5th lines; they're our hosts.
This editing of /etc/hosts allows our browser to point to the right server (localhost) for anything you've got Apache pushing out.
The end
Hopefully you've got a working setup, and are now able to develop sites quickly and easily on your local system. I haven't mentioned MySQL much in
this tutorial, but did mention Adminer. To use adminer, put the .php file somewhere on one of your hosts' file structures
and navigate to it. The server used for login is localhost irrespective of what the virtual host is called.