PHP Include Always Fails - php

Apache HTTP Server 2.2.21 with VirtualHosts under SuExec
PHP 5.3.8 via fcgid
Arch Linux 2011.08.19
I am in the process of migrating from shared hosting to VPS. The code I had ran fine before the move but is now failing at this line:
require_once($_SERVER['DOCUMENT_ROOT'] . 'includes/content/header.php');
Error log says:
PHP Fatal error: require_once(): Failed opening
required '/srv/www/hostname/public/includes/content/header.php'
(include_path='.:/usr/share/pear') in
/srv/www/hostname/public/index.php on line 3
I tried the same line without the document root part, with and without ./, etc. with no luck. No difference with require, include_once, or include, either. Yet, I can verify that the file exists at that exact location by copy-pasting from the error log and cding to it…
But just to be absolutely sure, I tested the return values of the includes as well as file_exists—they all return false. Yet all of the files are chown'd by the SuExec user/group, and no combination of permissions helps (on directories or files); have tried from 644 to 777. What's going on here?
Edit:
Same result with files in the same directory.
Apache & SuExec error logs reports nothing.
Safe Mode is set to "Off" in php.ini.
dirname(__FILE__) and exec('pwd') return the same as $_SERVER['DOCUMENT_ROOT'] but without the trailing slash.
fread, file_get_contents, and realpath(dirname(__FILE__)) all return false.
set_include_path() has no effect.
Running require via php-cgi directly from the command line returns Internal Server Error while include returns blank output; running either via php returns blank output.
Here's my vhost config:
<VirtualHost *:80>
ServerAdmin admin#hostname.com
DocumentRoot "/srv/www/hostname/public/"
ServerName hostname.com
ServerAlias www.hostname.com
SuexecUserGroup hostname hostname
ErrorLog "/srv/www/hostname/logs/error.log"
LogLevel debug
CustomLog "/srv/www/hostname/logs/access.log" combined
<Directory /srv/www/hostname/public>
Order allow,deny
Allow from all
</Directory>
# http://www.linode.com/forums/viewtopic.php?t=2982
<IfModule !mod_php5.c>
<IfModule !mod_php5_filter.c>
<IfModule !mod_php5_hooks.c>
<IfModule mod_actions.c>
<IfModule mod_alias.c>
<IfModule mod_mime.c>
<IfModule mod_fcgid.c>
AddHandler php-fcgi .php
Action php-fcgi /fcgid-bin/php-fcgid-wrapper
Alias /fcgid-bin/ /srv/www/hostname/fcgid-bin/
<Location /fcgid-bin/>
SetHandler fcgid-script
Options +ExecCGI
Order allow,deny
Allow from all
</Location>
ReWriteEngine On
ReWriteRule ^/fcgid-bin/[^/]*$ / [PT]
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</IfModule>
</VirtualHost>

First make sure the file exists...
Then try to navigate to it using
www.hostname/public/includes/content/header.php (if your using
something local it would be
localhost/public/includes/content/header.php)
If that does not load either something is wrong with your
installation or your file is corrupt.
Try loading the header from somewhere else

Did you check permission of folders containg that files ?
Folder should has read permission for apache (or any user that runs http-server).
Have you try add path to include path ?
$path = '/includes/content';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
http://php.net/manual/en/function.set-include-path.php

It always works to me ->
if ( DIRECTORY_SEPARATOR == '/' )
{
$path = dirname(__FILE__).'/';
}
else
{
$path = str_replace('\\', '/', dirname(__FILE__)).'/';
}
give a try!
[EDITED]
using dirname() function always works! $path = dirname(__FILE__).'/'; perhaps the problem is in your server. Not in the programming (script).

DerfK over at ServerFault nailed it: it was an open_basedir restriction.

Circling back to post the solution:
I didn't realize that open_basedir was unaffected by Safe Mode being off—it was looking in /srv/http/ but not /srv/www/, which would be the containing directory for /srv/www/hostname/public/includes/content/.

Related

Virtual Host not parsing PHP

Alright, I have spent hours searching through very similar questions but I still don't seem to have found the answer:
I have set up a virtual host with the following in my httpd-vhosts.conf
Listen 80
<VirtualHost *:80>
ServerName test.dev
ServerAlias test.dev
DocumentRoot "/Users/OrangeSoda/OneDrive/Projects/Test/Website"
ErrorLog "/private/var/log/apache2/test.dev-error_log"
CustomLog "/private/var/log/apache2/test.dev-access_log" common
<Directory "/Users/OrangeSoda/OneDrive/Projects/Test/Website">
Options all
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
In my http.conf I have Include /private/etc/apache2/extra/httpd-vhosts.conf as well as LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so uncommented.
When I go to test.dev/index.php which just has <?php echo "PHP TEST"; ?>, I see the contents of the file output as a string.
It seems as though PHP is not enabled and is therefore not being recognized.
If i go to localhost, I do see the "It Works!" page, so I know php is working, just for some reason not on my virtual host.
Someone suggested to put php_admin_flag engine on but that just makes the whole page crash.
Does anybody understand why php is not enabled here?
Have you checked if port 80 is opened? It might help more if you include detailed log.
You're on the right track but you need to actually load php onto the virtual host. On the <Directory> entry, add these lines :
<IfModule sapi_apache2.c>
php_admin_flag engine on
</IfModule>
<IfModule mod_php5.c>
php_admin_flag engine on
</IfModule>
I got the solution from here.
It looks like mod-php may not be installed. What OS are you on?
On Ubuntu/debian:
sudo apt-get install php5
on redhat:
sudo yum install php
The solution was that while php was enabled, for some reason the default setting in httpd.conf was not to read .php files as .php.
Although I am not really sure why that would not be the default setting, I added the following to my httpd.conf and now it works just fine:
AddType application/x-httpd-php .php

ProxyPassMatch and Options +Indexes (mod_autoindex)

I have a simple setup with Apache2.4 and PHP-FPM and I am trying to enable +Indexes option but I get 404 "File not found." when trying to access a folder that doesn't have an index file even when autoindex is enabled.
Here's part of my vhost:
#php
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/var/run/fpm/fatal.sock|fcgi://
#super public directory with Indexes!
<Location /pub>
Options +Indexes
IndexOptions +FancyIndexing
</Location>
When I try to access http://domain.com/pub/ I expected to see a list of files I put there but instead I get error 404 Not Found.
I wonder where this comes from since ProxyPassMatch shouldn't forward the request because there is no .php in the query so next is directory index which looks for index.php which doesn't exists (404) but why then mod_autoindex doesn't work?
When I remove the ProxyPassMatch line the autoindex works just fine and I see the folder content listed.
Any ideas?
I found the answer here http://blog.famillecollet.com/post/2014/03/28/PHP-FPM-and-HTTPD-2.4-improvement
As the ProxyPassMatch directive is evaluated at the very beginning of each request:
AddType (for MultiView) or DirectoryIndex directives are not usable
right management per directory is not available
each Alias directive needs another proxy rule
The SetHandler directive, evaluated later, is much more flexible / usable.
So I changed my vhost to look like this and got rid of the ProxyPassMatch directive.
<FilesMatch \.php$>
SetHandler "proxy:unix:/var/run/fpm/fatal.sock|fcgi://"
</FilesMatch>
Note: this solution applies to Apache 2.4.9+
(I do wonder if there are any performance difference and in what direction?)

Apache: The requested URL / was not found on this server. Apache

I've installed Apache 2.2 server and PHP 5.3 on Windows XP SP3. After the initial install, Apache loaded the test page, i.e.,
http:/localhost (C:/Program Files/Apache2.2/htdocs/index.html) showed "It works!".
After configuring Apache and installing PHP, trying to load http:/localhost/phptest.php i.e. (C:/testsite/htdocs/phptest.php).
But this throws an error:
Not Found. The requested URL /phptest.php was not found on this server.
I also get the same error now loading
http://localhost
httpd.conf edits:
ServerName localhost:80
DocumentRoot "C:/testsite/htdocs"
<Directory "C:/testsite/htdocs">
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
LoadModule php5_module "c:/testsite/php/php5apache2_2.dll"
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
PHPIniDir "C:/testsite/php"
File php.ini edits:
include_path = ".;C:\testsite\php\includes"
extension_dir = "C:/testsite/php/ext/"
System path:
The PHP directory was added to the Windows path, e.g.
PATH=C:\Windows\System32;C:\many_dir;C:\testsite\php
The only errors in the Apache error.log are:
Warning: DocumentRoot [C:/Program Files/Apache Software Foundation/Apache2.2 /docs/dummy-host.localhost] does not exist
Warning: DocumentRoot [C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host2.localhost] does not exist
Warning: DocumentRoot [C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.localhost] does not exist
Warning: DocumentRoot [C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host2.localhost] does not exist
The Apache service restarts successfully and is running. I can't find anything amiss. Can anyone spot any stupid errors?
Try changing Deny from all to Allow from all in your conf and see if that helps.
In httpd.conf file you need to remove #
#LoadModule rewrite_module modules/mod_rewrite.so
after removing # line will look like this:
LoadModule rewrite_module modules/mod_rewrite.so
And Apache restart
I had the same problem, but believe it or not is was a case of case sensitivity.
This on localhost:
http://localhost/.../getdata.php?id=3
Did not behave the same as this on the server:
http://server/.../getdata.php?id=3
Changing the server url to this (notice the capital D in getData) solved my issue.
http://localhost/.../getData.php?id=3
Non-trivial reasons:
if your .htaccess is in DOS format, change it to UNIX format (in Notepad++, click Edit>Convert )
if your .htaccess is in UTF8 Without-BOM, make it WITH BOM.

ubuntu server 12.04 - php files downloading

hoping someone can help me, i am having the same problem with the php files trying to download.
i have tried all answers but the:
ln -s /etc/apache2/mods-available/php5.conf /etc/apache2/mods-enabled/php5.conf ln -s /etc/apache2/mods-available/php5.load /etc/apache2/mods-enabled/php5.load
when i try them i get unable to create symbolic link file exists, so i guess thats done all ready, anyone else have anything more i can try, i am new to linux and i followed this guide to set everything up: http://www.howtoforge.com/perfect-server-ubuntu-12.04-lts-apache2-bind-dovecot-ispconfig-3
the strange thing is joomla and drupal sites work, but own php script doesent.
i have tried everything on this page:
apache2 on ubuntu - php files downloading
but none of them work.
when i try to run this command: sudo a2enmod php
i get, module php does not exist. i guess i need to run a command to install it? if so what is it please.
thanks all.
Heres More info as requested:
not sure where isp config stores the folders for each website, but i connected to the ftp site and uploaded into the web folder, same as i did for the working drupal and joomla sites that are working
sites enabled:
/
000-apps.vhost#
000-default#
000-ispconfig.conf#
000-ispconfig.vhost#
100-analytics.cyberglide.co.uk.vhost#
100-cyberglide.co.uk.vhost#
100-hideandcollars.co.uk.vhost#
100-jubileeleather.co.uk.vhost#
100-rookselectrical.co.uk.vhost#
100-wolds-uniforms.co.uk.vhost#
all sites are working (they are running joomla or drupal) but www.jubileeleather.co.uk/index.php
had to put up a html file until i can get it sorted.
php script:
<?php
//Add required class and config files below
require ('_class/cms.php');
require ('config/db.php');
//Get Required Page, if its not set call it the index
$page = (isset($_GET['page'])) ? $_GET['page'] : 'index';
//If the page is set as index
if($page == 'index'){
//Include Index
require ('public/index.php');
}
//Else if the page is contact
elseif ($page == 'contact') {
//Include Contact
require ('public/contact.php');
//If the page is none of the above
} else {
//Include Layout
require ('public/layout.php');
}
?>
i am using putty via ssh, how would i copy content from a file to post on here? also which files would you need? thanks again.
here is the contents of the jubileeleather v host file, not sure what i am looking for in here:
AllowOverride None
Order Deny,Allow
Deny from all
DocumentRoot /var/www/jubileeleather.co.uk/web
ServerName jubileeleather.co.uk
ServerAlias www.jubileeleather.co.uk
ServerAdmin webmaster#jubileeleather.co.uk
ErrorLog /var/log/ispconfig/httpd/jubileeleather.co.uk/error.log
Alias /error/ "/var/www/jubileeleather.co.uk/web/error/"
ErrorDocument 400 /error/400.html
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 405 /error/405.html
ErrorDocument 500 /error/500.html
ErrorDocument 502 /error/502.html
ErrorDocument 503 /error/503.html
<IfModule mod_ssl.c>
</IfModule>
<Directory /var/www/jubileeleather.co.uk/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/clients/client3/web4/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# suexec enabled
<IfModule mod_suexec.c>
SuexecUserGroup web4 client3
</IfModule>
# Clear PHP settings of this website
<FilesMatch "\.ph(p3?|tml)$">
SetHandler None
</FilesMatch>
# php as fast-cgi enabled
# For config options see: http://httpd.apache.org/mod_fcgid/mod/mod_fcgid.html
<IfModule mod_fcgid.c>
IdleTimeout 300
ProcessLifeTime 3600
# MaxProcessCount 1000
DefaultMinClassProcessCount 0
DefaultMaxClassProcessCount 100
IPCConnectTimeout 3
IPCCommTimeout 360
BusyTimeout 300
</IfModule>
<Directory /var/www/jubileeleather.co.uk/web>
AddHandler fcgid-script .php .php3 .php4 .php5
FCGIWrapper /var/www/php-fcgi-scripts/web4/.php-fcgi-starter .php
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory /var/www/clients/client3/web4/web>
AddHandler fcgid-script .php .php3 .php4 .php5
FCGIWrapper /var/www/php-fcgi-scripts/web4/.php-fcgi-starter .php
Options +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
# add support for apache mpm_itk
<IfModule mpm_itk_module>
AssignUserId web4 client3
</IfModule>
<IfModule mod_dav_fs.c>
# Do not execute PHP files in webdav directory
<Directory /var/www/clients/client3/web4/webdav>
<FilesMatch "\.ph(p3?|tml)$">
SetHandler None
</FilesMatch>
</Directory>
DavLockDB /var/www/clients/client3/web4/tmp/DavLock
# DO NOT REMOVE THE COMMENTS!
# IF YOU REMOVE THEM, WEBDAV WILL NOT WORK ANYMORE!
# WEBDAV BEGIN
# WEBDAV END
</IfModule>
Apart from having the own PHP files in a directory with PHP parsing enabled (can't really say how it's done with Apache as I'm not using for some time), you should check if your script has the correct extension (should be always .php) and script opening tag <?php - don't use short tag <? to avoid possible problems when moving the script to live server.
Tho since your files are returned for download, I think you don't have your scripts in directory with enabled PHP parsing. Posting your Apache and PHP config files could help us to identify the problem.

How do fix apache error "not within configured docroot" under Ubuntu

Greetings experts and gurus, I am looking for some help with an apache php configuration problem.
I have been running several websites from an apache2 setup on an ubuntu server for some time now without problems using the line NameVirtualHost * in my etc/apache2/conf.d/virtual.conf file. I recently updated the server version to the latest lts version and I am now unable to run php files.
I am running all my sites for the location "/home/www/[site-name]/htdocs" and I have setup and enabled all my sites in /etc/apache2/sites-available. I have also disabled the default site.
for each sites file I have specified the following:
# Indexes + Directory Root.
# DirectoryIndex index.html index.htm index.php
DocumentRoot /home/www/[site-name]/htdocs/
# CGI Directory
ScriptAlias /cgi-bin/ /home/www/[site-name]/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
# Logfiles
ErrorLog /home/www/[site-name]/logs/error.log
CustomLog /home/www/[site-name]/logs/access.log combined
I restart apache and enter the url for a php test page on my server and I am met with an "Internal Server Error". When I check the error log I get:
Script "/home/www/[site-name]/htdocs/test.php" resolving to "/home/www/[site-name]/htdocs/test.php" not within configured docroot.
For some reason when looking into the error, suphp came up a lot. According to this link:
Don't be fooled into thiking this has anything to do with the Apche virtual host document root, this is actually another setting in the suphp config file. Including the paths which contained the RoundCube scripts fixed this one. For example:
docroot=/var/www:/usr/share/roundcube:/var/lib/roundcube:${HOME}/public_html
You need to edit your /etc/suphp/suphp.conf file and change the docroot to whatever is appropriate.
It looks you missed the virtual hosts configuration for every site name:
<VirtualHost IP:80>
ServerName yourdomainname
ServerAlias www.yourdomainname
DocumentRoot /home/www/[site-name]/htdocs/
ScriptAlias /cgi-bin/ /home/www/[site-name]/cgi-bin/
<Location /cgi-bin>
Options +ExecCGI
</Location>
ErrorLog /home/www/[site-name]/logs/yourdomainname.ua-error.log
CustomLog /home/www/[site-name]/logs/yourdomainname-access.log combined
</VirtualHost>
<Directory "/home/www/[site-name]/htdocs/">
Options FollowSymLinks
AllowOverride None
Order allow,deny
<Limit GET POST OPTIONS>
Order allow,deny
Allow from all
</Limit>
<LimitExcept GET POST OPTIONS>
Order deny,allow
Deny from all
</LimitExcept>
</Directory>
Thanks for all the help, I got there in the end. It seems that upgrading to Ubuntu 10.04 turned on suPHP. It has ended up being a good thing as the reason why I was getting errors was down to file management in my htdocs getting sloppy.
To fix my issues I had to do several things:
First I turned on suphp error messages in /etc/apache2/sites-available/[site-name]. This gave me the true error, which told me that some of my pages had the wrong permissions. I then set all my folder permissions in www to 755 and the files to 644. I also had to lower the min_uid and min_gid fileds in suphp.conf to 33.

Categories