How to run CGI scripts on Nginx - perl

I have problem setting up CGI scripts to be run on Nginx, so far I've found http://wiki.nginx.org/SimpleCGI this stuff but problem is that I can't make perl script run as service so that it will run in background and even in case of restart it will start running automatically
Do you have any idea? I'm running Centos 5
I've found some solutions here but I couldn't integrate code given there with this Perl script
I'm completely zero at Perl, please help me
Thanks

Nginx doesn't have native CGI support (it supports fastCGI instead). The typical solution for this is to run your Perl script as a fastCGI process and edit the nginx config file to re-direct requests to the fastCGI process. This is quite a complex solution if all you want to do is run a CGI script.
Do you have to use nginx for this solution? If all you want to do is execute some Perl CGI scripts, consider using Apache or Lighttpd as they come with CGI modules which will process your CGI scripts natively and don't require the script to be run as a separate process. To do this you need install the web server and edit the web server config file to load the CGI module. For Lighttpd, you will need to add a line in the config file to enable processing of CGI files. Then put the CGI files into the cgi-bin folder.

Install another web server(Apache, Lighttpd) that runs on different port. Then proxy your CGI request to the webserver with nginx.
You just need to add this to nginx configuration, after installed a web server on 8080
location /cgi-bin {
proxy_pass http://127.0.0.1:8080;
}
Take a look at Nginx Location Directive Explained for more details.

Nginx is a web server. You need to use an application server for your task, such as uWSGI for example. It can talk with nginx using its native very effective binary interface called uwsgi.

I found this hack using FastCGI to be a little nicer than running another web server. http://nginxlibrary.com/perl-fastcgi/

I found this: https://github.com/ruudud/cgi It says:
===
On Ubuntu: apt-get install nginx fcgiwrap
On Arch: pacman -S nginx fcgiwrap
Example Nginx config (Ubuntu: /etc/nginx/sites-enabled/default):
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log;
location / {
root /srv/static;
autoindex on;
index index.html index.htm;
}
location ~ ^/cgi {
root /srv/my_cgi_app;
rewrite ^/cgi/(.*) /$1 break;
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /srv/my_cgi_app$fastcgi_script_name;
}
}
Change the root and fastcgi_param lines to a directory containing CGI scripts, e.g. the cgi-bin/ dir in this repository.
If you are a control freak and run fcgiwrap manually, be sure to change fastcgi_pass accordingly. The path listed in the example is the default in Ubuntu when using the out-of-the-box fcgiwrap setup.
===
I'm about to try it.

Related

How do I set my VPS Webmin/Virtualmin server to show data from MongoDB in the hosted website?

This is my first question I hope I do it right. So :
I developed a MERN website, on which I have perfect connection with a MongoDB db as well as an Amazon S3 one.
I am currently trying to host it on a Hostinger VPS with Virtualmin and Webmin. The data is in thanks to FTP working, so the website design shows but the mongoDB data is missing.
So far :
DNS set properly,
SSH all good,
mongo shell installed inside of server through the console, I can see my db and the data
new user created successfully with mongo method db.createUser(), attached to my db
So my question is : what are the following steps to make the way to data, through the server, to the website ?
I'm new to this and I've searched for several days now everywhere without any success, and the hosting support is lost on the matter...
Thanks!
By default, Virtualmin installs the LAMP/LEMP stack. there is no support for MERN/MEAN or node js based applications. you have to manually configure your server through the terminal by ssh.
follow the instruction.
Apache NodeJS installation
There is no GUI support for node based apps. but you can manage other services like mail, DNS, firewall and SSL etc for your app through Virtualmin and Webmin.
In case it helps anyone I did succeed in setting up the server, it's quite a bit of work. Here's my config :
I set Nginx to listen to the https requests from the front and send them to the back. The config file is called "default", in the folder sites-available, and has the following content :
server {
listen 80;
listen 443 ssl;
root /the/frontend/root/folder;
server_name _;
ssl on;
ssl_certificate /the/ssl/.crt/file;
ssl_certificate_key /the/ssl/.key/file;
# react app & front-end files
location / {
try_files $uri /index.html;
}
# node api reverse proxy
location /api/ {
proxy_pass http://localhost:portlistenedbybackend/api/;
}
}
The React frontend comes with a .env file that is integrated in the build. In it I set the url to where the frontend sends requests (these are then caught by Nginx). Be careful to set this url to the domain of your website when deploying, so in my case : https://example.com/api
The production process manager pm2 is useful to keep alive the backend at all times so I installed it and used it for the Node backend. The command to add the backend main server file (in my case server.js) to pm2 from the console : sudo pm2 start your/serverfile/address
Here's a couple of links that proved very useful to understand how to configure the server :
Applicable to Amazon server but a lot applicable here too : https://jasonwatmore.com/post/2019/11/18/react-nodejs-on-aws-how-to-deploy-a-mern-stack-app-to-amazon-ec2
A guide to log journal in console for debugging :
https://www.thegeekdiary.com/beginners-guide-to-journalctl-how-to-use-journalctl-to-view-and-manipulate-systemd-logs/
For setting up Webmin server : https://www.serverpronto.com/kb/cpage.php?id=Webmin
At first I discarded Webmin and Virtualmin since all I could find (from support included) was tutorials to setup the server via console. So bits by bits I set it up. Then, finally, I got from the support a tuto to setup the server from Webmin. But to this day I can't say wether that made a difference in the structure. But at least it's clean.
Last but not least, a couple of console commands I found very useful :
systemctl status theserviceyouwanttosee
systemctl start theserviceyouwanttostart
systemctl stop theserviceyouwanttostop
systemctl restart theserviceyouwanttorestart
Examples of services : nginx, mongod...
Test if Nginx is setup properly : sudo nginx -t
reload nginx config file after any modification : sudo nginx -s reload
See the last errors logged by nginx : sudo tail -f /var/log/nginx/error.log
Save current pm2 settings : pm2 save
Backend execution logs : sudo pm2 logs
Identify the processes still running with mongo (useful if mongod won't restart properly) : pgrep mongo
Kill the mongo process to be able to start fresh : kill <process>
Show all services used by server : sudo systemctl
See all processes in execution and stats of the CPU amongst other things : top
I'm still new to all of this despite my couple of weeks on the subject so this description is most probably improvable. don't hesitate to suggest any improvement, mistake, or ask questions, I'll do my best to answer them.
Cheers!

Nginx not executing .pl extensions. It downloads the file instead [duplicate]

I have problem setting up CGI scripts to be run on Nginx, so far I've found http://wiki.nginx.org/SimpleCGI this stuff but problem is that I can't make perl script run as service so that it will run in background and even in case of restart it will start running automatically
Do you have any idea? I'm running Centos 5
I've found some solutions here but I couldn't integrate code given there with this Perl script
I'm completely zero at Perl, please help me
Thanks
Nginx doesn't have native CGI support (it supports fastCGI instead). The typical solution for this is to run your Perl script as a fastCGI process and edit the nginx config file to re-direct requests to the fastCGI process. This is quite a complex solution if all you want to do is run a CGI script.
Do you have to use nginx for this solution? If all you want to do is execute some Perl CGI scripts, consider using Apache or Lighttpd as they come with CGI modules which will process your CGI scripts natively and don't require the script to be run as a separate process. To do this you need install the web server and edit the web server config file to load the CGI module. For Lighttpd, you will need to add a line in the config file to enable processing of CGI files. Then put the CGI files into the cgi-bin folder.
Install another web server(Apache, Lighttpd) that runs on different port. Then proxy your CGI request to the webserver with nginx.
You just need to add this to nginx configuration, after installed a web server on 8080
location /cgi-bin {
proxy_pass http://127.0.0.1:8080;
}
Take a look at Nginx Location Directive Explained for more details.
Nginx is a web server. You need to use an application server for your task, such as uWSGI for example. It can talk with nginx using its native very effective binary interface called uwsgi.
I found this hack using FastCGI to be a little nicer than running another web server. http://nginxlibrary.com/perl-fastcgi/
I found this: https://github.com/ruudud/cgi It says:
===
On Ubuntu: apt-get install nginx fcgiwrap
On Arch: pacman -S nginx fcgiwrap
Example Nginx config (Ubuntu: /etc/nginx/sites-enabled/default):
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/access.log;
location / {
root /srv/static;
autoindex on;
index index.html index.htm;
}
location ~ ^/cgi {
root /srv/my_cgi_app;
rewrite ^/cgi/(.*) /$1 break;
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
fastcgi_param SCRIPT_FILENAME /srv/my_cgi_app$fastcgi_script_name;
}
}
Change the root and fastcgi_param lines to a directory containing CGI scripts, e.g. the cgi-bin/ dir in this repository.
If you are a control freak and run fcgiwrap manually, be sure to change fastcgi_pass accordingly. The path listed in the example is the default in Ubuntu when using the out-of-the-box fcgiwrap setup.
===
I'm about to try it.

What's happening with search.cpan.org and how to install an redirector?

EDITED question
It last 2 weeks search.cpan.org was down many times. Yes i know here is a metacpan, but zilion of links from the net points to search.cpan.org so the metacpan isn't the "only" solution.
Want make a local redirector, by entering to my /etc/hosts something like:
search.cpan.org 127.0.0.1
and run an simple PSGI (or Apache) server on localhost:80 what should done the redirects. So the requests for "search.cpan.org" would be processed at "localhost" with the script and it should return valid 302 responses and redirect to metacpan.org.
#Mpapec pointed to mcpan.org - what doing exactly for what i looking, so now enoudh redirect every request to "mcpan.org".
After edited my /etc/hosts as above, tried the next apache config:
<VirtualHost *:80>
ServerName search.cpan.org
RedirectPermanent / http://search.mcpan.org/
</VirtualHost>
but doesn't work. Would be nice to get some help. Or an alternative, a simple app.psgi script would be nice too.
So, the questions are:
how to configure local apache for redirects search.cpan.org -> search.mcpan.org
or how to write a simple app.psgi for running it with plackup -p 80 for the same function
and one offtopic question:
know anyone something about the status and a future of search.cpan.org?
search.cpan.org is run by Graham Barr. For questions about websites that he runs, you'll have to contact him.
The source code is not available. That's why MetaCPAN sprang up.
I wouldn't bother with a redirector. Maybe a Greasemonkey script, though :)
I was an configuration error in my apache. The next works.
1.) edit /etc/hosts and add a line IP.OF.YOUR.LOCAL.WEBSERVER search.cpan.org, e.g.:
127.0.0.1 search.cpan.org
2.) for apache (i have 2.4) into httpd.conf enter
<VirtualHost *:80>
ServerName search.cpan.org
Redirect / http://search.mcpan.org/
</VirtualHost>
3.) Be sure than your apache listening on port 80 e.g. have a directive
Listen 80
With the above, every request to "search.cpan.org" get redirected to "search.mcpan.org" and the "mcpan" redirects it to "metacpan". It is suboptimal, would be nicer to have a set of rewrite rules what redirects directly into "metacpan", but works.

How to redirect domain:port to domain with Glassfish?

I have a domain like www.example.com, I'm using Glassfish, which hosts my application at port 12544.
So I wonder two things :
How can I redirect this www.example.com:12544 to www.example.com ?
And the same way but for https ? I mean, https://www.example.com ?
I am really new to Glassfish any help will be very appreciated.
The best way is to place Glassfish behind Apache http server and configure apache to point requests to glassfish. Therefore http requests are handled by Apache and all www.example.com requests are pointed to www.example.com:12544 internally. Below is a brief explanation of how to do this.
Install Glassfish 3+ (make sure your listener is created and activated on 8009 and your jk is enabled)
Install Apache (2.2+)
get the mod_jk connector and place it into apache modules folder to do the configurations.
create a worker.properties file and place it into apache conf folder. It should contain the following properties:
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
Open httpd.conf file in apache conf folder and place the following commands (outside Virtual Host):
LoadModule jk_module modules/mod_jk.so
#location of the worker file
JkWorkersFile conf/worker.properties
#where to put jk logs
JkLogFile logs/mod_jk.log
#log level [deug/error/info]
JkLogLevel debug
#Log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# Indicate to send SSL KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# Set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to Glassfish
JkMount /*.jsp worker1
# Send all webapp requests to Glassfish
JkMount /* worker1
You need also to add a VirtualHost section in your conf file. This maps your domain with the path in Glassfish so Apache will be able to see it. The following tells apache to map all /myapp/* links to glassfish
<VirtualHost 111.111.111.111:80>
ServerAdmin admin#domain
ServerName domain
JkMount /myapp/* worker1
</VirtualHost>
Note: if your Glassfish listener is not created you can created from cmd using glassfish asadmin with the following command:
asadmin create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector
Restart Apache and Glassfish for the new configurations to be updated. Apache should now see your Glassfish on port 80. So your www.example.com:12544 will be served on www.example.com.

nginx and catalyst configuration

I am having trouble deploying a Catalyst application using nginx and
fastcgi. I attempting to do this under ubuntu 12.04.
I have successfully configured nginx to serve static content from my
app's /root subdirectory. However, when I try to any of my dynamic
urls, I get a 404 error in my application's error log saying the
(unmapped) url is not found, which leads me to believe that nginx is
attempting to serve the request akin to a static page instead of
sending it to my Catalyst app.
To restate, hitting 'localhost:3001/root/static.html' results in the
static content being successfully displayed in the browser, but
hitting 'localhost:30001/expense/editor' results the following error:
"GET /expense/editor HTTP/1.1" 404
(where '/expense/editor' is a path in my app, one that I can
successfully access when running the built-in Catalyst development
server).
I am launching the Catalyst app as:
> perl script/budgetweb_fastcgi.pl -l localhost:3003
I also tried running /etc/init.d/fcgiwarp. I am unclear if I need to run a
separate fastcgi wrapper, or if the perl script above is my fastcgi
wrapper. I edited fcgiwrap to use TCP sockets (127.0.0.1:3003), which
then prevented me from running both /etc/init.d/fcgiwrap and
script/budgetweb_fastcgi.pl at the same time, since they both use the
same socket. So I'm guessing I'm only supposed to use the Catalyst
script? Also, when running fcgiwrap, I get 502 "bad gateway" errors
when attempting to access static content.
Any help, or pointers to help, will be much appreciated. So far I have looked at the following pages (among others; StackOverflow will only allow me to post two links):
Catalyst wiki
HOWTO: Deploy a Catalyst application using FastCGI and nginx
Here is my nginx config file for the server:
server {
listen 3001;
server_name budgetweb.com;
root /local/www/money/budgetweb;
location /root {
add_header Cache-control public;
root /local/www/money/budgetweb/;
}
location / {
access_log /local/www/money/budgetweb/logs/access.log;
error_log /local/www/money/budgetweb/logs/error.log;
index index.html index.htm index.pl;
try_files $uri =404;
gzip off;
fastcgi_pass localhost:3003;
fastcgi_index index.pl;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /local/www/money/budgetweb$fastcgi_script_name;
fastcgi_param SCRIPT_NAME /;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
# Disable gzip (it makes scripts feel slower since they have to complete
# before getting gzipped)
gzip off;
# include /etc/nginx/fcgiwrap.conf;
}
The fastcgi.pl script included with Catalyst is your FastCGI wrapper. All you should have to do is start that on a socket, then point your webserver to that socket and everything should pass through. The only thing you'll want to do for a production system is create a start/stop script that will start and stop your application on startup and shutdown. The start command will look pretty much like what you ran above (you may want to add a '-d' flag to daemonize it.)
On your webserver configuration, configuring '/' to point to your application should be fine. You might try removing the 'index', 'try_files', and 'fastcgi_index' configuration lines, that might be causing nginx to try and statically serve the content instead of passing the request to your application.