Posted on
Apache Web Server

Installing Apache with PHP support (`libapache2-mod-php`)

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

How to Install Apache with PHP Support Using libapache2-mod-php on Linux

When setting up a web server, Apache HTTP Server combined with PHP is one of the most popular stacks due to its efficiency, extensive support, and flexibility. In this article, we'll walk through the installation of Apache with PHP support via the libapache2-mod-php package on a Linux system. This guide is beginner-friendly and hopes to equip you with the necessary knowledge to get your web server running smoothly with PHP supported by Apache.

Step 1: Update Your System

Before installing any new packages, it's a good practice to update your system's package list to ensure you are installing the latest versions available. Open your terminal and run:

Using apt (Ubuntu)

sudo apt update
sudo apt upgrade

Using dnf (RHEL, CentOS, Fedora)

sudo dnf check-update
sudo dnf upgrade

Using zypper (openSUSE)

sudo zypper refresh
sudo zypper up

This command will update the package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories.

Step 2: Install Apache

Apache is available in the default Linux repositories and can be easily installed using the package management system. To install Apache, run:

Using apt (Ubuntu)

sudo apt install apache2

Using dnf (RHEL, CentOS, Fedora)

sudo dnf install httpd

Using zypper (openSUSE)

sudo zypper install apache2

Once the installation is complete, you can check the status of Apache to confirm that it’s running:

Common command across distributions

sudo systemctl status apache2

You should see a status indicating that the service is active. Additionally, you can verify that Apache is properly installed by accessing your server’s IP address or localhost in your web browser, which should display the Apache2 default page.

Step 3: Install PHP and the Apache PHP Module

To install PHP and the Apache PHP module, which allows Apache to handle PHP files, use the following command:

Using apt (Ubuntu)

sudo apt install php libapache2-mod-php

Using dnf (RHEL, CentOS, Fedora)

sudo dnf install php php-mysqlnd

Using zypper (openSUSE)

sudo zypper install php7 php7-apache2-module

This command installs the latest version of PHP and the Apache 2 PHP module on your system. After the installation, it's generally a good idea to restart Apache to ensure all new configurations are applied:

Common command across distributions

sudo systemctl restart apache2

Step 4: Test PHP Processing

To test if Apache is correctly processing PHP files, create a simple PHP script:

echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/phpinfo.php

This command creates a file named phpinfo.php in the web root directory with a basic PHP script that outputs information about PHP's configuration. You can view this page by navigating to http://your_server_ip/phpinfo.php in your web browser. If PHP is configured correctly, this page will display a lot of information about your PHP installation.

Step 5: Secure Your PHP Installation

Now that PHP is installed, consider securing it by modifying some settings in the php.ini file (usually located in /etc/php/VERSION/apache2/php.ini, where VERSION is your PHP version). Critical settings to review include:

  • expose_php = Off
  • display_errors = Off in a production environment
  • disable_functions to list dangerous PHP functions you want to disable

Make sure to restart Apache after making changes to the PHP configuration:

Common command across distributions

sudo systemctl restart apache2

Conclusion

Setting up Apache with PHP on a Linux system is straightforward, especially with the help of the libapache2-mod-php package which simplifies configuring PHP to run under Apache. Whether you're preparing a development environment or deploying a production server, this setup is reliable and capable of serving dynamic web content. While this guide primarily focused on installation and basic configuration, ensure you delve into security practices and advanced configurations to make the best use of your Apache PHP setup. This fundamental knowledge will help you maintain a robust, secure, and efficient web server.

Further Reading

Here are some further reading examples to deepen your understanding and knowledge of setting up and optimizing web servers:

  1. Apache HTTP Server Documentation - Offers comprehensive information about configuration, module details, and security best practices. Apache Documentation

  2. PHP: The Right Way - A practical guide to PHP best practices and accepted coding standards. PHP: The Right Way

  3. DigitalOcean Community Tutorials - A resource for practical examples and tutorials on setting up Apache with PHP. DigitalOcean Apache PHP Tutorials

  4. Apache Performance Tuning - Strategies for optimizing the performance of your Apache web server. Apache Performance Tuning

  5. Securing Apache and PHP - Guides to securing your Apache server and PHP installation to safeguard against common vulnerabilities. Securing Apache Web Server
    PHP Security Guide

These resources should provide a robust foundation for both setting up a new server and enhancing the security and performance of an existing server.