TutorialsAugust 4, 20264 min read

How to Install Apache on an Ubuntu VPS

Install Apache (httpd) on Ubuntu 24.04, set up virtual hosts, enable useful modules, and serve your first site with clean commands.

NBy Nxeon

Apache (the apache2 package on Ubuntu) is the veteran web server that popularised .htaccess and per-directory config. It is a great fit if you run PHP apps or need flexible per-folder rules. This guide gets Apache installed on Ubuntu 24.04, opens the firewall, and stands up a virtual host for your domain.

Install Apache

Update the package index and install:

sudo apt update
sudo apt install apache2 -y

The service starts automatically. Verify it:

systemctl status apache2

Visit http://YOUR_SERVER_IP and you will see the Ubuntu Apache default page. If you have just provisioned the box, our first 10 minutes on a new VPS checklist is worth a read first.

Open the firewall

Apache also registers UFW profiles:

sudo ufw app list
sudo ufw allow 'Apache Full'

Apache Full covers ports 80 and 443. Pair this with the broader lockdown in our UFW firewall basics guide.

The Nxeon game-server control panel — live console, player slots, and TPS
The Nxeon game-server control panel — live console, player slots, and TPS

Understand the layout

Apache on Ubuntu differs from other distros in its directory conventions:

  • Main config: /etc/apache2/apache2.conf
  • Virtual hosts: /etc/apache2/sites-available/, enabled via symlinks in sites-enabled/
  • Modules: enabled with a2enmod, sites with a2ensite
  • Web root: /var/www/html
  • Logs: /var/log/apache2/

Create a virtual host

Make a document root and a landing page:

sudo mkdir -p /var/www/example.com/public_html
echo '<h1>Apache on Nxeon</h1>' | sudo tee /var/www/example.com/public_html/index.html
sudo chown -R www-data:www-data /var/www/example.com

Create /etc/apache2/sites-available/example.com.conf:

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

Enable your site, disable the default, and reload:

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

apache2ctl configtest is the Apache equivalent of nginx -t — always run it before reloading.

Enable common modules

Two modules almost everyone needs:

sudo a2enmod rewrite
sudo a2enmod headers
sudo systemctl restart apache2

rewrite powers .htaccess URL rewriting (essential for WordPress and most PHP frameworks). To let .htaccess files take effect, set AllowOverride All in a <Directory> block for your document root.

Add PHP (optional)

If you are serving PHP, install the module and restart:

sudo apt install php libapache2-mod-php -y
sudo systemctl restart apache2

For a full PHP web stack, many people prefer Nginx with PHP-FPM — see our LEMP stack guide. If you specifically want .htaccess portability, Apache is the friendlier choice.

Secure it with HTTPS

Add a free certificate with Certbot's Apache plugin:

sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache

Our full Certbot walkthrough covers renewal and troubleshooting.

Host several sites with virtual hosts

Apache's virtual hosts let one server answer for many domains. Create a second config the same way — /etc/apache2/sites-available/another.com.conf with its own ServerName and DocumentRoot — then enable it:

sudo a2ensite another.com.conf
sudo systemctl reload apache2

Apache matches the incoming Host header to the right ServerName, so each domain serves its own document root from the same box. Give every site its own log files, as in the config above, so you can trace traffic per domain.

Check what Apache is doing

Two commands are invaluable when something is off. sudo apache2ctl -S prints the virtual host map — which domain resolves to which config and document root — catching overlaps instantly. And to watch requests live:

sudo tail -f /var/log/apache2/access.log

For errors, tail error.log in the same directory. Together they answer most "why is my site doing that" questions in seconds.

FAQ

Should I run Apache and Nginx at the same time?

Not on the same ports. They will conflict on 80/443. Some setups use Nginx as a reverse proxy in front of Apache, but for a single site pick one. Compare the trade-offs in our Nginx install guide.

Why is my .htaccess file being ignored?

Two reasons usually: the rewrite module is not enabled (sudo a2enmod rewrite), or AllowOverride is set to None. Set it to All in the <Directory> block for your site and restart Apache.

How do I check Apache's config for errors?

Run sudo apache2ctl configtest. For a runtime view of what is loaded, apache2ctl -M lists active modules and apache2ctl -S shows the virtual host layout.

Where are Apache's log files on Ubuntu?

Under /var/log/apache2/. The global error.log and access.log live there, plus any per-site logs you defined with CustomLog.

Nxeon web hosting on a VPS gives you full root to run Apache exactly how you like, with one-click Ubuntu images and free migration help if you are moving a PHP site from cPanel or another host.

#apache#ubuntu#web server#vps#linux#seobatch

Deploy your first server in under a minute

Creating an account is free and takes no card details. You pay when you deploy — choose a billing term and pay from your wallet or by card at checkout.