Apache (Ubuntu 22.04)

Apache (Ubuntu 22.04)

  • OS: ubuntu 22

Description

Apache is a free web server that can be installed on the Ubuntu 22.04 operating system. It is used to host websites and applications. Apache is extremely popular because it is very flexible and can be configured to work with different operating systems and programming languages. Apache is open source and has a large community of users who maintain and develop the software.

Software included

Package Version
Apache latest
Docker 3.20.10
Docker compose 2.12.2
containerd.io 1.6.10

Initial start of the service

This application uses Docker to provide faster startup of the underlying software (Apache) and direct use of it. This eliminates the need to familiarize yourself with its specifics in terms of configuration and installation. To start working directly with the application, all you have to do is upload the content that you want Apache to serve to the /opt/apache/data/ directory. Once you've done this, you can visit the IP address assigned to your virtual machine in your browser to view the content you've uploaded.

Note: By default, Apache can be accessed directly by IP address. To start using Apache with your real domain, you first need to point the domain in question to your IP address and change the settings for where Apache opens from through its admin panel.

Quick settings and frequently asked questions

  • Logging into the cloud service is done using an SSH key or password that you have specified.
  • Docker is installed as recommended by the official docker documentation.
  • The standard docker and docker compose commands are now available in this application.
  • When the virtual machine is first started, it takes a few minutes before a Docker container containing Apache is started.
  • Apache data is located in /opt/apache/data/.
  • By default, Apache runs on all available interfaces on the machine. Apache access ports are 80 and 443.

Additional Docker settings

Additional Apache settings that you can change and that are controlled directly by Docker are set as variable values in the /opt/docker-apache/docker-compose.yml configuration file:

Note: After changing any of the values, you must recreate the Docker container on which the application is running. For more information, please read the Recreating Docker containers section.

  • APACHE_HTTP_PORT_NUMBER: Internal port used by Apache to communicate between docker and the virtual machine for HTTP traffic. It doesn't need to be changed for it to work unless you want to change the behavior of the docker application. Default: 8080
  • APACHE_HTTPS_PORT_NUMBER: Internal port used by Apache to communicate between docker and the virtual machine for HTTPS traffic. It doesn't need to be changed for it to work unless you want to change the behavior of the docker application. Default: 8443

Adding a custom Apache vhost to the configuration

If you have multiple domains or want a specific configuration for a specific site, you have the flexibility to add your own virtual host configurations. To do this, follow these steps:

Step 1 - creating a file containing the configuration you want

Example contents of a configuration file my_vhost.conf which you can save anywhere, for example in /opt/apache-vhosts/my_vhost.conf:

<VirtualHost *:8080>
  ServerName www.example.com
  DocumentRoot "/app"
  <Directory "/app">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

Please note that this configuration file contains standard Apache configuration options. For more information, we advise you to familiarize yourself with the official documentation.

Step 2 - activating the new configuration file

  1. Open /opt/docker-apache/docker-compose.yml and add your new configuration file as an additional docker volume, e.g.:
---
version: '2'

services:
  apache:
    image: 'bitnami/apache:latest'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /opt/apache-vhosts/my_vhost.conf:/vhosts/my_vhost.conf:ro
  1. Restart your docker containers:
cd /opt/docker-apache/
docker compose restart

Change the full Apache configuration (http.conf)

In case you need to make a change to the main httpd.conf file, you always have the option to overwrite it with your own. To do this, follow the steps below:

Step 1 - upload your configuration file

You can upload it to /opt/apache-conf/httpd.conf, for example

Step 2 - enable httpd.conf in the configuration

  1. Open /opt/docker-apache/docker-compose.yml and add your new configuration file as an additional docker volume, e.g.:
---
version: '2'

services:
  apache:
    image: 'bitnami/apache:latest'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - /opt/apache-conf/httpd.conf:/opt/bitnami/apache/conf/httpd.conf
  1. Restart your docker containers:
cd /opt/docker-apache/
docker compose restart

SSL certificates

Use of own (paid) SSL certificates

If you already have ready-made, valid certificates, you need to make a few configuration modifications before you can use them. To achieve this, please follow these steps:

Step 1 - placing the certificates

Create a directory where the certificates will be stored:

mkdir /opt/certs/

Copy the certificates into the directory you just created and modify their permissions:

chown 1001:1001 /opt/certs/ -R

Step 2 - modify the Docker configuration

Open the docker configuration file /opt/docker-apache/docker-compose.yml with a text editor and find the following snippet:

  apache:
    image: docker.io/bitnami/apache:2.4
    ports:
      - 80:8080
      - 443:8443
    volumes:
      - '/opt/apache/data:/app'

Edit it so it looks like this:

  apache:
    image: docker.io/bitnami/apache:2.4
    ports:
      - 80:8080
      - 443:8443
    volumes:
      - '/opt/apache/data:/app'
      - '/opt/certs/cert.pem:/opt/bitnami/apache/conf/server.crt'
      - '/opt/certs/privkey.pem:/opt/bitnami/apache/conf/server.key'
      - '/opt/certs/ca-bundle.pem:/opt/bitnami/apache/conf/server-ca.crt'

Note that /opt/certs/cert.pem is the certificate path, /opt/certs/privkey.pem is the private key path, and /opt/certs/ca-bundle.pem is the CA bundle/Intermediate chain. You can name the files and directories as you wish, but they must be specified correctly in the configuration.

Step 3 - restart the Docker containers

cd /opt/docker-apache/
docker compose up -d

Issuing certificates with Letsencrypt

If you wish, you can always issue free certificates from Letsencrypt as well. To do this, please follow the steps below:

Step 1 - install the necessary packages

Login to your server as root and run the following commands:

apt-get update
apt-get install certbot

Step 2 - issuance of the certificate

Please note that in order for a free certificate to be issued, you must be able to verify your domain ownership in some way. Letsencrypt provides several options for verifying your domains, in this example we will use verification by running a temporary web server.

To do this, temporarily stop the container with the docker stop docker-apache-apache-1 command. Important: Performing this step will temporarily stop your site from working. Make sure you perform this action at a time when it will least affect your users.

Issue the certificate with the command certbot certonly --standalone --preferred-challenges http -d example.com, replacing "example.com" with the domain you pointed to your Cloud server IP address.

Step 3 - put the new certificates in the configuration

Open the docker configuration file /opt/docker-apache/docker-compose.yml with a text editor and find the following snippet:

  apache:
    image: docker.io/bitnami/apache:2.4
    ports:
      - 80:8080
      - 443:8443
    volumes:
      - '/opt/apache/data:/app'

Edit it so it looks like this:

  apache:
    image: docker.io/bitnami/apache:2.4
    ports:
      - 80:8080
      - 443:8443
    volumes:
      - '/opt/apache/data:/app'
      - '/etc/letsencrypt/live/example.com/cert.pem:/opt/bitnami/apache/conf/server.crt'
      - '/etc/letsencrypt/live/example.com/privkey.pem:/opt/bitnami/apache/conf/server.key'
      - '/etc/letsencrypt/live/example.com/chain.pem:/opt/bitnami/apache/conf/server-ca.crt'

With this modification, you end up "binding" the external certificates issued by Letsencrypt to the configuration that resides inside the docker container. Replace "example.com" with your domain name. After you change the configuration, you need to fix the certificate permissions with the command chown 1001:1001 /etc/letsencrypt/archive/example.com/ -R, again replacing "example.com" with your domain name.

Step 4 - start the services

Since the docker container is temporarily stopped, it needs to apply the new configuration and start it. Please run the following commands:

cd /opt/docker-apache/
docker compose up -d

Step 5 (optional) - automatic certificate renewal

Automatic renewal can be arranged via cron. You can set the following cron configuration to make auto-renewal happen:

# Automated Letsencrypt renewal
0 0 * * * /usr/bin/certbot renew --pre-hook 'docker stop docker-apache-apache-1' --post-hook 'chown 1001:1001 /etc/letsencrypt/archive/example.com/ -R; docker start docker-apache-apache-1'

Of course, you can change the runtime whenever you want. In the above example, this task will run every day at 00:00.

Working with Docker

The organization of this application is entirely done using Docker. In this way, we can provide you with ready-made applications faster. In addition, their configuration is more flexible and quite controllable. To be able to control this application, however, you need to know some basic Docker functionalities and features.

Restarting all services

Restarting the database and the Docker container that contains Apache itself, along with the rest of the software, can be done in two ways:

  1. By restarting the entire virtual machine
  2. Restarting the Docker containers, which is the faster option. To do this, access your machine via SSH, and run the following commands:
sudo su -
cd /opt/docker-apache
docker compose restart

Recreating Docker containers

Recreation of a docker container may be necessary if you've changed the configuration inside the docker-compose.yml file. If there are changes, please follow these steps:

cd /opt/docker-apache/
docker compose stop
docker compose up -d

Checking the status of the Docker application

To check, you need to run the following commands:

sudo su -
docker ps -a

Please pay attention to the STATUS column. It should show how long the service has been running, for example Up X minutes. If the status is Restarting then you need to check what is wrong with the application. In such a case, you can check by following the application logs as follows:

  1. Get the application name that you can see in the NAMES column from the output of the docker ps -a command. View the logs with the docker logs <NAME> command, where is the name of the container.

Changing settings

The settings in section Additional Docker settings can be set in the docker-compose.yml file. The path to it is described again in this section. To change a setting, open the configuration file and note the environment section under apache. Each configuration option must be described on a new line with a dash in front, as in the following example:

    environment:
      - APACHE_HTTP_PORT_NUMBER=8080

Please note that the correct indentation (number of spaces) must also be observed, as this is important for yaml configuration files.