# A Beginner's Guide to Install and Configure Apache 2 Web Server on Ubuntu 22.04

As the internet continues to grow and expand, the need for web servers becomes increasingly important. Web servers are the backbone of the Internet, and they enable websites and web applications to be accessed by users all over the world. One of the most popular web servers available today is Apache, which is open-source, free to use, and widely supported. In this beginner's guide, I will walk you through the steps to install and configure Apache 2 on Ubuntu 22.04.

### Video Tutorial

%[https://www.youtube.com/watch?v=YeoDypn2rkM] 

### Pre-requirements

* A server running Ubuntu 22.04
    
    (Get free $100 on your [Digitalocean](https://cutt.ly/digitaloceanfree100) account to deploy your first server)
    
* A non-root user with sudo privileges
    
* A basic understanding of the Linux command line
    

### Installation Steps

**Step 1: SSH into your server**

In order to perform the installation and configuration steps we need to log in or the server via ssh. For login, you will be needing a terminal or PowerShell. So please open it. There are 2 methods that we can use to log in to the server via ssh.

* **Method 1: Via Password**
    
    To log in using a password run this in your terminal/PowerShell
    
    ```bash
    ssh username@server_ip_address_here
    ```
    
    Then it will ask for your password please provide your password for the server.
    
* **Method 2: Via SSH Key**
    
    If you have set up an ssh key instead of a password for login then you need to run this in your terminal/PowerShell.
    
    ```bash
    # This is only for the 1st time
    chmod 400 /path/to/the/private_key.pem
    
    # This is for every time
    ssh -i "/path/to/the/private_key.pem" username@server_ip_address_here
    ```
    

> Note: If you use different port than `22` Then you need to use the `-p` flag after the ssh commands.
> 
> For example:
> 
> ```bash
> # In this example I have used port 2022 as the ssh port
> ssh username@server_ip_address_here -p2022
> ```

**Step 2: Update Ubuntu**

Before we begin, it is essential to make sure that your Ubuntu system is up-to-date. This can be done using the following commands:

```bash
sudo apt update
sudo apt upgrade
```

The first command updates the package list, and the second command upgrades any outdated packages to their latest version.

**Step 3: Install Apache 2**

Now that we have updated Ubuntu, it's time to install Apache 2. This can be done using the following command:

```bash
sudo apt install apache2
```

Once the installation is complete, you can verify that Apache 2 is running by entering your server's IP address in your web browser. You should see the Apache 2 default page.

### **Configuration Steps**

**Step 1: Configure Apache 2**

Now that we have installed Apache 2, it's time to configure it. The Apache configuration file is located at /etc/apache2/apache2.conf. You can open it using a text editor like `nano` or `vim`:

```bash
sudo nano /etc/apache2/apache2.conf
```

Here are some key configurations to pay attention to:

`ServerName` - This directive should be set to your server's domain name or IP address.

`DocumentRoot` - This directive sets the location of your website's files.

`DirectoryIndex` - This directive sets the default page that is served when a user visits your website. By default, Apache 2 serves index.html.

No changes are needed here. But still if you want you can change some options. Once you have made your changes, save the file and exit.

**Step 2: Create a Virtual Host**

If you want to host multiple websites on your server, you will need to create a virtual host. A virtual host allows Apache 2 to serve multiple websites on a single server. To create a virtual host, create a new file in the `/etc/apache2/sites-available/` directory:

```bash
sudo nano /etc/apache2/sites-available/example.com.conf
```

Here is an example of a virtual host configuration:

```apache
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
```

In this example, I have created a virtual host for `example.com`. I have set the `ServerName` and `ServerAlias` directives to point to our website's domain name. The `DocumentRoot` directive sets the location of our website's files. Finally, the `ErrorLog` and `CustomLog` directives set the location of Apache 2's log files.

Once you have created your virtual host configuration, you will need to enable it using the following command:

```bash
sudo a2ensite example.com.conf
```

This command creates a symbolic link in the `/etc/apache2/sites-enabled/` directory to your virtual host configuration.

**Step 3: Restart Apache 2**

Now that we have made our changes to the Apache 2 configuration, we need to restart the service for the changes to take effect. This can be done using the following command:

```bash
sudo systemctl restart apache2
```

This command restarts the Apache 2 service.

**Step 4: Configure Firewall**

If you are running a firewall on your server, you will need to configure it to allow traffic to your web server. By default, Ubuntu 22.04 comes with a firewall called UFW (Uncomplicated Firewall). You can check if UFW is running using the following command:

```bash
sudo ufw status
```

> Attention!!!
> 
> Please allow your ssh port before enabling the UFW or else you will lose access of your server.
> 
> ```bash
> # For default port
> sudo ufw allow 22
> 
> # For custom port
> sudo ufw allow custom_port_number_here
> ```

If UFW is not running, you can enable it using the following command:

```bash
sudo ufw enable
```

To allow incoming HTTP traffic, you can run the following command:

```bash
sudo ufw allow http
```

To allow incoming HTTPS traffic, you can run the following command:

```bash
sudo ufw allow https
```

**Step 5: Test Your Web Server**

Now that you have installed and configured Apache 2, it's time to test your web server. Open your web browser and enter your server's IP address or domain name. You should see your website's default page or the page for the virtual host you created earlier.

Congratulations! You have successfully installed and configured Apache 2 on Ubuntu 22.04.

### Conclusion

In this beginner's guide, I have walked you through the steps to install and configure Apache 2 on Ubuntu 22.04. I covered key configurations such as the `ServerName`, `DocumentRoot`, and `DirectoryIndex` directives, and I showed you how to create a virtual host for hosting multiple websites. I also covered how to restart Apache 2 and configure the firewall to allow incoming HTTP and HTTPS traffic. With this knowledge, you should be able to set up a basic web server on Ubuntu 22.04 using Apache 2.
