Porting Pyrhon CMS from Apache to Nginx and uwsgi - python

I've written a simple CMS in Python. It currently runs on Apache, and it consists of a bunch of python scripts in /usr/lib/cgi-bin. I'm trying to get the CMS working in Nginx. I've never used Nginx before, so I'd appreciate some input from anyone who's familiar with it.
I'd like to run the CMS without having to modify it - ideally I'd like to be able use the CMS with Apache and Nginx, and I don't want to maintain two separate versions.
The main script is /usr/lib/cgi-bin/pyindex.py. I've set up two locations in /etc/nginx/sites-available/default:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/dir.html /cgi-bin/pyindex.py?q=$uri;
}
location /cgi-bin/ {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8080;
}
I installed uwsgi and uwsgi-plugin-python and set up /etc/uwsgi/apps-available/mydomainname.com.xml as follows:
<uwsgi>
<plugin>python</plugin>
<plugin>cgi</plugin>
<socket>127.0.0.1:8080</socket>
<chdir>/usr/lib/cgi-bin/</chdir>
<pythonpath>/usr/lib/cgi-bin/</pythonpath>
<module>pyindex.py</module>
<cgi>/cgi-bin=/usr/lib/cgi-bin/</cgi>
<cgi-allowed-ext>.py</cgi-allowed-ext>
<master/>
<processes>4</processes>
<harakiri>60</harakiri>
<reload-mercy>8</reload-mercy>
<cpu-affinity>1</cpu-affinity>
<stats>/tmp/stats.socket</stats>
<max-requests>2000</max-requests>
<limit-as>512</limit-as>
<reload-on-as>256</reload-on-as>
<reload-on-rss>192</reload-on-rss>
<no-orphans/>
<vacuum/>
</uwsgi>
When I point my browser at any dynamic page, I see this error message:
uWSGI Error
Python application not found
So it looks like nginx is successfully passing requests to uwsgi, but uwsgi isn't set up correctly.
The log in /var/log/uwsgi/app/mydomainname.com.log shows that pyindex.py runs when uwsgi starts up, and falls over because the QUERY_STRING environment variable isn't available. The script doesn't get executed when a request is passed from nginx to uwsgi.
Is there a way to set this up so that pyindex.py isn't executed on start up, but executes when uwsgi receives a request from nginx?
I'm using Nginx v 1.2.1, uwsgi 1.2.3, Python 2.7 on Raspbian.
Thanks in advance

Forget about the uWSGI python plugin, you do not need it.
You need the CGI plugin:
http://uwsgi-docs.readthedocs.org/en/latest/CGI.html

Related

Running a Python script that creates a webpage on a server

I have a Python script that creates a web page displaying some text at localhost port 8080. I have access to a server, and would the script to run there so that it is always running and available anywhere.
I am using the bottle, requests and json libraries.
This is the last line of code in the script that is executed to locally create a web page:
run(host='localhost', port=8080, debug=True)
How would I change this to run on a server? I also have WinSCP and PuTTy to add the script to the public_html directory of the server and I can change permissions. Sorry, I am a novice in this subject.
Thanks for any help!
That depends on the Apache httpd configuration. But, assuming most defaults were left in place and Apache httpd serves index.html, you can just create your webpage (which you tested locally on port 8080) and overwrite that file. But, this only works well if you have a static page and no logic in your Python code. If you want to combine the power of Apache httpd and Python (have the former call the latter) you'll have to use mod_wsgi.
Seeing as you're novice in this subject, I suggest starting off with a simple Apache httpd server according to their quickstart. Then, once you have a few successful requsets under your belt, add mod_wsgi.

Configure Nginx for socket connect

I'm working on a Flask app where I need to have a socket connection to all files in a specific directory. Right now I'm using a Python3 http.server (https://docs.python.org/3/library/http.server.html) which does what I need, but it's not recommended for production. Is there a way to configure Nginx to function in a similar way?
When configuring your server block include this.
`location /{
include uwsgi_params;
uwsgi_pass unix:///path/to/your/socket.sock;
}
It will automatically create a socket file when you run the uwsgi server.
The functioning of uwsgi is beyond the scope of this question if you want to check it out look at:
detailed uwsgi guide

Python-DjangoREST-uWSGi-nginx

This is my first time posting and its a pretty long question.
I'm a beginner and I've decided to learn and use Python and DjangoREST to build my first web application.
Unfortunately, I have a lot of issues that I'm not sure how to get answers to. Querying Google one at a time for each component is giving me answers that sometimes conflicts with another component.
First things first, the following is my project setup:-
OS->Ubuntu 16.04.1
Backend->Python3 and DjangoREST
Webserver->uWSGI and nginx
Database->PostgreSQL
Client Side software->AngularJS(which requires the setup of Node.js and NPM)
Now I installed pip and went through the process of creating a virtualenv and ran a test Django app. I was able to get that done.
When I went through the documentation of uWSGI and Nginx over here http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html, I came across the following line
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
This has me slightly confused. Is there a specific order in which I need to install the software I need? And if yes, could you all tell me the correct order?
Thanks in advance!
This is not actually the order you should install software. This is the communication flow in which you(your browser) connect to the django application on your server.
To make work your application in your server you should do the followings:
First install nginx (reverse proxy server and a web server as well)
Install Gunicorn (wsgi server implementation)
Clone your django project in any directory from your remote git (or simply upload it from local pc)
now run the below command inside the project directory
gunicorn YOUR_PROJECT_NAME.wsgi:application \
--name GIVE_A_NAME_AS_YOUR_WISH \
--worker-connections=1000 \
--threads=3 \
--workers NUM_WORKERS \
--user=USER --group=GROUP \
--bind=SERVER_HOST:ANY_PORT_AS_YOUR_CHOICE (TYPICALLY IT IS 8000)
Point number 4 is enough to run your project in your server. check it in the below link
http://YOUR_HOST_ADDRESS:8000/
Now you can redirect all of your traffic to this application by nginx web server configuration. To do so make a file inside /etc/nginx/conf.d/ naming my_app.conf. inside this file copy paste the below code:
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:8000;
}
}
Hopefully it is okay !

Deploy Bottle Application on Nginx

I have an Ubuntu 12.04 server setup that currently runs a Ruby on Rails application on a Passenger / Nginx install. I decided to play around with some Python and wrote a small application using Bottle. I would like to deploy this application to my server.
I followed this guide on setting up my server to run Python applications. When I run sudo service uwsgi restart I get the following error message:
Restarting app server(s) uwsgi
[uWSGI] getting INI configuration from
/usr/share/uwsgi/conf/default.ini [uWSGI] parsing config file /etc/uwsgi/apps-enabled/example.net.xml
open("./python_plugin.so"): No such file or directory [core/utils.cline 4700]
!!! UNABLE to load uWSGI plugin: ./python_plugin.so: cannot open shared object file: No such file or directory !!!
Sat Dec 8 18:29:14 2012 - [WARNING] option "app" is deprecated: use the more advanced "mount" option
I really don't know a ton about Python, I have installed the plugins I need via easy_install
Which are:
pymongo
beautifulsoup
bottle
My question is: how do I deploy this simple application to my server?
Thank you
I found out that Passenger will run WSGI apps. I followed the instructions on this post http://kbeezie.com/using-python-nginx-passenger/ and had no trouble getting it working.
It was actually pretty easy in the end.
Here is my adaptor in case anybody else has trouble:
https://github.com/nick-desteffen/astronomy-pics/blob/master/passenger_wsgi.py

Python 2.6.2, Django 1.0.3, Windows XP, Page not found: /

I'm just starting to learn Python and Django and an unable to get the most basic app working. I've setup Python, added python to the Path environment variable, installed Django using install.py script.
I created an app by running the command
django-admin.py startproject my_project
updated the settings.py file for a database
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'mysitedb'
Ran the command
python manage.py syncdb
And finally, started everything up
python manage.py runserver
To this point, everything looks to have run successfully. When I got to view http://localhost:8000/ I get the error "Page not found: /"
I originally installed Django version 1.1, but got the same error, so I removed it and tried the older version 1.0.3. Niether work. Any help would be appreciated.
To complete this question - #artran's answer of changing the port
python manage.py runserver 8001
will work.
When python runs the server, it automatically uses port 8000 (hence http://127.0.0.1:8000/). It uses this port as to not tread on the toes of other applications using localhost ports. However, you may still have an application or service running through this port. As such using port 8001 or any other port you may consider free should work.
To repair this in the future, you need to run a program of which can finger all your ports and determine what application is using the :8000 port.
It sounds like you need to create some apps for your project and set up the urls. As you are just starting you'd be best following the tutorial right through to get a feel for it all.
You probably have ADMIN_MEDIA_PREFIX = "" in your settings. Django intercepts requests to this url and attempts to serve admin media, thus when you make it an empty string, it will attempt to intercept ALL requests, resulting in nothing working.

Categories

Resources