httpd Command in Linux

Introduction

In this guide, you will discover how to install, set up, and administer the Apache HTTP Server on an Ubuntu 22.04 system. This tutorial covers the necessary steps, including installing the Apache package, initiating, halting, and restarting the server, and configuring virtual hosts for multiple domains. The instructions are practical and easy to understand, making this a valuable resource for anyone looking to learn web server administration using the Linux command-line interface.

Install Apache HTTP Server on Ubuntu 22.04

This section details how to install the Apache HTTP Server on an Ubuntu 22.04 system.

First, refresh your package lists:

sudo apt-get update

Example output:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done

Now, install the apache2 package which contains the Apache HTTP Server:

sudo apt update
sudo apt-get install -y apache2

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libexpat1 libxml2 procps
Suggested packages:
  www-browser
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libexpat1 libxml2 procps
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,679 kB of archives.
After this operation, 7,542 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 libapr1 amd64 1.7.0-6ubuntu0.22.04.1 [94.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy/main amd64 libaprutil1 amd64 1.6.1-4ubuntu2 [87.1 kB]
...
Selecting previously unselected package apache2.
(Reading database ... 14342 files and directories currently installed.)
Preparing to unpack .../apache2_2.4.52-1ubuntu4.1_amd64.deb ...
Unpacking apache2 (2.4.52-1ubuntu4.1) ...
Selecting previously unselected package apache2-bin.
Preparing to unpack .../apache2-bin_2.4.52-1ubuntu4.1_amd64.deb ...
Unpacking apache2-bin (2.4.52-1ubuntu4.1) ...
...
Setting up apache2 (2.4.52-1ubuntu4.1) ...
Creating config file /etc/apache2/apache2.conf with new version
Creating config file /etc/apache2/ports.conf with new version
...
Processing triggers for systemd (249.11-0ubuntu3.6) ...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for ufw (0.36.1-2ubuntu1) ...

Once the installation is complete, confirm that the Apache HTTP Server is running by checking its status:

sudo systemctl status apache2

Example output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-28 06:29:07 UTC; 10s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 1196 (apache2)
      Tasks: 55 (limit: 4686)
     Memory: 7.1M
        CPU: 86ms
     CGroup: /system.slice/apache2.service
             ├─1196 /usr/sbin/apache2 -k start
             ├─1197 /usr/sbin/apache2 -k start
             └─1198 /usr/sbin/apache2 -k start

The output above indicates the Apache HTTP Server is up and running.

Start, Stop, and Restart the Apache HTTP Server

This section explains how to control the Apache HTTP Server using systemd commands, including starting, stopping, and restarting the service.

To start the Apache HTTP Server, execute the following command:

sudo systemctl start apache2

Example output:

You can then verify that the Apache HTTP Server has started correctly by using the systemctl status command:

sudo systemctl status apache2

Example output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-28 06:29:07 UTC; 10s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 1196 (apache2)
      Tasks: 55 (limit: 4686)
     Memory: 7.1M
        CPU: 86ms
     CGroup: /system.slice/apache2.service
             ├─1196 /usr/sbin/apache2 -k start
             ├─1197 /usr/sbin/apache2 -k start
             └─1198 /usr/sbin/apache2 -k start

To stop the Apache HTTP Server, use the following command:

sudo systemctl stop apache2

Example output:

To confirm the server is no longer running, use systemctl status again:

sudo systemctl status apache2

Example output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: inactive (dead) since Fri 2023-04-28 06:29:17 UTC; 5s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
    Process: 1321 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
   Main PID: 1196 (code=exited, status=0/SUCCESS)

To restart the Apache HTTP Server, which is useful for applying configuration changes, use:

sudo systemctl restart apache2

Example output:

Confirm Apache HTTP Server is active again:

sudo systemctl status apache2

Example output:

● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-04-28 06:29:22 UTC; 5s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Process: 1190 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
    Process: 1321 ExecStop=/usr/sbin/apachectl stop (code=exited, status=0/SUCCESS)
    Process: 1358 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
   Main PID: 1364 (apache2)
      Tasks: 55 (limit: 4686)
     Memory: 7.1M
        CPU: 86ms
     CGroup: /system.slice/apache2.service
             ├─1364 /usr/sbin/apache2 -k start
             ├─1365 /usr/sbin/apache2 -k start
             └─1366 /usr/sbin/apache2 -k start

The Apache HTTP Server should now be running following the restart command.

Configure Apache Virtual Hosts

This section walks you through setting up Apache virtual hosts, allowing you to host several websites on the same physical server by using different domain names. This is a common task for a systemadmin.

Create directories to store the web files for your virtual hosts:

sudo mkdir -p /var/www/example.com /var/www/example.org

Create basic index.html files to serve as default content for each virtual host:

echo "<h1>Welcome to example.com</h1>" | sudo tee /var/www/example.com/index.html
echo "<h1>Welcome to example.org</h1>" | sudo tee /var/www/example.org/index.html

Now, configure the virtual hosts within Apache. Open the default Apache configuration file:

sudo nano /etc/apache2/sites-available/000-default.conf

Replace the original file content with the following virtual host configurations:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example.com
</VirtualHost>

<VirtualHost *:80>
    ServerName example.org
    DocumentRoot /var/www/example.org
</VirtualHost>

Save the file and exit the text editor.

Enable the newly configured virtual hosts:

sudo a2ensite 000-default.conf

Finally, restart the Apache HTTP Server for the changes to take effect:

sudo systemctl restart apache2

You can now access http://example.com and http://example.org in your web browser to view the welcome pages of each virtual host. Note that you'll likely need to modify your local machine's hosts file or configure DNS for these domains to resolve to your server's IP address.

Summary

This lab provided a comprehensive guide to installing the Apache HTTP Server on Ubuntu 22.04. You learned how to install the Apache package, manage the Apache service (start, stop, restart), and set up Apache virtual hosts to host multiple websites on a single server. The process started with updating the package index, installing the `apache2` package as root using `sudo`, and then using `systemctl` to manage the service. Finally, you gained hands-on experience configuring virtual hosts, which is a fundamental skill for any aspiring systemadmin working with Linux servers.

400+ Linux Commands