lighttpd + mod_fastcgi + django - django

I am having trouble deploying my django app to a server that uses lighttpd (on which I'm root.)
Here's my lighttpd.conf:
server.modules = (
# "mod_access",
# "mod_alias",
# "mod_compress",
# "mod_redirect",
"mod_status",
#"mod_rewrite",
# "mod_fastcgi",
# "mod_accesslog",
)
status.status-url = "/server-status"
status.config-url = "/server-config"
$HTTP["host"] == "myurl.com" {
server.document-root = "/var/www/myurl"
server.errorlog = "/var/log/lighttpd/myurl/error.log"
accesslog.filename = "var/log/lighttpd/nixcraft/access.log"
server.error-handler-404 = "/e404.html"
fastcgi.server = (
"/myurl/nonorientable.fcgi" => (
"main" => (
# Use host / port instead of socket for TCP fastcgi
# "host" => "127.0.0.1",
# "port" => 3033,
"socket" => "/tmp/myurl.sock",
"check-local" => "disable",
)
),
)
}
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm")
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/x-javascript", "text/css", "text/html", "text/plain" )
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
server.modules += ( "mod_auth" )
auth.debug = 2
auth.backend = "plain"
auth.backend.plain.userfile = "/etc/lighttpd/.lighttpdpassword"
auth.require = ( "/" =>
("method" => "basic",
"realm" => "",
"require" => "user=john"))
And then I ran this on my server (from the folder /var/www/myurl):
sudo python manage.py runfcgi daemonize=false socket=/tmp/myurl.sock maxrequests=1
At this point, when I try to load myurl.com, all I get is the directory listing: my django app is not executed. I thought that the problem was that mod_fastcgi is commented in my conf, it actually appears on myurl.com/server-config. If I uncomment it, lighttpd complains that I'm trying to load the same module twice and exits, so my guess is that it is imported by default.
I'm using django1.4 (latest stable) and lighttpd 1.4.28 on ubuntu 12.04, and I'm pretty stuck. I followed the official doc (https://docs.djangoproject.com/en/1.4/howto/deployment/fastcgi/)

It looks like your missing the rewrite directive given in the docs:
url.rewrite-once = (
"^(/media.*)$" => "$1",
"^/favicon\.ico$" => "/media/favicon.ico",
"^(/.*)$" => "/mysite.fcgi$1",
)
Update: You need to change the fastcgi.server section from /myurl/nonorientable.fcgi to /mysite.fcgi so that it matches the rewrite above.

Related

AWS PHP 7 Session got reset in every reset in

I am very new with AWS while deploying my project i found that session is not getting stored .
so with help of google and stack-overflow i was trying to resolve is and trying following code
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
ini_set('display_errors', 'on');
//ini_set('session.gc_maxlifetime', 1500);
ini_set("session.save_handler", "files");
$dir=getcwd() . '/ses'; //folder with 777 permission
ini_set('session.save_path', $dir);
//ini_set('session.name', "name-session");
ini_set('session.gc_probability', 1);
ini_set('session.use_only_cookies', 0);
//ini_set('session.serialize_handler', '');
$sId = session_id();if ($sId == '') { session_start(); }
print_r($_SESSION);
$_SESSION['loggedIn']="swarna";
$_SESSION['name']=session_name();
$_SESSION['cookie_params']=session_get_cookie_params();
$_SESSION['cache_expire']=session_cache_expire();
$_SESSION['session_id']=session_id();
$_SESSION['maxlifetime']=ini_get("session.gc_maxlifetime");
echo "<br> \n <pre>";
print_r($_SESSION);
print_r(scandir($dir));
echo "<br> \n </pre>";
but it didn't worked each time i refresh it is printing the first print_r($_SESSION); as empty and second one with different session id while expedition first one with same data which i have stored last execution .
Array ( )
Array
(
[loggedIn] => swarna
[name] => name-session
[cookie_params] => Array
(
[lifetime] => 0
[path] => /
[domain] =>
[secure] =>
[httponly] =>
)
[cache_expire] => 180
[session_id] => qip9hisbsh901gbpbjef4qe70c
[maxlifetime] => 1440
)
Array
(
[0] => .
[1] => ..
[2] => sess_2rkl7v7h593ih942i1k14o9s4d
[3] => sess_4ccmmm0efq7quj26pm6r0ue9sn
[4] => sess_8rvh2nd9n0ng4vtekevh6p26ef
[5] => sess_lj4r99u9gf22ur935jr4gbcd97
[6] => sess_qip9hisbsh901gbpbjef4qe70c
)
now i am using php7 in AWS Amazon Linux 2 server . can any one guide me what to be done ? what is wrong their .
i have also tried
this has nothing to do with AWS or ec2, I recommend you comment out all PHP into variables at the beginning of the script and always have session_start() at the beginning script

How use authorized user to make requests to another project with the same ldap authorization?

I can't make request to another project with the same auth
I've tried to make
req.basic_auth curent_user.username, current_user.encrypted_password
But it said currnet_user is not defined
I've tried to set in project2
skip_before_filter :authenticate_user!, only: :get_data
Bit it returns
{"status":"422","error":"Unprocessable Entity"}
The code is
uri = URI(["#{request.protocol}#{request.host_with_port}", 'clients', Rails.configuration.system_client_name, 'project1', 'get_data'].join('/'))
res = nil
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https'
) do |http|
req = Net::HTTP::Post.new(uri.request_uri)
# ??? req.basic_auth curent_user.username, current_user.encrypted_password
res = http.request(req)
end
data = res.body
Project1
def get_data
render json: {q: 1}, status: 200
end
Gems are the same in both projects
devise_ldap_authenticatable (0.8.1)
devise (>= 3.0)
net-ldap (>= 0.3.1, < 0.6.0)
I've made no auth
In project2:
skip_before_action :verify_authenticity_token, only: : get_data
skip_before_action :authenticate_user!, only: : get_data
In project1:
uri = URI(["#{request.protocol}#{request.host_with_port}", 'clients', Rails.configuration.system_client_name, get_data, 'get_num_of_synonyms'].join('/'))
res = nil
Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https'
) do |http|
req = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
res = http.request(req)
end

error in fetching mail using griddler(mandrill)

I am implementing griddler(using Mandrill) to fetch e-mail in my app.
This is my class to receive mail
#app/email_receivers/incoming.rb
class EmailReceiver < Incoming::Strategies::Mandrill
def receive(mail)
%(Got message from #{mail.to.first} with subject "#{mail.subject}")
end
end
req = Rack::Request.new(Rails.env)
result = EmailReceiver.receive(req)
This is my rails controller that calls receive method of EmailReceiver class.
#emails_controller.rb
class EmailsController < ActionController::Base
def create
if EmailReceiver.receive(request)
render :json => { :status => 'ok' }
else
render :json => { :status => 'rejected' }, :status => 403
end
end
end
Routes for calling create method of EmailsController
#routes.rb
Rails.application.routes.draw do
post '/emails' => 'emails#create'
end
Note:- After running server in production environment
rails s -e production
I got this error
/home/bvc-2/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/request.rb:192:in `[]=': string not matched (IndexError)
Where "puts req" in
req = Rack::Request.new(Rails.env)
turns out to be:
#<Rack::Request:0xc3bace8 #env="production">
And my config/application.rb file is:-
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
ActionMailer::Base.smtp_settings = {
:address =>"smtp.gmail.com",
:port => 587,
:domain => "gmail.com",
:user_name => "***#gmail.com",
:password => "pass*******",
:authentication => 'plain',
:enable_starttls_auto => true
}
module ComplaintSystem
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
config.active_job.queue_adapter = :delayed_job
config.time_zone = 'Mumbai'
config.active_record.default_timezone = :local
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
end
end
This type of error happens when you try to access a string variable as a Hash by mistake.
s = "hello world"
s["position"] = "programming is fun"
# > IndexError: string not matched
# > from (irb):5:in `[]='
# > from (irb):5
Look at your full stack trace and see where you are trying to do such operation.

Running C++ as FastCGI in Lighttpd

I'm trying to run C++ binaries as FastCGI in Lighttpd, but it won't start. I tried something like this:
fastcgi.server += (".cpp" =>
( "localhost" =>
(
"socket" => "/tmp/mysocket",
"bin-path" => "/var/www/index.cpp",
"max-procs" => 1
))
)
But I can't get it working. I want to keep the C++ in memory for starting really fast.
You should adapt your configuration to something like this:
fastcgi.server = (
"/api" => (
"api.fastcgi.handler" => (
"socket" => "/var/run/lighttpd/lighttpd-fastcgi-test-" + PID + ".socket",,
"check-local" => "disable",
"bin-path" => "/var/www/localhost/cgi-bin/test.fcgi",
"max-procs" => 30,
)
)
)
For all requests: localhost/api/some_test lighttp will call your fcgi executable /var/www/localhost/cgi-bin/test.fcgi

Serving multiple apps on different sub-domains with Lighthtpd

I am trying to serve different subdomains with different applications, all using the same Lighthtpd server as a front server. All applications serving sub-domains are also running on the same server as Lighthtpd.
I have setup DNS records as follows (both are A records)
mydomain.com xx.xx.xx.xx
sub.mydomain.com xx.xx.xx.xx
xx.xx.xx.xx is same for both records.
Relevant parts of my Lighttpd configuration file as as follows
$HTTP["host"] =~ "sub.mydomain.com" {
fastcgi.server = (
"/ideas.fcgi" => (
"main" => (
"host" => "127.0.0.1",
"port" => "9030",
)
),
)
url.rewrite-once = (
"^(/.*)$" => "/ideas.fcgi$1",
)
}
$HTTP["host"] =~ "mydomain.com" {
proxy.balance = "round-robin" proxy.server = ( "/" =>
( ( "host" => "127.0.0.1", "port" => 9010 ) ) )
}
The process running on port 9010 is a Java web app and the process running on 9030 is a django webapp running as an fcgi process started with the following command
./manage.py runfcgi method=threaded host=127.0.0.1 port=9030
My problem is - in my browser both the url's, mydomain.com and sub.mydomain.com get the same web application (the app intended for mydomain.com).
It seems that the Lighttpd setting for sub.mydomain.com is either being ignored or overriden by the setting for mydomain.com. I even tried reversing the physical location of both settings in the configuration file, but that did not help either.
I am sure this configuration should be possible. Am I missing something ?
The line $HTTP["host"] =~ "mydomain.com" is doing a regular expression match, so it matches the Django subdomain as well as the domain. If you use a string equals match as below, then the Django subdomain will not match.
$HTTP["host"] == "mydomain.com"
There might be a better way to do this (e.g. a command to skip all further virtual hosts once you've matched the first one), but I'm afraid my lighttpd knowledge is limited.