How to completely reset an Apache instance in Ubuntu

While experimenting with mod_python, I ended up completely hosing my local Apache install. I had the configuration files in a bad state, so I thought the easiest approach would be to drop all of /etc/apache2, remove the Apache packages then re-install them. But once that happened, /etc/apache2 wasn’t remade as I had hoped! When I tried to start or stop Apache, it spat back:

1
.: 44: Can't open /etc/apache2/envvars

Oh right, thanks… The problem, I believe, was that I hadn’t really removed all the right packages and hadn’t used “–purge”. So here’s what I did to erase Apache’s memory of my mistakes:

Find and drop all Apache packages

1
2
3
4
5
6
7
8
9
10
APACHE_PKGS=`sudo dpkg --get-selections | grep apache | cut -f 1`
# Make sure things are sane:
echo $APACHE_PKGS
# Example output: apache2 apache2-mpm-prefork apache2-utils apache2.2-common \
# libapache2-mod-php5 libapache2-mod-python libapache2-svn
# Likely if you have a Python application:
# libapache2-mod-python libapache2-mod-python-doc libapache2-mod-wsgi
# Or if you roll with the PHP:
# libapache2-mod-php5
sudo apt-get remove --purge $APACHE_PKGS

Re-install Apache

1
2
3
# You might want to consider not re-installing the whole list;
# instead dump it out, audit, and install what you need:
sudo apt-get install $APACHE_PKGS

After this, all the configuration files were back, and I had a default Apache install.

Note: this was performed on an Ubuntu 9.04 system running 2.6.24-23-xen kernel. YMMV.

Be Sociable, Share!

Post to Twitter Post to Delicious Post to Digg Post to Reddit

This entry was posted in CLI and tagged , , . Bookmark the permalink.

81 Responses to How to completely reset an Apache instance in Ubuntu

  1. Paint another laptop on the front of your fighter plane: you saved my bacon!

  2. prabhakar says:

    i have get this msg while running above commands “FusionForge Could Not Connect to Database: ”

    help pls … :(

  3. prabhakar says:

    My Ubuntu version is 10.04

  4. @prabhakar This sounds like more an issue to ask folks at FusionForge, I have not used that application. What command did you run just before the error was thrown?

  5. jori says:

    @prabhakar
    it worked for me under 11.04 (natty), so most prob it will under 10.04 as well. So thanx to Sammy for his post -> it helped.

    The only thing I wonder, is why the installation routine for apache2 doesn’t purge and reinstall the configuration files (I thought the purge was exactly meant for that). For me t h a t doesn’t make less sense . . . but that’ll be strange developers’ ideas that we users are not supposed to bring into question ;-) . As a ‘normal’ user I would expect to be the main package (being purged and installed completely) and then for pros additionally the possibility to manage single packs like separately and not the other way round . . .

  6. vasya says:

    your instruction and reboot helps, thx!

  7. che3ver says:

    Thank you! This worked great in 10.04.

  8. Kelly says:

    This worked great! Thanks

  9. Excellent Googled about for hours and hours till I found your site. says:

    I tried so much I didn’t know what to try next but with your help I sorted the problem in minutes thanks for all Sean

  10. Nikhil says:

    Lifesaver!
    Worked like a charm on 10.04. Seriously, your link needs to be on top for “deleted /etc/apache2″ :-)
    Thanks!

  11. Pingback: TheSlim » Как полностью удалить Apache в Ubuntu

  12. j says:

    does this works on ubuntu 10.10 server?

  13. anthony says:

    I love you!! … saved me a shedload of hassle .. thanks…

  14. Ankit says:

    Thanks a lot buddy! You saved my day

  15. Squeage says:

    You are awesome! I looked everywhere for the fix and you were the only one with it. Thanks!

  16. Filete says:

    Someone build a statue for this guy. You’ve saved my ass

  17. sudhir says:

    you made me breath easy!!!! thanks!!!!

  18. Zaur says:

    Super! Works!

  19. sam says:

    Mate!!! Thanks so much!!! It worked like a dream! Thanks again :)

  20. Claus Conrad says:

    Thank you – this was just what I needed, you saved me a lot of time.

  21. Thanks so much for the little script! Was definitely helpful.

  22. Dave says:

    Well done!

    Still works on 12.04 and works a treat. Now to get my PHP working again :)

  23. William says:

    GOOD TO KNOW. I will probably be using this trick a few times in the near future!

    Thank you kindly, W

  24. chris says:

    awesome :)
    i had a lot of trouble resetting that apache

  25. David says:

    WOO!! perfect! thanks!

  26. Ian Pride says:

    Awesome thanx, worked perfect after playing around too much lol.

  27. JoJo says:

    that’s a great idea and i would have never thought of that, thank you! however the code i had to write for ubuntu (pinguy) 12.04 with kernel 3.2.0-25 for that first command was :

    APACHE_PKGS=$(sudo dpkg --get-selections | grep apache | cut -f 1)

    as setting a value of the output of a command to a variable is:
    VARIABLE=$(command)

    this worked like a charm, thank you very much!

  28. Matt says:

    Thank you, worked very fine on Debian Squeeze!

  29. Manish Pathak says:

    Thanks a lot Samuals.

  30. Russell England says:

    Brilliant! Worked for me on Ubuntu 12.04

  31. Andrey says:

    Thank you.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>