Apache Web Server in Ubuntu


In the last post we saw how to install Apache web server in Red-Hat distributions. In this post, we’ll see how to install and configure Apache web server in Ubuntu or any Debian distribution.

Apache in Debian is way different from Apache in Red-Hat systems. In any Red-Hat distribution, to configure a web server, all you need to edit is httpd file, and the hosts file so browser can get the webpage locally. But in Debian distributions, you’ll be given a default settings, we need to make a copy of that file and configure the copy, not the original file. Then enable the site and edit the hosts file.

Lets see how to configure Apache web server in Debian distributions.

  1. sudo apt-get install apache2 -y
    Install Apache2 web server. Default location of Apache is ‘/etc/apache2

    Install Apache

    Install Apache

     

  2. sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/yoda
    Copy the default file provided by Ubuntu, in thesamelocation. Save it with a name that you want to give to you website. We’lluseyoda in this case.

    Copy default file

    Copy default file

     

  3. sudo vim /etc/apache2/sites-available/yoda
    Open the configuration file with your favourite editor.

    Edit configuration file

    Edit configuration file

     

  4. This is the default file, without any changes.

    Default File

    Default File

     

  5. Add the following lines:
    ServerAdmin [email address of the admin]
    ServerName www.[your-website-name].com
    ServerAlias *.[your-website-name].com
    DocumentRoot [path of the directory containing all the files]
    Save and Quit.
    ServerAlias is necessary because it will return the webpage even when user doesn’t put www. For eg. the server will respond to ‘http://www.yoda.com‘ or ‘http://yoda.com

    Make these changes

    Make these changes

     

  6. mkdir -p /var/www/html/[your-website-name]
    If the DocumentRoot doesn’t exist, create it. We created ‘html‘ folder in ‘www‘ and then ‘yoda‘ (name of the website) folder in ‘html‘ using one command. If you alrready have your document root, skip this step.

    Create document root

    Create document root

     

  7. sudo vim /var/www/html/yoda/index.html
    Create a home page for your website. Remember, if you want to put anything on the website, it should be present in ‘/var/www/html/yoda‘ because it is the document root. Apache will look only in this folders for all html, php image, css and other files.

    create home page

    create home page

     

  8. Create a simple home page.

    Edit the home page

    Edit the home page

     

  9. ifconfig
    Run ‘ifconfig‘ to check your machine’s IP address. Note it down somewhere, because we will need it in the next step.

    Check your IP

    Check your IP

     

  10. sudo vim /etc/hosts
    Open the hosts file. When you enter any web address in browser, it’ll first check the browser’s cache. If it doesn’t find the IP address of website, it goes to the hosts page. We need to add ourwebsite’surl here along with our IP address, to tell the browser the website resides in our machine.

    Edit hosts file

    Edit hosts file

     

  11. www.[your-website-name].com       [Your IP-address]
    Edit the hosts file, enter the name of your website and your IP-address (Step 9).

    Edit hosts file

    Edit hosts file

     

  12. sudo a2ensite [your-website-name]
    Enable your website using ‘a2ensite‘ command. Even if you do all the steps and skip this simple command, apache won’t reply to your request.

    Enable your website

    Enable your website

     

  13. sudo service apache2 restart
    Restart apache 2 server.

    Restart the server

    Restart the server

     

  14. Enter the website’s address in the URL.

    Check Website.

    Check Website.


Leave a Reply