Apache is a popular open source web server that is widely used to host web pages.
In this tutorial, you’ll be creating a virtual host environment to run multiple websites with Apache on your Ubuntu 22.04.4 LTS server allowing you to add several domains hosted on just 1 IP address.
Installing Apache
We’ll begin by updating the local repository with the following command
Next, install the Apache server
Run this command to start the Apache server
Run this command to verify that Apache is running
You can test if your Apache server is live by typing your public IP address in your web server, you should get the following result
Configuring Your Own Website
Apache by default is setup to serve documents from **/var/www. **So you will need to create store your webpage directory in this folder. We will be using demo.com here as follows:
You will need to modify the ownership of this directory so as to be accessible and executable by your user account. This is done by executing the following:
Next you will change the permission settings of the directory to allow everyone to have read and execute permissions while the $USER has read, write and execute permissions
Next create an index.html file that will be used to host your site’s html code
Insert the following code in the index.html **file by pressing **i **to write the file. And to save it press **ESQ **to exit insert mode, *and *:wq **after to save and quit the the file editor
<head>
<title>Welcome to Your_domain!</title>
</head>
<body>
<h1>Success! The your_domain virtual host is working!</h1>
</body>
</html>
Setting up the VirtualHost Configuration File
The next step is to setup the configuration file for the webserver in the /etc/apache2 folder — which is where the Apache configuration file is located. The file also uses the **.conf **extension. Create the configuration file using the following command
Insert this code into the file
ServerAdmin webmaster@localhost
ServerName demo.com
ServerAlias www.demo.com
DocumentRoot /var/www/demo.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file
Activating VirtualHost file
After setting up the website, the next step is to activate the virtual host file. Do that by running the following commands
First enable the file with the **a2ensite **command
Then disable the default configuration file
Check for configuration errors using the following command:
The output of that command should be “ Syntax OK” to indicate that it is properly configured
Restart the Apache server to implement the changes
Congrats! Your website is now hosted on your machine. To view the website navigate to http://demo.com