-
Notifications
You must be signed in to change notification settings - Fork 103
WSGI Support in wouso
For scalable deployment (web server front-end, multiple-threads etc.), uWSGI is considered among the best solutions in conjunction with Django and Python.
You'll find a quite extensive tutorial on Django, Python and uWSGI with nginx on this page.
Useful documentation is also found on the Django website.
For uWSGI support you need to install the uwsgi
package using pip:
pip install uwsgi
There is already a uWSGI setup file in the repository in the wouso/wsgi.py
file. If you have everything setup, as shown in the README file you may test the current uWSGI setup by running the following command inside the wouso/
folder:
uwsgi --http :8000 --wsgi-file wsgi.py
You should get an output such as this:
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 500
your memory page size is 4096 bytes
detected max file descriptor number: 65536
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8000 fd 4
spawned uWSGI http 1 (pid: 30208)
uwsgi socket 0 bound to TCP address 127.0.0.1:46233 (port auto-assigned) fd 3
Python version: 2.7.8 (default, Oct 18 2014, 12:52:27) [GCC 4.9.1]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x155c3c0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72768 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x155c3c0 pid: 30207 (default app)
*** uWSGI is running in multiple interpreter mode **
This opens a server on port 8000
. By pointing your brower to http://localhost:8000
you may now access your WoUSO instance using uWSGI.
Note: If you are using SQLite in your setup you need to run the uwsgi
command above in the folder where the databasefile (most likely named data.db
) is located. It's natural for the SQLite database file to be located inside the wouso/
folder, so there should be no problem.
Now that uWSGI works properly it's time to integrate to a real web server. Web server requests are sent to uWSGI. A set of uWSGI processes will listen on a socket (either a Berkeley or a UNIX socket) requests from the web server, process those and come back with a reply.
Instructions on making this happen are adapted from this article.
You should first configure uWSGI to start with its process manager. To do that, go to the scripts/uwsgi/
subfolder in the repository and update the startup-script
and wouso_uwsgi.ini
files. You need to fill several paths, the one marked with TODO
.
Please check the sample
sample-startup-scriptand
sample_wouso_uwsgi.inifiles for filled examples of those files. Be advised that the
sample_wouso_uwsgi.inifile has been tested against an SQLite setup. As such the
chdirvariable points to the
wouso/' subfolder (where the database file data.db
is located) and the module
variable is now simply wsgi
, not wouso.wsgi
since we already are part of the wouso
folder.
After filling those two files, you may now run the startup-script
file and get an output similar to the one below:
(sandbox)razvan@einherjar:~/projects/rosedu/wouso/wouso-razvand-fork.git/scripts/uwsgi$ ./sample-startup-script
[uWSGI] getting INI configuration from /home/razvan/projects/rosedu/wouso/wouso-razvand-fork.git//scripts/uwsgi/sample_wouso_uwsgi.ini
*** Starting uWSGI 2.0.9 (64bit) on [Sat Jan 31 22:03:03 2015] ***
compiled with version: 4.9.1 on 31 January 2015 21:09:00
[...]
current working directory: /home/razvan/projects/rosedu/wouso/wouso-razvand-fork.git
detected binary path: /home/razvan/projects/rosedu/wouso/wouso-razvand-fork.git/sandbox/bin/uwsgi
chdir() to /home/razvan/projects/rosedu/wouso/wouso-razvand-fork.git
[...]
mapped 800448 bytes (781 KB) for 10 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1664fd0 pid: 8804 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 8804)
spawned uWSGI worker 1 (pid: 8805, cores: 1)
spawned uWSGI worker 2 (pid: 8806, cores: 1)
spawned uWSGI worker 3 (pid: 8807, cores: 1)
spawned uWSGI worker 4 (pid: 8808, cores: 1)
spawned uWSGI worker 5 (pid: 8809, cores: 1)
spawned uWSGI worker 6 (pid: 8810, cores: 1)
spawned uWSGI worker 7 (pid: 8811, cores: 1)
spawned uWSGI worker 8 (pid: 8812, cores: 1)
spawned uWSGI worker 9 (pid: 8813, cores: 1)
spawned uWSGI worker 10 (pid: 8814, cores: 1)
You'll also know this works by the presence of a socket file (wouso.sock
) in your wouso (top-level) folder:
razvan@einherjar:~/projects/rosedu/wouso/wouso-razvand-fork.git/scripts/uwsgi$ ls -l ../../wouso.sock
srw-rw-rw- 1 razvan razvan 0 Jan 31 22:21 ../../wouso.sock
We require a web server that receives user requests and then passes them on to the uWSGI server processes. We will use Nginx.
First step is to install nginx
. On a Debian-based system you would issue the command
sudo apt-get -y install nginx
The configuration for nginx
happens in the /etc/nginx
folder.
You'll then neeed to create an nginx
configuration. At its simplest, you can update the /etc/nginx/sites-available/default
file with the contents of the file scripts/uwsgi/nginx/nginx-config
in the repository. You would need to update the values inside that file accordingly; you may look at the scripts/uwsgi/nginx/sample-nginx-config
file for an example.
After that, with the uWSGI server processes, you would restart nginx
:
sudo service nginx restart
and then point your browser the to the URL http://localhost
.
Be advised that it's not secure to point your web server to a local configuration file such as uwsgi_params
. It would be best to place that in a folder where an unprivileged user can update it.
In a server environment you may want to run the uWSGI server processes at startup time (boot time). As the web server runs at startup, you would also need the uWSGI server processes to run then.
The simplest way to do that is to add a line to the /etc/rc.local
file, similar to the one below:
su - razvan -c 'nohup /home/razvan/projects/wouso-razvand-fork.git/scripts/uwsgi/startup-script > /home/razvan/projects/wouso-razvand-fork.git/uwsgi.log 2>&1 &'
In the above line, we're switching to the razvan
user and then running the startup script as a daemon process (using nohup
). We're also redirecting any output from the server processes to a log file.
With that line in place in the /etc/rc.local
file, the uWSGI server processes wil run at startup time.