How to install Apache on a VPS and set up a virtual host

On Ubuntu or Debian you install Apache with sudo apt install apache2, write a site file in /etc/apache2/sites-available/ holding a VirtualHost that points at your folder, enable it with sudo a2ensite, check it with sudo apache2ctl configtest and reload. On AlmaLinux or Rocky the package is called httpd. On an unmanaged VPS the installation and upkeep are yours.

A virtual host is how Apache serves different sites depending on the name in the request. With a single site it is still worth creating one: it keeps the configuration tidy for when the second site arrives.

Step by step on Debian and Ubuntu

1 Install it. sudo apt update and sudo apt install apache2. Open ports 80 and 443 in the firewall, if you have one (ports and firewall on a VPS).
2 Create the site folder and a test file: sudo mkdir -p /var/www/yourdomain.tld/public.
3 Write the virtual host in sudo nano /etc/apache2/sites-available/yourdomain.tld.conf<VirtualHost *:80>
ServerName yourdomain.tld
ServerAlias www.yourdomain.tld
DocumentRoot /var/www/yourdomain.tld/public

<Directory /var/www/yourdomain.tld/public>
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/yourdomain.tld-error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.tld-access.log combined
</VirtualHost>
4 Enable the site and disable the welcome one. sudo a2ensite yourdomain.tld.conf and sudo a2dissite 000-default.conf.
5 Switch on the rewrite module if the application uses pretty addresses (WordPress, Laravel): sudo a2enmod rewrite.
6 Test and reload. sudo apache2ctl configtest should answer “Syntax OK”; then sudo systemctl reload apache2.
7 Point the domain at the server IP and check: pointing a domain at your own VPS. Error logs are in /var/log/apache2/.

What changes on AlmaLinux and Rocky

Debian and Ubuntu AlmaLinux and Rocky Linux
Package and service apache2 httpd (systemctl enable --now httpd)
Where the site goes sites-available + a2ensite a .conf file in /etc/httpd/conf.d/
Test the configuration apache2ctl configtest apachectl configtest
Logs /var/log/apache2/ /var/log/httpd/
Service user www-data apache
“AllowOverride All” lets .htaccess rule the site. It is what WordPress and most PHP applications expect, but every request now looks for that file in every folder. If you control the server configuration you can put the rules in the virtual host itself and leave AllowOverride None. For the rewrite rules themselves, see .htaccess for PHP projects.
A “403 Forbidden” right at the start is nearly always a missing Require all granted or a folder Apache’s user cannot walk through: file permissions on a VPS. On AlmaLinux or Rocky it can be SELinux: SELinux and AppArmor.
Changed just a site file? reload is enough and drops nobody. Keep restart for when you add or remove modules.

Installed it and the server stopped answering on the network? Tell us what you changed and we will check our side.

Open a support ticket

SEE ALSO

Installing nginx on Ubuntu

Nginx or Apache: which to choose

Several websites on one VPS

How far our support goes

RECOMMENDED PRODUCT

VPS server with root access

Resources of your own, the OS you choose, reinstall whenever you like. from $7.61/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?