PHP Extension: Works from command line, but not in httpd (Apache) server - apache

I'm getting the strangest problem with my PHP extension. When I use it from the command line, like so:
php -r '$mc = new MyClass("foo"); echo $mc->getField();'
it prints out "foo" as expected.
However, if I try to do the same thing from within my index.php, I get a class not found error.
Fatal error: Class 'MyClass' not found in /var/www/html/index.php on line 12
How could this be happening? I've even set httpd to run as the same user as myself when running the php -r command.
My index.php looks like so:
<html>
<head>
<title>PHP Test</title>
</head>
<body>
<p>
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mc = new MyClass("foo");
echo $mc->getField();
?>
</p>
</body>
</html>
The php portion of my httpd.conf looks like so:
# PHP Configuration for Apache
#
# Load the apache module
#
LoadModule php5_module modules/libphp5.so
#
# Cause the PHP interpreter handle files with a .php extension.
#
<Files *.php>
SetOutputFilter PHP
SetInputFilter PHP
LimitRequestBody 9524288
</Files>
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php
And obviously my php.ini is set up correctly, because my php -r command works. What could possibly be causing this issue?

The php executable you're running on the command line is different to the libphp5.so library you're running via Apache.
Put the following into a file, upload it to your server and view it via the web and you'll probably see that your module hasn't been loaded:
<?php phpinfo(); ?>
This file will tell you what modules have been loaded, which php.ini file is being used, and lots of other information. It should help you diagnose your problem. You'll probably need to set an Apache configuration parameter to either load the correct php.ini file or load the .so file of your module directly.

It turns out that the issue was due to SELinux. If it is turned off like so:
echo 0 >/selinux/enforce
Everything magically works.

I have got the same issue and solved restoring the SELinux context for the extension files, which should be ideal instead of disabling SELinux, as the following example:
# restorecon -R -v /usr/lib64/php
# restorecon -R -v /etc/php.d

Related

Need help installing fork cms 4.5.0 on apache 2.4.25

I am trying to install fork cms on my laptop. I checked the system requirements for all php extensions and made changes to apache configuration including:
LoadModule rewrite_module modules/mod_rewrite.so
and
AllowOverride FileInfo in 3 separate locations.
To be honest I am new to this so I need to do more reading as to the implications of those changes.
The fork cms files are in the root of my server, which is htdocs in xampp.
When I type localhost/forkcms I get the access forbidden error 403.
I am unsure what else I need to change in order to allow for the installation to begin.
Any help will be greatly appreciated.
In your vhost.conf file you'll have to add this and replace directory with path to your project's root
<Directory "C:/xampp/htdocs/projectroot">
AllowOverride All
</Directory>
After doing that and it still not works, try setting the debug flag on within index.php (Root)
// get environment and debug mode from environment variables
$env = getenv('FORK_ENV') ?: 'prod';
$debug = getenv('FORK_DEBUG') === '1';
$env = 'dev';
$debug = 1;
If that still doesn't work, try and clear the cache by using bash capable command like too.
php app/console cache:clear
cd tools && ./remove_cache
You might have some permission issues as you're working with Windows.
chmod -R 777 app/console
chmod -R 777 app/logs
chmod -R 777 app/cache
chmod -R 777 src/Frontend/Cache
chmod -R 777 src/Frontend/Files
chmod -R 777 src/Backend/Cache
This should do the trick, if it doesn't help just let me know.
I recently had trouble getting forkcms to install on localhost with xampp.
I tried many answers, but the final solution was to open httpd.conf and change
DocumentRoot "C:/xampp/htdocs" to DocumentRoot "C:/xampp/htdocs<Name of my project>"
This changes the default root to the directory where you installed forkcms, it should be changed back after you finish your work on forkcms.
Another solution might be to rename the htdocs folder to something else, make another htdocs folder and put the root files from forkcms in there.
I also had to enable the intl plugin from the php.ini file by removing the ; from ;extension=php_intl.dll

CentOS 7 + PHP7 -- php not rendering in browser

I have a clean install of apache/httpd and php7.1.0 running on CentOS 7.
When I execute from the command line:
php -v
I get the expected response:
PHP 7.1.0 (cli) (built: Dec 1 2016 08:13:15) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
But when I try to hit my phpinfo.php page, all I get is...
<?php phpinfo(); ?> literally outputted to the screen - can someone tell me what I'm missing, did I forget to enable a mod?
For PHP 7 (May apply to previous versions as well), but I had to do this:
Add this to the bottom of /etc/apache2/apache2.conf
or for Centos /etc/httpd/conf/httpd.conf
SetHandler application/x-httpd-php
Fabien's answer worked for me, but apache started to serve css/js files with the wrong mime type. I fixed it adding this at the end of /etc/httpd/conf/httpd.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
That means that PHP isn't enabled in Apache. PHP addresses that here - step 8 should solve your problem.
As an addition: what I usually do on a new install, is install an entire LAMP-server. On Ubuntu, that's done with sudo apt-get install lamp-server^ (note: the caret is not a typo).
This was solution for me - adding this line into httpd.conf where your app's ServerName and DocumentRoot are:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/laravel/public/$1
Comment out the "Directory" line in the /etc/httpd/conf.d/php.conf file. Then restart the httpd server
ran into the same problem. On a Centos 8 Stream installation.
First check if php is installed and working, for example:
php /var/www/html/info.php
If this works, and apache is not serving/interpreting the php file, than install php-fpm (mod_php was deprecated):
dnf install php-fpm
systemctl enable php-fpm
systemctl status php-fpm
systemctl restart httpd
Check in the browser.
If successful, delete the info.php file

After install OS X El Capitan, Apache not working

I just installed in my MacBook pro "El capitan" and I can't type any host set it in vhost. Everything was working perfectly before in Yosemite. When I type one of my virtualhost I get:
403 "Forbidden You don't have permission to access / on this server."
What did I try ?
go to /etc/apache2/httpd.conf
and uncoment this lines:
Include /private/etc/apache2/extra/httpd-vhosts.conf
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
/etc/apache2/extra/httpd-vhosts.conf is the same file that was working before in Yosemite since long time ago, so I haven't modified.
To install everything in Yosemite I used homebrew.
Another problem related with "El capitan", before I was able to type http://localhost/~myuser/ and I could access to any folder in /Users/myuser/Sites now I get this message: 404 not found.
I hope some help, I am not specialist in servers and it's really difficult to me to set it up, and is very annoying to waste a lot of time anytime there is a new upgrade in the OS.
The upgrade process keeps a copy of your old httpd.conf. Unless you were way out of date, just overwrite the new file with your old one. Keep a copy of the new just in case...
sudo cp /etc/apache2/httpd.conf /etc/apache2/httpd.conf.after-update
sudo mv /etc/apache2/httpd.conf.pre-update /etc/apache2/httpd.conf
sudo apachectl restart
I had the same trouble.
Type this command line you will have the error from apache :
apachectl configtest
If you have this error :
AH00526: Syntax error on line 20 of /private/etc/apache2/extra/httpd-mpm.conf: Invalid command 'LockFile', perhaps misspelled or defined by a module not included in the server configuration.
so this link can help : https://apple.stackexchange.com/questions/211015/el-capitan-apache-error-message-ah00526
I experienced that after the upgrade to El Capitan the line
Include /private/etc/apache2/extra/httpd-userdir.conf
in
/private/etc/apache2/httpd.conf
was commented again. After uncommenting it and restarting apache, everything worked alright again.
I have merged to two versions (httpd.conf and http.conf~previous) and after that I discovered that the v-host wasn't working anymore.
After disabling rule 19 #NameVirtualHost *:80 in /private/etc/apache2/extra/httpd-vhosts.conf everything worked again!
LockFile is a directive of Apache ≤ 2.2. El Capitan comes with Apache version 2.4. so, you need to disable LockFile
First, just to be sure apache is stopped:
sudo apachectl stop
Then edit your httpd-mpm.conf:
Comment this lines
#
# The accept serialization lock file MUST BE STORED ON A LOCAL DISK.
#
<IfModule !mpm_winnt_module>
<IfModule !mpm_netware_module>
LockFile "/private/var/log/apache2/accept.lock"
</IfModule>
</IfModule>
Start apache sudo apachectl start and that's all
As some have mentioned the problem is that the upgrade to El Capitan backed up your previous httpd.conf and renamed it httpd.conf~previous and gave you a new httpd.conf. What you need to do though with these files to fix your problem is:
Use nano or other editor to open httpd.conf~previous and look for these 2 lines:
DocumentRoot "/Users/my-machine-name/Prev-Localhost-Path
<Directory "/Users/my-machine-name/Prev-Localhost_path">
Copy them and then using nano open httpd.conf and look for these 2 lines:
DocumentRoot "/Library/WebServer/Documents"
<Directory "/Library/WebServer/Documents">
Comment them out by entering the '#' character in front of each one, and then below them paste in the two lines you copied from your original file. Save the file and exit.
After that you'll need to reboot. My setup would not work until I made these changes and then rebooted. But perhaps the suggestion by DSG to restart apache will suffice:
sudo apachectl restart

WEBMO-httpd.conf is empty where apache2.conf is not

I have installed Web MO on Ubuntu 12.04 , I successfully complete the step given in link Web MO installation
But when I click on the URL specified at the end of setup process it shows as 404 NOT FOUND error
So I start configure the Apache Web Server
Steps for configuring Web Server
as mentioned in web server configuration
cd /etc/httpd/conf not found in /etc folder
in /etc folder there exist an apache2.conf file and httpd.conf file , but httpd.conf file is empty where apache2.conf file is not.
I want to edit some lines in httpd.conf
how can I do these , is there is any way to get contents in httpd.conf file
Thanks in Advance....
To find your server config file. You can use the following command for httpd, apaceh2 and apachectl (and maybe even more):
"apache name" -V | grep SERVER_CONFIG_FILE
so for apache2 it will be
apache2 -V | grep SERVER_CONFIG_FILE

When executing CGI, give me only plain text

When I tried to execute CGI, it gives me only plain text.
I installed PHP5.3.11 with apache2
All of them were installed manually from source code, not from apt-get
## Apache2
./configure --prefix/=usr/loca/apache2
make
make install
## php5
./configure --prefix=/usr/local/php5 --with-apxs2=/usr/local/apache2/bin/apxs
make
make test
make install
The above are commands that I used to install apache2 and php5.
I guess my installation for apache2 and php5 is correct, since I can run php code on my server. So I tried to execute CGI script after properly, I guess, setting a configuration.
However it shows only plain text of CGI script.
###Start###
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";
for ($i=0; $i<10; $i++){
print $i."<br />";
}
###End###
perl is properly installed at /usr/bin/perl.
I set my httpd.conf like below.
<Directory "/usr/local/apache2/cgi-bin">
Options +ExecCGI
AddHandler cgi-script .cgi .pl
Require all granted
</Directory>
I think cgi-php5 is installed correctly as well, since when I execute "php-cgi -h", it gives me a result.
And when I open phpinfo() through browser,
GATEWAY_INTERFACE is set to CGI/1.1
Any thing I am missing? I appreciate for your help.
I found the answer.
my mistake was on httpd.conf and it was really stupid mistake. :-(
I had to uncomment the following line.
LoadModule cgi-script blahblah
No one knows how many hour I spend for this stupid mistake :-(