Logo
My Journal
Blog

Timeline

Blog

Install Apache and Enable mod_rewrite Ubuntu Linux

The Apache HTTP Server, commonly referred to simply as Apache IPA: /əˈpætʃi/, is a web server notable for playing a key role in the initial growth of the World Wide Web and in 2009 became the first web server to surpass the 100 million web site milestone. Apache was the first viable alternative to the Netscape Communications Corporation web server (currently known as Sun Java System Web Server), and has since evolved to rival other Unix-based web servers in terms of functionality and performance. The majority of all web servers using Apache are Linux web servers.

First install the apache2.2 php5 and mysql

sudo apt-get install apache2 php5 php5-gd mysql-server-5.0 phpmyadmin

This will install all the necessary applications. You may be prompted to decide on a MySQL root password (set one if you want), and to configure PHPMyAdmin (select apache2 as the server type). After this is complete, we need to perform some minor configuration to get PHPMyAdmin working. Type:

sudo gedit /etc/apache2/apache2.conf

Add this line at the bottom

# Enable PHPMyAdmin
Include /etc/phpmyadmin/apache.conf

Now Save and Exit then restart Apache

sudo /etc/init.d/apache2 restart

Everything should now be configured – go to http://127.0.0.1 for webpages, and http://127.0.0.1/phpmyadmin for database administration. The webroot is located in /var/www/ by default.

since you dont have access to folder /var/www as normal user you will have to fix it using this step:

Adding a virtual host to your LAMP server

This allows us to access a different folder as our webroot from a different address in our browser – face it, ‘localhost’ or 127.0.0.1

Start by adding a new host. This is easy to do via System –> Administration –> Network; click on the Hosts tab, select the localhost item from the list and click properties. You may need to click the ‘Unlock’ button and enter your password first. Underneath the entry ‘localhost’, on a new line, add your entry of choice; for example, we’ll use ‘modastig’. Add this, and close all the dialogs.

Now we need to edit a configuration file, so type into the console:

sudo gedit /etc/apache2/httpd.conf

In the resulting file (which may well be empty), add the definition for your host! My username is ‘mod’, and I want my webpages to be served from a directory named ‘www’ in my home, so I would add:

<VirtualHost *>
ServerName modastig
DocumentRoot /home/mod/www
</VirtualHost>

I can now add files to /home/mod/www, and access them by visiting http://modastig

Now enable mod_rewrite

cd /etc/apache2/mods-enabled
sudo touch rewrite.load
sudo gedit rewrite.load (you may use any editor to edit this file)

now paste this following line

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

Then edit

sudo gedit /etc/apache2/sites-available/default

Find the following

Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

and change it to

Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all

and finally restart Apache

sudo /etc/init.d/apache2 restart

17 Comments
    • dota
      Jun 17, 2008 at 6:01 AM / Reply

      dota…

      Dota is a really great game! Thanks for this post mate!…

    • binary
      Jul 3, 2008 at 7:33 AM / Reply

      man a2enmod

      a2enmod rewrite
      /etc/init.d/apache2 restart

    • thewebpromoter
      Jul 23, 2008 at 9:48 AM / Reply

      Hey buddy, great guides here. I have enabled my phpMyAdmin, Apache and MySQL in my local Server.

      Thanks a lot. What do you want in exchange? Scripts? You have a lot of them. Domain? Ah, you already have it. Thanks for everything bro…

    • nadya
      Aug 14, 2008 at 3:35 AM / Reply

      thank you very much. i read a lot of sites and still had problem with this rewrite stuff. What helped me was the part

      ServerName modastig
      DocumentRoot /home/mod/www

    • LysDiod
      Sep 24, 2008 at 11:16 PM / Reply

      Thx alot, it worked for me 🙂

    • r2d2
      Oct 26, 2008 at 8:08 AM / Reply

      It worked perfectly… thanks a lot. This linux stuff is complicated but i’ll keep trying. I hope some day i understand it better….

    • CraZy675
      Nov 19, 2008 at 7:41 AM / Reply

      To quote binary (adding sudos):

      man a2enmod

      sudo a2enmod rewrite
      sudo /etc/init.d/apache2 restart

      If you cd to mods-enabled and do an ls -l you will notice that these are all just symbolic links (same with sites-enabled). Binary wanted you to read the man page of a2enmod so you would understand what your doing. This post seems to just be an unnecessary hack. You don’t need to touch or edit any file they are already there for you just not enabled.

    • Juan González
      Jan 21, 2009 at 12:00 AM / Reply

      Dude, as CraZy675 says, you just have go to the /etc/apache2/mods-enabled and do this:

      “ln -s ../mods-available/rewrite.load rewrite.load”

      That’s all.

    • thewebpromoter
      May 14, 2009 at 3:40 PM / Reply

      Hey bro, what I want it to serve my scripts at MS formatted partition named /media/New Volume? what should I do with the “sudo gedit /etc/apache2/sites-available/default” ?

      Thanks in advance

    • xMoDx
      May 15, 2009 at 12:58 AM / Reply


      ServerName domain2
      DocumentRoot /media/New_Volume

      but im not so sure if this will work if it is MS formated “NTFS”, test it and show me the resutlts,

    • Guido
      May 31, 2009 at 12:05 PM / Reply

      Thank you very much!
      Good tutorial and it really worked 😉
      Guido

    • dwalls
      Sep 14, 2009 at 9:30 AM / Reply

      Thanks the modrewrite works good.

    • sergio
      Jul 13, 2010 at 2:03 PM / Reply

      great post . thanks

    • _Vince
      Mar 11, 2011 at 3:01 AM / Reply

      Ah, great. Finally a step by step guide, been trawling through dozens of pages, finally one that I can understand and works! 😀

    • Splinky
      Jan 22, 2012 at 10:13 PM / Reply

      Hey,
      thx bro for this article ,it fix my prob with mod_rewrite in drupal

Leave A Comment