The following small tutorial guides you through installing apache 2.2 and virtual hosting on ubuntu (8.04 and 9.04)
To install apache 2.2, go to terminal and execute the following command
sudo apt-get install apache2.2-common
Also install an apache MPM package.
sudo apt-get install apache2-mpm-itk
Now you can start and stop apace2.2 by running the following commands in terminal respectively.
sudo /etc/init.d/apache2 start
sudo /etc/init.d/apache2 stop
Go to firefox and load
http://localhost/
If you see the page saying “It works!”, you have successfully setup apache 2.2 on your machine.
Now let’s do virtual hosting.
Open terminal and execute
cd /etc/apache2
ls
You can see the files and folders under apache2.
apache2.conf conf.d envvars httpd.conf mods-available mods-enabled ports.conf sites-available sites-enabled
The apache2.conf file is the main apache configuration files. Now it’s not advisable to edit to conf file directly. So here’s how we do virtual hosting.
sudo vim sites-available mylocalhost
And paste the following in the file and save it.
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
ServerAdmin webmaster@mylocalhost
ServerName mylocalhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Also edit the hosts file in your system.
sudo vim /etc/hosts
Add the following line in the file below the line 127.0.0.1 localhost and save it.
127.0.0.1 mylocalhost
Now we’ve to enable the site (mylocalhost). To do so, execute
sudo a2ensite mylocalhost
Now run
/etc/init.d/apache2 reload
Start apache using the command
sudo /etc/init.d/apache2 start
or restart apache if it’s already running using the command
sudo /etc/init.d/apache2 restart
If you don’t see any error messages, tht’s it! We’re done.
Go to browser and check the url
http://mylocalhost/
You shld see “It works!” page.
By adding virtualhosts file with different document roots and servernames, you can run many apps. In case if you want to run an app on any port other than 80, you shld add the following line first.
Listen <port_no.>
Because, by default apache listens only to port 80.
Similarly you can install modules in modules-available and enable them using
a2enmod <module>
Tht’s it from me.
Happy virtual hosting.