How to Install Apache on Ubuntu 24.04?
Apache is known for its robust performance, strong security features, and ability to work well with other software components. Ubuntu 24.04, being a long-term support release, provides a stable and secure environment for running services like Apache. Together, they form a solid foundation for serving web content to users worldwide. Based on its significance, this article will explain the step-by-step instructions to install and set up a virtual host on Ubuntu 24.04:
Let’s begin with the installation.
How to Install Apache on Ubuntu 24.04?
The purpose of installing Apache on Ubuntu 24.04 is to set up a reliable and efficient web server that can handle the hosting needs of websites and web applications. To install Apache on Ubuntu 24.04, follow the below methods:
Step 1: Update Packages List
Before installing any new software, updating the system packages list is always a good convention. For this, run the below command:
sudo apt update && sudo apt upgrade
Step 2: Install Apache
Once the packages are updated, install Apache using the “apt” command below:
sudo apt install apache2
Step 3: Adjust the Firewall
Now, secure the Apache server by adjusting the firewall settings. It allows traffic on ports 80 and 443 by running:
sudo ufw enable # Activate Firewall sudo ufw app list # Display Available Rules sudo ufw allow 'Apache Full' # Allow Traffic
Step 4: Check Web Server
Users can verify that Apache is running by accessing your server’s public IP address in a web browser. It displays the default Ubuntu 24.04 Apache web page:
Note: Users can find the server IP address of the working machine via the below command:
hostname -I
Step 5: Verify Apache Installation
Users can also check the Apache services via the “systemctl” command with the “status” utility as below:
sudo systemctl status apache2
Optional: Configure Apache Services
To configure the Apache Services on Ubuntu 24.04, use the “systemctl” command with the following utilities:
sudo systemctl start apache2 # Start Apache sudo systemctl stop apache2 # Stop/Halt the Apache Daemon sudo systemctl status apache2 # Display Status sudo systemctl reload apache2 # Reload Apache sudo systemctl enable apache2 # Enable Apache to start sudo systemctl disable apache2 # Disable Apache Daemon
That is all from the installation. Let’s move forward to the configuration.
How to Set Up Virtual Hosts Using Apache on Ubuntu 24.04?
Setting up Virtual Hosts using Apache is a fundamental task for server administrators and web developers looking to host multiple websites on a single server. This process involves creating a configuration file for each website in the /etc/apache2/sites-available directory, which defines how the Apache web server responds to various domain requests.
For setting up virtual hosts using Apache on Ubuntu 24.04, follow the below steps:
Step 1: Create a Directory for Virtual Host Files
First, create a directory for the website under “/var/www/” and assign the proper permissions. Let’s create a “linuxgenie.com” directory as below:
sudo mkdir /var/www/linuxgenie.com
Note: Or navigate to “/var/www/html” to find the default index.html file. Then, replace or modify it with your website’s files.
Step 2: Assign Ownership of the directory
Now, assign the ownership permission to the directory with the below command:
sudo chown -R $USER:$USER /var/www/linuxgenie.com
Step 3: Create a Webpage
It a time to create the web page named “index.html” under the above-created “linuxgenie.com” directory:
sudo nano /var/www/linuxgenie.com/index.html
Add the following lines in the .html file. Users can also display webpage content based on preferences/needs:
<html> <head> <title>Hello Linuxgenie Users</title> </head> <body> <h2>Linuxgenie is empowering linux users</h2> </body> </html>
Step 4: Create a New Virtual Host File
Now, create and edit a new virtual host file in “/etc/apache2/sites-available/your_domain.conf” using a Nano (text editor). Our configuration file named “linuxgenie.com.conf” is as below:
sudo nano /etc/apache2/sites-available/linuxgenie.com.conf
Add the following configuration to the file, replacing “linuxgenie.com” with your actual domain name:
<VirtualHost *:80> ServerAdmin webmaster@localhost ServerName linuxgenie.com ServerAlias www.linuxgenie.com DocumentRoot /var/www/linuxgenie.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Note: By default, Apache serves its website in the “/etc/apache2/sites-available/000-default.conf” file.
Step 5: Enable the New Virtual Host Files
Now, enable new sites using the “a2ensite” utility by mentioning the “linuxgenie.com.conf” host file:
sudo a2ensite linuxgenie.com.conf
Optional: Users can also disable the default site with the “a2dissite” command by specifying the configuration file name “000-default.conf” as below:
sudo a2dissite 000-default.conf
Step 6: Test Configuration Errors
Now, users can test for configuration errors with the “apache2ctl” command with the “configtest” utility. If the output says “Syntax OK”, then proceed:
sudo apache2ctl configtest
If users find any issue add the server name with the IP address in the “servername.conf” configuration file such as “10.0.2.15”. Then, enable the new virtual Host files using the “sudo a2enconf servername” command:
sudo nano /etc/apache2/conf-available/servername.conf
Or add the server name with the IP address on the “/etc/apache2/apache2.conf” file:
Step 7: Reload Apache
Now, users need to reload the Apache services (for applying changes) using the systemctl command:
sudo systemctl reload apache2
Step 8: Verification
Finally, ensure the domain name is pointed to the server’s IP address on the web browser:
Remember to replace “domain name” with your actual domain name and “your_server_IP” with the server’s IP address.
Optional: Secure Apache With Encryption
Secure your site with a free TLS/SSL certificate from Encryption by installing Certbot and running the below command:
sudo apt install certbot python3-certbot-apache sudo certbot --apache
By following these steps, you’ll have a basic website running on Apache in Ubuntu 24.04. Now, your virtual hosts are set up and ready to serve the domain.
Conclusion
To install/configure Apache on Ubuntu 24.04, update system packages and install Apache using the “sudo apt install apache2” command. Then, enable Apache to run on boot with “sudo systemctl enable apache2”. For starting the Apache service, use the “sudo systemctl start apache2” command. Finally, open the web browser and verify that Apache is running by visiting “http://your_server_ip”.
After installation, users can set up virtual hosts by following the above procedure. In this way, Apache has been successfully installed as well as configured on Ubuntu 24.04.