This write-up will discuss the method to install and configure Apache Web Server on Ubuntu 22.04. So, let’s start!
How to install Apache Web Server on Ubuntu 22.04
To set up Apache Web Server on Ubuntu 22.04, you must follow the below-given step-by-step instructions.
Step 1: Update system repositories
Press “CTRL+ALT+T” to open the terminal and run the below-given command to update system repositories:
Step 2: Install Apache Web Server on Ubuntu 22.04
After updating system repositories, write out the following command for installing the “apache2” web server on your Ubuntu 22.04 system:
Wait for a few minutes as the Apache installation will take some time to complete:
Step 3: Configure Firewall
To access Apache from outside, it is required to open specific ports on your system. To do so, firstly, check the list of the applications profiles that need access:
Execution of the above-given command will print out different apache profiles on the terminal:
Next, we will utilize the “Apache Full” profile for enabling network activities on the port “80”:
After doing so, check the current firewall status:
Now, let’s move towards the configuration side of Apache Web Server on Ubuntu 22.04.
How to Configure Apache Web Server on Ubuntu 22.04
To configure Apache Web Server on Ubuntu 22.04, look at the below-given procedure.
Step 1: Check apache2 service status
Before moving towards the main configuration of Apache, we will verify the “apache2” service is currently active or not:
The below-given shows that the “apache2” service is active on our system:
You can also utilize your favorite web browser for the specified verification. To do so, open a web browser and check what the “localhost” web page beholds for you:
Navigation to the “localhost” web page indicates that Apache Web Server is working properly. Now, we will set up a virtual host in Apache.
Step 2: Set up Virtual Host in Apache Web Server
To set a virtual host in Apache, create a directory that can be utilized to store data on the website. For this purpose, we will move to the “/var/www” directory using the following “cd” command:
Then, we will create a directory for our domain “example.com”. Here, you can specify your domain name in the below-given command:
Utilize the “chown” command for changing the ownership of the “example.com” directory with the “www-data” environment variable:
Step 3: Creating a web page
To create a sample “index.html” web page for our website, we will use the “nano” editor:
Write out the following code in the opened file:
After adding the code, press “Ctrl+O” to save the file:
Step 4: Creating a Virtual host file
At this point, we have created a directory for our domain and updated its ownership. Now, we will create a virtual host file under the default directory of Apache host files:
In the opened virtual host file, add the following lines of code. Also, you have to replace the information related to “ServerName”, “ServerAlias”, and “DocumentRoot” according to your settings:
ServerAdmin admin@localhost
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Press “Ctrl+O” to save the added code of the virtual host configuration file:
Step 5: Enable Virtual host file
Execute the following “a2ensite” command for enabling the created virtual host file:
Then disable the default configuration file:
After performing the specified operation. Restart the “apache” service on your Ubuntu 22.04 system:
Step 6: Error testing
In the last step of Apache2 configuration, test the configuration errors:
In case of having an error-free configuration file, the execution of the above-given command will let you know that the Syntax is “OK”:
However, there exist chances that you may face the following error after testing the configuration file:
If that’s the case, then edit the “servername.conf” file and add your “domain” name:
After specifying the domain name in the opened configuration file, press “Ctrl+O” to save the added content:
Next, enable the new configuration with the help of the following command:
Lastly, restart the “apache2” service on your Ubuntu 22.04 system:
Step 7: Virtual Host testing
Finally, test your Virtual host by navigating to the specified domain. In our case, the domain is “example.com”:
The displayed information justifies that Apache Web Server is ready to serve on the specified domain.
Conclusion
To install Apache Web Server on Ubuntu 22.04, execute the “$ sudo apt install apache2” command and configure firewall “$ sudo ufw allow ‘Apache Full’”. Then, create a directory under the default configuration files of the Apache Web server, change its ownership, and set up a virtual host server. After performing the specified operations, restart the “apache2” service, and you are good to go. This write-up discussed the method to install and configure Apache Web Server on Ubuntu 22.04.