Perl Uncaught SyntaxError: Invalid or unexpected token - linux

My perl code has a error on the first line #!/usr/bin/perl when deploying on linux server. But it work properly in my local b/c I used this line #!C:\xampp\perl\bin\perl.exe.
Can somebody tell me the reason?

Ah I have solved this problem by enable mod cgi in my web server. In my case I use apache2 so I added some configs like:
Explicitly using Options to permit CGI execution
You could explicitly use the Options directive, inside your main server configuration file, to specify that CGI execution was permitted in a particular directory:
<Directory "/var/www/mypro">
Options ExecCGI
</Directory>
The above directive tells Apache to permit the execution of CGI files. You will also need to tell the server what files are CGI files. The following AddHandler directive tells the server to treat all files with the cgi or pl extension as CGI programs:
AddHandler cgi-script .cgi .pl
After that load cgi module:
a2enmod cgi
Restart my apache server. It worked!
If you use another web server like nginx you can find some way to enable cgi in your config file.

Related

How to setup python3 as cgi on mac running apache2

I setup apache2 on a mac (big sur), and it works for an html page. But I installed python3 on the machine, yet if I try to load a .py file in the home directory my web browser the system treats it like I'm running a local program and opens it in an editor.
I can't find any doc on how to get python3 to run as cgi in the apache server. Any help?
UPDATE: I added to the httpd.conf file:
Options +ExecCGI
AddHandler cgi-script .py
Now it spits back the file as just plain text, even though it ends in ".py" but still won't execute.
UPDATE: I removed the # from the load cgi lines. Now it responds "Forbidden: you do not have permission to access this resource". The file is 755, and all other files in the home directory are accessible.
I had to put
<Directory "/...the directory/">
Options +ExecCGI
</Directory>
At the bottom of the httpd.conf file. Then I had further problems with a "malformed header". I checked the error log and it was seeing the actual content of the python script, so it must have been running the script. I added
print("Content-Type: text/html\r\n\r\n")
as the first output in the program and that fixed it. The \r\n\r\n tells it that i am done with headers and onto the body.

Apache configuration 2.4

I need to perform the following configuration on my Apache server, version 2.4, with CentOS system.
The instructions I received are as follows:
Configuring Another Web Server to Serve the Files If another web
server is using port 80 on the same server as Centova Cast, you will
need to use this method.
This method depends upon your knowledge of the other web server you
are using; you must be familiar enough with your web server of choice
to configure it appropriately. Centova Technologies cannot provide
support or assistance with any changes to your web server's
configuration.
To allow "Let's Encrypt" to validate your domain ownership, you must
configure your web server such that any requests for files under the
following URI (replacing example.com with your own domain name):
http://example.com/.well-known/acme-challenge/ ...are configured to
serve files from the following directory:
/usr/local/centovacast/etc/ssl/acme-challenges/ So for example, if you
create a file called
/usr/local/centovacast/etc/ssl/acme-challenges/test.txt, you should be
able to visit http://example.com/.well-known/acme-challenge/test.txt
in your browser and see the contents of the test.txt file.
A few example configurations are provided below. Please note, however,
that differences in your web server configuration may require
additional settings not mentioned here.
Example apache:
Alias /.well-known/acme-challenge /usr/local/centovacast/etc/ssl/acme-challenges
<Directory /usr/local/centovacast/etc/ssl/acme-challenges>
Options None
AllowOverride None
Apache 2.x
<IfModule !mod_authz_core.c>
Order allow,deny
Allow from all
</IfModule>
Apache 2.4
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
I've tried everything and I can't get it to work, how should the correct configuration be done?
My configuration file is located at: /etc/httpd/conf/httpd.conf

Apache server 2.4.7

While I am running my first program in Apache /2.4.7 (ubuntu) it shows error as Follows:
Not Found The requested URL /hello was not found on this server. Apache/2.4.7 (Ubuntu) Server at 127.0.0.1 Port 80.
I tried changing apache2.conf as below:
Options ExecCGI Indexes
FollowSymLinks <------
AllowOverride None
Require all granted
AddHandler cgi-script .py <--------
Still not able execute my program ?
Thanks
Based on the little info provided, there are a couple things to try:
Seems like you are trying to load a Python script, so include the extension of the script filename in the URL. As in: http://127.0.0.1/hello.py
Check for owner and permissions for the script. Does Apache have access to execute them?

Disabled PHP for Single Directory with HTACCESS (PHP-FPM)

I want to disable the parsing of PHP files in a specific directory, and found out how to do that here. The problem is that my server uses PHP-FPM, and I get Invalid command 'php_flag', perhaps misspelled or defined by a module not included in the server configuration when I try to use php_flag in my httpd.conf file.
How can I disable parsing of PHP files in a given web-accessible directory with an htaccess/httpd.conf file that is located above web root on a server using PHP-FPM?
Since Fedora 27 switched to php-fpm recently, I, too, ran into this problem. Unfortunately, the old ways of doing things with mod_php do not apply to php-fpm.
I did find another question here that definitely seemed more relevant:
Apache: Disable php in a directory
The gist of it was was to use a <directory> and <filesmatch> block in your config file, and use SetHandler ! for every directory you didn't want PHP code interpreted.
e.g.:
<Directory "/path/to/no/phpfiles">
<Files "*.*">
SetHandler !
</Files>
</Directory>
This is tested and working on Fedora 27, PHP-FPM 7.1.12.
Unlike using the fpm configs directly, this technique works recursively, so placing it at the top level of a tree of stuff you don't want PHP interpreting works as expected.
I disable .htaccess files in my configurations, but this technique should still work. However, <directory> is not valid inside a .htaccess file. Hopefully just removing it, and leaving:
<Files "*.*">
SetHandler !
</Files>
Should be sufficient.
How can I disable parsing of PHP files in a given web-accessible
directory with an htaccess/httpd.conf file that is located above web
root on a server using PHP-FPM?
You can't persay the way you're trying it without mod_php (which you don't want) which is why you're getting that error. PHP-FPM is not an Apache module. It runs independent of Apache. That's actually it's purpose to be used on heavily loaded sites and it can control all the PHP processes.
One way you might be able to achieve this is to specify the exact path you want to run PHP, in your virtualhost file with the Directory directive. Instead of just having the PHP handler stuff, enclose it with the Directory directive with the actual path you want it to run. Here is an example.
<VirtualHost *:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html/
ErrorLog /var/www/example.com/error.log
CustomLog /var/www/example.com/access.log combined
<Directory /path/to/php/only/folder/>
#then you PHP handler stuff
<IfModule mod_fastcgi.c>
AddType application/x-httpd-fastphp5 .php
Action application/x-httpd-fastphp5 /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi_example.com
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi_example.com -socket /var/run/php5-fpm_example.com.sock -pass-header Authorization
</IfModule>
</Directory>
</VirtualHost>
Then restart Apache. This should limit it to that directory. You might have to remove the php handler info from the other config so it's not overwritten.
Note I have not tested this solution.

How do I change the working directory of Apache2/mod_perl2

I have a mod_perl2 module that writes some files to the working directory. The standard working directory of mod_perl2 or Apache2 seems to be "/". Of course, Apache is not allowed to write files in this directory.
Can anyone tell me how I can change the working directory or the directory mod_perl2 writes to?
I use Apache2 Prefork on SLES 10.
Can you do a perl chdir(script-dir) at the beginning of the script ?
On a second thought that would defeat the purpose when you try to use in threaded scenario where changing the working directory might break the threads.
You could try RegistryPrefork module ?
# httpd.conf
PerlModule ModPerl::RegistryPrefork
Alias /perl-run/ /home/httpd/perl/
<Location /perl-run>
SetHandler perl-script
PerlResponseHandler ModPerl::RegistryPrefork
PerlOptions +ParseHeaders
Options +ExecCGI
</Location>

Resources