Hi I am a newbie to Django AND Python. Working currently on a dummy project using Django 1.9. So following the excellent Django Documentation I could complete the deployment using apache/mod_wsgi and it is running on a test web server on my local machine.
But I am wondering about the Python source files :
I have given the path to my development directory - say /Python-labs/mysite/ to mod_wsgi and apache configuration ( httpd.conf ) files. This development directory obviously contains all the .py files - the Python source files.
No-where in the documentation i found any mention to remove the source files etc. So I googled but could not get anything as a standard step to remove source files from the production deployment.
Got this - the closest to what I am asking for :
Django Remove Source Files
Django Deployment Without Source Code
but again it does not make it crystal clear about the questions below :
My Questions :
Is it a standard step in Django deployment to remove source files or not ?
( or Did I miss something very obvious from the documentation ? )
Is it Ok if we put .py files on production server ? Or is it intended that way ?
Thanks a lot in Advance !
It is typical to deploy source files with Django. As long as the project is set properly (__init__.py included for all project/app directories) then Django will compile .py files to .pyc.
To answer your questions -
1. Don't serve your production files directly; use a good VCS to push files to your production server or, if quick and dirty, copy your project to your production server. But uncompiled .py files are standard for most Django projects.
2. Yes.
For more insight, this question may help.
Related
EDIT: I found the issue from .pyc files. For some reason some compiler has created .pyc files which contained old code. When I uploaded files to the server, server won't compile .py files. Instead it will run .pyc files uploaded from my computer. I deleted all .pyc files, deployed, and now the server runs fresh code /EDIT
When I run gcloud app deploy I quite often deploy a wrong version of my app. My app is running on GAE standard environment is written using Python 2.7.
I can easily reproduce the problem by having one of my urls return a hard coded string, for example "test1". Now when I change this between deployments, I quite often receive a previously written string from the endpoint.
When running the app on the local server the changed return string is correct, but after deploy the string can be from an earlier version.
I have to deploy my app to both test and production environments and I am worried about deploying wrong code. When deploying the gcloud console correctly shows that only 2 files are being uploaded (if I have only edited the static return string).
I have tried killing all other versions from the App Engine console.
Also tried using flag --stop-previous-version.
I have also tried adding new endpoints and after gcloud says the deployment was successful these endpoints are still inaccessible.
How can I make sure my current code gets deployed correctly?
1) Make sure you change the version number in app.yaml
2) Go to
https://console.cloud.google.com/appengine/versions?project={your project id}
to tell GCP which version to serve, and which to stop.
Your note regarding the pyc files suggests that you may have customized your app.yaml's skip_files section and accidentally wiped its default values in the process, which would normally prevent deploying pyc (and other potentially interfering files) to GAE. From that doc (emphasis mine):
The skip_files has the following default:
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
The default pattern excludes Emacs backup files with names of the form
#...# and ...~, .pyc and .pyo files, files in an RCS revision control directory, and Unix hidden files with names beginning with a
dot (.).
To extend the above regular expression list, copy and paste the above
list into your app.yaml and add your own regular expressions.
So to no longer worry about manual cleaning the .pyc files make sure you still have the above patterns in your skip_files section, in particular - ^(.*/)?.*\.py[co]$ which is the one responsible for the .pyc files.
I've created a simple project with vuejs as the frontend and flask as backend.
The project was constructed by following the example here, thus the resultant file structure is like that the vuejs build files "dist" is on the same folder as that of the driving python script, run.py.
The project was tested and working fine locally now I ran into problems trying to deploy it on my Ubuntu server hosted by digitalocean.
I followed this article to learn how to deploy - the article was well written but I believed I need to change the apache config file (/etc/apache2/sites-available/000-default.conf) a little to specify the static files of my project as shown in the screenshot.
The question is how? I don't know. When I followed the article word by word and launched the web app, it showed errors like:
Loading failed for the <script> with source “http://my_website.com/static/js/manifest.0e78d562f6b86d93f516.js”. vue-amazon:1:1
static is a standard vuejs folder under "dist" in my file structure.
I found the issue has nothing to do with the way how Flask project is deployed - it's about the configuration of the vuejs frontend.
I need to adjust the setting of assetsPublicPath in vuejs's config file, to a proper location where the index.html can find the obfuscated javascripts.
For example, if my project is called "ABC", and i want the url looks like:
http://my-site.com/ABC
I need to have this in the vuejs config file:
env: require('./prod.env'),
index: path.resolve(__dirname, '../../dist/index.html'),
assetsRoot: path.resolve(__dirname, '../../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '../../ABC/dist/',
before I run
npm run build
I am following a step-by-step tutorial blog on the flask micro framework for python.
I bumped into an issue, when they require me to 'setup' a configuration file in the root of the application folder, so it can easily be accessible if needed.
They called it config.py.
My question is, if the local path to my application is /home/test/application1/, should I create the file inside the ./application1/ directory? What gets me confused in this somewhat obvious question is that I did a quick search for other config.py files in the local directory inside /home/test/application1/, where I found 4 other files. There were in the following directories:
/home/test/application1/flask/lib/python2.7/site-packages/flask/testsuite/config.py
/home/test/application1/flask/lib/python2.7/site-packages/flask/config.py
/home/test/application1/flask/local/lib/python2.7/site-packages/flask/testsuite/config.py
/home/test/application1/flask/local/lib/python2.7/site-packages/flask/config.py
So should I create a new config.py file in the directory that I first mentioned or should I add some lines in one of the previously created config.py files.
Here is the source of the step-by-step tutorial:
It is at the beginning, right after Configuration.
Unlike other frameworks, Flask does not have a lot of rules, in general you can implement things in the way they make sense to you.
But note that the other config.py files that you found are all in the virtual environment, and are all scripts that come with Flask. They have nothing to do with the application configuration.
I wrote the tutorial you are following. In the tutorial I'm putting config.py outside of the application package. This is what I like, I consider the configuration separate from the application. My thinking is that you should be able to run the same application with different configuration files, so that for example, you can have a production configuration, a testing configuration and a development configuration, all different.
I hope this helps.
I have a django project which I want to deploy on apache2 Http Server.
However, I want to automatically copy all the python files from some directory to apache srv directory which is /srv/www/myproject. Is there any automatic python tool which could solve the purpose.
I have looked into DistUtils and setup.py but I am unsure about how I would copy all .py files (along with directory structure) to the apache directory.
Any help will be appreciated!!!
Take a look to fabric, it is a great tool to do automatic deployments
From the django tutorial:
If your background is in PHP, you're probably used to putting code
under the Web server's document root (in a place such as /var/www).
With Django, you don't do that. It's not a good idea to put any of
this Python code within your Web server's document root, because it
risks the possibility that people may be able to view your code over
the Web. That's not good for security.
Put your code in some directory outside of the document root, such as
/home/mycode.
For copying files, you can use something as simple as FTP/SCP which you can automate; or you can use more full blown deployment options like fabric (see this blog entry for a step-by-step guide on fabric + virtualenv + apache mod_wsgi).
You can use any tool to automate the task; but please put the code in the appropriate non-web browsable directory.
I am new to web development. So be gentle. AND thanks in advance.
I am developing on windows env. and deploying on a linux server w/ Python 2.6.2 installed.
Running apache2.2 as Virtual Host, and I am using mod_wsgi. I plan to serve media files from the same Virtual Host.
I have a django site and I am now ready to deploy. I am stuck, and every site I go to seems to be outdated/incomplete/overmyhead.
https://docs.djangoproject.com/en/1.3/howto/deployment/modwsgi/
http://www.djangobook.com/en/2.0/chapter12/
...only 2 links as for the newbness
The top link seems to be what I need yet I am still confused on these things:
What does the file structure look like on the server
I cannot change/edit server files myself, I rely on the dba for that
I have django.wsgi, and django.wsgi~, where do those go?
Where do I put my project in relation to those wsgi files?
The httpd.conf file is something that the server has on it? or do I create another?
Do I need to put django in any way shape or form on the server? If so where? And what about the packages like registration, defaults?
Again sorry for the newbness, I have been banging my head for 2 weeks on this.
Any help/links will be greatly appreciated unless they link me to the django-docs. I have read those...A LOT! thanks
Also go read:
http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango
and watch:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
The latter includes Django examples and talks a bit about locations, permissions etc.
The first of these is even linked to in the document in the Django documentation.
It doesn't matter where the files go. They just have to be readable/executable by the user that the web server is running under.
I don't know what you mean by django.wsgi~, that sounds like a backup file created by your editor - you don't need that.
Yes the httpd.conf is the configuration file for Apache. Some distributions (eg Debian and Ubuntu) split this up into separate files for each site that the server runs. If your administrator is the only one who can edit files, he will know about this already.
Yes, you need Django, and any third-party packages.
#Nathan
An easier option for you while you are learning this is not to really have to many expenses.
I could also suggest you take a look at Heroku - allows you to easily deploy your applications in minutes.
Up until recently they only supported RoR and they have brought in support for Django and Python - they have some really well documented tutorials as well.
I hope this helps
Heroku Django / Python tutorial
I have written up an simple deployment guide for django applications it can be found here. It goes all the way from project setup and deployment. I also have references setup. I honestly believe it answers all your questions, I would give it a look.
Goodluck.