Servers & VPS

How to Install and Configure Apache on a Linux VPS Step by Step

Learn to install Apache on your Linux VPS, create Virtual Hosts for multiple domains, and enable SSL with Let's Encrypt in under 30 minutes.

Detailed image of illuminated server racks showcasing modern technology infrastructure.

To install Apache on a Linux VPS running Ubuntu or Debian, just run sudo apt install apache2 -y. Within seconds you'll have a functional web server listening on port 80. What comes next — Virtual Hosts, SSL, permissions — is what turns that basic install into a production-ready server.

This guide covers everything from the first command to having a live site with HTTPS. If you don't have a VPS yet, check our VPS servers guide for options available in Mexico.

Step 1: Install Apache on Your VPS

Update the package list before installing anything:

sudo apt update
sudo apt install apache2 -y

Once installed, Apache starts automatically. Check its status:

sudo systemctl status apache2

You should see active (running) in green. Enable it to start automatically after a server reboot:

sudo systemctl enable apache2

Open your browser and enter your VPS's public IP. If you see the Apache welcome page ("It works!"), the installation was successful.

On CentOS, AlmaLinux, or Rocky Linux

The package is called httpd in the Red Hat ecosystem:

sudo dnf install httpd -y
sudo systemctl enable --now httpd

Configuration files live in /etc/httpd/conf/ instead of /etc/apache2/, but the logic is the same.

Step 2: Open the Required Ports in the Firewall

A properly configured VPS has an active firewall. Allow HTTP and HTTPS traffic:

# Ubuntu / Debian (UFW)
sudo ufw allow 'Apache Full'
sudo ufw status

# CentOS / AlmaLinux (firewalld)
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

The Apache Full UFW profile opens ports 80 and 443 in a single command.

Step 3: Create Virtual Hosts for Your Domains

A Virtual Host tells Apache how to handle requests for a specific domain. You can host dozens of sites on the same VPS using one Virtual Host per domain.

Recommended Directory Structure

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

Create a quick test page:

nano /var/www/yourdomain.com/public_html/index.html

Paste something simple:

<!DOCTYPE html>
<html>
<body>
  <h1>Hello from yourdomain.com</h1>
</body>
</html>

Virtual Host Configuration File

Create the config file in the available sites directory:

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

File contents:

<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/public_html
    ErrorLog ${APACHE_LOG_DIR}/yourdomain.com-error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain.com-access.log combined
</VirtualHost>

Enable the site and reload Apache:

sudo a2ensite yourdomain.com.conf
sudo apache2ctl configtest
sudo systemctl reload apache2

The a2ensite command creates a symlink from sites-available/ to sites-enabled/. Only sites in sites-enabled/ are active.

Directory Purpose
/etc/apache2/sites-available/ Defines all Virtual Hosts (active or not)
/etc/apache2/sites-enabled/ Only the ones Apache loads on startup
/var/www/ Conventional root for site files
/var/log/apache2/ Per-site access and error logs

Step 4: Enable SSL with Let's Encrypt (Free HTTPS)

HTTPS is no longer optional: Google treats it as a ranking signal and modern browsers display warnings on non-SSL sites. Let's Encrypt provides free certificates and Certbot automates everything.

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com

Certbot will ask for your email and terms acceptance. Choose to redirect all HTTP traffic to HTTPS when prompted (option 2). The end result is an HTTPS-enabled Virtual Host with an automatic redirect from port 80.

Verify automatic renewal:

sudo certbot renew --dry-run

If it completes without errors, the renewal process is working correctly. Certificates renew automatically every 90 days with no manual intervention needed.

Key Takeaways

  • Installing Apache on Ubuntu takes a single command; what matters is the post-install configuration.
  • Use Virtual Hosts to host multiple domains on the same VPS — one .conf file per domain.
  • Always open ports 80 and 443 in the firewall before testing the web server.
  • Let's Encrypt + Certbot gives you free HTTPS with automatic renewal — no excuse not to enable it.
  • Always run apache2ctl configtest before reloading Apache to avoid downtime from config errors.

If you'd rather have an expert configure and maintain Apache on your VPS, the team at elenlace.com provides web server administration services in Mexico — get in touch and tell us about your project.

FAQ

What is the difference between Apache and Nginx?

Apache uses a process-per-connection model (prefork/worker) that makes it easy to use modules like mod_php. Nginx uses an asynchronous, event-driven model that makes it more efficient with thousands of simultaneous connections. For most websites, both work perfectly. Apache is easier to configure for beginners thanks to .htaccess files.

Can I use Apache and Nginx together?

Yes. A common pattern is to use Nginx as a reverse proxy at the front, handling incoming connections and static content, with Apache behind it processing PHP. This combines Nginx's efficiency with Apache's compatibility. It's a more advanced setup but well-documented.

How do I check Apache logs to diagnose errors?

Logs are in /var/log/apache2/. The global error file is error.log; the access file is access.log. If you defined custom log paths in your Virtual Host (as in this guide's example), look there instead. Use sudo tail -f /var/log/apache2/error.log to watch errors in real time.

What do I do if Apache won't start after editing the config?

Run sudo apache2ctl configtest to see exactly where the syntax error is. This command checks all configuration files and points to the problematic line. Fix the error, run the test again, and then start Apache with sudo systemctl start apache2.

Prefer it done for you? El Enlace handles hosting and professional web development.

Further reading

Other providers and guides worth comparing:

← All