Set PHP limits in Virtual Host FPM/FastCGI - php

I am trying to create individual setting of PHP (like memory_limit..) for Virtual Hosts But I dont know how could I do that.
I am using Fedora 27 deamon httpd, VHosts works good. When I put SetEnv PHPRC /var/www/pokus.cz/php.ini it doesnt work ( Yes I reloaded deamon).
I try just write php_value memory_limit 64M to VHost.conf file but it prints error
Invalid command "php_value"
I Think that, it is because there is no mod_php in Fedora 27 httpd, but it uses FPM/FastCGI. How could I solve that, Thanks alot for any advise :)

If you want to set individual PHP parameters to your VHosts on server and there is PHP-FPM running on it, you could specify it by different FPM pools.
Just copy the default FPM pool called www (etc/php-fpm.d/www.conf) and edit:
name parameter (specify new socket name) and
listen parameter (specify new socket name, which will VHost run)
(You can edit the php parameters there, like memory_limit...)
Now you have to add this to your VHost conf file:
<FilesMatch \.(php|phar)$>
SetHandler "proxy:unix:/run/php-fpm/yourchoosenname.sock|fcgi://localhost"
</FilesMatch>
This will start your php scripts on new FPM pool for this specific VHost.
Dont forget to reload your web server (I use Apache, so systemctl reload httpd) and PHP-FPM.

Related

Can't change max_input_vars limit in php.ini

I need to change PHP max_input_vars variable but after change in php.ini and server restart phpinfo shows old value all the time. This is what i do:
in phpinfo() i've got
...
Loaded Configuration File /etc/php.ini
...
max_input_vars 1000
from server CL I've edit /etc/php.ini and uncomment line with max_input_vars it looks like this:
...
max_input_vars = 5000
...
after this changes and restarting web server
systemctl restart httpd
phpinfo still shows max_input_vars as 1000
It looks like php use some other php.ini or values are overwritten because with other settings like max_execution_time is the same.
If i'm doing it wrong way? Is this possible that phpinfo shows different configuration file than the one used?
phpinfo output:
As you're using PHP-FPM, you need to check it's configs for overrides, typically in /etc/php-fpm/*.conf.
php_admin_value[max_input_vars] = XXX
After validating there are no overrides that affect your settings, restart the php-fpm service and as a best-practice also Apache.
systemctl restart php-fpm httpd
Restarting the PHP-FPM service is required because Apache passes requests to the running PHP instance(s) loaded into memory from the PHP-FPM service. As PHP is already loaded into memory by PHP-FPM, the PHP-FPM service needs to be restarted for PHP configuration changes to be applied.
Apache prior to PHP-FPM typically relied on starting the PHP process using the Apache mod_php, thereby requiring the restarting of the Apache service for configuration changes to be applied immediately, or until mod_php reloaded the PHP instance.
Based on your configuration, you should add additional PHP config settings to /etc/php.d/zzz-custom.ini instead of /etc/php.ini. PHP will load the config files in alphabetical order, resulting in the zzz-custom.ini file being loaded last and be used as the final values of the PHP settings.
Using the /etc/php.d/zzz-custom.ini file will also prevent the loss of your custom php.ini settings and the need to make extensive changes to the default /etc/php.ini file when updating PHP versions.
However, /etc/php-fpm/*.conf settings will take precedent over any /etc/php.d/*.ini configs.
first: Method is edit the PHP. ini file
Locate your PHP. ini file. ...
If you find your existing PHP. ini, open the file and locate the following line of code (xx represents a number): max_input_vars = xx ; And set it to your desired limit. ...
If you created your own PHP. ...
Save your changes, and reboot your localhost or your server.

Using heroku-fcgi to handle files without a .php extension (Apache)

Overview
I'm trying to host a few legacy PHP apps on Heroku with Apache. They all relied on the following deprecated syntax to parse any unknown file types (without the .php extension) as PHP.
DefaultType application/x-httpd-php
This has been replaced by AddType in Apache 2.4 (Heroku currently uses v2.4.37). Heroku also uses mod_proxy_fcgi to process PHP files via fcgi://heroku-fcgi.
Issue
I have a file foo.test and I want to have it handled by PHP FPM. Taking cues from the docs and the default Apache config provided by Heroku, here's what I've tried:
# .htaccess
<FilesMatch \.test$>
<If "-f %{REQUEST_FILENAME}">
SetHandler proxy:fcgi://heroku-fcgi
</If>
</FilesMatch>
# apache_app.conf (properly loaded via Procfile)
ProxyPassMatch "^/(.*\.test(/.*)?)$" "fcgi://heroku-fcgi/app/$1"
With both of these I get a plain-text 403 Access denied. response from PHP FPM. I'm sure both configs are properly loading and pointing to the FCGI handler because changing the endpoint results in other errors.
My Apache skills are long since rusty and I can't seem to find any good pointers online. The Apache error log is also clean. Any ideas (without the obvious "change all extensions to PHP, you dumbass") would be appreciated!
Fairly obvious solution. PHP FPM has its own configuration with a security.limit_extensions flag. It defaults to .php.
The solution was to unset that value: security.limit_extensions =. This naturally can pose some security threats, but these apps are only going up for static demo.
I was using heroku/heroku-buildpack-php but forked that to update this file. The htaccess FilesMatch should work now but I just ended up placing it into the Apache config file to avoid repetition across the sites I'll be serving.
security.limit_extensions can be customized with a configuration file passed as a Procfile argument.
https://devcenter.heroku.com/articles/custom-php-settings#php-fpm-settings
PHP-FPM settings:
In addition to php_value and php_flag for php.ini specific settings, any pool specific PHP-FPM configuration directives are valid in that configuration file, so you can use it to fine tune PHP-FPM’s behavior.
So you can set up it like the following
Procfile
web: vendor/bin/heroku-php-apache2 -C apache.conf -F fpm_custom.conf web/
apache.conf
<FilesMatch \.test$>
<If "-f %{REQUEST_FILENAME}"> # make sure the file exists so that if not, Apache will show its 404 page and not FPM
SetHandler proxy:fcgi://heroku-fcgi
</If>
</FilesMatch>
fpm_custom.conf
security.limit_extensions = .php .test

How do you set the PHP error log location in the Apache configuration file?

We have an out-of-the-box PHP application running on a subdomain, and it's logging errors in its DocumentRoot:
/var/www/appname/path/to/document/root/php-errors.log
...instead of where we want the error logs to go:
/var/www/appname/logs/php-errors.log
Since I don't want to change the code in the out-of-the-box application, and I don't want to change the error log location for all the subdomains (read: file php.ini), how can I do this in just the Apache configuration file that relates to the subdomain?
From error_log per Virtual Host?:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/domains/example.com/html
ErrorLog /var/www/domains/example.com/apache.error.log
CustomLog /var/www/domains/example.com/apache.access.log common
php_flag log_errors on
php_flag display_errors on
php_value error_reporting 2147483647
php_value error_log /var/www/domains/example.com/php.error.log
</VirtualHost>
For those wishing to do this using php-fpm (which I meant to originally post about), here's how you do it:
Ensure you're logged in as root, or using sudo for all your commands.
Go into your php-fpm directory*, using the command below:
cd /etc/php/fpm/
In that directory, edit your php.ini file and add the following to the end:
If you want to set error log by host
[HOST=www.example.com]
error_log=/var/www/myapplication/path/to/my/error.log
[HOST=sub.example.com]
error_log=/var/www/mysubapplication/path/to/my/error.log
If you want to set error log by path
(handy if you're working on a server with an IP address but no domain)
[PATH=/var/www/myapplication]
error_log=/var/www/myapplication/path/to/my/error.log
[PATH=/var/www/mysubapplication]
error_log=/var/www/mysubapplication/path/to/my/error.log
You now need to go into the pool directory*, using the command below:
cd /etc/php/fpm/pool.d/
In that directory, edit the file www.conf. Note the values for user and group in that file, whose out-of-the-box setting is www-data for both. Look for the term catch_workers_output and make sure it is uncommented and set to yes, like so:
catch_workers_output = yes
Now you need to create the error log file(s) and ensure that php-fpm has access to it. This is why you needed to note the values for user and group from the previous file edit. Create an error log file for each HOST/PATH you set when editing php.ini and give them the appropriate permissions and ownership, for example:
touch /var/www/myapplication/path/to/my/error.log
chmod 0660 /var/www/myapplication/path/to/my/error.log
chown www-data:www-data /var/www/myapplication/path/to/my/error.log
Finally, restart your php-fpm service using the command** below:
service php-fpm restart
* Note: If, like me, you install declared versions of php-fpm, the directory paths will change to (for example) the following:
/etc/php/5.6/fpm/
/etc/php/5.6/fpm/pool.d/
/etc/php/7.1/fpm/
/etc/php/7.1/fpm/pool.d/
** The service takes a specific versioned name if you installed declared versions, and you will need use (for example) the following:
service php5.6-fpm restart
service php7.1-fpm restart
I configure it like this:
File /etc/apache2/envvars
# For supporting multiple Apache 2 instances
if [ "${APACHE_CONFDIR##/etc/apache2-}" != "${APACHE_CONFDIR}" ] ; then
SUFFIX="-${APACHE_CONFDIR##/etc/apache2-}"
else
SUFFIX=
fi
export APACHE_LOG_DIR=/var/log/apache2$SUFFIX
File /etc/apache2/sites-available/000-default.conf
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

how to configure server to look in to .xphp extention files for php code

In my project I want to store all new/specific code into files with xphp extension but I want server to parse those file as .php.
Can we configure this. If yes how?
I am using Apache server with php 5.6.3, with access to server configurations.
Thanks in Advance.
in apache2 you can add multiple handlers as follows:
within your .conf file which is often located in /etc/apache/sites-available/ in linux/ubuntu , add this line
AddType: application/xhttpd-php .xphp
this will only take effect on the virtual host which you are adding this line to,
if you want your changes take effect on all your hosts, add this line to your /etc/apache2/apache2.conf
if you don't have an access to you server configurations files :
you can add this line to your .htaccess file :
Addhandler application/x-httpd-php .html .php .xphp
don't forget to restart your apache2 after changing the conf files
sudo service apache2 restart

Getting blank PHP page over Apache

In a newly setup digitalOcean cloud server (CentOS), I have installed php and Apache. The webserver is running fine:
[root#a2m5cent01 httpd]# service httpd status
httpd (pid 11232) is running...
[root#a2m5cent01 httpd]# php --version | head -1
PHP 5.3.3 (cli) (built: Dec 11 2013 03:29:57)
But browser is showing blank pages (white page) if I try to visit any php page.
Here is what I have done so far to troubleshoot:
Created a page with following content: <?php phpinfo(); ?>. It displays a blank page when viewed from browser.
Just to ensure, apache is pointing to the correct directory, placed a static .html page there, and saw it comes out fine in browser, so apache is working and directory is correct.
In /etc/php.ini, changed display_errors directive to On. Still blank page
In Apache config file (/etc/httpd/conf/httpd.conf) found this line Include conf.d/*.conf. Inside conf.d directory, there is a php.conf file containing the line:
LoadModule php5_module modules/libphp5.so. Ensured that this .so file actually exists in this place.
In the same file I have these two lines as well: AddHandler php5-script .php and AddType text/html .php
Executed the php page from CLI, it works fine - so php is working locally.
Then why is it always shows a blank/white page over the browser? What else am I missing?
EDIT
Based on suggestions from #Nathan,
I checked Apache error log file, could not see any error being
reported there.
My /etc/php.ini says, php error_log is located as syslog. So I checked /var/log/messages but could not find any PHP error message
Next I put some normal HTML in the php file containing phpinfo() call. Interestingly I found that even the normal HTML texts are also not coming. It still produces blank page.
Then I checked Apache access log. Surprise! There is no GET request for any of the PHP files I tried to load in the browser. But GET request for all the non-php files are there with 200 return code.
Apache is not even logging any access request for PHP files. Any idea why would that happen?
check out your phpinfo() script.
<?php
phpinfo();
?>
missing the "php" behind the first "?" will give a blank page
I think your php installation with apache is faulty. Thats why you can not see any php page in your webserver. Clean remove all the existing apps, like httpd,php,php-fpm,php-cli etc. and try to clean isntall in this order
yum install httpd -y
yum install php php-common php-cli php-gd php-curl php-fpm -y
then make sure you restart yout httpd server.
service httpd restart
Install mod_fastcgi:
yum install mod_fastcgi
Start the service:
service php-fpm start
Restart Apache:
service httpd restart
5. Configuration of Apache with PHP-FPM
Open the fastcgi.conf file:
nano /etc/httpd/conf.d/fastcgi.conf
Add this to the end of the file:
<IfModule mod_fastcgi.c>
DirectoryIndex index.html index.shtml index.cgi index.php
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -host 127.0.0.1:9000 -pass-header Authorization
</IfModule>
After that search after "FastCgiWrapper" and make sure it's set to "off" then save the file.
The /usr/lib/cgi-bin/ directory must exist, so we create it:
mkdir /usr/lib/cgi-bin/
If mod_php is installed and enabled, we need to disable it so open the configuration at /etc/httpd/conf.d/php.conf:
nano /etc/httpd/conf.d/php.conf
Comment out the AddHandler and AddType lines so it looks like here:
#
# PHP is an HTML-embedded scripting language which attempts to make it
# easy for developers to write dynamically generated webpages.
#
<IfModule prefork.c>
LoadModule php5_module modules/libphp5.so
</IfModule>
<IfModule worker.c>
LoadModule php5_module modules/libphp5-zts.so
</IfModule>
#
# Cause the PHP interpreter to handle files with a .php extension.
#
#AddHandler php5-script .php
#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 line to allow PHP to pretty-print .phps
# files as PHP source code:
#
#AddType application/x-httpd-php-source .phps
Save the file and restart Apache:
service httpd restart
Are you navigating to the php file directly? Or are you just going to the directory root?
If the later, Apache might not be recognizing .php as the directory index.
To test, try create a .htaccess file in your web root containing the following line:
DirectoryIndex index.php
I have the same issue...
The problem is in the iptables. (It seems like it)
Try with:
service iptables stop
## check if it stop...
service iptables status
Then try to reload the page again.
If you had other solution please share.
[edit]
Restarting the iptables service is working for me.
Try:
service iptables restart
First of all you should check the permissions of your file.
If you don't grant read-permission to public, Apache produces a blank page without showing any errors.
It's been sometime, but I wanted to come back to this question to update that the issue was with the directory permission setup.
The FPM user I was using didn't have necessary permission to execute the index.php file in the web root.
To avoid these issues in the future, I have created an automated bash script that will automatically create and configure webservers in DigitalOcean boxes. Please take a look here https://github.com/akash-mitra/fairy
This script will automatically,
Installs Nginx
Create virtual server block for nginx
Installs PHP, PHP APC, PHP Curl etc.
Supports PHP Fast Process Manager (php-fpm)
Installs Memcached
Installs Database (MariaDB / MySQL)
Optionally Installs PHP Composer and Laravel
Configures and Strengthens SSH
Activates Firewall
Optionally enables SWAP space in DO server and fixes a locale issue
Since everything looks at its default state, have you checked this part just for confirmation
cat /etc/php.ini | grep log_errors
If log_errors is disabled then enable it and check this below log after restarting httpd.
/var/log/httpd/error_log
--
Also check this part from php configuration.
cat /etc/php.ini | grep error_reporting
This value should be enabled by default so that display_errors will work.
error_reporting = E_ALL
Sorry to repost an old thread...this is important.
I also was having these problems where no html response was outputting
After double-checking php.ini or my apache conf files and was still receiving no output, I later found out that I was suppressing the error of an include / require of a class, with #, which was nested within a constructor function. There was a syntax error in the included file, which stopped all output altogether when errors were thrown.
So, check your handlers first.
If you are storing all your output into vars first and you are including various scripts first that fail you'll have to see those errors. If you suppress file handler errors, you'll get a blank screen if you have a syntax error in the file.
Search your files for all instances of # in your php code. Then turn #include "/path_to/script.php"; to include "/path_to/script.php"; or anything #$foo into $foo as such var might reference a dependency that is causing your script to end with nothing showing in the httpd error log or in the http response.

Categories