Monday, October 3, 2022

How To Install Apache web server on Ubuntu 22.04

In this guide on the orcacore website, we want to teach you How To Install the Apache web server on Ubuntu 22.04.

Apache HTTP server is the most widely used web server software. Developed and maintained by Apache Software Foundation, Apache is open-source software available for free.

Requirements

Before we start to teach you how to install Apache you need to log in to your server with a non-root user with sudo privileges. To do this, you can check our article about the Initial server setup with Ubuntu 22.04.

Also, you need to have a domain name that pointed to your server’s IP address.

How to Install Apache web server on Ubuntu 22.04

After you finish the Initial server setup with Ubuntu 22.04 you can easily install Apache on your server.

Install Apache on Ubuntu 22.04

Apache is available in Ubuntu’s default software repository. you just need to update the local package index and then install the apache.

Run the following command to update the packages:

sudo apt update

Then, install the apache2 package :

sudo apt install apache2

After you install Apache on Ubuntu 22.04 you need to adjust the firewall.

Adjust the firewall on Ubuntu 22.04

Apache registers itself with UFW to provide a few application profiles that can be used to enable or disable access to Apache through the firewall.

You list available applications with the following command:

sudo ufw app list

Your output should be like this:

Output:
Available applications:
Apache
Apache full
Apache secure
OpenSSH

Allow Apache with the following command:

sudo ufw allow 'Apache'

You can see the active status and a List of allowed HTTP traffic with the following command:

sudo ufw status
Output
Status: active
To Action From
-- ------ ----
Apache ALLOW Anywhere
Apache (v6) ALLOW Anywhere (v6)

At this point,  you install Apache on Ubuntu 22.04 and now you can check your web server.

Check your Apache Webserver

You can check that the Apache service is running with the following command:

sudo systemctl status apache2
Output
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor prese>
Active: active (running) 
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 1926 (apache2)
Tasks: 55 (limit: 2282)
Memory: 6.6M
CGroup: /system.slice/apache2.service
├─1926 /usr/sbin/apache2 -k start
├─1928 /usr/sbin/apache2 -k start
└─1929 /usr/sbin/apache2 -k start

It’s better to request a page from Apache to test it.  for doing this you need your server’s IP address. you can get your IP with the following command:

hostname -I

Or you can use the icanhazip tool to get your IP. run the following command:

curl -4 icanhazip.com

Enter your server’s IP address in your favorite browser.

http://your_server_ip

you will see the page like the image below:

Apache web server default page

When you see this page it means that the Apache is working correctly. Here we go to see some basic information about the Apache process.

Mange The Apache web server process on Ubuntu 22.04

When you are done with the install Apache, you can learn some basic management commands.

You can stop your web server with the following command:

sudo systemctl stop apache2

To start it again you can use:

sudo systemctl start apache2

Also, you can stop and then start the service with:

sudo systemctl restart apache2

If you have made some configuration changes you need to reload your service after doing that with the following command:

sudo systemctl reload apache2

To disable the service run the following command:

sudo systemctl disable apache2

Then you can re-enable it to start the service every time the server boots:

sudo systemctl enable apache2

Set up Apache virtual hosts on Ubuntu 22.04

When using the Apache web server, you can use virtual hosts (similar to server blocks in Nginx) to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name.

Now follow the steps below:

1)Create a directory for your domain:

sudo mkdir /var/www/your_domain

2)Set ownership of the HTML directory with the $USER environmental variable:

sudo chown -R $USER:$USER /var/www/your_domain

3)To set up an Apache virtual host you need to set default permissions for your webroot:

sudo chmod -R 755 /var/www/your_domain

4)create a sample index.html page using Vi editor:

sudo vi /var/www/your_domain/index.html

Type this in your editor:

<html>
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>

Save and close the file when you are finished.

Then continue to set up Apache virtual host.

5)You need to create a sites-available directory:

sudo vi /etc/apache2/sites-available/your_domain.conf

Type this in your editor:

<VirtualHost *:80>
ServerAdmin [email protected]
ServerName your_domain
ServerAlias www.your_domain
DocumentRoot /var/www/your_domain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Save and close the file when you are finished.

6)Enable the file with the a2ensite tool:

sudo a2ensite your_domain.conf

7)Disable the default site defined in 000-default.conf:

sudo a2dissite 000-default.conf

8)test for configuration errors:

sudo apache2ctl configtest

You should see the following output:

Syntax OK

9)Restart your web server with the following command:

sudo systemctl restart apache2

Now you are ready to test your Apache virtual host on Ubuntu 22.04 configuration.

http://your_domain

You will see your Apache virtual host test page like the image below:

Apache web server virtual hosts

Conclusion

At this point, you learn to Install Apache Web Server on Ubuntu 22.04. You also learn to set up the Apache virtual hosts.

Hope you enjoy it.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles