Linux httpd Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to install, configure, and manage the Apache HTTP Server on an Ubuntu 22.04 system. The lab covers the essential steps, including installing the Apache package, starting, stopping, and restarting the server, and configuring virtual hosts. The instructions provided are practical and can be easily followed, making this lab a valuable resource for anyone interested in web server administration using the Linux command-line interface.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/echo -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} linux/apt -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} linux/cd -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} linux/mkdir -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} linux/nano -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} linux/service -.-> lab-422726{{"`Linux httpd Command with Practical Examples`"}} end

Install Apache HTTP Server on Ubuntu 22.04

In this step, we will install the Apache HTTP Server on the Ubuntu 22.04 Docker container.

First, let's update the package index:

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

Next, install the Apache HTTP Server package:

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) ...

After the installation is complete, we can verify the Apache HTTP Server is running:

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 shows that the Apache HTTP Server is running and active.

Start, Stop, and Restart the Apache HTTP Server

In this step, we will learn how to start, stop, and restart the Apache HTTP Server.

First, let's start the Apache HTTP Server:

sudo systemctl start apache2

Example output:

To verify the Apache HTTP Server is running, we can use 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

Next, let's stop the Apache HTTP Server:

sudo systemctl stop apache2

Example output:

To verify the Apache HTTP Server is stopped, we can use the systemctl status command 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)

Finally, let's restart the Apache HTTP Server:

sudo systemctl restart apache2

Example output:

Verify the Apache HTTP Server is running 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 output shows that the Apache HTTP Server is running again after the restart.

Configure Apache Virtual Hosts

In this step, we will configure Apache virtual hosts to host multiple websites on the same server.

First, let's create two directories for our virtual hosts:

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

Next, create a default index.html file 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, let's configure the Apache virtual hosts. Open the default Apache configuration file:

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

Replace the contents of the file with the following:

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

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

Save and close the file.

Enable the new virtual host configurations:

sudo a2ensite 000-default.conf

Finally, restart the Apache HTTP Server to apply the changes:

sudo systemctl restart apache2

Now, you can visit http://example.com and http://example.org in your web browser to see the respective welcome pages.

Summary

In this lab, we learned how to install the Apache HTTP Server on Ubuntu 22.04, start, stop, and restart the server, and configure Apache virtual hosts. We first updated the package index and then installed the Apache HTTP Server package. We then explored commands to manage the Apache service, such as starting, stopping, and restarting it. Finally, we configured Apache virtual hosts to host multiple websites on the same server.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like