To set up a virtual host on Amazon Linux 2023 (AL2023), you primarily use the Apache HTTP Server and configure it with virtual host directives in configuration files located in the directory.

Prerequisites –
- An EC2 instance running Amazon Linux 2023.
- An installed Apache web server. If not installed:
- Configured security group rules (inbound) to allow HTTP (port 80) and HTTPS (port 443) traffic.
- A registered domain name pointing to your EC2 instance’s public IP address.
Steps to Configure an Apache Virtual Host
- Navigate to the Apache configuration directory: The recommended practice on Amazon Linux is to create configuration files in , as Apache automatically includes files in this directory that end with a extension.
cd /etc/httpd/conf.d - Create a virtual host configuration file: Use a text editor like or to create a new configuration file for your domain (e.g., ).
sudo nano yourdomain.com.conf - Add the VirtualHost directives: Add the following configuration block to the file, replacing and with your actual domain name and desired document root path.
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/html/yourdomain.com
<Directory /var/www/html/yourdomain.com>
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/httpd/yourdomain.com-error.log
CustomLog /var/log/httpd/yourdomain.com-access.log combined
</VirtualHost> - Create the document root directory: Create the directory specified in the directive:
sudo mkdir -p /var/www/html/yourdomain.com - Place content in the document root (optional test): Add a simple HTML file to test the configuration.
sudo echo "Welcome to YourDomain.com" > /var/www/html/yourdomain.com - Verify Apache configuration syntax: Check for any errors in your configuration files before restarting the service.
sudo httpd -t - Restart Apache: Apply the changes by restarting the service.
sudo systemctl restart httpd
After these steps, your Amazon Linux 2023 instance should be configured to serve the website associated with from the specified document root. You can configure SSL/TLS for your virtual host using Certbot.