php request downloading the actual script after changing php version with .htacces - php

I work on a shared server that has multiple php versions. I read on the internet that simply adding a .htaccess file with the following line in my folder dictates what version of php will be used when the scripts in that folder run: "AddHandler application/x-httpd-php52 .php"
After adding this line when i call myScript.php i get the actual content of the file
<?php
phpinfo();
?>
Accessing the script in the browser downloads a copy of the script
What am i doing wront ?

If your browser is downloading the script that means the php module or handler is not loaded.
check the php module in httpd.conf. do not forget to restart apache.

Related

How to run a PHP file from ubuntu?

How to run a php file from ubuntu platform in the localhost?
I have also installed LAMP in my system.
When I try to run the php file, in the browser, it says "The requested URL is not found-404 ERROR found".
I do not know how to proceed with this.
My php files are in the directory as shown here "/usr/var/html/a.php".
There are two options.
Access the php file through a local webserver(ie thru a local website). The web-server will deal with the requested php file. It will use either,
Inbuilt PHP module to interpret the php file, or
PHP through CGI (eg.CGI, FastCGI)
If your apache(check if apache is running using service apache2 status!!) is set to the default configuration, this could be as simple as
http://localhost/path/to/your.php
Remember by default, the base directory for apache is /var/www/html/, so you need not include this in the url.
Use the php binary directly from a terminal.
php /path/to/your/file.php
After installation of Lamp system in Ubuntu. Please follow the below two steps to run your php file.
Place your php file (.php) in /var/www/html/ (default path)
Please run url as localhost/withfilename.php
Example : I have placed welcome.php file in the /var/www/html/welcome.php
then url will be http://localhost/welcome.php

how does apache php5_module work?

In my /etc/apache2/httpd.conf, I open it.
LoadModule php5_module libexec/apache2/libphp5.so
my vhost config
I want to ask how does apache work with the php code.
When I update the php code. I need not to send any commands to apache. I send a request to the apache, I get the right response as the updated php code making.
Does apache read the php code file when it handle every request.
Or does apache read the php code first time, then parse it, then save the parsed code into apache runtime memory? When the php file update, apache catches the system signal, reloads its runtime memory
Apache as a web server doesn't understand/know your file if it doesn't know its mime_type. So, first thing before parsing any file it checks its extension in mime.conf file and gets its mime_type based on that it parses the file. Now if your file is PHP it will start parsing it since apache is loaded with all the enabled modules one of them is libphp.so which contains the php parser and all the php methods definitions.

How can I host PHP files on Apache without executing?

If I host a PHP file on Apache, it will just be executed. I want clients to be able to download the PHP file without changing the .php extension. I don't want to disable PHP either.
if you don't want to play with apache configuration or .htaccess, then use PHP itself to get it done
create a file showcode.php with following code
echo file_get_contents('/path/to/your/php/file/having/viewable/code/for/users/');

Apache serves empty file when symlinking to a php script outside of httpdocs

As stated above, when symlinking to a php file outside of the httpdocs directory Apache/php serves an empty file. However if I change the extension to html the file is served properly as an html document. This leads me to believe that it is not a permissions error but could be something related to the php configuration (could open_basedir be causing this?) or Apache.
Does anyone know what could be causing this behavior?
EDIT: For anyone wondering about this, the problem was cause by the php open_basedir restriction.
Turn on error reporting in PHP and check your PHP error log. 100% guaranteed that you had a fatal scripting or configuration error.
I tested symlinking a PHP script from the webroot / htdocs. Worked just fine.
Made the following files
/some/directory/myphpscript.php (the script)
/some/directory/htdocs/symlink.php (webroot)
In the myphpscript.php I added a hello world script (<?php echo 'Hello world'; ?>). The symlink was created from command line using the follow command when standing in /some/directory/htdocs/:
ln -s ../myphpscript.php symlink.php
Make sure
User running Apache can access the file
The symlink is correct. If your use relative paths, it must be relative to the webroot (e.g. ../../path/to/script.php). Test the link by opening the file (e.g. nano symlink.php).
Check out the Apache error.log for error messages.

Page not found: Running a php file on an Apache sever

I get page not found error when I try to run my php file running on the root folder of my apache. Other php files run fine(anyway this is a magento based site). Is it because i transferred the file through FTP?
Can a server be configured not to run some specific files? How can i get round this? has is got something to do with .htacess?
Be sure you uploaded your file into the /www or /httpdocs subdirectory.
If so, check the permissions of your file.
Does the fiel have the same extension as the other php files that work.
ie. myfile.php or myfile.php4
The default config for apache specifies that php files have the extension "php4" as php version 3.0 and before are not compatible with the current versions. Usually the plain "php" extension is re-enabled but maybe not in your case.

Categories