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 thoughts on “Install Apache and Enable mod_rewrite Ubuntu Linux

  1. Pingback: dota

  2. thewebpromoter

    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…

    Reply
  3. nadya

    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

    Reply
  4. r2d2

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

    Reply
  5. CraZy675

    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.

    Reply
  6. Juan González

    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.

    Reply
  7. thewebpromoter

    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

    Reply
  8. xMoDx Post author


    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,

    Reply
  9. _Vince

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *