Posted in

How to Install Apache2, PHP, MariaDB, Virtual Host & SSL on Ubuntu

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: Install MariaDB

sudo apt install mariadb-server mariadb-client -y

Then run secure installation:

sudo mysql_secure_installation

Answer the prompts as follows:

  • Enter current password for root: Press Enter (if blank)
  • Set root password? β†’ Y β†’ Enter new password
  • Remove anonymous users? β†’ Y
  • Disallow root login remotely? β†’ Y
  • Remove test database and access to it? β†’ Y
  • Reload privilege tables now? β†’ Y

Test MariaDB login:

mysql -u root -p

Enter the password you just created.


🏠 Step 5: 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 6: 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 7: 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.

2 thoughts on “How to Install Apache2, PHP, MariaDB, Virtual Host & SSL on Ubuntu

Leave a Reply to Fariz Anwar Cancel reply

Your email address will not be published. Required fields are marked *