ApacheWeb ServerHTTP7 min read

Install and configure Apache

Install Apache HTTP Server with virtual hosts and essential modules to serve websites.


Apache HTTP Server is one of the most widely used web servers in the world. It supports dynamic modules, .htaccess and virtual hosts.

Step 1 — Install Apache

bash
sudo apt update
sudo apt install apache2 -y

Step 2 — Verify the service

bash
sudo systemctl status apache2

Visit http://YOUR_VPS_IP in your browser to see the default page.

Step 3 — Configure the firewall

bash
sudo ufw allow 'Apache Full'

Step 4 — Create a Virtual Host

Create the site directory:

bash
sudo mkdir -p /var/www/mydomain.com/html
sudo chown -R $USER:$USER /var/www/mydomain.com/html
echo '<h1>Apache running on Baires Host</h1>' > /var/www/mydomain.com/html/index.html

Create the configuration file:

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

Content:

apache
<VirtualHost *:80>
    ServerAdmin admin@mydomain.com
    ServerName mydomain.com
    ServerAlias www.mydomain.com
    DocumentRoot /var/www/mydomain.com/html

    <Directory /var/www/mydomain.com/html>
        AllowOverride All
    </Directory>

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

Step 5 — Enable the site and modules

bash
sudo a2ensite mydomain.com.conf
sudo a2dissite 000-default.conf
sudo a2enmod rewrite
sudo apache2ctl configtest
sudo systemctl reload apache2

Useful modules

bash
sudo a2enmod ssl        # HTTPS
sudo a2enmod headers    # Security headers
sudo a2enmod proxy      # Reverse proxy
sudo a2enmod proxy_http

Management commands

bash
sudo systemctl start apache2
sudo systemctl stop apache2
sudo systemctl restart apache2
sudo systemctl reload apache2

Was this guide helpful?