Setup uWSGI as webserver with pyramid (no NGINX) - pyramid

Most of the available tutorials show how to set up uWSGI with an upstream HTTP server (like NGINX). But uWSGI alone can act beautifully as router/proxy/load-balancer - refer this
For my project, I didn't want to setup NGINX at this moment so I started off exploring the option of serving webpages through uWSGI. The answer here shows how to set it up with Pyramid.

I am using pyramid_mongodb scaffold, which I have modified to get it working on python3. See here for details.
Assuming that we have a Pyramid project (created with pcreate -s pyramid_mongodb MyProject).
Here are the uWSGI configurations needed in development/production.ini
[uwsgi]
http = 0.0.0.0:8080
#http-to /tmp/uwsgi.sock - use this for standalone mode
#socket = :9050
master = true
processes = 2
harakiri = 60
harakiri-verbose = true
limit-post = 65536
post-buffering = 8192
daemonize = ./uwsgi.log
pidfile = ./orange_uwsgi.pid
listen = 128
max-requests = 1000
reload-on-as = 128
reload-on-rss = 96
no-orphans = true
#logto= <log file>
log-slow = true
virtualenv = <path to virtual environment>
#file = /path/to/pyramid.wsgi
#callable = application
need-app = true
Also since we are using uWSGI we can comment out server portion from the ini
#[server:main]
#use = egg:waitress#main
#host = 0.0.0.0
#port = 6544
To run the server use
uwsgi --ini-paste development.ini

Much more easier! No need to modify at all the "development.ini" file.
Create in the App folder where your "development" and "production" ini files reside, a file called "wsgi.app" with the following content:
from pyramid.paster import get_app,setup_logging
ini_path = '/pathto/myapp/development.ini'
setup_logging(ini_path)
application = get_app(ini_path,'main')
create let's say "myapp.conf" with it's content:
[uwsgi]
socket = 127.0.0.1:3053
uid = daemon
gid = daemon
venv = /pathto/myenv
project_dir = /pathto/myapp
chdir = %(project_dir)
master = true
plugins = plugins/python/python
check-static = %(project_dir)
static-skip-ext = .py
static-skip-ext = .pyc
static-skip-ext = .inc
static-skip-ext = .tpl
pidfile2 = /var/run/uwsgi/myinfo.pid
disable-logging = true
processes = 8
cheaper = 2
enable-threads = true
offload-threads = N
py-autoreload = 1
wsgi-file = /pathto/myapp/wsgi.py
and the NGINX configuation is very simple:
server {
listen [xxxx:xxxx:xxxx:xxx:xxxx:xxxx]:80; #for IPv6
listen xxx.xxx.xxx.xxx:80; #for IPv4
server_name myapp.domain.com;
location / {
try_files $uri #uwsgi;
}
location #uwsgi {
include uwsgi_params;
uwsgi_pass 127.0.0.1:3053;
}
}
restart nginx with "/path/to/usr/sbin/nginx -s reload"
start the uwsgi process -> change to "cd /usr/local/uwsgi-2.0.9" -> ./uwsgi -ini /var/www/myapp.conf

Related

How to run a docker container declaratively on nixos server?

How do I run a docker container declaratively on nixos server ? I am trying to run whoogle on a nix server and I don't wish to manually restart the whoogle container everytime I restart the server.
After bit of guidance from the r/nixos community settled with this.
{ config, pkgs, ... }:
let
host = "mydomain";
in
{
virtualisation = {
podman = {
enable = true;
dockerCompat = true;
};
oci-containers = {
backend = "podman";
containers.whoogle-search = {
image = "benbusby/whoogle-search";
autoStart = true;
ports = [ "8080:5000" ]; #server locahost : docker localhost
};
};
};
services.nginx.virtualHosts = {
"search.${host}" = {
enableACME = true;
forceSSL = true;
locations."/".proxyPass = "http://localhost:8080";
};
};
}
Use --restart always with docker command at initial start
enable docker service to start at boot
And your container will start at boot

Cannot place text inside file using linux

I have installed moodle in AWS EC2 instance. But config.php file is missing. And I created a file using **
touch config.php
** command. But I couldnt place my content inside that file.
My content is
<?php // Moodle configuration file
unset($CFG); global $CFG; $CFG = new stdClass();
$CFG->dbtype = 'mariadb'; $CFG->dblibrary = 'native'; $CFG->dbhost
= 'localhost'; $CFG->dbname = 'moodledb'; $CFG->dbuser = 'root'; $CFG->dbpass = 'pass'; $CFG->prefix = 'mdl_'; $CFG->dboptions = array ( 'dbpersist' => 0, 'dbport' => '', 'dbsocket' => '', 'dbcollation' => 'utf8mb4_general_ci', );
$CFG->wwwroot = 'http://localhost/moodle'; $CFG->dataroot = 'L:\\xampp\\moodledata'; $CFG->admin = 'admin';
$CFG->directorypermissions = 0777;
require_once(__DIR__ . '/lib/setup.php');
I have connected to AWS using Putty from windows.
Use cat and heredoc:
cat > config.php <<'EOF'
your content
goes here
EOF
And you can skip touch this way.

SaltStack: Create a server via salt-cloud and set endpoints autmatically in Azure?

I'm a bit stuck with my server deployment recipes...
So far, I can create and provision my servers via commandline:
First I run salt-cloud and afterwards I provision it via salt-ssh...
But since I'm using some special ports (https,...) I also have to set/open input/endpoints (=ports) on Microsoft Azure.
This is where I stuck: is there any way of telling salt-cloud, to automatically execute a script, which opens the endpoints automatically?
My preferred result would be:
run salt-cloud => sets up new machine and opens all necessary ports
run salt-ssh to provision them
I have already looked at salt orchestration, but it looks like it's more for server fleets, instead of single (external) server configuration.
Any hints?
You could write a custom bootstrap script that would open the ports that you want.
As Utah_Dave said, I wrote a ruby script to add the ports...
# ruby
# execute with ruby startscript.rb
require 'json'
PROVIDER = "yourprovider"
SERVER = "yourserver"
ENDPOINTS = {
"SSH2" => 17532,
"HTTPS" => 443,
"HTTP" => 80,
"SlangerHTTP" => 8080,
"Slanger" => 4567,
"CouchDB" => 5984
}
def get_missing
service_existing = false
while !service_existing
begin
res = `salt-cloud --out=json -f list_input_endpoints #{PROVIDER} deployment=#{SERVER} service=#{SERVER}`
result = JSON.parse(res)
service_existing = true
rescue => e
puts e
end
end
existing_services = result[PROVIDER]["azure"].keys
missung_services = ENDPOINTS.keys - existing_services
end
missung_services = get_missing
while missung_services.any?
print "#{Time.now} [#{SERVER}] Services missing: #{missung_services.join(", ")}"
missung_services.each do |m|
print "."
`salt-cloud --out=json -f add_input_endpoint #{PROVIDER} name=#{m} port=#{ENDPOINTS[m]} protocol=tcp deployment=#{SERVER} service=#{SERVER} role=#{SERVER}`
end
print "\n"
missung_services = get_missing
end

NODE JS - Forever starting path problems

I have a server.js file defined as follows:
var iniparser = require('iniparser');
var inihost;
var inidbuser;
var inidbpass;
var inidbname;
var config = iniparser.parseSync('../setup_db/config.ini');
inihost = config.db_hostname;
inidbuser=config.db_username;
inidbpass=config.db_password;
inidbname=config.db_name;
.....
on reboot I have a crontab that should automatically forever starts the server:
#reboot /usr/bin/sudo /usr/local/bin/forever start /var/www/html/rubrica/chat/server.js
Interestingly if I launch the server from any directory with the complete path like:
forever start /var/www/html/rubrica/chat/server.js
the server starts just fine..if , however, i run the SAME command from withtin the /root/.forever/ directory the server will give me the following error:
ENOENT, no such file or directory '../setup_db/config.ini'
So whenever i reboot the machine I get that error...how is such a thing possible?
The argument to iniparser.parseSync() is not relative to the current file like require() but rather the current working directory. Use:
var path = require('path');
var config = iniparser.parseSync(path.join(__dirname, '../setup_db/config.ini'));

Couchbeam in YAWS page

I am here because I am trying to use Couchbeam form my page on a YAWS.
I have tested CB and it worked correctly from Terminal, using:
erl -pa ebin -pa deps/ibrowse/ebin -s couchbeam
Now I am trying to replicate what I did locally on my webpage.
I believe the issue is that I don't know how to tell erl to do 'erl -pa ebin -pa deps/ibrowse/ebin -s couchbeam' from a yaws page.
I have tried to simply running all the needed apps, but I am getting this:
Stack: [{ibrowse_lib,url_encode,["test"],[]},
{couchbeam,save_doc,3,[{file,"src/couchbeam.erl"},{line,383}]},
{m50,out,1,
[{file,"/Users/Nesh/.yaws/yaws/default/m50.erl"},{line,35}]},
{yaws_server,deliver_dyn_part,8,
[{file,"yaws_server.erl"},{line,2647}]},
{yaws_server,aloop,4,[{file,"yaws_server.erl"},{line,1152}]},
{yaws_server,acceptor0,2,[{file,"yaws_server.erl"},{line,1013}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,227}]}]
This is my erl code:
<erl>
startApp() ->
application:start(crypto),
application:start(private_key),
application:start(ssl),
application:start(sasl),
application:start(ibrowse),
application:start(couchbeam).
out(Arg) ->
startApp(),
Host = "localhost",
Port = 5984,
Prefix = "",
Options = [],
S = couchbeam:server_connection(Host, Port, Prefix, Options),
Options = [],{ok, Db} = couchbeam:open_db(S, "erlang", Options),
Doc = {[{<<"_id">>, <<"test">>},{<<"content">>, <<"web text">>}]},
{ok, Doc1} = couchbeam:save_doc(Db, Doc).
</erl>
I wouldn't recommend running Couchbeam from within a .yaws page like this. You should instead either create an Erlang release such that Couchbeam and Yaws are both executed within the same Erlang VM and then use a Yaws appmod to call into Couchbeam, or maybe you should consider making Couchbeam a bootstrap yapp for Yaws.
If you really think you're having load path problems, you can specify load paths in the yaws.conf file via the ebin_dir directive. For example:
ebin_dir = deps/ibrowse/bin
ebin_dir = couchbeam/ebin
But the stack trace you show seems like it's missing something, so it's hard to tell you exactly what's wrong.
I managed to fix it doing this:
I've added these lines in yaws.conf:
ebin_dir = /usr/local/var/yaws/couchbeam/deps/ibrowse/ebin
ebin_dir = /usr/local/var/yaws/couchbeam/deps/jiffy/ebin
ebin_dir = /usr/local/var/yaws/couchbeam/deps/mochiweb/ebin
ebin_dir = /usr/local/var/yaws/couchbeam/ebin
Note: I put the folder 'couchbeam' in /usr/local/var/yaws/
then I modified the code in this way:
load_deps() ->
application:start(sasl),
application:start(ibrowse),
application:start(jiffy),
application:start(inets),
application:start(xmerl),
application:start(compiler),
application:start(syntax_tools),
application:start(mochiweb),
application:start(couchbeam).
out(Arg) ->
load_deps(),
Host = "localhost",
Port = 5984,
Prefix = "",
Options = [],
S = couchbeam:server_connection(Host, Port, Prefix, Options),
Options = [],{ok, Db} = couchbeam:open_db(S, "erlang", Options),
Doc = {[{<<"content">>, <<"Checking webpage">>}]},
{ok, Doc1} = couchbeam:save_doc(Db, Doc),
{html, "Document has been added"}.
</erl>

Resources