Introduction
Apache HTTP Server, commonly referred to as Apache, is a powerful and widely used open-source web server software. Apache 2.4 is the latest version, boasting enhanced performance, security features, and scalability. If you’re running an Ubuntu 22.04, this comprehensive guide will take you through the step-by-step process of how to Install Apache 2.4 on Ubuntu 22.04. By following these instructions, you’ll have a robust foundation for hosting websites, applications, and more.
Table of Contents
- Introduction
- Why Choose Apache 2.4?
- Installing Apache 2.4 on Ubuntu 22.04
- Step 1: Update System Packages on Ubuntu 22.04
- Step 2: Install Apache 2.4 on Ubuntu 22.04
- Step 3: Check Apache version on Ubuntu 22.04
- Step 4: Check Apache service on Ubuntu 22.04
- Step 5: Configure Firewall on Ubuntu 22.04
- Step 6: Test Apache Web Server
- Step 7: Explore Apache Configuration
- Step 8: Setting Up Virtual Hosts in Apache2
- Step 9: Secure Your Website with HTTPS (Optional)
- Step 10: Optimize Performance
- Conclusion
Why Choose Apache 2.4?
Before we dive into the installation steps, let’s understand the benefits of Apache 2.4 and why it’s an excellent choice:
- Performance Improvements: Apache 2.4 introduces several performance enhancements, such as reduced memory usage and better handling of concurrent requests. This ensures smoother operations, even during high traffic periods.
- Enhanced Security: Security is a top priority. Apache 2.4 includes advanced security features like improved authentication and authorization modules, making it more resilient against cyber threats.
- Modular Architecture: Apache’s modular architecture allows you to add or remove modules as needed, optimizing the server for your specific use case.
Installing Apache 2.4 on Ubuntu 22.04
Follow these steps to install Apache 2.4 on your Ubuntu 22.04 and harness its powerful features for your web hosting needs:
Step 1: Update System Packages on Ubuntu 22.04
Begin by updating the package list to ensure you’re working with the latest software versions of your Ubuntu 22.04:
samm@apache:~$ sudo apt-get update
samm@apache:~$ sudo apt-get upgrade
This ensures your system is up-to-date before installing new software.
Step 2: Install Apache 2.4 on Ubuntu 22.04
With the package list updated, install Apache 2.4 using the following command:
samm@apache:~$ sudo apt install -y apache2
During installation, any required dependencies will also be installed automatically.
Step 3: Check Apache version on Ubuntu 22.04
Check for the currently active version of PHP on your Ubuntu 22.04 with the following command:
samm@apache:~$ apache2 -v
Server version: Apache/2.4.54 (Ubuntu)
Server built: 2023-06-08T15:59:07
Step 4: Check Apache service on Ubuntu 22.04
Once the installation is complete, Apache 2.4 on your Ubuntu 22.04 should be up and running. You can check its status using the following command:
samm@apache:~$ sudo systemctl status apache2
● apache2.service - Apache 2.4 HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2022-07-08 00:10:04 WIB; 1s ago
Docs: https://httpd.apache.org/docs/2.4/
Process: 31247 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
Main PID: 31276 (apache2)
Tasks: 11 (limit: 9442)
Memory: 22.8M
CGroup: /system.slice/apache2.service
├─31276 /usr/sbin/apache2 -k start
├─31278 /usr/sbin/apache2 -k start
├─31279 /usr/sbin/apache2 -k start
├─31280 /usr/sbin/apache2 -k start
├─31281 /usr/sbin/apache2 -k start
└─31284 /usr/sbin/apache2 -k start
Jul 08 00:10:04 app-vdam systemd[1]: Starting Apache 2.4 HTTP Server...
Jul 08 00:10:04 app-vdam systemd[1]: Started Apache 2.4 HTTP Server.
If the status shows “active (running),” it means Apache 2.4 on Ubuntu 22.04 has been successfully installed and is operational.
PHP application with Apache 2.4 web server
In Apache we can use a built-in module called mod_php to process PHP code directly within the Apache process.
But first you need to install Apache web server package, PHP, and Apache PHP, this time we use php version 7.4, you can use the other php versions.
samm@apache:~$ sudo apt install libapache2-mod-php7.4
Enable mod_php module:
samm@apache:~$ sudo a2enmod php7.4
Restart Apache web server after the configuration.
samm@apache:~$ sudo systemctl restart apache2
Step 5: Configure Firewall on Ubuntu 22.04
To allow external traffic to access your web server, you need to configure the firewall. Use ufw
(Uncomplicated Firewall) to enable the necessary ports:
samm@apache:~$ sudo ufw allow http
samm@apache:~$ sudo ufw allow https
samm@apache:~$ sudo ufw reload
Or you can just allow “Apache Full”
samm@apache:~$ sudo ufw allow 'Apache Full'
The “Apache Full” profile allows both HTTP (port 80) and HTTPS (port 443) traffic.
Step 6: Test Apache Web Server
Open a web browser and enter your server’s IP address or domain name. If Apache 2.4 is functioning correctly, you should see the default Apache page.
The displayed output above provides confirmation of the successful installation and proper functioning of the Apache Web Server on Rocky Linux 9. Typically, basic Apache installations are designed to support single-page websites. For in-depth insights into setting up and deploying a Single Page Website in a production environment, we recommend exploring our comprehensive LAMP setup guide. Moving forward, the subsequent section delves into more advanced configurations within the Apache setup.
Step 7: Explore Apache Configuration
Apache’s configuration files are located in the /etc/apache2
directory. The main configuration file is apache2.conf
, while site-specific configurations are stored in the sites-available
and sites-enabled
directories. To make changes to the configuration, use a text editor:
samm@apache:~$ sudo vi /etc/apache2/apache2.conf
Remember to restart Apache after making configuration changes:
samm@apache:~$ sudo systemctl restart apache2
Step 8: Setting Up Virtual Hosts in Apache2
Create the directory for your_domain as follows:
samm@apache:~$ sudo mkdir /var/www/sammlinux
Next, assign ownership of the directory to the user you’re currently signed in as with the $USER environment variable:
samm@apache:~$ sudo chown -R $USER:$USER /var/www/sammlinux
To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:
samm@apache:~$ sudo chmod -R 755 /var/www/sammlinux
Next, create a sample index.html page using vim editor:
samm@apache:~$ sudo vi /var/www/sammlinux/index.html
Inside, add the following sample HTML:
<html>
<head>
<title>Welcome to sammlinux!</title>
</head>
<body>
<h1>Success! Welcome to sammlinux. And its working</h1>
</body>
</html>
Save and close the file when you are finished.
Make a new one at /etc/apache2/sites-available/sammlinux.conf:
samm@apache:~$ sudo vi /etc/apache2/sites-available/sammlinux.conf
Add in the following configuration block, which is similar to the default, but updated for your new directory and domain name:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName sammlinux.com
ServerAlias www.sammlinux.com
DocumentRoot /var/www/sammlinux
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Save and close the file when you are finished.
Now enable the file with the a2ensite tool:
samm@apache:~$ sudo a2ensite sammlinux.conf
Disable the default site defined in 000-default.conf:
samm@apache:~$ sudo a2dissite 000-default.conf
Next, test for configuration errors:
samm@apache:~$ sudo apache2ctl configtest
You should receive the following output
. . .
Syntax OK
Restart Apache to implement your changes:
samm@apache:~$ sudo systemctl restart apache2
You can test this by navigating to http://sammlinux.com
Step 9: Secure Your Website with HTTPS (Optional)
For secure data transmission, consider setting up HTTPS using SSL/TLS certificates. You can use Let’s Encrypt to obtain free SSL certificates.
Step 10: Optimize Performance
To further optimize Apache’s performance, you can adjust various settings. For instance, you can adjust the KeepAlive
timeout to manage how long a connection is kept open:
samm@apache:~$ sudo vi /etc/apache2/apache2.conf
Inside the file, modify the KeepAliveTimeout
value according to your requirements.
Conclusion
Installing Apache 2.4 on your Ubuntu Server 22.04 opens the door to a world of powerful web hosting capabilities. Following this step-by-step guide ensures a seamless installation process, granting you access to enhanced performance, security, and scalability. With Apache 2.4 as your web server, you’re poised to provide a robust and secure environment for hosting websites, applications, and services. Embrace the power of Apache 2.4 and propel your online presence to new heights.
Also Read Our Other Guides :
- How To Install Apache on Rocky Linux 9
- How To Install Apache Web Server on CentOS 7
- How To Install Nginx on Ubuntu 22.04: A Comprehensive Guide
- How To Install PHP 7.4 on Ubuntu 22.04
- How To Install MariaDB 10.6 on Ubuntu Server 22.04
Finally, now you have learned how to Install Apache on Ubuntu on your Ubuntu 22.04 Server.