Servers & VPS

How to Configure Apache on a VPS: Complete Tutorial

Learn to configure Apache on a Linux VPS step by step: installation, VirtualHosts, essential modules, and basic security hardening.

Closeup of many cables with blue wires plugged in modern switch with similar adapters on blurred background in modern studio

Configuring Apache on a VPS means installing the web server, creating a VirtualHost per site, enabling the required modules, and applying basic security hardening — all in under an hour from an SSH terminal.

Apache is the world's most widely used web server and the default option in most LAMP stacks in Mexico. Its flexibility for hosting multiple sites, its native .htaccess integration, and its mature module ecosystem make it ideal for both small projects and mid-scale applications.

Step 1 — Install Apache on Ubuntu/Debian

Update the repositories and install Apache with a single command:

sudo apt update
sudo apt install apache2 -y

Enable the service to start automatically with the system:

sudo systemctl enable apache2
sudo systemctl start apache2

Verify that Apache is running:

sudo systemctl status apache2

Open a browser and enter your VPS IP — you should see the Apache welcome page. If you don't, check that port 80 is open in your provider's firewall.

Step 2 — Configure VirtualHosts (One Domain per Site)

VirtualHosts allow a single Apache server to host multiple websites with different domains. Each site has its own configuration file in /etc/apache2/sites-available/.

Create the directory structure

sudo mkdir -p /var/www/yoursite.com/public_html
sudo chown -R $USER:www-data /var/www/yoursite.com
sudo chmod -R 755 /var/www/yoursite.com

Create the VirtualHost configuration file

sudo nano /etc/apache2/sites-available/yoursite.com.conf

Paste the following block and adjust the domain and path:

<VirtualHost *:80>
    ServerName yoursite.com
    ServerAlias www.yoursite.com
    DocumentRoot /var/www/yoursite.com/public_html
    ServerAdmin [email protected]

    <Directory /var/www/yoursite.com/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

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

The Options -Indexes directive is important: it prevents Apache from listing directory contents when no index file exists, avoiding accidental exposure of internal files.

Enable the site and reload Apache

sudo a2ensite yoursite.com.conf
sudo systemctl reload apache2

To disable the default site that ships with Apache (prevents it from showing when accessing by IP):

sudo a2dissite 000-default.conf
sudo systemctl reload apache2

Step 3 — Essential Apache Modules

Apache works through modules you can enable or disable based on your needs. These are the most important ones for a general-purpose VPS:

Module Purpose Enable command
mod_rewrite Friendly URLs / redirects (essential for WordPress, Laravel, etc.) sudo a2enmod rewrite
mod_ssl HTTPS support sudo a2enmod ssl
mod_headers Security HTTP headers (HSTS, CSP, X-Frame-Options) sudo a2enmod headers
mod_deflate Gzip compression of responses sudo a2enmod deflate
mod_expires Client-side caching of static assets sudo a2enmod expires

Enable all at once and reload:

sudo a2enmod rewrite ssl headers deflate expires
sudo systemctl reload apache2

For PHP sites, you will also need libapache2-mod-php installed as part of the LAMP stack. Browse the VPS servers category for related setup guides.

Step 4 — Basic Security Hardening

Apache's default installation reveals server information that attackers can exploit. These settings harden it without affecting functionality:

Hide the Apache version and operating system

sudo nano /etc/apache2/conf-available/security.conf

Find and change these two lines:

ServerTokens Prod
ServerSignature Off

Add security headers

In your VirtualHost configuration file, inside the <VirtualHost> block, add:

Header always set X-Content-Type-Options "nosniff"
Header always set X-Frame-Options "SAMEORIGIN"
Header always set X-XSS-Protection "1; mode=block"
Header always set Referrer-Policy "strict-origin-when-cross-origin"

Block browser access to .htaccess files

You already covered Options -Indexes in the VirtualHost. Additionally, in the global security config file, confirm that access to .ht* files is denied:

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

This protects .htaccess and .htpasswd from being read by a browser.

For complete server configuration and ongoing management, the team at elenlace.com can handle all your VPS infrastructure in Mexico.

Key takeaways

  • Apache installs in two commands on Ubuntu/Debian; everything else is configuration.
  • One VirtualHost per domain is the standard practice: it keeps sites isolated and organized.
  • Options -Indexes and AllowOverride All are the two most critical directives in directory configuration.
  • The modules rewrite, ssl, headers, deflate, and expires cover 90% of typical use cases.
  • Hiding ServerTokens and adding security headers are the first hardening steps that should never be skipped.

Want a production-ready Apache server without worrying about configuration? Contact us at elenlace.com — we configure and secure your VPS so you can focus on your business.

FAQ

How do I verify the Apache configuration has no errors before reloading?

Use the command sudo apachectl configtest or sudo apache2ctl -t. If the syntax is correct, it responds with Syntax OK. If there is an error, it indicates the exact file and line number. Always run this command before doing systemctl reload apache2.

What is the difference between systemctl reload and systemctl restart for Apache?

reload reloads the configuration without interrupting active connections — this is what you should use in production. restart stops and restarts the entire process, briefly cutting existing connections. Only use restart when installing a new module that requires a full restart.

Can I host multiple domains on the same VPS with Apache?

Yes, that is exactly the purpose of VirtualHosts. There is no strict technical limit; the real limit is server resources (RAM, CPU). On a 2 GB RAM VPS with moderate-traffic sites, hosting 5 to 15 domains without issues is common.

What is the difference between a2ensite and editing apache2.conf directly?

a2ensite simply creates a symbolic link from sites-available/ to sites-enabled/, which is what Apache actually reads on startup. It is the standard approach on Debian/Ubuntu systems because it keeps files organized and makes enabling/disabling sites reversible with a single command.

Compare providers

Other providers and guides worth comparing:

← All