Hosting PHP-Based Web Application on Linux (RHEL 9, CentOS 9) with Apache

Kcsanjeeb
5 min readJan 10, 2024

--

In the ever-evolving landscape of web development, Linux remains a reliable choice for hosting dynamic websites, particularly those powered by PHP. Red Hat Enterprise Linux 9 (RHEL 9) and CentOS 9, both renowned distributions, provide a robust platform for deploying PHP applications. This guide outlines the process, step by step, from installing essential packages to hosting a simple PHP page.

Step 1 : Install Required Packages Using Yum:

Begin by installing the necessary packages using the Yum package manager. The primary packages we need are HTTPd for the web server and PHP for server-side scripting.

sudo yum -y install httpd
sudo yum -y install php
  • yum: The Yellowdog Updater Modified (YUM) is a package management utility for RPM (Red Hat Package Manager) compatible Linux systems. It simplifies the process of installing, updating, and removing software packages.
  • -y: This flag automatically assumes 'yes' to prompts, making the installation process non-interactive.
  • install: The command to install software packages.
  • httpd: This package provides the Apache HTTP Server, a widely used open-source web server.
  • php: This package includes the PHP interpreter, essential for server-side scripting in web development.

Step 2: Verify Package Installation:

After installation, it’s crucial to confirm that the packages have been installed successfully. You can use the rpm command for this:

rpm -q httpd
  • rpm: Stands for Red Hat Package Manager. It is a low-level package manager for RPM-compatible Linux systems.
  • -q: Query option. It is used to query the installed packages on the system.
  • httpd: The package name for the Apache HTTP Server.

Step 3: Start and Enable HTTPd Service:

Start the HTTPd service using the following command:

sudo systemctl start httpd

To check the status of the service, use:

sudo systemctl status httpd

Ensure that the service is not only active but also enabled to start on boot:

sudo systemctl enable httpd
  • systemctl: A central management and control utility for systemd, which is the system and service manager for Linux.
  • start: Initiates the specified service, in this case, the HTTPd service.
  • status: Displays the status of the specified service, confirming whether it is active and running.
  • enable: Configures the specified service to start automatically at boot.

Step 4: Create a PHP Page:

Navigate to the web server’s default directory and create a simple PHP page using the vi text editor:

cd /var/www/html
sudo vi index.php

/var/www/html: The default directory for web content in many Linux distributions.

Add the following PHP code to the file:

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Folder Structure

Step 5: Restart HTTPd Service:

After creating the PHP file, restart the HTTPd service to apply changes:

sudo systemctl restart httpd

restart: Stops and then starts the specified service. This is necessary to apply changes made to the PHP file.

Step 6: Configure Firewall for Web Application:

Securing your server is paramount, and enabling the firewall is a crucial step in protecting your PHP-based web application. The firewall-cmd tool provides a convenient way to manage firewall settings.

To view the current firewall configuration, including allowed services and ports, use the following command:

sudo firewall-cmd --list-all

This command displays a comprehensive list of the firewall settings, showing which services and ports are currently allowed.

output of command : firewall-cmd — list-all before adding services or port

From the image above you can see that http service, or 80/tcp port are not present. If the HTTP service is not already allowed, you can explicitly add it to the firewall rules. This is particularly important if you are relying on the default HTTP port (port 80). Use the following command:

sudo firewall-cmd --add-service=http

This command opens the firewall for the default HTTP port (port 80/tcp) and allows incoming traffic for the HTTP service.

In case you are using a custom port for your Python-based web application, you need to explicitly allow that port in the firewall. For example, to allow traffic on port 8080, use the following command:

sudo firewall-cmd --add-port=80/tcp

Replace 80 with the actual port number used by your application.

output of command : firewall-cmd — list-all after adding services or port

# To Remove Firewall Rules:

If, for any reason, you need to remove the firewall rules for the HTTP service or a specific port, use the following commands:

Remove HTTP Service Rule:

sudo firewall-cmd --remove-service=http

This command removes the rule allowing traffic for the HTTP service.

Remove Custom Port Rule:

sudo firewall-cmd --remove-port=80/tcp

Replace 80 with the port number you want to remove.

By configuring the firewall, you add an additional layer of security to your Python-based web application. Always ensure that the necessary ports and services are allowed while restricting unnecessary access. This step is vital for creating a secure hosting environment for your web applications.

Step 6: Verify Your Application in the Browser:

To confirm that your PHP-based web application is up and running, open your web browser and navigate to the IP address or domain name of your server. For example, if your server’s IP address is 192.168.208.102, enter “http://192.168.208.102" in the browser’s address bar. If configured correctly and the firewall settings are in place, you should see your Python-powered webpage displayed in the browser.

hostname -I

Alternatively, you can use:

ifconfig

Enter the IP address in your browser, and you should see the PHP page you created.

Hosted Php (index.php) file in a browser.

Step 7: Customizing IP Address with NetworkManager:

For a custom IP address, configure the network using NetworkManager. Details for this step may vary based on your network setup.

In summary, hosting a PHP-based website on Linux involves installing essential packages, configuring the web server, creating a PHP page, and accessing it via a browser. This guide provides a foundation for more complex web hosting setups and serves as a starting point for developers deploying PHP applications on RHEL 9 or CentOS 9.

--

--