2

I'm using Ubuntu 18.04 with the default install of Apache, in which I configured a virtual directory. I would like to run Apache as a srevice, that automatically starts when the system reboots. I noticed Apache wasn't running upon system restart and then noticed this

$ sudo /etc/init.d/apache2 start
[sudo] password for davea: 
[....] Starting apache2 (via systemctl): apache2.serviceJob for apache2.service failed because a timeout was exceeded.
See "systemctl status apache2.service" and "journalctl -xe" for details.
 failed!
davea

I wasn't sure what it meant so I tried getting the status as recommended above ...

$ sudo systemctl status apache2.service
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: failed (Result: timeout) since Mon 2021-01-18 19:00:16 UTC; 4min 33s ago
  Process: 800 ExecStart=/usr/sbin/apachectl start (code=killed, signal=TERM)

Jan 18 18:58:47 prod systemd[1]: Starting The Apache HTTP Server...
Jan 18 18:58:47 prod apachectl[800]: Invoking 'systemctl start apache2'.
Jan 18 18:58:47 prod apachectl[800]: Use 'systemctl status apache2' for more info.
Jan 18 19:00:16 prod systemd[1]: apache2.service: Start operation timed out. Terminating.
Jan 18 19:00:16 prod systemd[1]: apache2.service: Failed with result 'timeout'.
Jan 18 19:00:16 prod systemd[1]: Failed to start The Apache HTTP Server.

I'm still unclear what else I need to do. I'm able to restart Apache without any problems using

$ sudo apachectl restart
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 2607:f298:5:101d:f816:3eff:feac:4c03. Set the 'ServerName' directive globally to suppress this message

Not sure why there is a warning or if I shoudl be concerned about it but I defined the "ServerName" in my conf file below

/etc/apache2/sites-available/000-default-le-ssl.conf

What else do I need to do to get Apache to run as a service upon system startup?

Edit: In response to comments, here's configtest and journalctl output immediately after restarting ...

$ sudo apachectl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 2607:f298:5:101d:f816:3eff:feac:4c03. Set the 'ServerName' directive globally to suppress this message
Syntax OK

$ sudo journalctl -xe
-- 
-- Unit UNIT has finished starting up.
-- 
-- The start-up result is RESULT.
Jan 22 01:36:35 prod systemd[1]: Started User Manager for UID 1001.
-- Subject: Unit user@1001.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit user@1001.service has finished starting up.
-- 
-- The start-up result is RESULT.
Jan 22 01:36:35 prod systemd[1028]: Reached target Default.
-- Subject: Unit UNIT has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit UNIT has finished starting up.
-- 
-- The start-up result is RESULT.
Jan 22 01:36:35 prod systemd[1028]: Startup finished in 219ms.
-- Subject: User manager start-up is now complete
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- The user manager instance for user 1001 has been started. All services queued
-- for starting have been started. Note that other services might still be starting
-- up or be started at any later time.
-- 
-- Startup of the manager took 219137 microseconds.
Jan 22 01:36:55 prod systemd-timesyncd[565]: Synchronized to time server [2001:67c:1560:8003::c8]:123 (ntp.ubuntu.com).
Jan 22 01:37:03 prod sudo[1172]: davea : TTY=pts/0 ; PWD=/home/davea ; USER=root ; COMMAND=/bin/journalctl -xe
Jan 22 01:37:03 prod sudo[1172]: pam_unix(sudo:session): session opened for user root by davea(uid=0)

Edit 2: Contents of /lib/systemd/system/apache2.service after attempting suggested answer ...

[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
EnvironmentFile=/etc/environment
ExecStart=/usr/sbin/apachectl start
ExecStop=/usr/sbin/apachectl stop
ExecReload=/usr/sbin/apachectl graceful
PrivateTmp=true
Restart=on-abort
TimeoutStartSec=300

[Install]
WantedBy=multi-user.target

Rebooting still fails to start Apache. However, when I manually run "sudo apachectl restart" that starts things up.

6
  • Is 000-default-le-ssl.conf actually being used by Apache? What configuration files do you see in /etc/apache2/sites-enabled?
    – user1091774
    Jan 19 at 2:02
  • Yes. I know it's being used b/c I'm able to visit mysite.com ("le" stands for LetsEncrypt, an initiative to get free HTTPS certs)
    – Dave
    Jan 20 at 19:27
  • The output of journalctl -xe is missing..
    – LarsJoo
    Jan 20 at 21:35
  • And "sudo apachectl configtest" is of course = Syntax OK..
    – LarsJoo
    Jan 20 at 21:38
  • As LarsJoo as suggested, could you update your question to include the output of journalctl -xe after Apache fails to start on reboot? It would also be helpful to see the output of sudo a2query -s, which will list all enabled confutation files. With this, it may be possible to begin providing specific suggestions to resolve the two issues outlined in your question 👍🏻
    – user1091774
    Jan 21 at 1:26
1

The Apache service generally has a window of 90 seconds to start. If it cannot do so during boot time, systemd will kill the process and report it as timed out. Under most circumstances, Apache can be started (and restarted) in well under a second, but it can get stuck behind a dependency during boot time if there is another process that requires additional time. I have seen this on occasion when the network interfaces take a little longer than usual to start up, or when the system is waiting for a response from an NTP server to synchronise the internal clock.

One of the simplest ways to reduce the frequency of this error is to increase the service timeout value from 90 to something a bit higher, like 180 or 300.

Here is how you can do this:

  1. SSH into the server

  2. Open the /lib/systemd/system/apache2.service file with sudo:

    sudo vi /lib/systemd/system/apache2.service
    

    Note: If you do not have a file at /lib/systemd/system/apache2.service, then the configuration file will be at /lib/systemd/system/apache2.service.d/apache2-systemd.conf.

  3. In the [Service] section, add this line (Press I to Insert/Edit):

    TimeoutStartSec=300
    

    Feel free to set the TimeoutStartSec value to something you're comfortable with, but do keep it above 90. Otherwise the timeouts at boot will continue.

    Once done, you should see something like this:

    [Service]
    Type=forking
    Environment=APACHE_STARTED_BY_SYSTEMD=true
    ExecStart=/usr/sbin/apachectl start
    ExecStop=/usr/sbin/apachectl stop
    ExecReload=/usr/sbin/apachectl graceful
    PrivateTmp=true
    Restart=on-abort
    TimeoutStartSec=300
    
  4. Save the file (Esc, :, W, Q)

At this point, you can reboot the server and see if Apache continues to time out. Please note that this is not solving the crux of the problem, which is the extended time required to boot, but it should enable Apache to start up after all the init processes are complete.

Bonus Fix

To eliminate the AH00558: apache2: Could not reliably determine the server's fully qualified domain name messages, you can set a default for the system. This will not affect the performance of the web server in any way:

  1. SSH into the server (if not already connected)

  2. Open the /etc/apache2/apache2.conf file with sudo:

    sudo vi /etc/apache2/apache2.conf
    
  3. At the very bottom of the file, add this line (Press I to Insert/Edit):

    ServerName 127.0.0.1
    
  4. Save the file (Esc, :, W, Q)

  5. Test the Apache configuration files for errors:

    sudo apachectl configtest
    

    You should see:

    Syntax OK
    

Now you can either restart Apache with a sudo service apache2 restart or, if you're doing this after updating the TimeoutStartSec value, you can reboot the server.

6
  • Hi, Added the complete /etc/apache2/apache2.conf as an edit to my question. Still not getting Apache to start when rebooting.
    – Dave
    Jan 22 at 15:44
  • @Dave edit /lib/systemd/system/apache2.service and change EnvironmentFile=/etc/environment to Environment=APACHE_STARTED_BY_SYSTEMD=true as shown in Matigo's answer ( no need for TimeoutStartSec=300. remove it ) then run sudo systemctl daemon-reload after that run sudo systemctl restart apache2.service and it should work.
    – Raffa
    Jan 24 at 8:47
  • I gave this a try but although Apache does seem to run after system reboot, the Python application connected to it (through mod_wsgi) no longer works because it is not finding expected environment variables.
    – Dave
    Jan 26 at 17:27
  • @Dave Oh I see. In this case do both ... first Environment=APACHE_STARTED_BY_SYSTEMD=true then in the line after it EnvironmentFile=/etc/environment and all should work fine.
    – Raffa
    Jan 26 at 17:45
  • Dear @Dave and Matigo, After looking at the answers here and searching the internet I have seen many questions like this but no solutions were provided anywhere I looked that address this issue directly. Given the above, I wrote an answer that is to the best of my knowledge direct and to the point. Hopefully you will find it helpful. Best regards
    – Raffa
    Jan 26 at 20:04
0

To enable a service to start on boot run:

sudo systemctl enable <serviceName>
1
  • I ran "sudo systemctl enable apache2", then restarted. No dice.
    – Dave
    Jan 22 at 1:40
0

A similar question was asked on our sister site Unix & Linux:

In that answer the solution was changing the file /etc/systemd/system.conf and the two lines:

#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s

On my machine it is 90 seconds but on the linked answer it was 15 seconds which was too short. If your says 15s then change it to:

DefaultTimeoutStartSec=90s
DefaultTimeoutStopSec=90s

Note removing the # turns the line from a comment to a command.

Also note your journalctl -xe doesn't appear to have the apache2 related errors? You probably need to press Page Up a few times. Alternately you can try using journalctl -b-0 instead and using Page Down.

0

The issue is simple and yet vital.

There is an execution environment variable missing in your systemd apache2 unit file AKA "apache2 service file" ie. /lib/systemd/system/apache2.service

This variable is $APACHE_STARTED_BY_SYSTEMD. It must be set and it goes under the [Service] section AKA systemd.exec or Execution environment configuration

This variable can be set like so:

Environment=APACHE_STARTED_BY_SYSTEMD=true

This variable is required for Apache to start and run correctly after Apache's integration with systemd since Apache version 2.4.42 and later. It provides systemd with handling and reporting capabilities of /usr/sbin/apachectl and the apache2 process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.