Hello, web developers! In this article, we'll see how to create virtual host in ubuntu 22.04. Virtual hosting is a method for hosting multiple domain names on a single server. This allows one server to share its resources, such as memory and processor cycles, without requiring all services provided to use the same host name.
The term Virtual Host refers to the practice of , (such as company1.example.com and company2.example.com ) on a single machine.
Ubuntu 22.04 Create Virtual Host
In this step, we'll install apache2 server using the following command.
sudo apt update
sudo apt install apache2
Next, we'll create example.test directory.
sudo mkdir /var/www/html/example.test
Then, assign ownership to the directory.
sudo chown -R $USER:$USER /var/www/html/example.test
sudo chmod -R 755 /var/www/html
Now, we'll create simple index.html file with hello world text. So, run the following command.
echo "<html><head><title>Test Website</title></head><body><h1>Hello, World!</h1></body></html>"
Then, we'll configure apache file.
sudo nano /etc/apache2/sites-available/example.test.conf
Replace example.test with your domain or preferred website name, and paste the following configuration.
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.test
DocumentRoot /var/www/html/example.test
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/example.test>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Next, enable the virtual host using the following command.
sudo a2ensite example.test.conf
Then, restart the apache using the following command.
sudo systemctl restart apache2
Add host entry by following command.
sudo nano /etc/hosts
Add the following line:
127.0.0.1 example.test
Replace example.test with the your domain you specified in your virtual host configuration.
Now, open the web browser and add the following link.
http://example.test
Replace example.test with your domain or server IP address.
Your virtual host should now be set up and accessible. And you can customize the content in the `/var/www/html/example.test` directory to host your website files.
You might also like:
- Read Also: How to Send WhatsApp Messages in Laravel 10
- Read Also: Step-by-Step Guide: Installing React.js on Ubuntu
- Read Also: How to Install Sweetalert2 in Laravel 10 Vite
- Read Also: Laravel 10 Install Font Awesome Icons Example