MHOSTMHOST

Apache: install and virtual hosts

In this article you'll install the Apache web server on your mHost VPS, set up a virtual host (VirtualHost) for a site, check the configuration, and open the necessary firewall ports — ending up with a working Apache that serves your site over HTTP.

What you'll need

  • SSH access to the server as root or as a user with sudo.
  • An mHost VPS on one of the supported OSes: Ubuntu 22.04/24.04, Debian 11/12, AlmaLinux/Rocky Linux 8/9.
  • (optional) a domain name whose A record points at the server's IP — for testing, the IP alone works fine.
Tip: if you need not just the web server but the whole stack — Apache + MySQL + PHP (plus Nginx) — you can use the ready-made LAMP recipe in VMmanager 6 (see the article "Ready-made VMmanager 6 recipes"; to run it, first log in to the panel — see "How to log in to VMmanager 6"). Below is how to install and configure just Apache by hand.

Step 1. Install Apache

Debian / Ubuntu

The package is called apache2:

bash
sudo apt update
sudo apt install apache2

AlmaLinux / Rocky Linux

The package is called httpd:

bash
sudo dnf install httpd

Step 2. Enable and start Apache

Debian / Ubuntu

bash
sudo systemctl enable --now apache2
sudo systemctl status apache2

AlmaLinux / Rocky Linux

bash
sudo systemctl enable --now httpd
sudo systemctl status httpd

The --now flag both enables autostart on boot and starts the service right away. The status output should show active (running). Open http://<IP>/ (your server's IP address) in a browser — you should see Apache's default test page.

Step 3. Create a virtual host (VirtualHost)

A virtual host describes a single site on the server: its own DocumentRoot (file directory), domain name, and logs. Below is an example for the domain example.com — replace it with your own.

Create a directory for the site's files and a test page (the same on every system):

bash
sudo mkdir -p /var/www/example.com/public_html
echo "<h1>It works: example.com</h1>" | sudo tee /var/www/example.com/public_html/index.html

Debian / Ubuntu

Create the file /etc/apache2/sites-available/example.com.conf:

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

and paste into it:

bash
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>

AlmaLinux / Rocky Linux

Create the file /etc/httpd/conf.d/example.com.conf:

bash
sudo nano /etc/httpd/conf.d/example.com.conf

and paste into it (the log paths differ — there's no APACHE_LOG_DIR variable here):

bash
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com
    DocumentRoot /var/www/example.com/public_html

    <Directory /var/www/example.com/public_html>
        Options FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog /var/log/httpd/example.com-error.log
    CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
Tip: AllowOverride All lets you override settings via .htaccess in the site's directory (handy for CMSes/apps later on); without the Require all granted line, Apache 2.4 will answer 403 Forbidden for every request to this directory.

Step 4. Enable the site and modules: a2ensite / a2enmod

Debian / Ubuntu

Debian and Ubuntu keep all sites in sites-available/ and enable them via a symlink in sites-enabled/; you don't need to create the symlink by hand — a2ensite does it (give it the site name together with .conf):

bash
sudo a2ensite example.com.conf

Apache modules are enabled with a similar command, a2enmod — for example, mod_rewrite (often needed together with AllowOverride All for .htaccess rules):

bash
sudo a2enmod rewrite
sudo systemctl restart apache2
Tip: after enabling a module you need a restart, not a reload — modules are loaded when the Apache process starts. You can disable a site with sudo a2dissite example.com.conf; the standard Apache test site is disabled the same way: sudo a2dissite 000-default.conf.

AlmaLinux / Rocky Linux

There's no separate a2ensite equivalent here: any *.conf file you drop into /etc/httpd/conf.d/ is loaded automatically by Apache at startup — via the IncludeOptional conf.d/*.conf directive in /etc/httpd/conf/httpd.conf. Most standard modules (including mod_rewrite) are already enabled by default on AlmaLinux/Rocky — their LoadModule directives live in /etc/httpd/conf.modules.d/.

Tip: to check which modules are currently loaded, run httpd -M | grep rewrite; if the module you need isn't listed, uncomment the matching LoadModule line in /etc/httpd/conf.modules.d/ and restart Apache.

Step 5. Test the configuration and reload Apache

Always check the configuration syntax before reloading — the command is the same on every distro:

bash
sudo apachectl configtest

A valid configuration replies with Syntax OK; on an error, apachectl points to the file and line.

Debian / Ubuntu

bash
sudo systemctl reload apache2

AlmaLinux / Rocky Linux

bash
sudo systemctl reload httpd
Tip: reload re-reads the configuration without dropping current connections — that's enough after a2ensite or VirtualHost edits. restart stops and starts the whole process — needed after enabling/disabling modules (Step 4).

You can check that the virtual host answered with the right site even before the domain points at the server, using the Host header:

bash
curl -H 'Host: example.com' http://localhost/

The response should be your test page "It works: example.com".

Done: if curl (or a browser — by IP or by domain) shows the contents of index.html, the virtual host is set up and working.

Step 6. Open the firewall ports

Apache listens on port 80 (HTTP) and, once you add TLS, also port 443 (HTTPS); these ports need to be open in the firewall, or the site will stay unreachable from outside.

Debian / Ubuntu

The apache2 package registers the Apache Full ufw profile itself, which opens both ports at once:

bash
sudo ufw app list
sudo ufw allow 'Apache Full'

If that profile isn't in the list (on Debian, it isn't always registered), open the ports by number instead:

bash
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
Important: for more on installing, enabling, and profiles in ufw, see "UFW: firewall setup".

AlmaLinux / Rocky Linux

Ports are opened through firewalld:

bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Tip: if sudo firewall-cmd --state shows not running, enable firewalld first: sudo systemctl enable --now firewalld.

What's next

Apache is installed, the virtual host is configured, and the ports are open — the site is reachable over HTTP. From here you can:

  • add another VirtualHost the same way (Step 3) — for a second domain on the same server;
  • set up a TLS certificate so the site loads over HTTPS;
  • if you haven't logged in to the panel yet, start with "How to log in to VMmanager 6"; the full list of ready-made recipes (including LAMP) is in "Ready-made VMmanager 6 recipes".