Learn how to install Apache2 on Ubuntu 24.04 LTS. Our Apache Support team is here to answer queries and concerns.
How to Install Apache2 on Ubuntu 24.04 LTS
Apache 2 is a widely used and reliable web server available today. With the release of Ubuntu 24.04 LTS, it’s the perfect time to take advantage of its long-term support and install Apache2 for hosting web applications.
Today, we will walk through the complete process of installing, configuring, and securing Apache 2 on Ubuntu 24.04 LTS.
An Overview:
Prerequisites
Before starting, ensure that a server running Ubuntu 24.04 LTS and SSH access with administrative (sudo) privileges is available.
Step 1. Install Apache2 Web Server
First, log in to the Ubuntu server and install Apache2 using the following command:
sudo apt install apache2 -y
Copy Code
After the installation is complete, start the Apache service:
sudo systemctl start apache2
Copy Code
Next, check the status of the service:
sudo systemctl status apache2
Copy Code
We will see an output indicating that Apache2 is active and running:
apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running)
Copy Code
For managing Apache without service disruption, consider using a graceful restart method, especially on production environments.
Step 2. Create a Directory for Your Web Application
The default Apache document root is `/var/www/html`.
Now, let’s create a dedicated directory for the application:
sudo mkdir /var/www/html/myapp
Copy Code
Furthermore, assign ownership of the directory to the current user to avoid permission issues:
sudo chown -R $USER:$USER /var/www/html/myapp
Copy Code
Step 3. Create a Simple Web Page
Go to the new web directory and create an HTML file:
cd /var/www/html/myapp
nano index.html
Copy Code
Then, paste the following HTML code:
<!DOCTYPE html>
<html>
<head>
<title>My Simple Web Application</title>
</head>
<body>
<h1>Welcome to My Web App!</h1>
<p>This is a simple web application running on an Apache server.</p>
</body>
<html>
Now, save and close the file.
Step 4. Configure Apache to Serve Your App
To have Apache serve the app either as the default site or under a custom domain, we need to create a new virtual host configuration:
sudo nano /etc/apache2/sites-available/myapp.conf
Copy Code
Now, add the following configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/myapp
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/html/myapp>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
Copy Code
To enable the new virtual host:
sudo a2ensite myapp.conf
Copy Code
Now, reload and restart Apache to apply the changes:
sudo systemctl reload apache2
sudo systemctl restart apache2
Copy Code
Step 5. Configure Apache to Listen on Custom Ports
Apache listens on port 80 by default. To add another port like 8080:
sudo nano /etc/apache2/ports.conf
Copy Code
We can add or modify to include:
Listen 80
Listen 8080
Copy Code
To update the Virtual Host configuration:
sudo nano /etc/apache2/sites-available/000-default.conf
Copy Code
We can modify or add as seen below:
<VirtualHost *:8080>
ServerAdmin webmaster@example.com
ServerName example.com
DocumentRoot /var/www/example.com/public_html
Copy Code
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
To restart Apache to apply new port settings:
sudo systemctl restart apache2
Copy Code
Step 6. Configure Firewall Rules for Apache2
Now, we have to make sure that the firewall allows web traffic by enabling UFW:
sudo ufw enable
Copy Code
To allow Apache traffic (HTTP and HTTPS):
sudo ufw allow 'Apache Full'
Copy Code
We can verify UFW status by running this command:
sudo ufw status
Copy Code
We will see rules like:
Apache Full ALLOW Anywhere
Apache Full (v6) ALLOW Anywhere (v6)
Copy Code
[Need assistance with a different issue? Our team is available 24/7.]
Conclusion
Apache provides the flexibility and reliability needed for any web environment, from setting up the initial server to configuring virtual hosts and firewall rules.
In brief, our Support Experts demonstrated how to use Certbot with Linode DNS for automated SSL Certificates.
0 Comments