Search

Easy Guide: How to Install Apache2 on CentOS 9 Step-by-Step

Apache is a widely used web server that allows you to deliver HTML and image content via HTTP or HTTPS protocols. In this guide, we will walk you through the step-by-step process of installing Apache2 on CentOS 9. By following these instructions, you will be able to set up your server quickly and efficiently.

What is Apache Web Server and its Function?

The Apache web server is one of the most popular web servers used today. It is designed to deliver HTML and image content to browsers through the HTTP or HTTPS protocols. When you enter a web address in your browser, the browser sends a request to the web server. If the requested page is present, it is displayed in your browser.

The web server plays a crucial role in serving websites and enabling online activities such as shopping, social networking, and more. Without a web server, websites would not be accessible on the internet. Apache web server, with its robust and reliable performance, ensures that websites are delivered efficiently to users.

"The Apache web server is the backbone of the internet, powering a large percentage of websites worldwide."

In addition to delivering web content, Apache web server also supports various features and functionalities. It enables the execution of server-side scripts, such as PHP, to generate dynamic web pages. It provides support for virtual hosting, allowing multiple websites to be hosted on a single server. Apache web server is also highly customizable, allowing administrators to configure various settings to optimize performance and security.

The Function of Apache Web Server:

  • Delivering HTML and image content to browsers
  • Executing server-side scripts for dynamic web pages
  • Supporting virtual hosting for multiple websites
  • Enabling custom configuration for performance and security

Overall, Apache web server is a reliable and versatile platform that powers a significant portion of the internet. Its role in delivering web content and supporting various functionalities makes it an essential component for website owners and administrators.

Why Choose Apache Web Server?

When it comes to choosing a web server for your CentOS 9 server, Apache2 is an excellent choice. With over 20 years of experience and widespread usage, Apache2 has proven to be reliable, performant, and versatile.

One of the greatest advantages of Apache2 is its compatibility with various platforms, including CentOS 9. Whether you're running a personal blog, an e-commerce website, or a web application built on popular CMSs like WordPress or Joomla, Apache2 provides excellent support for hosting dynamic websites.

Support for Dynamic Websites

If you're using a content management system (CMS) like WordPress, Joomla, or Nextcloud, Apache2 is the ideal web server. It seamlessly integrates with these CMSs and employs various modules to efficiently process PHP scripts, manage databases, and deliver dynamic content to your visitors.

Flexibility for Web Application Developers

Apache2's ability to start external programs, regardless of the programming language used, makes it a popular choice for web application developers. Whether you're working with Python, Ruby, or any other language, Apache2 provides a flexible platform to host your web applications.

With its robust features, extensive documentation, and a large community of users, Apache2 is a trusted and powerful web server that can handle the demands of modern websites and web applications on your CentOS 9 server.

Steps to Install Apache2 on CentOS 9

Installing Apache2 on CentOS 9 is a straightforward process that can be completed in a few simple steps. Follow the instructions below to install Apache2 on your CentOS 9 server:

  1. Update your system: Before installing any software, it's always a good idea to update your system. Run the following command to update your CentOS 9 server:
sudo yum update
  1. Install Apache2: Once your system is up to date, you can proceed with the installation of Apache2. Run the following command to install Apache2:
sudo yum install httpd
  1. Start Apache2: After the installation is complete, start the Apache2 service by running the following command:
sudo systemctl start httpd
  1. Enable Apache2: To ensure that Apache2 starts automatically when your system boots up, run the following command:
sudo systemctl enable httpd
  1. Verify Apache2 installation: To confirm that Apache2 has been installed successfully, open a web browser and enter your server's IP address or domain name. If you see the Apache2 default page, it means that the installation was successful.

That's it! You have now successfully installed Apache2 on your CentOS 9 server. You can now begin configuring Apache2 to suit your specific needs and start hosting your websites or web applications.

Installation on Ubuntu and Debian Linux

If you are using Ubuntu or Debian-based Linux distributions, the process of installing Apache2 is quite similar to CentOS 9. Follow these steps to get Apache2 up and running on your system:

  1. Open the terminal on your Ubuntu or Debian system.
  2. Update the package lists by running the command: sudo apt update.
  3. Install Apache2 by executing the command: sudo apt install apache2.
  4. During the installation, you may be prompted to confirm and proceed. Simply type 'Y' and hit enter.
  5. Once the installation is complete, Apache2 will start automatically. You can verify the installation by opening a web browser and entering 'localhost' in the address bar. If you see the default Apache2 page, it means Apache2 has been successfully installed.

Note: If you encounter any issues during the installation process, make sure you have a stable internet connection and check for any error messages in the terminal. You can also refer to the official documentation or seek support from the Ubuntu or Debian community.

Configuring Apache2 on Ubuntu and Debian

After the installation, you can customize the Apache2 configuration to suit your specific needs. The main configuration file is located at /etc/apache2/apache2.conf. You can edit this file using a text editor of your choice.

Some common configuration tasks include changing the default document root, enabling or disabling modules, and setting up virtual hosts. Make sure to restart the Apache2 service after making any changes to the configuration file using the command: sudo systemctl restart apache2.

Now that Apache2 is installed and configured on your Ubuntu or Debian system, you are ready to start hosting websites and web applications. Enjoy the power and flexibility of this popular web server!

Installation on RedHat, Fedora, Amalinux, Rocky, CentOS, Oracle, Amazon Linux

Installing Apache2 on RedHat-based systems like CentOS 9 is a straightforward process. Follow the steps below to successfully install and configure Apache2 on your server.

  1. Update the system: Before proceeding with the installation, it is recommended to update the system packages. Run the following command in the terminal:
$ sudo yum update
  1. Install Apache2: Use the yum package manager to install Apache2 by running the following command:
$ sudo yum install httpd
  1. Start the Apache2 service: Once the installation is complete, start the Apache2 service using the following command:
$ sudo systemctl start httpd
  1. Enable Apache2 to start on boot: To ensure that Apache2 starts automatically on system boot, run the following command:
$ sudo systemctl enable httpd
  1. Adjust firewall settings: If you have a firewall enabled, you need to allow incoming HTTP and HTTPS traffic. Use the following commands to open the necessary ports:
$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --permanent --add-service=https
$ sudo firewall-cmd --reload

Configuring Virtual Hosts

If you want to host multiple websites on your Apache2 server, you can configure virtual hosts. Virtual hosts allow you to run multiple websites on the same server, each with its own domain name and content. Follow the steps below to configure virtual hosts:

  1. Create a new virtual host configuration file: Run the following command to create a new configuration file for your virtual host:
$ sudo vi /etc/httpd/conf.d/example.conf
  1. Edit the virtual host file: Use a text editor to edit the virtual host file. Replace "example.com" with your domain name and update the document root directory if necessary. Save the changes and exit the editor.
<VirtualHost *:80>
  ServerName example.com
  DocumentRoot /var/www/example
  <Directory /var/www/example>
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>
  1. Restart Apache2: After configuring the virtual host, restart Apache2 to apply the changes:
$ sudo systemctl restart httpd

Congratulations! You have successfully installed and configured Apache2 on your RedHat-based system. Now you can start hosting websites and serving content through your Apache2 server.

Managing Apache2 Service

Once you have successfully installed Apache2 on CentOS 9, you can easily manage the service using a variety of commands. These commands allow you to start, stop, restart, and check the status of the Apache2 service. Here are some of the most commonly used commands:

Starting Apache2 Service

To start the Apache2 service, use the following command:

sudo systemctl start apache2

Stopping Apache2 Service

To stop the Apache2 service, use the following command:

sudo systemctl stop apache2

Restarting Apache2 Service

If you make changes to the Apache2 configuration or simply need to restart the service, use the following command:

sudo systemctl restart apache2

Checking Apache2 Service Status

To check the status of the Apache2 service and see if it is running, use the following command:

sudo systemctl status apache2

By using these commands, you have full control over the Apache2 service on your CentOS 9 server. Whether you need to start, stop, restart, or check the status, managing Apache2 is made simple and efficient.

Customizing Apache2 Configuration

After successfully installing Apache2 on CentOS 9, you have the flexibility to customize its configuration to meet your specific needs. The Apache2 configuration file, located at /etc/apache2/apache2.conf, allows you to modify various settings such as the DocumentRoot and listen port.

To make changes to the configuration file, open it using a text editor of your choice. You can use the vi command followed by the file path:

vi /etc/apache2/apache2.conf

Within the configuration file, you'll find a range of directives that control the behavior of Apache2. Here are some key parameters you can customize:

DocumentRoot

The DocumentRoot directive specifies the directory where your website files are stored. By default, Apache2 serves content from /var/www/html. If you want to change this directory, update the value after the directive, save the file, and exit the text editor. For example, to change the DocumentRoot to /var/www/mywebsite, modify the line:

DocumentRoot /var/www/html

to:

DocumentRoot /var/www/mywebsite

Listen Port

The Listen directive determines the port on which Apache2 listens for incoming connections. By default, Apache2 listens on port 80. If you wish to change the port, locate the line:

Listen 80

and update the port number. For example, to change the port to 8080, modify the line to:

Listen 8080

After making any changes to the configuration file, save it and exit the text editor. Finally, restart the Apache2 service for the changes to take effect:

sudo systemctl restart apache2

Congratulations! You have successfully customized the Apache2 configuration on your CentOS 9 server. Feel free to explore the configuration file further to customize other aspects of Apache2 based on your requirements.

Uninstalling Apache2

If you no longer need Apache2 on your CentOS 9 server and want to uninstall it, follow these simple steps:

  1. Open your terminal or SSH into your CentOS 9 server.
  2. Switch to the root user or use the sudo command to gain root privileges.
  3. Run the following command to uninstall Apache2:

sudo yum remove httpd

This command will remove Apache2 and any dependencies associated with it from your server. You may be prompted to confirm the removal, so simply type 'y' and press Enter to continue.

Once the uninstallation process is complete, Apache2 will be removed from your CentOS 9 server, freeing up system resources and disk space.

Conclusion

Congratulations! You have successfully installed and configured Apache2 on CentOS 9. By following the step-by-step instructions in this guide, you have set up your web server quickly and efficiently. Now, you can serve websites and web applications with ease.

Apache2 is a powerful and widely used web server that has been trusted by users for over 20 years. Its reliability and performance make it a top choice for serving dynamic websites created using popular CMSs like WordPress, Joomla, and Nextcloud. Additionally, Apache2's ability to start external programs regardless of the programming language used makes it a favorite among web application developers.

Remember to regularly update and secure your server to ensure optimal performance and protect against vulnerabilities. Keep an eye out for any new releases or security patches for Apache2, as they will help keep your server safe and up-to-date. With your Apache2 web server up and running, you can now enjoy the benefits of fast and efficient website delivery.

Sys Admin

Sys Admin

I am a Red Hat Certified Engineer (RHCE) and working as an IT Professional since 2012...