Centos httpd php fpm

CentOS 7 Apache 2.4 with PHP-FPM

PHP-FPM does have some advantages depending on the solution and the common path is to use Nginx with PHP-FPM. However what happens when you want to utilize the normal features of Apache, such as basics like .htaccess files, but still keep the tuning options open that come with PHP-FPM? Well, there is a module for that!

This guide is going to assume a fresh CentOS 7 server to illustrate everything from start to finish, and will assume that all sites on this server will use the same php-fpm pool.

First, installed the required packages for your web server:

[[email protected] ~]# yum install httpd httpd-tools mod_ssl php-fpm

Now update the Apache configuration to use the mpm_event_module instead of the mpm_prefork_module:

[[email protected] ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf # LoadModule mpm_prefork_module modules/mod_mpm_prefork.so LoadModule mpm_event_module modules/mod_mpm_event.so

Then tell Apache to send all PHP requests over to PHP-FPM by creating a new configuration file:

[[email protected] ~]# vim /etc/httpd/conf.d/php.conf # Tell the PHP interpreter to handle files with a .php extension. # Proxy declaration # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time ProxySet disablereuse=off # Redirect to the proxy SetHandler proxy:fcgi://php-fpm # # Allow php to handle Multiviews # AddType text/html .php # # Add index.php to the list of files that will be served as directory # indexes. # DirectoryIndex index.php # # Uncomment the following lines to allow PHP to pretty-print .phps # files as PHP source code: # # # SetHandler application/x-httpd-php-source #

Tweak PHP-FPM to use sockets instead of TCP connections for performance purposes as follows:

[[email protected] ~]# vim /etc/php-fpm.d/www.conf ; listen = 127.0.0.1:9000 listen = /var/run/php-fpm/default.sock . listen.allowed_clients = 127.0.0.1 listen.owner = apache listen.group = apache listen.mode = 0660 user = apache group = apache

And lastly, enable the services to start on boot and start them up:

[[email protected] ~]# systemctl enable php-fpm [[email protected] ~]# systemctl enable httpd [[email protected] ~]# systemctl start php-fpm [[email protected] ~]# systemctl start httpd

If you are using a software firewall on the server, open ports 80/443 accordingly. This example will open them up to the world. Adjust yours accordingly:

[[email protected] ~]# firewall-cmd --zone=public --permanent --add-service=http [[email protected] ~]# firewall-cmd --zone=public --permanent --add-service=https [[email protected] ~]# firewall-cmd --reload

Finally, test a site to ensure PHP is working and is using PHP-FPM by creating the file below, then visiting the page at x.x.x.x/info.php:

Using multiple PHP-FPM pools

What happens if you want to isolate each site to their own PHP-FPM pool instead of using a shared pool? That is easy enough to do. Assuming that you followed everything in this guide to get to this point, do the following.

First, disable the global Apache configuration for PHP:

[[email protected] ~]# mv /etc/httpd/conf.d/php.conf /etc/httpd/conf.d/php.conf.bak

Create a new PHP-FPM pool for this specific site and update it accordingly:

[[email protected] ~]# cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/example.com.conf [[email protected] ~]# vim /etc/php-fpm.d/example.com.conf ; listen = 127.0.0.1:9000 listen = /var/run/php-fpm/example.com.sock . listen.allowed_clients = 127.0.0.1 listen.owner = apache listen.group = apache listen.mode = 0660 user = apache group = apache

Then update the site’s Apache vhost to point to a new PHP-FPM pool in both the 80 and 443 stanzas. Be sure to update the socket accordingly for your site in the 2 sections below! (ie: unix:/var/run/php-fpm/example.com.sock)

[[email protected] ~]# vim /etc/httpd/vhost.d/example.com.conf ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/vhosts/example.com # Proxy declaration # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time ProxySet disablereuse=off # Note: If you configure php-fpm to use the "ondemand" process manager, then use "ProxySet disablereuse=on" # Redirect to the proxy SetHandler proxy:fcgi://php-fpm . ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/vhosts/example.com # Proxy declaration # we must declare a parameter in here (doesn't matter which) or it'll not register the proxy ahead of time ProxySet disablereuse=off # Note: If you configure php-fpm to use the "ondemand" process manager, then use "ProxySet disablereuse=on" # Redirect to the proxy SetHandler proxy:fcgi://php-fpm .

Then restart the services:

[[email protected] ~]# systemctl restart php-fpm [[email protected] ~]# systemctl restart httpd

Finally, test a site to ensure PHP is working and is using PHP-FPM by creating the file below, then visiting the page at example.com/info.php:

[[email protected] ~]# vim /var/www/vhosts/example.com/info.php

Источник

How to Install Apache with PHP-FPM on CentOS 8

The common way to run PHP with Apache is the mod_php module. But PHP-FPM has several advantages over this. The PHP-FPM (FastCGI Process Manager) dramatically increases the performance of your Apache/PHP environment. So this is useful for high load websites. This tutorial will help you to configure PHP-FPM with Apache on CentOS 8 and RHEL 8 Linux system.

Prerequsities

  • Shell access to the CentOS 8 system with sudo privileges account.
  • Complete initial server setup for newly installed systems.

Step 1 – Install Apache

The Apache packages are available under the default AppStream repository. You can simply update the DNF cache and install Apache web server packages using the following commands.

sudo dnf update sudo dnf install httpd httpd-tools mod_ssl

The mod_ssl package provides the functionality to use an SSL certificate for secure HTTP. After installation, enable the httpd service and start.

sudo systemctl enable httpd sudo systemctl start httpd

Step 2 – Install PHP with PHP-FPM

The Remi repository contains the latest PHP packages for the CentOS 8 Linux system. So first of all, you need to add the REMI repository to your system. Just execute the following command to add the repository.

sudo dnf install dnf-utils http://rpms.remirepo.net/enterprise/remi-release-8.rpm

Then enable the required DNF module for PHP installation. Here we are enabling the module for installing PHP 7.4. You can change this to PHP 7.3 or PHP 7.2 as per your requirements.

sudo dnf module reset php sudo dnf module enable php:remi-7.4

Now, install the PHP on your system. As we are going to use FastCGI Process Manager (FPM) for this setup. So install the php-fpm package as well.

sudo dnf update sudo dnf install php php-fpm php-gd php-mysqlnd

You may also require some more PHP modules, So install them before going next. After completing PHP installation enable PHP-FPM service and start it.

sudo systemctl enable php-fpm sudo systemctl start php-fpm

Make sure the php-fpm service is running.

sudo systemctl status php-fpm ● php-fpm.service - The PHP FastCGI Process Manager Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled) Active: active (running) since Mon 2019-12-09 21:44:57 PST; 1h 24min ago Main PID: 29280 (php-fpm) Status: "Processes active: 0, idle: 5, Requests: 3, slow: 0, Traffic: 0req/sec" Tasks: 6 (limit: 10321) Memory: 24.6M CGroup: /system.slice/php-fpm.service ├─29280 php-fpm: master process (/etc/php-fpm.conf) ├─29281 php-fpm: pool www ├─29282 php-fpm: pool www ├─29283 php-fpm: pool www ├─29284 php-fpm: pool www └─29285 php-fpm: pool www Dec 09 21:44:57 tecadmin.example.com systemd[1]: Starting The PHP FastCGI Process Manager. Dec 09 21:44:57 tecadmin.example.com systemd[1]: Started The PHP FastCGI Process Manager.

Step 3 – Configure PHP-FPM

At this step, you have installed all the required packages. Let’s start the configuration process. First, edit PHP-FPM configuration file for Apache:

sudo vim /etc/php-fpm.d/www.conf

Make the changes like below. The latest versions of Apache can connect to the socket using a proxy. So make sure listen is set to a socket file.

Then set user and group the same as Apache server using. If you need to connect FPM from a remote system change listen.allowed_clients to LAN IP instead of 127.0.0.1.

Источник

Читайте также:  Java try with resources пример
Оцените статью