Where to put django files on server? - python

I have a question. I work with one guy who is developing in Php, I am developing in Django right know. This is my first time doing something for other people and he firstly asked me to put my application to Apache Web server document root(I think he didn't know that much about Django, and he did everything this way), so I did it, because I didn't know that much about Apache and servers. But right know I am scared because I have all the python files in document root and I realized that this Web server document root \www\myproject might be accessible to other people. How should I change it, where should I put it, what is the best practice? I am deploying with mod_wsgi and Apache 2.4. In the documentation it says, that I shouldn't put code in Web server document root too, but if I put it somewhere else can I access it the same way, like I used to when it was on the Web server document root? Will I have to do something other than changing paths in Apache config?

You should put the Django project anywhere on the machine except for the apache document root, for security reasons as you have pointed out.
You can then configure apache to pint to the location of your wsgi.py which can live in the Django project if you wish:
WSGIScriptAlias / <path to the wsgi.py>
I'm not aware of a standard place to put the Django project root directory, I guess it depends on the OS and the distribution.

Related

Deploying Python on AWS Elastic Beanstalk

I'm coming from a php background and now I'd like to host a Python web application on AWS but have a few questions about it.
Do I need to use a framework like Django/Flask? Is there anyway I can use core Python to do the same? Back in php, although I tried learning Laravel in the middle, I was able to just use core php with apache and host up my website.
Tried deploying my Python code on my AWS but got an error that application.py was missing. I'd like to know, what exactly is application.py and what is it supposed to contain? Similarly, what's wsgi.py I see here and there. Are these actual files I need to create for the web app to be hosted? Is there some specific code that has to go into them or are the pre-created files by frameworks like Django, etc? Because I could hardly find too much information on them online.
I had recently tried following this tutorial from the AWS official site but to no luck.
The reason for being reluctant to use Django is the shortage of time to learn it. But if it were to make the task of hosting a Python web app easier, I would definitely look at it.
And how is the version of Python set? Because the Python codes I've written use the python3 libraries for BeautifulSoup and urllib.
I have read a lot of articles on the web but the first thing I get on searching for Python on the web or with AWS, is Django or Flask or something. How exactly does it work? When it came to php, it was simple copying the files into the /www/ folder of the server machine and I could access the website via it's url. Maybe I've read too many posts to put them all together so could someone please set it straight for me? It would be greatly appreciated!
Thanks a lot!
You will waste a lot more time trying to write a WSGI application yourself from scratch. Use a framework, it will save you a lot of time.
PHP is very different to WSGI and WSGI sits well below the level of functionality that PHP provides out of the box. PHP is more like what frameworks in Python provide. So go learn a framework. If Django seems too complicated, try Flask first.
Also don't try and do it on AWS from the outset, learn the frameworks by using the development servers they provide on your own box. Just work through their respective tutorials.
Actually AWS Elastic Beanstalk give you pure Python (2.6/2.7/3.4) with Apache + mod_wsgi as web proxy. You can look all supported Python environment in here. So, you can use any Python web framework (such as Django, Flask, etc.) in your web app. If you can, choose common and supported framework by AWS (Django/Flask).
You can think: Python + Apache + mod_wsgi is equivalent to PHP + Apache + mod_php.
Please take a look into AWS Elastic Beanstalk documentation for how to working with Python here. Read the Deploying a Django Application or Deploying a Flask Application if you choose one of them. You need to provide what Elastic Beanstalk environment (mod_wsgi) needs.
Same as PHP, Python actually only copy and paste the files. If you want to make Python web app without framework, you need to follow the WSGI standard. You can take look into this question. In my opinion, better if you use a framework, because it handles the WSGI part for you.
First of all this is a good blog post to start from if you are using Django
I don't know much about Flask, with Django once you understand the core concepts it's not hard at all.
application.py is the file that aws looks for as stated in the blog post I pointed to:
By default eb assumes our wsgi file is called application.py
this can be changed to your local wcgi.py file that Django makes when you start your project with django.
Beware that you want to use your static url correctly so aws will read them from the right folder. I personally disagree about the way the static files configuration in the post.
It's better to stay with the aws default which is "static" and just set static url in django settings to "/static/"

Django deployment final step

I have a Django project with 1 app that is working locally and I am trying to make it work on the server but I imagine I am still missing something...
The steps I have followed are:
1) create a virtualnev
2) Install django and the libraries I need
3) copy my local project to the server, keeping the same directory structure
4) create the file passenger_wsgi.py (python passenger_wsgi.py did not return any error)
After this do I need to do anything like python manage.py runserver? Or with this I should be already able to see the site through mydomain/my project/ my app (when I do ot just get an error 404)?
I have read the django book and followed the tutorial, but this part is not well described anywhere...
Thanks in advance for any help!
Deployment is explained in the documentation.
You need to actually serve your application with some kind of HTTP server and something to run your python code. Some of the possible combinations are:
nginx with uWSGI
Any web server as reverse proxy with gunicorn
Apache with mod_wsgi
Your hosting service may or may not give you the choice or even the possibilty to do this.
There is a list of Django friendly hosters in the Django wiki.
Well firstly you might want to go through the previously asked questions.
When you are deploying using passenger you do not need to run manage.py runserver , etc. The passenger_wsgi file should take care of it.
You might want to check this link out, in the first answer also contains link to Dreamhost which details quite extensively on how to achieve the same.
Visit Deploying Django app using passenger
From my personal experience I found Nginx and uwsgi setup to be much more easier to handle and work and debug in the logs, but this is subjective to your needs and platform you may have.

Python Shared Hosting

I would love to be able to use Python and Django for web applications at the company I work for. We currently use PHP because everyone is familiar with it and easy to deploy for a large number of clients. We can host anywhere between 10 to 100 websites on a single virtual server.
Is it possible to serve a number of websites from a single Apache and Python installation? Each website must have their own domain among other things, such as email accounts.
I wouldn't use Apache, the current best practice is an Nginx frontend proxying requests to uWSGI servers. Read about the uWSGI Emperor mode. It's very versatile. http://uwsgi-docs.readthedocs.org/en/latest/Emperor.html. Each individual app can be modified, removed added to dynamically. We use it at PythonAnywhere to serve thousands of web applications
There are other WSGI servers that you can use as well. uWSGI just seems the most scalable in my experience.
Yes, It is definitely possible. In our setup, typically we have django behind mod_wsgi, Apache and nginx
You can configure apache's Virtualhost, to point to a specific mod_wsgi which in turn points to specific code.
Quoting from here - Refer to the SO post for further information.
There are at least two methods you can try to serve from a single
instance:
Use apache + mod_wsgi and use the WSGIApplicationGroup and/or
WSGIProcessGroup directives. I've never needed these before so can't
be completely sure these will work the way you want, but regardless
you can definitely use mod_wsgi in daemon mode to greatly improve
your memory footprint.
You can play with Django middleware to deny/allow URLs based on the
request hostname (see HttpRequest.get_host() in the Django docs).
For that matter, even though it would be a slight performance hit,
you can put a decorator on all your views that checks the incoming
host.
Yes, you can easily serve up many sites using a single Apache / mod_wsgi installation. Typically you would do that with a separate virtualhost section for each website. See the virtualhost docs. You want to use a different servername directive in each virtual host config to specify what hostnames get routed to which config. See more detailed documentation in name based virtual hosts

Setting up Django app on a shared hosting server

Okay so i have shared hosting so it means i have access to just about nothing so i'm having problems setting up my django application.
Looking at google searches there is a way to do it using a .htaccess and .wgsi file but I'm having no luck. My knowledge with anything like this is low as i normally use php.
So far i have added some code into the htaccess file and it always comes up with this error..
Not Found
The requested URL /dispatch.wsgi/ was not found on this server.
You should take a look at this :
In this section of django documentation they explain the how to :)
Running Django on a Shared-Hosting Provider with Apache
Many shared-hosting providers don’t allow you to run your own server daemons or edit the httpd.conf file. In these cases, it’s still possible to run Django using Web server-spawned processes.
have a nice day :)
Do you have the dispatch.wsgi on the specific path? it should be somewhere like the public folder that serves your static files, like /public_html or /www.
I have used shared hosting for django from a small orange. I got mine working by following this tutorial of theirs, maybe working for you too.

What is a pythonic webserver equivalent to IIS and ASP?

For very simple, internal web-apps using ASP I was able to just switch IIS 'on' and then write some ASP scripts in the www directory that would start working immediately.
Is there an equivalent webserver app for Python scripts that I can run that will automatically start serving dynamic pages (python scripts) in a certain folder (with virtually no configuration)?
Solutions I've already found are either too limited (e.g. SimpleHTTPRequestHandler doesn't serve dynamic content) or require configuring the script that does the serving.
There's always CGI. Add a script mapping of .py to "C:\Python27\python.exe" -u "%s" then drop .py files in a folder and IIS will execute them.
I'd not generally recommend it for real work—in the longer term you would definitely want to write apps to WSGI, and then deploy them through any number of interfaces including CGI—but it can be handy for quick prototyping.
For development or just to play around, here's an example using the standard Python library that I have used to help friend who wanted to get a basic CGI server up and running. It will serve python scripts from cgi-bin and files from the root folder. I'm not near a Windows computer at the moment to make sure that this still works. This also assumes Python2.x. Python 3.x has this, it's just not named the same.
Make a directory on your harddrive with a cgi-bin folder in it (Ex. "C:\server\cgi-bin")
In a command window, navigate to "C:\server" directory
Type the following assuming you've installed python 2.7 in C:\Python27:
"c:\python27\python.exe -m CGIHTTPServer"
You should get a message like "Serving HTTP on 0.0.0.0 port 8000"
Linux is the same - "python -m CGIHTTPServer" in a directory with a cgi-bin/ in it.
WSGI setups are fairly easy to get started, but in no anyway turn key. django MVC has a simple built in development server if you plan on using a more comprehensive framework.
My limited experience with Python web frameworks has taught me that most go to one extreme or the other: Django on one end is a full-stack MVC framework, that will do pretty much everything for you. On the other end, there are Flask, web.py, CherryPy, etc., which do much less, but stay out of your way.
CherryPy, for example, not only comes with no ORM, and doesn't require MVC, but it doesn't even have a templating engine. So unless you use it with something like Cheetah, you can't write what would look like .asp at all.

Categories

Resources