How to Install and Configure Apache on an Ubuntu VPS
Apache HTTP Server is one of the most widely used open-source web servers. It supports virtual hosts, SSL/TLS encryption, URL rewriting, and a large collection of modules for hosting websites and web applications.
This guide explains how to install and configure Apache on Ubuntu 24.04 LTS or Ubuntu 26.04 LTS, create a virtual host, configure the firewall, and secure your website with a free Let's Encrypt SSL certificate.
Prerequisites
- An Ubuntu 24.04 LTS or Ubuntu 26.04 LTS VPS.
- Root access or a user with sudo privileges.
- A domain name pointing to your VPS public IP address.
- Ports 80 and 443 accessible from the internet.
Need a Linux server? Explore Owned Networks KVM VPS hosting for full root access and flexible hosting resources.
1. Update Ubuntu and Install Apache
Update the package repositories and install Apache:
sudo apt update && sudo apt upgrade -y
sudo apt install -y apache2
Enable Apache and verify that the service is running:
sudo systemctl enable --now apache2
sudo systemctl status apache2 --no-pager
apache2 -v
Open http://YOUR_SERVER_IP in your browser. You should see the default Apache Ubuntu page.
2. Configure the UFW Firewall
If UFW is enabled, allow HTTP and HTTPS traffic:
sudo ufw allow 'Apache Full'
sudo ufw status
The Apache Full profile allows incoming connections on ports 80 (HTTP) and 443 (HTTPS).
Important: Before enabling UFW for the first time, allow your SSH port to avoid losing remote access.
3. Create a Website Directory
Create a document root for your website. Replace example.com with your actual domain:
sudo mkdir -p /var/www/example.com/public_html
sudo chown -R "$USER":www-data /var/www/example.com
sudo chmod 755 /var/www/example.com /var/www/example.com/public_html
Create a test HTML page:
cat > /var/www/example.com/public_html/index.html <<'EOF'
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Apache on Ubuntu VPS</title>
</head>
<body>
<h1>Apache is running successfully!</h1>
<p>Hosted on an Ubuntu VPS with Owned Networks.</p>
</body>
</html>
EOF
4. Configure an Apache Virtual Host
Apache uses virtual hosts to serve multiple websites from a single server.
Create a configuration file:
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
ServerAdmin [email protected]
DocumentRoot /var/www/example.com/public_html
<Directory /var/www/example.com/public_html>
Options -Indexes +FollowSymLinks
AllowOverride None
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/example.com-error.log
CustomLog ${APACHE_LOG_DIR}/example.com-access.log combined
</VirtualHost>
Enable the website and disable the default virtual host:
sudo a2ensite example.com.conf
sudo a2dissite 000-default.conf
sudo apache2ctl configtest
sudo systemctl reload apache2
Visit http://example.com to verify that your website is accessible.
5. Enable Apache Modules
Apache provides modules for additional functionality, including URL rewriting and SSL/TLS support.
Enable commonly used modules:
sudo a2enmod rewrite ssl headers
sudo apache2ctl configtest
sudo systemctl reload apache2
The rewrite module is commonly used by content management systems such as WordPress.
If your application requires .htaccess files, change AllowOverride None to AllowOverride All in the appropriate Directory block, then reload Apache. Only enable overrides where necessary.
6. Install Certbot and Enable Free SSL
Let's Encrypt provides free SSL/TLS certificates for securing websites with HTTPS.
Install Certbot and the Apache integration:
sudo apt install -y certbot python3-certbot-apache
Ensure both example.com and www.example.com resolve to your VPS before requesting a certificate.
Generate and configure the certificate:
sudo certbot --apache -d example.com -d www.example.com
Follow the prompts to complete domain validation and configure HTTPS.
If you do not use the www subdomain, request a certificate only for your primary domain:
sudo certbot --apache -d example.com
Verify automatic certificate renewal:
sudo certbot renew --dry-run
Certbot typically configures automatic renewal through a systemd timer or scheduled task.
7. Configure Basic Apache Security
Apache can be configured to reduce unnecessary information disclosure and improve browser-side security.
Create a security configuration file:
sudo nano /etc/apache2/conf-available/owned-security.conf
Add:
ServerTokens Prod
ServerSignature Off
TraceEnable Off
<IfModule mod_headers.c>
Header always set X-Content-Type-Options "nosniff"
Header always set Referrer-Policy "strict-origin-when-cross-origin"
Header always set X-Frame-Options "SAMEORIGIN"
</IfModule>
Enable the configuration:
sudo a2enconf owned-security
sudo apache2ctl configtest
sudo systemctl reload apache2
Note: Ubuntu may already define some Apache security directives in /etc/apache2/conf-available/security.conf. Review existing settings to avoid conflicting configurations. Security headers should also be tested against your application's requirements.
8. Useful Apache Management Commands
Use these commands to manage and troubleshoot Apache:
# Check Apache status
sudo systemctl status apache2 --no-pager
# Validate configuration
sudo apache2ctl configtest
# Display virtual hosts
sudo apache2ctl -S
# Reload configuration
sudo systemctl reload apache2
# Restart Apache
sudo systemctl restart apache2
# View Apache errors
sudo tail -n 50 /var/log/apache2/error.log
# Check listening ports
sudo ss -tlnp | grep apache2
# Verify HTTPS response
curl -I https://example.com
9. Troubleshooting Common Apache Problems
403 Forbidden: Verify document root permissions, directory access rules, and the existence of an index file.
404 Not Found: Confirm the requested file exists and verify the virtual host's DocumentRoot configuration.
Apache fails to start: Another service, such as Nginx, may already be using ports 80 or 443.
sudo apache2ctl configtest
sudo journalctl -u apache2 -n 50 --no-pager
sudo ss -tlnp | grep -E ':80|:443'
SSL certificate errors: Verify DNS records, certificate validity, and the domain names included in the certificate.
Virtual host not loading: Check that the configuration is enabled and that the domain matches the ServerName or ServerAlias directive.
sudo apache2ctl -S
ls -l /etc/apache2/sites-enabled/
Related Ubuntu VPS Tutorials
Continue configuring your server with these Owned Networks knowledgebase guides:
- How to Set Up and Secure a New Ubuntu VPS
- How to Install and Configure Nginx on an Ubuntu VPS
- How to Host WordPress on an Ubuntu VPS with Nginx and Free SSL
- How to Migrate a Website from Shared Hosting to a VPS Without Downtime
Host Your Website with Owned Networks
Deploy Apache, WordPress, or custom web applications on an Owned Networks KVM VPS with full root access, scalable resources, and SSD/NVMe storage options.
Whether you prefer Apache or Nginx, a VPS provides greater control over your web server configuration and hosting environment.
Explore Owned Networks VPS Hosting and deploy your Ubuntu server today.