Install Apache Server on Ubuntu Linux

In this tutorial we will install the Apache web server on Ubuntu Linux machine. If you are looking for a manual that explains how you can install a web server with MySQL and PHP on Linux machine, you must visit our LAMP guide in the previous section.

Download and Install Apache

Open your Terminal (Applications > Accessories > Terminal)  and run the following lines:

  1. sudo apt-get install apache2
    [sudo] password for administrator: YOUR USER PASSWORD
    After this operation, 10.3 MB of additional disk space will be used.
    Do you want to continue [Y/n]? Y
  2. Open your web browser and then enter the following into the web address: http://localhost
    You will see a message saying "It works!".

Apache Configuration

By default, the apache DocumentRoot is “/var/www” directory. The files such as PHP or HTML will be placed in this directory and will be visible from the URL http://localhost. We want the “localhost” to point to another folder instead, /home/user/Public.

Apache2 has the concept of sites, which are separate configuration files that Apache2 will read. These are available in the "/etc/apache2/sites-available" file. You can create many different site configurations available, and activate only those that you need.

We want the default site to be the "/home/user/Public/" directory (the "user directory" stands for your profile or your user name on linux).

  1. Create a new site, for example websol:
    sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/websol
  2. Edit the new configuration file in a text editor:
    gksudo gedit /etc/apache2/sites-available/websol
    Find the following lines:
    DocumentRoot /var/www change to DocumentRoot /home/user/Public
    <Directory /var/www/> change to <Directory /home/user/Public/>
  3. Change all occurrence of "AllowOverRide None" to "AllowOverRide All" (to allow .htaccess file)
  4. Save the file
  5. Deactivate the old site
    sudo a2dissite default
  6. Activate the new site
    sudo a2ensite websol
  7. Enable the mod_rewrite module:
    sudo a2enmod rewrite
  8. Restart your apache
    sudo /etc/init.d/apache2 restart
  9. Test the new site, type console:
    echo '<b>Hello! It is working!</b>' > /home/user/Public/index.html
  10. Open your web browser and then enter the following into the web address: http://localhost and you will see a message saying "Hello! It is working!"

The following commands allow you to start, restart, stop Apache.

  1. sudo /etc/init.d/apache2 start
  2. sudo /etc/init.d/apache2 stop
  3. sudo /etc/init.d/apache2 restart