Commands to Install Apache Web Server on Ubuntu 22.04 or 20.04 LTS

If you want to learn how to run and access a website from a remote location, we need a web server and what could be more convenient than getting started with Apache if you are new to the world of hosting . Here in this tutorial, we will learn the command to install and start Apache web server on Ubuntu 22.04 LTS Jammy JellyFish or Ubuntu 20.04 LTS focal fossa.

Apache is the most widely used web server on the Internet. And can be easily configured due to its flexible nature and ability to extend functionality using a large number of modules. It was developed and maintained under the Apache Foundation.

Usually Apache web server is installed with PHP and MySQL/MariaDB. However, Apache itself only provides static HTML pages. However, when combined with PHP (PHP Hypertext Preprocessor) as a scripting language, it can serve dynamic HTML pages generated from database content.

Most popular content management systems (CMS) such as WordPress or Joomla therefore also need PHP and the MySQL database. And regarding Apache installation, all popular Linux distributions such as RedHat, CentOS, Ubuntu Debian and others have Apache packages in their respective base repository, so user can install it with a single command. We can use Apache on Windows, but being an open source project and native to Linux systems and their dominance in the world of servers, hosting services only offer it on Linux to get the most out of this server software website.

While these developers are on Windows and working on a web project that requires Apache, usually opt for XAMPP or WampServer as the projects to get the whole LAMP stack up and running easily.

Steps to Install Apache Web Server on Ubuntu 22.04 | 20.04 LTS

What do you need to follow the steps in this tutorial?

• Ubuntu Server 22.04|20.4|18.04
• A non-root user with sudo rights
• Internet connection

1. Proper update

If you have just installed a new Ubuntu server on your local machine, Cloud or some VPS, run the system update command first. This will refresh the repository cache and install if any updates are available for your system.

sudo apt update

2. Command to Install Apache on Ubuntu 22.04 | 20.04 LTS

Then, as mentioned at the beginning of the tutorial, we don’t need to add any third-party repository because the package to install the Apache web server is already in the Ubuntu Linux base repository.

sudo apt install apache2

3. Start and enable the Apache service

After the installation is complete, first check whether the Apache service is already running in the background or not using the given command:

systemctl status apache2

If it is not running, start it and activate it with this command:

sudo systemctl enable --now apache2

4. Allow Port 80/443 on Ubuntu 22.04 | 20.04

Depending on where you installed your Ubuntu 20.04/22.04 server, we need to open the firewall for two ports 80 for HTTP and 443 for HTTPS. If you’re in the cloud, use your service provider’s network firewall to open these ports. Whereas, in case the system also has the firewall enabled, run:

Check whether the system firewall is active or not.

sudo ufw status

If the output is ‘active‘ then run

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

5. Access the demo webpage

Now that the Apache web server is up and running, it’s time to go to its demo web page to confirm that everything is working as perfectly as we want.

Open your browser on a system that can access the IP address or domain where you installed Apache. And point it to URL as given syntax:

http://server-ip-address

To replace the Server IP address with the real IP address of your Ubuntu Server 22.04 or 20.04. If you don’t know the address, in your server’s command terminal, run:

ip a

Reload Restart or Stop Apache Commands

There are many scenarios where after installing a web application, we either need to restart or stop the Apache web server service. And if you are in one of these scenarios, here are the commands to follow:

To recharge : This means, let the webserver keep running, but just refresh or reload the configuration file.

sudo systemctl reload apche2

To restart properly: In this case, Apache will leave the process threads when they are terminated and idle, then reload the configuration.

sudo /etc/init.d/apache2 graceful

Restart: This will kill all threads, stop the webserver and restart it.

sudo systemctl restart apache2

Stop: Completely stop the web server service.

sudo systemctl stop apache2

To start:

sudo systemctl start apache2

To deactivate Apache to start automatically with system startup, use:

sudo systemctl disable apache2

While to enable it again:

sudo systemctl enable apache2

Completely uninstall Apache from Ubuntu 22.04 or 20.04

To completely remove the web server, if you don’t need it, run:

sudo apt remove apache2*

For more information, see the official Apache documentation, while for more information, see how to install WordPress on an Apache server running on CentOS/AlmaLinux/Rocky/RHEL/Oracle.