How to Install And Configure Apache In Ubuntu

If you intend to set up a web server (or streaming server) in your Ubuntu machine, apache is one important module that you must install. In this tutorial, we will show you how to install and configure apache for your Ubuntu.

Note: If you just want to have a quick setup of LAMP server, follow the guide here.

Getting apache onto your Ubuntu machine is easy. Using either the Synaptic Package Manager, Ubuntu Software Center, search and install the “apache2” module. Alternatively, you can open a terminal and type the following command:

Once the installation finished, open a browser and go to the URL “http://localhost“. If you see the word “It Works!“, then your installation of apache is successful.

apache-install-success

Start, Stop and Restart Apache

After you have installed Apache, it will be added to the init.d list and will auto start whenever you boot up your computer. The following commands allow you to start, restart, stop Apache.

To prevent Apache from autostart when booting up:

To restore Apache back to the autostart list:

Note: the above commands will work in debian-based distro (including Ubuntu) only.

Changing the default localhost folder

By default, apache will operate on the “/var/www” folder. This means that whatever files you place in this /var/www folder will be visible from the URL http://localhost. In some instances, you may want the “localhost” to point to another folder instead, say /home/user/public_html. Here is how you do it:

First, make sure the /home/damien/public_html folder exists. Create a simple html file, name it index.html and place it in the public_html folder.

Open a terminal and type:

Change DocumentRoot /var/www to DocumentRoot /home/user/public_html.

Change <Directory /var/www/> to <Directory /home/user/public_html/>.

apache-edit-virtual-host

Save and exit the file.

Restart the apache

Now, in your browser, reload the URL http://localhost. You should see the html file that you have placed in the public_html folder.

apache-test-success

Configuring different sites

The above trick allows you to change the default operating folder of apache, however, some of you might not want to override the default settings. An alternative is to create multiple sites and point apache to the active site.

Create a new settings file for your new site.

Next, edit this settings file.

Change DocumentRoot /var/www to DocumentRoot /home/user/public_html.

Change <Directory /var/www/> to <Directory /home/user/public_html/>.

Save and exit the file.

Disable the default setting and make active the site1 settings

Lastly, restart the apache.

With this trick, you can create multiple site configuration file, each pointing to a different folder. You can then easily switch between the sites with the a2dissite and a2ensite command

Enabling .htaccess file

.htaccess file is a powerful file that can be used to control and customize a site server behavior without editing the core Apache module. By default, the .htaccess functionality is turned off and all instances of .htaccess files are completely ignored. The server will not even attempt to read .htaccess files in the filesystem.

To enable .htaccess file, open up the settings file that you have created earlier:

Scroll down the file until you see the part “<Directory /home/user/public_html/>“. Underneath that line of code, change AllowOverride None to AllowOverride All.

apache-allowoverride

Save and exit the file.

Recommended read:
1. Install LAMP server in Ubuntu

2. How to install VLC-Shares in Ubuntu and stream audio/video to Android

15 comments

    • Yes, Xampp is great, but it is not meant for production use. You would still need to configure your Apache if you want to deploy your server for production use.

  1. Its working but getting the below error after restarting apache 

    * Restarting web server apache2                                                apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName
     … waiting .apache2: Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName

    Should I be worried ????

    • That is fine unless you want to host a website on your machine accessible by the outside world.

  2. after running am getting the whole scripts in the web page and nothing other than that. how to get an active web page?..

    •  If you follow the steps above, you can place your web page in the folder “public_html” and go to “http://localhost” in your browser. The web page should show up. To access the web page from outside (with a domain name and IP), you will need to install a DNS server and your domain name to point to your IP address.

  3. hey guys plz tel me how to configure so that i could change the whole  url name .for ex: if i type ‘my ip:// foldername1’   then the url shud change to ‘foldername2’

  4. should I add some ppa to my sources for security updates? Also should I be worried about getting hacked?

    • There are plenty of ways you can be hacked on a server. It could be a SQL injection, illegal ftp access, password crack etc, I think you should be more worried about getting hacked from the frontend rather than the backend (apache)

  5. I was struggling with my server for a week to make rewrite work!
    Thanks for your tip. you saved me!

Comments are closed.

Sponsored Stories