...
Skip to main content
Print

How to Install Apache on Ubuntu

You install Apache on Ubuntu with apt install apache2. Then you open the firewall, create a virtual host for your domain and add PHP and HTTPS if desired. Apache is one of the most widely used web servers and works directly with .htaccess files, which is useful for WordPress, among others.

Step 1: install Apache

apt update
apt install apache2 -y

Step 2: open the firewall

ufw allow 'Apache Full'

Open the IP address of your VPS in your browser. If you see the Apache default page, the web server is working.

Step 3: create a virtual host

mkdir -p /var/www/example.com
nano /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    <Directory /var/www/example.com>
        AllowOverride All
    </Directory>
</VirtualHost>
a2ensite example.com.conf
a2enmod rewrite
apache2ctl configtest
systemctl reload apache2

Step 4: add PHP (optional)

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

Step 5: secure with HTTPS

apt install certbot python3-certbot-apache -y
certbot --apache -d example.com -d www.example.com

Related articles

Table of Contents