How to create Virtual Hosts on Ubuntu 22.04 with Apache
Maksudur Rahman
Software Engineer
Creating a virtual host in Ubuntu 22.04 involves several steps, and I'll guide you through the process using Apache as the web server. Make sure you have Apache installed on your system before proceeding. If not, you can install it by running:
sudo apt update
sudo apt install apache2
Once Apache is installed, follow these steps to create a virtual host:
Step 1: Create a Directory for Your Website
sudo mkdir /var/www/example.com
Replace "example.com" with your domain or the name of your project.
Step 2: Set Permissions
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com
Step 3: Create an HTML File
nano /var/www/example.com/index.html
Add some content to the file (e.g., a simple "Hello, World!" message).
Step 4: Create a Virtual Host Configuration File
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configuration:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/example.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Make sure to replace "example.com" with your domain or project name.
Read More E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
Step 5: Enable the Virtual Host
sudo a2ensite example.com.conf
Step 6: Disable the Default Site (Optional)
sudo a2dissite 000-default.conf
Step 7: Restart Apache
sudo systemctl restart apache2
Step 8: Update Hosts File (Optional)
If you are working on a local machine, you may want to update your hosts file to map the domain to your server's IP address. Open the host's file:
sudo nano /etc/hosts
Add the following line:
127.0.0.1 example.com
Replace "example.com" with your domain.
Now, you should be able to access your virtual host by entering the domain in your web browser. I hope, it will help you generate a virtual host step by step. Thank you for your time.