为什么“apachectl restart”有效但“apachectl start”无效?

为什么“apachectl restart”有效但“apachectl start”无效?

我使用的是 Ubuntu 18.04。我似乎无法使用“sudo apachectl start”重新启动 Apache2。我看到这个

~$ sudo apachectl start
Invoking 'systemctl start apache2'.
Use 'systemctl status apache2' for more info.
Job for apache2.service failed because a timeout was exceeded.
See "systemctl status apache2.service" and "journalctl -xe" for details.
Action 'start' failed.
The Apache error log may have more information.

当我检查状态时,我看到

$ 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 Fri 2021-01-22 16:01:25 UTC; 2min 2s ago
  Process: 1453 ExecStart=/usr/sbin/apachectl start (code=killed, signal=TERM)

Jan 22 15:56:25 prod systemd[1]: Starting The Apache HTTP Server...
Jan 22 15:56:25 prod apachectl[1453]: Invoking 'systemctl start apache2'.
Jan 22 15:56:25 prod apachectl[1453]: Use 'systemctl status apache2' for more info.
Jan 22 16:01:25 prod systemd[1]: apache2.service: Start operation timed out. Terminating.
Jan 22 16:01:25 prod systemd[1]: apache2.service: Failed with result 'timeout'.
Jan 22 16:01:25 prod systemd[1]: Failed to start The Apache HTTP Server.

和“journalctl -xe”说......

$ sudo journalctl -xe
Jan 22 15:56:25 prod apachectl[1453]: Use 'systemctl status apache2' for more info.
Jan 22 16:00:00 prod sshd[1475]: Received disconnect from 113.128.15.170 port 50036:11: Bye Bye [preauth]
Jan 22 16:00:00 prod sshd[1475]: Disconnected from authenticating user root 113.128.15.170 port 50036 [preauth]
Jan 22 16:00:01 prod CRON[1477]: pam_unix(cron:session): session opened for user root by (uid=0)
Jan 22 16:00:01 prod CRON[1478]: (root) CMD (/bin/bash -l -c '/opt/scripts/import_coop_data_from_google_sheet.sh >> /tmp/cronoutput.txt 2>&1')
Jan 22 16:00:18 prod sshd[1499]: Received disconnect from 218.93.208.150 port 25505:11:  [preauth]
Jan 22 16:00:18 prod sshd[1499]: Disconnected from authenticating user root 218.93.208.150 port 25505 [preauth]
Jan 22 16:00:44 prod CRON[1477]: (CRON) info (No MTA installed, discarding output)
Jan 22 16:00:44 prod CRON[1477]: pam_unix(cron:session): session closed for user root
Jan 22 16:01:25 prod systemd[1]: apache2.service: Start operation timed out. Terminating.
Jan 22 16:01:25 prod sudo[1446]: pam_unix(sudo:session): session closed for user root
Jan 22 16:01:25 prod systemd[1]: apache2.service: Failed with result 'timeout'.
Jan 22 16:01:25 prod systemd[1]: Failed to start The Apache HTTP Server.
-- Subject: Unit apache2.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- Unit apache2.service has failed.
-- 
-- The result is RESULT.
Jan 22 16:02:26 prod sshd[1525]: Received disconnect from 221.181.185.135 port 57424:11:  [preauth]
Jan 22 16:02:26 prod sshd[1525]: Disconnected from authenticating user root 221.181.185.135 port 57424 [preauth]
Jan 22 16:02:28 prod sshd[1528]: Invalid user drcomadmin from 207.244.247.57 port 45316
Jan 22 16:02:28 prod sshd[1528]: Received disconnect from 207.244.247.57 port 45316:11: Bye Bye [preauth]
Jan 22 16:02:28 prod sshd[1528]: Disconnected from invalid user drcomadmin 207.244.247.57 port 45316 [preauth]
Jan 22 16:03:07 prod sudo[1531]: davea : TTY=pts/0 ; PWD=/home/davea ; USER=root ; COMMAND=/bin/systemctl status apache2.service
Jan 22 16:03:07 prod sudo[1531]: pam_unix(sudo:session): session opened for user root by davea(uid=0)
Jan 22 16:03:09 prod sudo[1531]: pam_unix(sudo:session): session closed for user root
Jan 22 16:03:28 prod sudo[1535]: davea : TTY=pts/0 ; PWD=/home/davea ; USER=root ; COMMAND=/bin/systemctl status apache2.service
Jan 22 16:03:28 prod sudo[1535]: pam_unix(sudo:session): session opened for user root by davea(uid=0)
Jan 22 16:03:28 prod sudo[1535]: pam_unix(sudo:session): session closed for user root
Jan 22 16:03:47 prod sudo[1538]: davea : TTY=pts/0 ; PWD=/home/davea ; USER=root ; COMMAND=/bin/journalctl -xe
Jan 22 16:03:47 prod sudo[1538]: pam_unix(sudo:session): session opened for user root by davea(uid=0)

但奇怪的是,当我运行“sudo apachectl restart”时,Apache 出现,我可以与它交互

$ sudo apachectl restart
httpd not running, trying to start

这是怎么回事?下面是我的服务配置文件...

$ sudo cat /lib/systemd/system/apache2.service
[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

答案1

理论上,它被设置为系统上的服务,并且执行程序可执行文件来尝试重新启动该服务将不起作用。您应该使用“systemctl restart apache2”。要停止它,“systemctl stop apache2”并启动“systemctl start apache2”

http://manpages.ubuntu.com/manpages/bionic/man5/systemd.service.5.html

相关内容