Start / Stop and Restart Apache 2 Web Server Command

How to start/stop/restart Apache 2 server on Linux/Unix?

How do I restart an Apache 2 Web Server under a Debian / Ubuntu / CentOS /
RHEL / Fedora Linux or UNIX-like operating systems? Can you tell me
command to start or stop Apache 2 web server running on Linux?







Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux or Unix terminal
Category Processes Management
Prerequisites Apache 2.x/2.4
OS compatibility *BSD Linux Unix WSL macOS
Est. reading time 5 minutes

Apache
is primarily used to serve both static content and dynamic Web pages on
the World Wide Web. Many web applications are designed expecting the
environment and features that Apache provides. Apache can be started or
restarted using any one of the following methods on Linux or Unix-like
systems.



First, login to your web-server using ssh client, if server is not in your local data center:

ssh root@your-server-com #Linode box

ssh ec2-user@aws-ip-here # AWS

ssh vivek@192.168.2.100 # My home dev server

Once logged in type the following commands as per your Linux or Unix variant.


Advertisement






Debian/Ubuntu Linux Specific Commands to Start/Stop/Restart Apache


You can either use service or /etc/init.d/ command as follows on Debian Linux version 7.x or Ubuntu Linux version Ubuntu 14.10 or older:


Restart Apache 2 web server, enter:


/etc/init.d/apache2 restart

OR

sudo /etc/init.d/apache2 restart

OR

sudo service apache2 restart


To stop Apache 2 web server, enter:


/etc/init.d/apache2 stop

OR

sudo /etc/init.d/apache2 stop

OR

sudo service apache2 stop


To start Apache 2 web server, enter:


/etc/init.d/apache2 start

OR

sudo /etc/init.d/apache2 start

OR

sudo service apache2 start


A note about Debian/Ubuntu Linux systemd users


Use the following systemctl command on Debian Linux version 8.x+ or Ubuntu Linux version Ubuntu 15.04+ or above:

## Start command ##

sudo systemctl start apache2.service

## Stop command ##

sudo systemctl stop apache2.service

## Restart command ##

sudo systemctl restart apache2.service

We can view status using the following command:

sudo systemctl status apache2.service

Outputs:


 apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2021-02-24 20:39:39 UTC; 5 days ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 115 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Process: 15247 ExecReload=/usr/sbin/apachectl graceful (code=exited, status=0/SUCCESS)
Main PID: 128 (apache2)
Tasks: 6 (limit: 4672)
Memory: 16.4M
CGroup: /system.slice/apache2.service
├─ 128 /usr/sbin/apache2 -k start
├─15254 /usr/sbin/apache2 -k start
├─15255 /usr/sbin/apache2 -k start
├─15256 /usr/sbin/apache2 -k start
├─15257 /usr/sbin/apache2 -k start
└─15258 /usr/sbin/apache2 -k start

Feb 27 00:00:23 ubuntu-db-mgmnt systemd[1]: Reloaded The Apache HTTP Server.
Feb 28 00:00:23 ubuntu-db-mgmnt systemd[1]: Reloading The Apache HTTP Server.


CentOS/RHEL (Red Hat) Linux version 4.x/5.x/6.x or older specific commands


Use the service command:

## Start ##

service httpd start

## Stop ##

service httpd stop

## Restart ##

service httpd restart


CentOS/Fedora/RHEL (Red Hat) Linux version 7.x or newer specific commands


Most modern RHEL based distro now using systemd, so you need to use the following systemctl command:

## Start command ##

sudo systemctl start httpd.service

## Stop command ##

sudo systemctl stop httpd.service

## Restart command ##

sudo systemctl restart httpd.service

The above commands works with RHEL, CentOS, RockyLinux, Fedora, and AlmaLinux.


Alpine Linux start / stop / restart Apache 2 using openrc


We need to use the service command as root user:

service apache2 start

service apache2 stop

service apache2 status

service apache2 restart

Session:


 * Stopping apache2 ...
* Starting apache2

FreeBSD Unix users


FreeBSD user can restart Apache as follows:

/usr/local/etc/rc.d/apache22 restart

service restart apache22

service stop apache22

service start apache22

Latest version of FreeBSD 13 comes with apache24, so commands are:

/usr/local/etc/rc.d/apache24 restart

service restart apache24

service stop apache24

service start apache24


Generic method to start/stop/restart Apache on a Linux/Unix/*BSD machines


First, use the type command or command command to find the apachectl or apachectl2 path:

type -a apachectl

type -a apache2ctl


Outputs from the Ubuntu Linux 20.04 LTS server:


apachectl is /usr/sbin/apachectl
apachectl is /sbin/apachectl

Then use the syntax is as follows (must be run as root user):

## stop it ##

apachectl -k stop

## restart it ##

apachectl -k restart

## graceful restart it ##

apachectl -k graceful

## Start it ##

apachectl -f /path/to/your/httpd.conf

apachectl -f /usr/local/apache2/conf/httpd.conf

The apachectl/apache2ctl is Apache HTTP server control interface. Other options are as follows:


Start the Apache daemon


apachectl start

# OR #

apache2ctl start


Stops the Apache daemon


apachectl stop

# OR #

apache2ctl stop


Restarts the Apache daemon by sending it a SIGHUP


apachectl restart

# OR #

apache2ctl restart


Shows a full status report from mod_status


apachectl fullstatus

# OR #

apache2ctl fullstatus


Displays a brief status report


apachectl status

# OR #

apache2ctl status


Gracefully restarts the Apache daemon by sending it a SIGUSR1


apachectl graceful

# OR #

apache2ctl graceful

We can do gracefully stops the Apache httpd daemon too? Try:

apachectl graceful-stop

# OR #

apache2ctl graceful-stop


Run a configuration file syntax test


apachectl configtest

# OR #

apache2ctl configtest


Summing up


You learned how to start, stop or restart the Apache 2 web server using command-line over ssh-based session. Use the man command or help command to read the following man pages:

man service

man systemctl

man httpd

man apachectl