How To Set Up Local Hosting Of A Website


It is often very helpful to test changes to a website before deploying to the production web server. If you have made changes to just one html file you can browse the file directly on your computer by referring to it as a file://path/filename.html. The disadvantage is that you may not be able to test all of the links to and from the file and you cannot test java servlets. A better way to test your changes is to host the entire Website, including any java servlets, on your local computer. Testing your Website on your local computer is normally called local hosting. This tutorial provides instructions for how to setup local hosting in linux or OSX. The basic steps required to setup local hosting are as follows:


  1. Install and setup Apache HTTP server (Or other web server) on your local computer
  2. Install and setup a servlet container such as Apache Tomcat on your local computer (Only needed if you are running servlets on your website)
  3. Setup environment variables and paths for Apache HTTP and Tomcat
  4. Create a new Virtual Host in Apache
  5. Enable vhosts in Apache
  6. Set File Permissions
  7. Restart Apache
  8. Start Tomcat if you have servlets
  9. Trick your computer into thinking that your url (yoururl.com) is localhost

For information on downloading and configuring Apache HTTP and Tomcat please refer to and follow the instructions on the Apache website. The following is detailed instructions for each of the major steps:


Creating a Virtual Host in Apache: To create a new Virtual Host in Apache you need to edit the <VirtualHost ...> section inside the /etc/apache2/extra/httpd-vhosts.conf as shown in the example below:


<VirtualHost *:80>

ServerAdmin username@mail.com
DocumentRoot "/Users/dparoulek/app/pnatts"
ServerName yoururl.com
ErrorLog "/private/var/log/apache2/yoururl.com-error_log"
CustomLog "/private/var/log/apache2/yoururl.com-access_log" common

<Location />
Order allow,deny
Allow from all
</Location>

<Directory /Users/yourusername/app/urldirectory>
Require all granted
</Directory>

</VirtualHost>


Enable Virtual Hosts in Apache: Enable vhosts by uncommenting the following line in /etc/apache2/httpd.conf:


# Virtual hosts
Include /private/etc/apache2/extra/httpd-vhosts.conf


Set File Permissions: To set file permissions run the following:


chmod -R 777 /Users/yourusername/app/urldirectory

Restart Apache: Use the following command to restart apache:


sudo apachectl restart

Start Tomcat: Use the following command to start tomcat:


sudo /$CATALINA_HOME/bin/startup.sh

Mapping your url to local host: To trick your computer into thinking that your url (yoururl.com) is localhost you need to edit /etc/hosts and add a line like the following:

127.0.0.1 yoururl.com

Now when you type yoururl.com into your browser you should see your website that is running on your local computer. If you have problems you can check the connections using the curl command:


curl localhost:8080