Search

Easy Guide: How to Install php-fpm on Centos 7

Welcome to our easy guide on installing php-fpm on Centos 7! If you're looking to enhance the performance of your websites or applications, php-fpm is the way to go. This step-by-step installation guide will walk you through the process, ensuring a smooth setup on your Centos 7 server.

Prerequisites for Installing php-fpm on Centos 7

Before you begin the installation process, make sure you have the following prerequisites in place:

  • An account with sudo privileges: You'll need a user account that has sudo privileges to perform the installation steps.
  • A terminal window or command-line: Access to a terminal window or command-line interface is necessary to execute the installation commands.
  • yum package manager: Centos 7 comes with the yum package manager pre-installed. Make sure you have it available for installing the required packages.

Having these prerequisites met will ensure a smooth installation process for php-fpm on Centos 7.

Next, we'll move on to enabling the software repositories necessary for accessing the PHP packages.

Enabling Software Repositories for php-fpm

Before you can install php-fpm on Centos 7, you need to enable specific software repositories. Follow the steps below to ensure access to the necessary PHP packages:

  1. Install the yum-utils package by running the following command in your terminal or command-line: sudo yum install yum-utils
  2. Once the installation is complete, enable the epel-release repository by executing the command: sudo yum-config-manager --enable epel-release
  3. Next, add Remi's Software Repository, which contains the PHP packages for Centos 7. Run the following command to activate the repository: sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

By enabling these software repositories, you will have access to the PHP packages required for installing php-fpm on Centos 7.

If you encounter any issues during the enabling of repositories, please refer to the official documentation or seek assistance from the community forums.

Installing PHP 7.2 with Apache on Centos 7

Now that you have enabled the necessary repositories, it's time to install PHP 7.2 with Apache on Centos 7. This step-by-step guide will walk you through the process:

  1. Open your terminal or command-line and run the following command to install the required packages: yum install php php-common php-opcache php-mcrypt php-cli php-gd php-curl php-mysql
  2. This command will install PHP and its extensions, such as PHP-GD for image processing and PHP-MySQL for connecting to a MySQL database.
  3. Once the installation is complete, you can verify it by running the command php -v. This will display the PHP version and other information.
  4. Next, you need to configure Apache to work with PHP. Open the Apache configuration file using a text editor: sudo vi /etc/httpd/conf/httpd.conf
  5. Search for the line that starts with "DirectoryIndex" and add "index.php" at the end, separated by a space. This will enable Apache to recognize PHP files as index files.
  6. Save the changes and exit the text editor. Then, restart Apache for the changes to take effect: sudo systemctl restart httpd

That's it! You have successfully installed PHP 7.2 with Apache on Centos 7. Now you can start developing and running PHP applications on your server.

If you encounter any issues during the installation process, make sure to check the official documentation for troubleshooting or seek help from the Centos community.

Configuring PHP-FPM and Apache

Now that you have successfully installed PHP-FPM on Centos 7, it's time to configure it to work seamlessly with Apache for optimal performance. The configuration process involves updating the PHP-FPM configuration file and making changes to the Apache configuration. Follow the steps below to configure PHP-FPM and Apache:

  1. Update the PHP-FPM configuration file: Open the php-fpm configuration file located at /etc/php-fpm.d/www.conf in a text editor. Look for the listen directive and specify the socket or IP address on which PHP-FPM should listen for incoming connections. You can also modify other settings such as the process manager, maximum number of child processes, and the user/group that PHP-FPM should run as.
  2. Update the Apache configuration: Open the Apache configuration file for your website or virtual host. This file is typically located at /etc/httpd/conf.d/your_website.conf. Add the following lines within the appropriate <VirtualHost> block:

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/website/$1

SetHandler "proxy:fcgi://127.0.0.1:9000/path/to/your/website/"

  1. Replace /path/to/your/website/ with the actual path to your website's root directory.
  2. Restart PHP-FPM and Apache: Save the changes made to the configuration files and restart both PHP-FPM and Apache for the changes to take effect. Use the following commands to restart the services:

sudo systemctl restart php-fpm

sudo systemctl restart httpd

With PHP-FPM and Apache now properly configured, your website will be able to handle PHP requests efficiently. The PHP-FPM process manager will handle the execution of PHP scripts, while Apache will serve the static content and act as a reverse proxy for PHP-FPM.

Next, in section 6, we will enable PHP-FPM and Apache to start automatically on boot and start the services.

Enabling and Starting PHP-FPM and Apache

To complete the installation process and ensure that PHP-FPM and Apache are running on your Centos 7 server, you need to enable and start the services. Follow these steps:

  1. Open a terminal window or command-line on your server.
  2. Enable PHP-FPM to start on boot by running the following command:
    sudo systemctl enable php-fpm
  3. Start the PHP-FPM service using the following command:
    sudo systemctl start php-fpm
  4. Next, enable Apache to start on boot with the following command:
    sudo systemctl enable httpd
  5. Start the Apache service using the command:
    sudo systemctl start httpd

Once you have completed these steps, PHP-FPM and Apache will be enabled and running on your Centos 7 server. You can now start using PHP-FPM to process PHP requests for your websites or applications.

If you are using a firewall on your server, it is important to open the necessary ports to allow incoming connections to PHP-FPM and Apache. Consult your firewall documentation for instructions on how to open ports.

Testing the Installation

Now that you have successfully installed PHP-FPM on your Centos 7 server, it's time to test if everything is working correctly. Follow these steps to ensure that your installation is functioning as expected:

  1. Create a new file with a .php extension in your web server's document root directory. For example, you can name the file "test.php".
  2. Open the file in a text editor and add the following code:

<?php
phpinfo();
?>

This code will generate a page that displays detailed information about your PHP configuration.

  1. Save the file and access it through your web browser by entering the URL of your server followed by "/test.php". For example, if your server's IP address is 192.168.0.100, you would enter "http://192.168.0.100/test.php" into your browser's address bar.
  2. If PHP-FPM is configured correctly, you should see a page with extensive information about your PHP installation, including PHP version, server information, and PHP modules.

Common Issues and Troubleshooting

If you encounter any issues during the testing process, here are a few common problems and their possible solutions:

  1. If you see a blank page or an error message, check the file permissions of your "test.php" file and make sure it is readable by your web server.
  2. If you see the PHP code instead of the expected output, it means that PHP is not being parsed properly. Double-check your web server configuration and make sure PHP-FPM is enabled.
  3. If you are still experiencing issues, consult the documentation or community forums specific to your web server and PHP-FPM version for further troubleshooting steps.

By following these steps and addressing any potential issues, you can ensure that PHP-FPM is properly installed and functioning on your Centos 7 server.

Using Multiple PHP-FPM Pools

If you need to isolate each site to its own PHP-FPM pool instead of using a shared pool, you can easily do so. This allows for greater control and flexibility in managing multiple websites or applications on your Centos 7 server.

To begin, disable the global Apache configuration for PHP so that each site can have its own PHP-FPM pool. This can be done by commenting out the appropriate lines in the Apache configuration file.

Next, create a new PHP-FPM pool for each site. Each pool can have its own set of configurations, such as the maximum number of child processes, the user and group that the pool runs as, and the listening socket or port.

Example PHP-FPM Pool Configuration:

[website1]
user = website1_user
group = website1_group
listen = /var/run/php-fpm/website1.sock
listen.owner = website1_user
listen.group = website1_group
listen.mode = 0660
pm = dynamic
pm.max_children = 10
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
 

Finally, update the Apache virtual host configuration for each site to point to the appropriate PHP-FPM pool. This can be done by adding the `ProxyPassMatch` directive and specifying the socket or port for the corresponding PHP-FPM pool.

By using multiple PHP-FPM pools, you can efficiently manage and scale your websites or applications on Centos 7, ensuring optimal performance and isolation between sites.

Conclusion

By following this easy installation guide, you can quickly set up PHP-FPM on your Centos 7 server. PHP-FPM offers additional features that make it an excellent choice for websites of any size. With step-by-step instructions and clear explanations, you can confidently install PHP-FPM and take advantage of its benefits.

Summary of the Installation Process

In this guide, we covered all the necessary steps to install php-fpm on Centos 7. First, we ensured that you have the prerequisites, including sudo privileges and a terminal window. Then, we enabled the required software repositories, such as epel-release and Remi's Software Repository.

Next, we installed PHP 7.2 with Apache using the yum package manager. After the installation, we configured PHP-FPM and Apache to work together, optimizing performance by using sockets instead of TCP connections. We also learned how to enable and start PHP-FPM and Apache on boot.

To confirm that the installation was successful, we tested PHP-FPM by creating a simple PHP file and accessing it through the web server. Finally, we explored the option of using multiple PHP-FPM pools to isolate each site on its own pool.

Start Using PHP-FPM on Centos 7 Today

Now that you have completed the installation of PHP-FPM on Centos 7, you can start reaping the benefits of this powerful tool. PHP-FPM offers improved performance and flexibility, making it an ideal choice for websites and applications.

With this installation guide as your reference, you have the knowledge and resources to successfully set up PHP-FPM on your Centos 7 server. Enjoy the enhanced performance and features that PHP-FPM brings to your web projects!

Sys Admin

Sys Admin

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