Vishal

Posted on November 15th

How to install PHP-FPM with Apache on Ubuntu

"How to install PHP-FPM with Apache on Ubuntu"

About Apache

Apache is a fast and flexible web server application with open-source code, which has greatly contributed to shaping the current Internet. Called the Apache HTTP Server, it was created and launched by the Apache Software Foundation or ASF in 1995. It has since made its way to being one of the top web server systems because it is believed to be fast, highly customizable, and full-featured.

In its basic form, Apache is a disseminating tool for web information and application content over the World Wide Web. It handles HTTP requests from clients (for instance, web browsers) and provides the requested results, documents, or resources. Apache has a modular design as a basic concept, which has been developed in such a way that numerous modules can be created by users to integrate with the basic functionalities of Apache. These modules can support other tasks, including authentication, rewriting URLs, or load distribution, making Apache versatile.

Apache is quite flexible, which can be considered one of the company’s major benefits. Contrary to many other programming languages, the server can be tailored to fit almost any requirements – from a personal website to a large-scale and popular application. Its configuration is made by means of text files, of which the most essential is the httpd.conf file. This configuration file enables the system administrator to identify the parameters of the server's operation, security features, performance, and virtual hosting parameters, among others.

Another significant feature is Apache's support of Virtual Hosts. Virtual hosting, in addition to creating the impression of multiple Apache servers, enables one actual server to host multiple websites or domains. This is especially helpful for web hosting service providers or an organization that may have several websites and needs to host them on one server. Apache supports both name-based and IP-based virtual hosting; this allows a lot of flexibility regarding how the sites are configured.

What is Ubuntu?

Ubuntu is one of the most widely used versions of GNU/Linux which is based on the Debian distribution, intended to provide an OS for as many people as possible. This is one of the most preferred and widely used Linux distributions as it can attract new users and skilled Linux professionals owing to its user-friendly interface, availability of a large number of contributions from the users, and high level of security.

Overview

  • Ubuntu operating system was released in October 2004 by the company founded by South Africa’s Mark Shuttleworth called Canonical Ltd. Because of the focus on community and affordable pricing, the name Ubuntu itself comes from African languages and translates to ‘humanity towards others’. Ubuntu is free to download, use, and share, and it comes in several flavors, each tailored to different use cases: Ubuntu is free to download, use, and share, and it comes in several flavors, each tailored to different use cases:
  • Ubuntu Desktop: The professional edition for home computers and notebooks. It utilizes a graphical user interface, and the following are some of the installed programs, for instance, Firefox Internet Browser, LibreOffice Suite, and many more multimedia programs. Ubuntu Server: A web server version to satisfy the needs of developers to have a fast, reliable, and secure Internet environment for Web hosting, cloud computing, and data centers.
  • Ubuntu Desktop: The standard version for personal computers and laptops. It has a graphical user interface and comes with preloaded software, for example, the Firefox internet browser, LibreOffice suite, and several multimedia applications.
  • Ubuntu Server: A web server version to meet the needs of developers and provide a fast, robust, and secure environment for web hosting, cloud computing, or data centers. It does not have a graphical user interface by default because it is intended for server tasks and optimized for performance.
  • Ubuntu Core: A lightweight, locked-down version for IoT and embedded systems that utilize snaps (self-contained application packages for security and to avoid hiccups).
  • Ubuntu Studio: A creative edition targeted towards artists and designers, allowing them to edit audio, video, stencils, and graphics in a single application.
  • Ubuntu Kylin: A version designed with the Chinese government’s endorsement and adjusted to Chinese conditions and preferences.

Features of Ubuntu

  1. User-Friendly Interface: The underlying GUI environment of Ubuntu has always been based on GNOME, although Ubuntu switched from its in-house developed Unity in 2017. One of the best things about Ubuntu is that it provides a bright and clear interface, which provides an opportunity for an average user who doesn’t want to deal deeply with Linux.
  2. Software and Package Management: It utilizes the APT (Advanced Package Tool) as the package manager, which makes it very convenient for the installation, updating, and removal of software. It also supports a special kind of package called “snap” packages, which are applications that contain all necessary dependencies.
  3. Security: One of Ubuntu’s strengths is the security of this operating system. It emphasizes security with features such as updates, LTS versions, and security features like firewalls and encrypts.
  4. Community and Support: Ubuntu has a powerful and dynamic community base that participates in the development of its new versions and also helps in case of problems a user may face. It has professional support services, thus making it suitable for businesses and enterprises to obtain their products from Canonical.
  5. Customization: Ubuntu is extremely configurable. When implemented, it provides an exceptionally high degree of customization. Users can select various graphical interfaces, adjust the configuration, and change the look of the operating system.
  6. Performance: Ubuntu is very friendly to all types of systems, ranging from older to current systems. It is especially known for its stability and, more importantly, operational performance, which qualifies it for both desktop and server use.

Update the System

First, update your package list by typing this command:

sudo apt update

Install Apache

If Apache is not already installed on your system, run this command to install it:

sudo apt install apache2

After installing, Start Apache with this command

sudo systemctl start apache2

Ensure that Apache is running:

sudo systemctl status apache2

Install PHP and PHP-FPM

Install PHP and the PHP FastCGI Process Manager (PHP-FPM) along with some common PHP extensions:

sudo apt install php php-fpm libapache2-mod-fcgid -y

Configure Apache to Use PHP-FPM

Enable the proxy_fcgi module and the setenvif module for Apache:

sudo a2enmod proxy_fcgi setenvif

Disable the default PHP module (if enabled):

sudo a2dismod php

Enable the mpm_event module (recommended for PHP-FPM):

sudo a2dismod mpm_prefork
sudo a2enmod mpm_event

Enable the PHP-FPM configuration:

sudo a2enconf php*-fpm

Restart Apache

Restart Apache to apply the changes:

sudo systemctl restart apache2

Test PHP-FPM Configuration

Create a test PHP file in the Apache web root to verify that PHP-FPM is working correctly:

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

Visit http://your-server-ip/info.php in your web browser. You should see the PHP information page if PHP-FPM is installed and configured correctly.

Secure Your Installation

After confirming that PHP-FPM is working, delete the info.php file to prevent unauthorized access to sensitive information:

sudo rm /var/www/html/info.php

Additional Steps (Optional)

  • Install Additional PHP Extensions: You may need to install other PHP extensions based on your application's requirements. You can search for available extensions using:

    sudo apt-cache search php
    
  • Configure PHP-FPM Settings: You can customize PHP-FPM settings by editing /etc/php/*/fpm/php.ini or /etc/php/*/fpm/pool.d/www.conf.
  • Secure Your Server: Always keep your server updated and follow best practices for securing Apache and PHP.

By following these steps, you should have PHP-FPM working with Apache on your Ubuntu system.

Comments

Leave a comment.

Share your thoughts or ask a question to be added in the loop.