How to Install Apache2 on Ubuntu

Here is how you can install Apache2 on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 / 18.04 LTS / 18.10 / 20.04 / 22.04.

Prerequisites

  • Have an Ubuntu 16.04 x64 / 18.04 x64 / 18.10 x64 / 20.04 x64 / 22.04 x64 instance.
  • Logged in as a root with sudo privileges.

Below are the steps we need to follow to install Apache2:

Install Apache2 on Ubuntu

Before to install Apache2 server, run the following commands first to update Ubuntu:

sudo apt update
sudo apt upgrade

Next, install Apache web server:

sudo apt install apache2

That’s it, Apache2 is installed and active. You can check the Apache2 service status by using the following command:

sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-04-16 00:35:04 UTC; 1h 30min ago
  Process: 1376 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
  Process: 1381 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 1396 (apache2)
    Tasks: 11 (limit: 502)
   Memory: 84.1M
   CGroup: /system.slice/apache2.service
           ├─1396 /usr/sbin/apache2 -k start
           ├─2159 /usr/sbin/apache2 -k start
           ├─2160 /usr/sbin/apache2 -k start
           ├─2161 /usr/sbin/apache2 -k start
           ├─2162 /usr/sbin/apache2 -k start
           ├─2163 /usr/sbin/apache2 -k start
           ├─2164 /usr/sbin/apache2 -k start
           ├─2165 /usr/sbin/apache2 -k start
           ├─2166 /usr/sbin/apache2 -k start
           ├─2167 /usr/sbin/apache2 -k start
           └─2198 /usr/sbin/apache2 -k start

Apr 16 00:35:04 yourdomain.com systemd[1]: Starting The Apache HTTP Server...
Apr 16 00:35:04 yourdomain.com systemd[1]: Started The Apache HTTP Server.

The other following commands used to stop, start and enable Apache2 service.

sudo systemctl stop apache2.service
sudo systemctl start apache2.service
sudo systemctl enable apache2.service

Note: Ubuntu 22.04 uses the UFW firewall by default, which stands for “Uncomplicated Firewall.” When the firewall is enabled, it will block all incoming connections. To allow incoming connections on certain ports, you have to configure the UFW firewall.

To open ports for a Apache2 web server, you have to execute the following command:

sudo ufw allow in "Apache Full"

Now to check your current firewall configuration settings, please run the following command:

sudo ufw status verbose

and the output will be:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22                         ALLOW IN    Anywhere
80,443/tcp (Apache Full)   ALLOW IN    Anywhere
22 (v6)                    ALLOW IN    Anywhere (v6)
80,443/tcp (Apache Full (v6)) ALLOW IN    Anywhere (v6)

Verifying the Apache2 Installation

To verify that Apache2 working, open your browser, write your domain name or server IP address i.e. http://YOUR_IP_OR_DOMAIN/and you will see the following default Ubuntu Apache welcome page.

Apache Installation Verification

Apache Configuration File’s Structure and Best Practices

  • All Apache configuration files are located in the /etc/apache2 directory.
  • apache2.conf is the main configuration file. It puts the pieces together by including all remaining configuration files when starting up the web server.
  • /etc/apache2/ports.conf. is always included from the main configuration file. It is used to determine the listening ports for incoming connections, and this file can be customized anytime.
  • Apache Virtual Hosts files are stored in /etc/apache2/sites-available directory. The configuration files found in this directory are not used by Apache unless they are linked to the /etc/apache2/sites-enabled directory.
  • They are activated by symlinking available configuration files from their respective *-available/ counterparts. These should be managed by using our helpers a2enmod, a2dismod, a2ensite, a2dissite, and a2enconf, a2disconf. See their respective man pages for detailed information.
  • It is a good idea to follow a standard naming convention. For example, if your domain name is yourdomain.com then the virtual host configuration file should be named /etc/apache2/sites-available/yourdomain.com.conf
  • Files containing global configuration fragments are stored in the /etc/apache2/conf-available/ directory. Files in the conf-available directory can be enabled by creating a symlink to the /etc/apache2/conf-enabled/ with the a2enconf command and disabled with the a2disconf command.
  • Apache log files (access.log and error.log) are located in the /var/log/apache/ directory. It is recommended to have different access and error log files for each vhost.
  • The default Ubuntu document root is /var/www/html. You can make your own virtual hosts under /var/www.

Conclusion

You have learned how to install Apache2 on Ubuntu server. You can repeat the steps we outlined above and install Apache2 on additional new Ubuntu servers.

If you have a question or facing any problems, feel free to leave a comment.

Related Post

How to Install MariaDB on Ubuntu

Here is how you can install MariaDB on Ubuntu. In particular on Vultr’s instance i.e. Ubuntu 16.04 LTS / 18.04 LTS / 20.04 LTS / 22.04 LTS.

How to Switch WordPress from PHP 7.2-FPM to PHP 7.3-FPM on Ubuntu 19.10 with Nginx

Here is you will learn how to switch WordPress from PHP 7.2-FPM to PHP 7.3-FPM on Ubuntu 19.10. In particular on Vultr‘s instance i.e. Ubuntu 19.10.

Leave a Reply