LinuxWeb Servers

Host websites on Apache Virtual Hosts With SSL

In this guide, we are going to use an Ubuntu 22 LTS instance with SSL Apache Virtual Hosts to host a website using Apache and PHP. This guide is based on our own configuration for blog.f2h.cloud. Our WordPress cluster has 8 NVMe VPS Servers. Instances are installed with Apache, PHP 8.1 and MySQL. The cluster is configured with MySQL Replication which distributes database changes to the 7 slaves. In this type of setup, we can lose 7 servers and still be online. The downside is that if the master is ever down, you can’t make database changes until it’s back online. You can use this guide to host one website or many websites from the same server. Or you can create a cluster like we have.

Install Apache, PHP and MySQL

WordPress Servers require either Apache or Nginx. PHP and MySQL to function. We’re using Apache because it’s easier to host multiple websites. But we will install PHP 8.1 and MySQL 8 too. Issue the commands below to host your first website on Apache.

# Initial Update
apt update && apt upgrade -y

# Install Dependencies
apt install software-properties-common ca-certificates lsb-release apt-transport-https
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php

# Update
apt update

# Install Dependancies 
apt install mysql-server apache2 php8.1 php8.1-bcmath php8.1-bz2 php8.1-common php8.1-curl php8.1-dba php8.1-enchant php8.1-gd php8.1-http  php8.1-imagick php8.1-imap php8.1-mbstring php8.1-mcrypt php8.1-memcache php8.1-memcached php8.1-mysql php8.1-opcache php8.1-soap php8.1-zip php8.1-xml

Create Domain SSL Apache Virtual Hosts

Apache requires the information of our domain and the location of our website’s files. This assumes you already have an SSL certificate. We are using a Cloudflare Origin Certificate. You could also use Certbot to install an SSL. Name your configuration file after the domain you will be hosting on your Apache server.

nano /etc/apache2/sites-available/blog.f2hcloud.conf

Inside the file, specify the virtual hosts. Switch the information for your domain and SSL information.

<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName f2h.cloud
        ServerAlias www.f2h.cloud
        DocumentRoot /home/f2hcloud/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

# Certificate Locations

        SSLEngine on
        SSLCertificateFile /network/cert.pem
        SSLCertificateKeyFile /network/key.pem
        SSLCertificateChainFile /network/origin.pem

    <Directory /home/f2hcloud/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

</VirtualHost>


# Configure traffic on port 80, insecure.

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName f2h.cloud
    ServerAlias www.f2h.cloud
    DocumentRoot /home/f2hcloud/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

# Disallow View site by server IP

<VirtualHost IPv4:80>
   DocumentRoot /var/www/blank
</VirtualHost>

Enable Access & Modules

Next, we need to ensure all of the services and modules needed to host a website with SSL on Apache hosts are loaded. The following commands assume your website’s files are located at /var/www/html. So ensure you specify the correct path to your website’s file.

# Allow handler access

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

# Enable Apache Modules
a2enmod headers ssl rewrite php8.1

# Enable Website In Apache Virtual Hosts. Specify your .conf created in
# prev step

a2ensite blog.f2h.conf

# Disable Default Website

a2dissite 000-default.conf

# Restart Apache

systemctl reload apache2

Configure Apache Virtual Hosts

host ssl website linux

So now we have created our virtual host’s file for the domain we want to host. Next, we need to make Apache is configured correctly. Inside the /etc/apache2/apache2.conf file. Find the <Directory /> line and make sure Require all say’s granted. You should also make sure the Allowoverride statement is set to ALL Save and close the file. Without this change, a permission denied error will be seen frontend.

nano /etc/apache2/apache2.conf

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Close the file and restart apache.

systemctl restart apache2

So now if you visit the IP address of your server, you should see a holding page. You would now add the relevant A records for your domain and upload your files to the /var/www/html folder. To host more sites, create more SSL Apache virtual host files and enable the configuration with a2ensite.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button