Create virtualhost inside user directory ubuntu 22.04

Create virtualhost inside user directory ubuntu 22.04

In this article i will show you how to create virtual host for your domain or sub domain as far as i know, watch step below.

Update and upgrade your system

like commonly type this sudo apt update && sudo upgrade -y

Install Apache web server

type this to install apache2 sudo apt install apache2.

Check your apache2 status with this syntax sudo systemctl status apache2 and you will see this.

image.png

Preparing directory user

you can create new user for that or existing user, you can set 1 user for 1 domain it will make easy for setup.

For creating new user

type this sudo adduser exampleuser, it will create new user folder in this location /home/exampleuser. then you can check you user with this command less /etc/passwd or cut -d: -f1 /etc/passwd.

Add user to Apache www-data group

add exampleuser to Apache www-data group sudo usermod -a -G www-data exampleuser. and add that user to sudo group with this sudo usermod -a -G sudo exampleuser. then you can verify that user successfull to add group groups exampleuser. you can see list of member www-data so type grep ^www-data /etc/group.

Create directory for application

prepare your application folder, make sure you create public_html folder inside your user directory, type this mkdir public_html make sure your directory like this /home/exampleuser/public_html.

Create VirtualHost conf

go to /etc/apache2/sites-available then create conf file using vim vim exampleuser.conf here conf code:

#exampleuser.conf
<VirtualHost *:80>

    ServerAdmin admin@domain.io
    ServerName exampleuser.com #your domain
    ServerAlias www.exampleuser.com #server alias
    DocumentRoot /home/arif/public_html #your path project

    <Directory />
            Options FollowSymLinks
            AllowOverride None
    </Directory>
    <Directory /home/exampleuser/public_html>
            AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/exampleuser-error.log
    CustomLog ${APACHE_LOG_DIR}/exampleuser-access.log combined

</VirtualHost>

then you can check your syntax with sudo apachectl config test

Enable Configuration

  • enable your config file sudo a2ensite exampleuser.conf.

  • enable userdir sudo a2enmod userdir.

  • enable mod rewrite sudo a2enmod rewrite.

  • then adding firewall sudo ufw allow "Apache full".

  • restart your apache sudo systemctl reload apache2.

Did you find this article valuable?

Support Arif Rahman Fauzi by becoming a sponsor. Any amount is appreciated!