php command line not executing the php file - command-line

I am using Php 7.3 on Debian 10. It functions well with various clients installed (Drupal, etc.).
However, with a simple file in a directory such as /var/www/some_directory/index.php, the file fails to execute and displays the PHP code (some_directory is not */public_html).
When I simply use the command line:
php -f /var/www/some_directory/index.php
I get the same error, which leads me to think that it is not related to the Apache2 configuration.
The content of the index.php file is fairly straightforward:
<html>
<body>
You have been redirected.
<php?
echo "This is a test";
?>
</body>
</html>
What confuses me even more is that if I type:
php <<< '<?php echo "This is a test";?>'
I get the expected behaviour.
What on earth is happening? Can someone help?
The .htaccess is just below this line. Its a lame copy of the Drupal .htaccess (hoping this would fix the bug).
Here is the file. Its a lame copy (with adaptations) of the .htaccess file contained in drupal directory (I was hoping this would be a quick fix).
<FilesMatch "\.(engine|inc|install|make|module|profile|po|sh|.*sql|theme|twig|t\
pl(\.php)?|xtmpl|yml)(~|\.sw[op]|\.bak|\.orig|\.save)?$|^(\.(?!well-known).*|En\
tries.*|Repository|Root|Tag|Template|composer\.(json|lock)|web\.config)$|^#.*#$\
|\.php(~|\.sw[op]|\.bak|\.orig|\.save)$">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order allow,deny
</IfModule>
</FilesMatch>
Options -Indexes
DirectoryIndex index.php index.html index.htm
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
<IfModule mod_php5.c>
php_value assert.active 0
php_flag session.auto_start off
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_flag mbstring.encoding_translation off
php_value always_populate_raw_post_data -1
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A1209600
<FilesMatch \.php$>
ExpiresActive Off
</FilesMatch>
</IfModule>
Here is the apache.conf file:
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

there is a typo, try it like this
<html>
<body>
You have been redirected.
<?php
echo "This is a test";
?>
</body>
</html>
or you can use it with the shebang
#!/usr/bin/env php
<html>
<body>
You have been redirected.
<?php
echo "This is a test";
?>
</body>
</html>

Related

Why isn't my site redirecting to https from http?

Good day, i have redirected my site using .htaccess but my http://www.example.com would not load unless i manually add the https:// to it.
Here is what my .htaccess looks like;
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.feathersignature.com/$1 [R,L]
RewriteOptions inherit
# BEGIN WordPress
# The directives (lines) between `BEGIN WordPress` and `END WordPress` are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php5_module>
php_value memory_limit 256M
php_value upload_max_filesize 50M
php_value post_max_size 50M
php_value max_execution_time 300
php_flag asp_tags Off
php_flag display_errors Off
php_value max_input_time 60
php_value max_input_vars 1000
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_value memory_limit 256M
php_value upload_max_filesize 50M
php_value post_max_size 50M
php_value max_execution_time 300
php_flag asp_tags Off
php_flag display_errors Off
php_value max_input_time 60
php_value max_input_vars 1000
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php56"
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
Please what can do to properly redirect my site? I am a new developer and i want to learn. Thank you

Magento custom rest API works on localhost but throws internal server error on server

We created a couple of custom API's that are working fine on localhost, but on the server one of them give's internal server error.
In the error log we got this:
[Fri Jul 14 16:06:46.397538 2017] [core:error] [pid 7894:tid 139744927274752] [client 5.2.174.151:40133] malformed header from script 'index.php': Bad header: content-type
In the API we set the header content type to application/json
<?php header('content-type','application/json'); ?>
The php versions on localhost and server are the same 5.6.31
And this is the htaccess:
DirectoryIndex index.php
<IfModule mod_php5.c>
php_value memory_limit 768M
php_value max_execution_time 18000
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
</IfModule>
<IfModule mod_php7.c>
php_value memory_limit 768M
php_value max_execution_time 18000
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<IfModule mod_ssl.c>
SSLOptions StdEnvVars
</IfModule>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
AddDefaultCharset Off
AddType 'text/html; charset=UTF-8' html
<IfModule mod_expires.c>
ExpiresDefault "access plus 1 year"
ExpiresByType text/html A0
ExpiresByType text/plain A0
</IfModule>
RedirectMatch 403 /\.git
<Files composer.json>
order allow,deny
deny from all
</Files>
<Files composer.lock>
order allow,deny
deny from all
</Files>
<Files .gitignore>
order allow,deny
deny from all
</Files>
<Files .htaccess>
order allow,deny
deny from all
</Files>
<Files .htaccess.sample>
order allow,deny
deny from all
</Files>
<Files .php_cs>
order allow,deny
deny from all
</Files>
<Files .travis.yml>
order allow,deny
deny from all
</Files>
<Files CHANGELOG.md>
order allow,deny
deny from all
</Files>
<Files CONTRIBUTING.md>
order allow,deny
deny from all
</Files>
<Files COPYING.txt>
order allow,deny
deny from all
</Files>
<Files Gruntfile.js>
order allow,deny
deny from all
</Files>
<Files LICENSE.txt>
order allow,deny
deny from all
</Files>
<Files LICENSE_AFL.txt>
order allow,deny
deny from all
</Files>
<Files nginx.conf.sample>
order allow,deny
deny from all
</Files>
<Files package.json>
order allow,deny
deny from all
</Files>
<Files php.ini.sample>
order allow,deny
deny from all
</Files>
<Files README.md>
order allow,deny
deny from all
</Files>
<Files magento_umask>
order allow,deny
deny from all
</Files>
ErrorDocument 404 /pub/errors/404.php
ErrorDocument 403 /pub/errors/404.php
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=edge"
<FilesMatch "\.(appcache|atom|bbaw|bmp|crx|css|cur|eot|f4[abpv]|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|topojson|tt[cf]|txt|vcard|vcf|vtt|webapp|web[mp]|webmanifest|woff2?|xloc|xml|xpi)$">
Header unset X-UA-Compatible
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
# with AJAX withCredentials=false (cookies NOT sent)
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Credentials "true"
Header always set Access-Control-Allow-Methods "POST, GET, PUT, PATCH, DELETE, OPTIONS"
Header always set Access-Control-Allow-Headers "Content-Type, Authorization"
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]]
# RewriteRule ^(.*)$ $1 [R=200,L]
Any suggestion will be highly appreciated, thank you.

Can not run app using fcgi of perl Dancer

I am testing to deploy the tester example of Dancer using fcgi. But it just didn't work.
I keep getting the error message:
File does not exist: /home/tester/MyApp/public/dispatch.fcgi/
However, this app can run successfully with cgi. And I have made the changes to http.conf according to dancer's deployment manual.
Can someone pointing me to some solutions or possible reasons for this error?
below is the http.conf:
<VirtualHost *:80>
ServerName localhost
# /srv/www.example.com is the root of your
# dancer application
DocumentRoot /home/tester/MyApp/public
ServerAdmin you#example.com
<Directory "/home/tester/MyApp/public">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler fastcgi-script .fcgi
</Directory>
ScriptAlias / /home/tester/MyApp/public/dispatch.fcgi/
ErrorLog /var/log/apache2/MyApp-error.log
CustomLog /var/log/apache2/MyApp-access_log common
</VirtualHost>
Thank you
I don't think that ScriptAlias is what you want to be using. From the documentation...
The ScriptAlias directive has the same behavior as the Alias
directive, except that in addition it marks the target directory as
containing CGI scripts that will be processed by mod_cgi's cgi-script
handler.
Basically Apache looks for a directory called '/home/tester/MyApp/public/dispatch.fcgi/' and every file in this dir is processed through mod_cgi. In this case it can't be found since it's a regular file.
Have you tried using mod_rewrite? My httpd config for Dancer is pretty much the same as yours except I'm using mod_rewrite
DocumentRoot /home/user/src/MyApp/public
<Directory "/home/user/src/MyApp/public">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler fcgid-script .fcgi #using fcgid instead of fastcgi
</Directory>
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dispatch.fcgi$1 [QSA,L]

Mod Perl in ubuntu

I am trying to configure apache2 running on ubunutu 12.04 to run perl script. But the script is not running when i submit the get request from the client.
Below is the default config i have made (after reading in internet):
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /home/Suresh/myFiles
<Directory /home/Suresh/myFiles>
Options Indexes FollowSymLinks MultiViews +ExecCGI
AddHandler cgi-script .pl
AllowOverride ALL
Order allow,deny
allow from all
ExpiresActive On
ExpiresDefault "access plus 6 hours"
<FilesMatch "\.(nff)">
Header set Cache-control "max-age=0, no-cache, proxy-revalidate"
Header set Content-Type "application/octet-stream"
Header set Pragma "no-cache"
Header unset Vary
Header set Connection "Keep-Alive"
</FilesMatch>
</Directory>
I have a perl script saved in the /home/Suresh/myFiles with chmod 777 permissions.
Below is the perl code:
#!/usr/bin/perl
use strict;
use CGI;
#require LWP::UserAgent;
my $q = new CGI;
my #rawCookies = split /~/, $ENV{'HTTP_COOKIE'};
my $extfile = '/home/suresh/Cookies.txt';
open(FH, ">>$extfile") or die "Cannot open file";
print FH "STB Cookies: ", $ENV{'HTTP_COOKIE'}, "\n";
close FH;
The perl works perfectly fine when run with perl command.
The script is not getting executed in default config file.
Can anyone suggest me what else needs to be done ?
You should use this conf:
<VirtualHost *:80>
Alias /perl/ /home/Suresh/myFiles
<Location /perl/>
SetHandler perl-script
PerlResponseHandler ModPerl::Registry
PerlOptions +ParseHeaders
Options +ExecCGI
Order allow,deny
Allow from all
</Location>
</VirtualHost>
Then you must make your script executable % chmod a+rx /home/Suresh/myFiles/script.pl
Make restart apacher server and go to http://localhost/perl/script.pl
That's all.
P.S. More info you may find there http://perl.apache.org/sitemap.html

Perl files in Virtualhosts cgi-bin are always downloaded (Debian 6 and Apache2)

I have been tearing my hair out trying to get this working but no matter what I do I can't get .pl files to execute in the cgi-bin of my virtual directory. I have been searching for solutions for the past 4 hours and have tried everything I have come across, and nothing works for me. Perl files are executing perfectly for my default site, just not for my virtual host. The only mopdified file in my Apache2 configuration is the /etc/apache2/sites-available/default file, and currently it is as follows (except for the sitename):
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
<Directory /var/www/cgi-bin>
Options +ExecCGI
AllowOverride All
AddHandler cgi-script cgi pl
Order allow,deny
allow from all
</Directory>
# ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
# <Directory "/usr/lib/cgi-bin">
# AllowOverride All
# Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
# Order allow,deny
# Allow from all
# </Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerAlias subdom.mysite.com
DocumentRoot /var/www/subdom
<Directory /var/www/subdom/cgi-bin>
Options +ExecCGI
AllowOverride All
AddHandler cgi-script cgi pl
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Any and all help is very much appreciated.
I think it is a typo: You need a period before pl.
AddHandler cgi-script .cgi .pl
Also check this: How do I configure Apache 2 to run Perl CGI scripts?
This sounds and looks familiar:
In my case, I made a mistake with my ScriptAlias directive. I uncommented the original one, but forgot to configure a new one.
As soon as I correctly changed and saved my sites-available/default config file from this:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
.. to this:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
.. and reloaded apache2, it just worked: it stopped displaying my scripts as text, and started running them as a script. Also, it no longer displayed /var/www/cgi-bin as a directory in the browser, but now correctly displays the error:
Forbidden
You don't have permission to access /cgi-bin/ on this server.