How to Start, Stop, and Restart Apache on Ubuntu 22.04?


Apache is an open-source and renowned web server for Ubuntu users. Like any web server, Apache acts as a medium between the web and the user, i.e., a user requests a specific website or content and Apache responds back with the result.

Apache on Ubuntu is backed up by a service. That service holds the control of the Apache on the system. Before using Apache, it is important to ensure that the service is running in an active state. Similarly, if the Apache services are not required, then the service can be stopped. Moreover, the service can be restarted to refresh the Apache server.

In today’s post, we will enlist the commands/methods to start/stop/restart the Apache on Ubuntu 22.04.

Prerequisites: Check the Service Name and the init System

The name of the service and the init system of your Ubuntu are the key parameters to manage the services. Usually, the “systemd” and “SysV” are the two init systems on Ubuntu which use “systemctl” and “service” commands to manage the service.

Let’s see how you can check the init system and the service name for Apache on Ubuntu 22.04:

Check/Trace the init System

Use the below command to check which init system you are using one of the commands:

ps -p 1 -o comm=
stat /sbin/init

The “systemd” is the init system. So, we must use the command “systemctl” associated with the “systemd”. However, we will list the commands of all the init systems to start, stop, or restart the Apache on Ubuntu.

Check/Trace the Service Name of the Apache Web Server

The service associated with Apache has a specific name (apache2 in Ubuntu 22.04). You can trace the name as per your init system (checked in Step1):

For Systemd:

ls /lib/systemd/system

For SysV:

ls /etc/init.d

In both cases, “apache2” is the service name.

How to Start, Stop, and Restart Apache on Ubuntu 22.04?

As discussed, the Apache Web server’s service name is “apache2” and “systemd” is the init system. Thus, the “systemctl” is the recommended command to start/stop/restart Apache on Ubuntu 22.04. However, we will list the commands to start/stop/restart Apache using other init systems (SysV) as well. Let’s start:

Start the Apache Web Server

Let’s learn how to start the Apache web server on Ubuntu:

systemctl Command

sudo systemctl start apache2

service Command

sudo service apache2 start

/etc/init.d Script

sudo /etc/init.d/apache2 start

Stop the Apache Web Server

Use one of the below commands to stop the Apache Web server. You need to adopt the recommended one as per your init system:

systemctl Command

sudo systemctl stop apache2

service Command

sudo service apache2 stop

/etc/init.d Script

sudo /etc/init.d/apache2 stop

Restart the Apache Web Server

To restart the Apache server on Ubuntu 22.04, use the systemctl, service, or the /etc/init.d script as per your init system.

systemctl Command

sudo systemctl restart apache2

service Command

sudo service apache2 restart

/etc/init.d Script

sudo /etc/init.d/apache2 restart

Additional Commands to Manage the Apache on Ubuntu 22.04

Until now, we have only talked about how to start/stop/restart the Apache. Apart from these, you can disable or enable the Apache service. Moreover, you can configure the Firewall rules for the Apache server. Let’s dig into these useful commands:

Disable the Apache Web Server

When the Apache is installed, it is enabled by default on Ubuntu 22.04. However, if you want to disable it, use the command:

sudo systemctl disable apache2

Enable the Apache Web Server

To enable the Apache server, the following command is used:

sudo systemctl enable apache2

Configure Firewall Rules for the Apache Web Server

Apache has to deal with inside and outside the network. Thus, the firewall rules for Apache must be configured to allow or disallow the network traffic. Let’s get in to understand the basic Firewall handling:

Before that, you must know there are three Apache profiles. These profiles are configured to attain a specific permission for Apache over the firewall.

  • Apache: Opens the standard port 80 to allow the unencrypted traffic.
  • Apache Secure: Opens/Uses port 443 to allow the TLS/SSL encrypted traffic.
  • Apache Full: Combination of Apache and Apache Secure profiles (opens both Ports 80 and 443).

It depends on which profile you want to configure on the Firewall.

Allow Apache

Use the below command to allow the “Apache” profile (for port 80 only):

sudo ufw allow 'Apache'

Deny the Incoming Traffic

To block the incoming traffic on port 80, you need to use the “deny” as follows:

sudo ufw deny 'Apache'

Delete the Apache Profile From the Firewall

If you deny the Apache profile only, it will not allow incoming traffic. However, the rule remains inside the Firewall. To root out the profile completely, you have to delete it. First, check the rule number inside the UFW using the command:

sudo ufw status

Now, delete the traced rule using the command:

sudo ufw delete 2

Similarly, you can check the other Apache rule(s) (such as for IPv6) and delete it using the “rule number” (trace it using the “sudo ufw status” command).

Bottom Line

Apache on Ubuntu 22.04 has the service named “apache2”. To manage the “Apache”, Ubuntu has the “systemd” as its init system. Thus, it is recommended to use the “systemctl” command to start/stop/restart the Apache on Ubuntu 22.04. Moreover, the “service” command, and the “/etc/init.d/apache2” script can also be used to serve the same purpose.

All in all, the methods to start/stop/restart the Apache Web Server are practically demonstrated for Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories