Blog

Start Apache on boot

I was reminded that by default, Linux distros don’t start services on boot. I found this out the hard way on EC2 when setting up an apache server.

I stood up a new EC2 instance on Amazon Web Services and setup a typical LAMP (Linux, Apache, Mysql, PHP) stack.  To test basic apache/php functionality, I’ll typically create a quick page, called test.php, that contains the following:

<?php phpinfo() ?>

It’s a simple test page, but lets me know that things are working and displays component and version information. Sure enough, it worked as expected. Shortly after that, I was making more changes and suddenly found the Web server unresponsive and no longer serving up pages. For the life of me, I couldn’t figure out what changed. After some rudimentary checks in EC2 (everything looked fine), I queried the status of httpd (apache). I’m using Amazon Linux, which is similar to Fedora/CentOS, but your mileage will vary based on your distro…

service httpd status

…and found out that apache wasn’t running! After starting apache, everything was fine. It turned out that a colleague had rebooted the EC2 instance. I was genuinely surprised that despite purposefully installing server components (e.g. apache, mysql, etc.) that these services DON’T, by default, start on boot, or whenever the system launches. Given the role(s) that most EC2 instances are used for, this seems pretty ridiculous, but I digress…

Fortunately, this is easily remedied:

sudo chkconfig httpd on

Interestingly enough, when I first ran this command on my Amazon Linux instance, I received the following error:

service httpd supports chkconfig, but is not referenced in any runlevel (run ‘chkconfig –add httpd’)

As the message indicates, it’s as easy as running the specified command. Man, an error message that’s actually useful!

sudo chkconfig --add httpd

It’s also easy to query that status of a given service and see if it’s setup for start on system boot:

sudo chkconfig --list httpd

After that it was smooth sailing, plus no more worrying about the Web site/services being available on subsequent reboots.

Leave Reply