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.

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

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

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

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

  1. Moos says:

    Would have “dpkg-reconfigure apache2″ worked?

  2. victortns says:

    i have the same error and i solve the problem with your text, thanks ;)

  3. @Moos: Not sure, will have to try and report next time I fubar Apache :-)

    @victortns: Glad to hear it! That’s exactly why I write this blog :-)

  4. lohitha says:

    I had the same issue with a corrupted apache installation.
    I could solve it with your suggestions.
    Thanx a lot for the info

  5. Rickey Moore says:

    Thanks!!!!!!!! That saved my bacon. It was brutal and scary though. BUT! I have all of my files back and that is what counts. Thanks! Ric

  6. joseliki says:

    thank you so much!!!! my server it works again ;)

  7. phreakincool says:

    Thanks!

  8. Zet0 says:

    Thx a lot !

  9. Comet says:

    thanks so much for this informative post. I had numerous problems with my installation, mostly due to deleting the default virtual host entry in webmin. tried reinstalling apache numerous times, but this did the trick PERFECTLY. THANKS A MILLION!

  10. Kallesson says:

    Thank god (well I guess you are god) for this post. Been looking for ages for an easy way to do this!

  11. tmac says:

    Very cool, just saved me a bunch of time on a hosed install.

  12. Jensca says:

    Hi this worked for me, thanks, can i copy to my personaly blog and link to yours?

  13. @Jensca Sure, link away! Glad it helped.

  14. Fatman says:

    I had the same problem. Thanks. Would have taken me ages to guess that.

    Though actually my Apache config wasn’t hosed. I just felt it was “unclean” and I wanted to start over.

  15. Ben Herman says:

    Thanks so much have a box that has been up 151 days now that i thought i would have to take down and reload due to a massive Apache fubar! This tutorial saved the day, and my up time count!

  16. @Ben Herman: Awesome! It’s all about the uptime, baby :-)

  17. Ntropy says:

    Thank you. After hours of failure, this process worked. Truly appreciated.

  18. Pingback: Can’t open /etc/apache2/envvars

  19. Ben Herman says:

    Wanted to let you know I put these instructions up on my blog and posted back to yours giving you credit. Thanks again.

  20. John says:

    You Sir, are a hero.

  21. Malaysia says:

    u r hero bro! TQ VERY MUCH!!

  22. Ganesh says:

    I new to ubuntu, and I m tryong to reinstall apache for 2 days. but u r command did all for me to config apache.
    thx dude!!

  23. Arema says:

    I had exactly the same problem: out of desperation I manually sudo and deleted the directory apache2. And then I was at a lost as to what to do next because installation did not return the config files. Thanks for this posting!

  24. vineet kansal says:

    Thanks a lot…for this information

  25. Trevor says:

    Thanks for posting this! Now I can start off with a clean slate.

  26. Derek says:

    Hey Thanks for that.. I was in the same situation and got apache back up working again.!
    cool.

  27. devasia joseph says:

    great !!! thanks

  28. Miguel GP says:

    You are my hero

  29. Julien Boyer says:

    Didn’t work at all for me. The old files were still there. I suppose I should have rm them, but I’ll try something else now…

  30. abdullah kahraman says:

    @ Moos: Would have “dpkg-reconfigure apache2″ worked? : Nope, It didn’t work for me..

    @Samuel Huckins: Thanks buddy :)

  31. sagar says:

    Thanks a ton dude…
    now my prob is that im using Ubuntu 10.04 Im new to Linux .earlier i used HFS software to share my hard disk data in my hostel LAN network.now i want to start a server through which my friends from hostel can access my server in there browser and download movies,music which i shared with them.i want to use apache to share my data in LAn.how can we do that.plz help.my email id is sagar7488@gmail.com.
    plz plz help me.

  32. Lokesh says:

    Yuuppp..thanks a lot yaar…
    Really it’s very helpful for me..i have just lost my apache..but due to your help..i have reconfigure easily…thanks again….look ahead…

  33. Pim says:

    This solved my problems with apache, I simply just wanted a fresh start.

    Thanks so much for sharing this! :-D

  34. Pingback: 100% apache cpu usage after upgrade Drija

  35. Frank says:

    Thanks! You saved my day!

  36. touchdeep says:

    It’s work

    I’ve problem like you but now solved

    Thank you very much

  37. Nikhil says:

    Oh, I too had the same trouble and was wondering how to restore my apache. Thanks for the post :-)

  38. Mark Watson says:

    Google likes you!!!

  39. Great tip Dude!

    I was facing the same issue and now my enviroment here really works very good!

    Thank you brow!
    (From Brazil! =D)

  40. Joe says:

    Thanks — can’t believe what a mess I made of my configuration files. Embarrassing.

  41. Evgeniy says:

    Thanks for the post. Your code helped with similar problem

  42. Lucas Lee says:

    Thanks, you’re awesome, dude!

  43. Lucas Lee says:

    I also want note that, if you tend to use php intead of python which is the case of this topic mentioned, then change it to:
    sudo apt-get remove –purge apache2 apache2-mpm-worker apache2-threaded-dev \
    apache2-utils apache2.2-common libapache2-mod-python \
    libapache2-mod-python-doc libapache2-mod-wsgi libapache2-mod-php5
    (just add ” libapache2-mod-php5″ on the end)

  44. Karen says:

    Help! What a mess I have. I am on unbuntu 10.10 and my apache2 config is really screwed up.

    apt-show-versions |grep apache
    apache2-mpm-prefork/maverick uptodate 2.2.16-1ubuntu3.1
    apache2-utils/maverick uptodate 2.2.16-1ubuntu3.1
    apache2.2-bin/maverick uptodate 2.2.16-1ubuntu3.1
    apache2.2-common/maverick uptodate 2.2.16-1ubuntu3.1
    libapache2-mod-php5/maverick uptodate 5.3.3-1ubuntu9.3
    libapache2-mod-python-doc/maverick uptodate 3.3.1-8ubuntu2

    root@fwc-net6:/etc/apache2# apt-get –purge remove apache2.2-common Reading package lists… Done
    Building dependency tree
    Reading state information… Done
    The following extra packages will be installed:
    apache2-mpm-itk libapache2-mod-php5filter
    Suggested packages:
    php-pear
    The following packages will be REMOVED:
    apache2-mpm-prefork* libapache2-mod-php5*
    The following NEW packages will be installed:
    apache2-mpm-itk libapache2-mod-php5filter
    0 upgraded, 2 newly installed, 2 to remove and 262 not upgraded.
    Need to get 0B/2,946kB of archives.
    After this operation, 4,096B disk space will be freed.
    Do you want to continue [Y/n]? y
    WARNING: The following packages cannot be authenticated!
    apache2-mpm-itk libapache2-mod-php5filter
    Install these packages without verification [y/N]? y
    dpkg: apache2-mpm-prefork: dependency problems, but removing anyway as you requested:
    libapache2-mod-php5 depends on apache2-mpm-prefork (>> 2.0.52) | apache2-mpm-itk; however:
    Package apache2-mpm-prefork is to be removed.
    Package apache2-mpm-itk is not installed.
    (Reading database … 117622 files and directories currently installed.)
    Removing apache2-mpm-prefork …
    .: 49: Can’t open /etc/apache2/envvars
    invoke-rc.d: initscript apache2, action “stop” failed.
    Selecting previously deselected package apache2-mpm-itk.
    (Reading database … 117618 files and directories currently installed.)
    Unpacking apache2-mpm-itk (from …/apache2-mpm-itk_2.2.16-1ubuntu3.1_i386.deb) …
    Setting up apache2-mpm-itk (2.2.16-1ubuntu3.1) …
    .: 49: Can’t open /etc/apache2/envvars
    invoke-rc.d: initscript apache2, action “start” failed.
    dpkg: libapache2-mod-php5: dependency problems, but removing anyway as you requested:
    php5 depends on libapache2-mod-php5 (>= 5.3.3-1ubuntu9.3) | libapache2-mod-php5filter (>= 5.3.3-1ubuntu9.3) | php5-cgi (>= 5.3.3-1ubuntu9.3) | php5-fpm (>= 5.3.3-1ubuntu9.3); however:
    Package libapache2-mod-php5 is to be removed.
    Package libapache2-mod-php5filter is not installed.
    Package php5-cgi is not installed.
    Package php5-fpm is not installed.
    (Reading database … 117622 files and directories currently installed.)
    Removing libapache2-mod-php5 …
    Module php5 disabled.
    Run ‘/etc/init.d/apache2 restart’ to activate new configuration!
    Purging configuration files for libapache2-mod-php5 …
    dpkg: warning: while removing libapache2-mod-php5, directory ‘/etc/php5/apache2′ not empty so not removed.
    Selecting previously deselected package libapache2-mod-php5filter.
    (Reading database … 117616 files and directories currently installed.)
    Unpacking libapache2-mod-php5filter (from …/libapache2-mod-php5filter_5.3.3-1ubuntu9.3_i386.deb) …
    Setting up libapache2-mod-php5filter (5.3.3-1ubuntu9.3) …

    Creating config file /etc/php5/apache2filter/php.ini with new version
    .: 49: Can’t open /etc/apache2/envvars
    invoke-rc.d: initscript apache2, action “force-reload” failed.

  45. @Luscas Lee Thanks, that’s helpful! I’ve updated the commands to make things clearer (hopefully).

    @Karen I don’t know what some of the packages you have installed are for, looks like you’ve got a lot in place. I’d try running kill -9 on any Apache processes first, make sure none of them are running. Then begin removing/purging Apache packages individually to reduce the complexity of the dependency resolution. Good luck :-(

  46. Art Elsea, Jr. says:

    I worked well for me. I had been attempting to remove Apache2 manually for a couple of days without success.

    Thanks,

    Art

  47. vee says:

    that real works!
    thank you

  48. Lik says:

    Solution of 49 error:
    sudo apt-get remove –purge $(dpkg -l apache* | grep ii | awk ‘{print $2}’) && sudo apt-get install apache2
    =)

  49. stolk says:

    Thank you so much! Apache should as default check for its directory I think…

  50. Tau says:

    Thanks a lot! That’s solved my problem.

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>