Create Apache Virtual host in Ubuntu 18.04

So this is something everyone struggles while installing LAMP stack on Ubuntu 18.04. I struggle too and I decided to note down proper steps so we all can easily follow them in future.

Before You follow the steps, just make sure to change with your specific domain. And one more thing, these steps are tested locally. For publishing live domain, there are different steps which I will post in future after proper testing.

So here are the steps.

Step 1

Create project directory

sudo mkdir -p /var/www/

Step 2

Add an HTML file in it for testing

sudo nano /var/www/

And add below code in it

<!DOCTYPE html>
<html lang="en" dir="ltr">
<meta charset="utf-8">
<title>Welcome to</title>
<h1>Success! home page!</h1>

Step 3

Add project directory to Apache www-data group.

sudo chown -R www-data: /var/www/

Step 4

Create apache config file for the project

sudo nano /etc/apache2/sites-available/

And add below code in it

<VirtualHost *:80>
    DocumentRoot /var/www/

    <Directory /var/www/>
        Options -Indexes +FollowSymLinks
        AllowOverride All

    ErrorLog ${APACHE_LOG_DIR}/
    CustomLog ${APACHE_LOG_DIR}/ combined

Step 5

Enable new project as apache site. This create some configuration files for apache automatically.

sudo a2ensite

Step 6

Create a symlink of project from sites-available folder to sites-enabled folder.

sudo ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

* Note: If this command do not work, just ignore it. Everything will be fine.

Step 7

Check if all configuration are done properly.

sudo apachectl configtest

It should show “Status OK”

Step 8

Restart Apache server

sudo systemctl restart apache2

Step 9

Edit hosts file to access site using domain name locally

sudo nano /etc/hosts

And add below code in it

Step 10

Add full access permission to the project folder so you can add/update/delete files and folders without using root.

sudo chmod -R 777 /var/www

And finally try running URL in browser. It should show the example page we created in Step 2.

If you find this helpful, please share it with others and leave comments.

Thank You.