Linux Administration: Configure Apache Web Server and Name-based Virtual Hosting

Update: this project has been optimized and containerized. See this UbuntuLAMP project on my github.

Related Articles:

Configure the web server:

# Start a Ubuntu container:
docker run -dit --name web-server --network docker_dev_net --ip 192.168.1.103 --mount source=my_smb_vol,destination=/var/www/html ubuntu

# NOTE: Originally, I used underscores in my container names and hostnames, but it was causing issues in some scenarios where underscores are considered invalid or get misinterpreted. So hyphens are used instead.

# Enter the container's shell prompt:
docker exec -it web-server bash

# Install network utilities for troubleshooting:
apt-get install iputils-ping iproute2 traceroute -y

# Install SSH:
apt-get install openssh-client openssh-server -y

# Install sudo and other utilities:
apt-get install sudo nano -y

# Install apache, php, and php-mysql:
apt-get install apache2 php8.1 php-mysql libapache2-mod-php -y

Verify that the services are running and start the them if not:

# ssh and apache2 service status
service ssh status
service apache2 status

# start the services:
service ssh start
service apache2 start

Configure hostname and name server (optional):

# Depending on your setup, you might need to specify the --hostname when creating your container:

--hostname=<your-hostname>

Verify PHP version:

# Create a info.php file under the /var/www/html directory with the following content:

<?php phpinfo();?>

Now that you should be able to visit the Apache server’s default landing page to verify that the Apache server is running and the info.php page to view your PHP version.

Configure name-based virtual hosting:

# The example websites I'm using are: example.mydomain.com, example1.mydomain.com, example2.mydomain.com.

# Create directories:

mkdir /var/www/html/example1.mydomain.com
mkdir /var/www/html/example2.mydomain.com

# example.mydomain.com will reside under /var/www/html as the "main" website.

# Change ownership of the directories accordingly. Here I changed the entire html directory and its subdirectories' ownership to www-data for convenience.

chown -R www-data:www-data /var/www/html

# Create Virtual Host configuration files for your sites.

touch /etc/apache2/sites-available/example.mydomain.com.conf
touch /etc/apache2/sites-available/example1.mydomain.com.conf
touch /etc/apache2/sites-available/example2.mydomain.com.conf

# example.mydomain.com.conf or use the 000-default.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@example.mydomain
    ServerName example.mydomain.com
    DocumentRoot /var/www/html
    DirectoryIndex index.html
    ErrorLog ${APACHE_LOG_DIR}/example.mydomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example.mydomain.com_access.log combined
</VirtualHost>

# example1.mydomain.com.conf and example2.mydomain.com.conf:
<VirtualHost *:80>
    ServerAdmin webmaster@example1.mydomain.com
    ServerName example1.mydomain.com
    DocumentRoot /var/www/html/example1.mydomain.com
    DirectoryIndex index.php
    ErrorLog ${APACHE_LOG_DIR}/example1.mydomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example1.mydomain.com_access.log combined
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@example2.mydomain.com
    ServerName example2.mydomain.com
    DocumentRoot /var/www/html/example2.mydomain.com
    DirectoryIndex index.php
    ErrorLog ${APACHE_LOG_DIR}/example2.mydomain.com_error.log
    CustomLog ${APACHE_LOG_DIR}/example2.mydomain.com_access.log combined
</VirtualHost>

# Enable the configuration files for the sites:

a2ensite example1.mydomain.com
a2ensite example2.mydomain.com

# Restart apache2 service and verify that apache2 is running:

service apache2 reload
service apache2 status

# Of course, you will need to add your own index.html or index.php file under the sites' directories.

Configure DNS records:

# I'm using my Cisco router as an example:

ip host example.mydomain.com 192.168.1.103
ip host example1.mydomain.com 192.168.1.103
ip host example2.mydomain.com 192.168.1.103

Now that you should be able to visit the sites by going to:

http://example.mydomain.com
http://example1.mydomain.com
http://example2.mydomain.com