Linux & software2 min read

Install Apache and set up virtual hosts

Install Apache2 on Debian or Ubuntu, run several websites with virtual hosts, enable important modules and track down errors.

Apache is the classic among web servers and runs on millions of machines. Its strengths are the large number of modules and .htaccess files, which let many PHP applications such as WordPress ship their own settings. This guide applies to Debian 12 and Ubuntu 24.04.

Install Apache

sudo apt update
sudo apt install apache2
systemctl status apache2

Open the firewall for HTTP and HTTPS:

sudo ufw allow 'Apache Full'

At http://YOUR-IP you will now see the "Apache2 Default Page".

Directory for the website

sudo mkdir -p /var/www/example.com/public_html
sudo chown -R $USER:$USER /var/www/example.com/public_html
echo '<h1>Apache is running</h1>' > /var/www/example.com/public_html/index.html

Create a virtual host

With virtual hosts you can run any number of websites on one server. Apache decides by the requested name which site it serves.

sudo nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

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

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

AllowOverride All allows .htaccess files in the directory. If you do not need them, set None, which is slightly faster.

Enable the site, disable the default site, test and reload:

sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

Important modules

Module Purpose Enable
rewrite pretty URLs, redirects in .htaccess sudo a2enmod rewrite
headers custom HTTP headers, security headers sudo a2enmod headers
ssl HTTPS sudo a2enmod ssl
proxy_http Apache as a reverse proxy sudo a2enmod proxy proxy_http

After enabling a module: sudo systemctl restart apache2.

PHP with Apache

For PHP applications, install PHP with the Apache module:

sudo apt install php libapache2-mod-php php-mysql

Test it with a file info.php containing <?php phpinfo(); and delete it afterwards, it reveals a lot about your server to attackers.

HTTPS

Certbot sets up the certificate for Apache automatically:

sudo apt install certbot python3-certbot-apache
sudo certbot --apache -d example.com -d www.example.com

More in Let's Encrypt certificate with Certbot.

Apache or Nginx?

Both are mature. Apache is convenient when your application ships .htaccess files. Nginx needs less memory with many simultaneous connections. Both only run on the same server if they use different ports; only one of them can use port 80. How to set up Nginx is described in Install Nginx.

Finding errors

sudo apache2ctl configtest
sudo tail -f /var/log/apache2/error.log
sudo apache2ctl -S

apache2ctl -S lists all active virtual hosts and the file that defines them. That helps when the wrong site shows up.

Written by TryNxt team

We build and run TryNxt ourselves and write down here what we learn along the way.