This guide will walk you through installing Apache2, PHP, MariaDB, setting up Virtual Hosts, and adding SSL certificates via Let’s Encrypt on Ubuntu.
Step 1: Update the System
sudo apt update && sudo apt upgrade -y
Step 2: Install Apache2
sudo apt install apache2 -y
Check Apache status:
sudo systemctl status apache2
Enable Apache on startup and restart:
sudo systemctl enable apache2
sudo systemctl restart apache2
Open your browser and visit:
http://YOUR_SERVER_IP
You should see the Apache default welcome page.
Step 3: Install PHP
sudo apt install php -y
Install required PHP modules (adjust based on your application needs):
sudo apt install libapache2-mod-php php-common php-bcmath php-mbstring php-mysql php-tokenizer php-zip php-curl php-gd php-mail php-mail-mime php-pear php-db php-xml php-json php-imagick imagemagick php-intl php-gmp php-cli php-ldap -y
Step 4: Configure Apache Default Virtual Host
Edit default virtual host file:
sudo nano /etc/apache2/sites-available/000-default.conf
Under DocumentRoot, add:
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Set preferred index order:
<IfModule mod_dir.c>
DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Enable the rewrite module:
sudo a2enmod rewrite
Restart Apache:
sudo systemctl restart apache2
Step 5: Create a Virtual Host for Your Domain
Create the project directory:
sudo mkdir /var/www/yourdomain.com
Create a new virtual host configuration file:
sudo nano /etc/apache2/sites-available/yourdomain.com.conf
Add this configuration:
<VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain.com
<Directory /var/www/yourdomain.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/yourdomain.com_error.log
CustomLog /var/log/apache2/yourdomain.com_access.log combined
</VirtualHost>
If you plan to use SSL:
<VirtualHost *:443>
ServerName yourdomain.com
DocumentRoot /var/www/yourdomain.com
<Directory /var/www/yourdomain.com>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/log/apache2/yourdomain.com_websecure_error.log
CustomLog /var/log/apache2/yourdomain.com_websecure_access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/yourdomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem
</VirtualHost>
Enable the site:
sudo a2ensite yourdomain.com.conf
Reload Apache:
sudo systemctl reload apache2
Enable required modules:
sudo a2enmod rewrite
sudo a2enmod ssl
Restart Apache:
sudo systemctl restart apache2
Check config syntax:
sudo apachectl configtest
Expected output: Syntax OK
Note: Don’t forget to point your domain’s A record to your server IP.
Step 6: Install SSL with Let’s Encrypt (Certbot)
Install Certbot:
sudo apt install python3-certbot-apache -y
Run Certbot:
sudo certbot --apache
You’ll be prompted to enter your domain(s), email address, and agree to terms. Follow the steps carefully.
Alternatively, specify domains directly:
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
Step 8: Test PHP Installation
Create a PHP info file:
sudo nano /var/www/yourdomain.com/info.php
Insert:
<?php phpinfo(); ?>
Then access:
http://yourdomain.com/info.php
You should see detailed PHP environment info.

I’m Ryan, a Network Engineer from Indonesia focused on network infrastructure, Server Management, and system administration, with a background in network security and cybersecurity. I enjoy building, troubleshooting, and understanding complex systems from the ground up.
