How To Configure Apache2 on Linux and Host Your Own Website

Intro

Virtual Hosting allows Apache to host more than one website on a single server. This is how sharing hosting works. It’s an ideal way to develop multiple web projects on the same machine.

This exercise assumes you have Apache2 running on Linux. I’m currently running Apache/2.2.17 on Ubuntu 11.04. To see what version of Apache you have installed, run apache2 -v from your terminal.

Configure Apache2 virtual hosting

Your web root folder is /var/www/, which is protected, so for development purposes, let’s move it to where you won’t need root privileges to access it, e.g., /home/matt/www/ (In this example, I’m using matt—replace this with your user directory)

In order to tell apache where we want /www/, we’ll need to edit the read-only file default in /etc/apache2/sites-available/:

  1. Open your terminal and run gksudo
  2. In the ‘Run program’ dialog box that opens, enter nautilus
  3. In the window that opens, navigate to /etc/apache2/sites-available in the file system, and edit the default file with a text editor like Gedit.
  4. Replace the 2 instances of /var/www/ with /home/matt/www/ and save.
  5. Reload the Apache configurations from your terminal by running sudo service apache2 reload
  6. http://localhost/ should now point to your new /www/ directory. Test this with a simple index.html or .php file.

Host a new site in Apache2

  1. Open your terminal and run cd /etc/apache2/.
  2. Then run cd sites-available.
  3. Create a new file called ‘mynewsite’: run sudo cp default mynewsite.
  4. Edit mynewsite as root and run: sudo nano mynewsite. If you prefer another text editor such as Gedit, you can run  sudo gedit mynewsite.
  5. When mynewsite opens in your text editor, replace what’s there with:<VirtualHost *:80>
    ServerAdmin your@email.com
    ServerName greatsite.com
    Options -MultiViews
    DocumentRoot /home/matt/www/projects/mynewsite/public_html
    <Directory /home/matt/www/projects/mynewsite/public_html/ >
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    allow from all
    </Directory>
    </VirtualHost>

    Be sure to edit the path /home/matt/www/projects/mynewsite/public_html/ both for DocumentRoot and for Directory to point to where you’d like to keep your projects.

  6. Save and close mynewsite – your new website configuration is ready.
  7. To set up the directories and subdirectories for your projects all at once, return to your home directory by running cd . Then run mkdir -p www/projects/mynewsite/public_html .
  8. Now, enable the configuration we created earlier by running: sudo a2ensite mynewsite . (a2 = Apache2 and en = enable).
  9. Tell your machine to listen for the address we chose for your site by running: sudo gedit /etc/hosts . When your text editor opens the file, add the name of the address next to localhost on the same line, like so:127.0.0.1    localhost greatsite.com 
  10. Save the file and exit your text editor.
  11. Reload the Apache configuration: sudo service apache2 reload .
  12. Load greatsite.com in your browser.

You now run Apache shared hosting!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s