Run Django project using wsgi - django

Hi i am trying to host the django site using WSGI app.My project resides in /opt/labs and the contents of this are django/ labs_site/ content/ home/
labs_site was the initial project that was derived from startproject and from labs_site/urls.py i link to content,home and other modules
i have followed the documentation in https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/ till renaming labs_site/wsgi.py as os.environ.setdefault("DJANGO_SETTINGS_MODULE", "labs_site.settings")
So after this what is the next step that i should follow.I want to run the project without the runserver

modify your apache settings now. If setting up wsgi file was your first step, then next step should be modifying your apachee config to point towards that file.
The link you posted points towards this page: https://docs.djangoproject.com/en/1.4/howto/deployment/wsgi/modwsgi/
that has all the necessary information. You can just choose between putting all that into http.conf file or creating site file for it under sites-available & sites-enabled folders in /etc/apache.
After that - restart your apache server by /etc/init.d/apache2 or service apache2 restart - whichever works.
Then since its your first time, youll see some errors in your /var/log/apache2/error.log. Fix them... and try again :)
Good luck.

Related

Django collectstatic keeps waiting when run through Github Action

We are facing a very weird issue. We ship a django application in a docker container through Github Actions on each push. Everything is working fine except collectstatic.
We have the following lines at the end of our CD github action:
docker exec container_name python manage.py migrate --noinput
docker exec container_name python manage.py collectstatic --noinput
migrate works perfectly fine, but collectstatic just keeps on waiting if ran through the github action. If I run the command directly on the server then it works just fine and completes with in few minutes.
Can someone please help me figuring out what could be the issue?
Thanks in advance.
Now I am far from the most experienced but I did this recently and I have some suggestions of where to look. I'm definitely not the greatest authority though.
I wasn't using docker so I can't say anything about that. From the issues, I had here are some suggestions I can recommend to try.
Take note that all of this was for a self-hosted runner. Things would be very different otherwise.
Check to make sure STATIC_ROOT and MEDIA_ROOT variables are set correctly in the settings file.
If the STATIC and MEDIA root variables are environment variables make sure you are serving the correct environment variables file like a .env file which I used.
I used django-environ to serve my environment variables. From the docs, it says to have the .env file in the same directory as the settings file. Well if you are putting the project on a production server with github actions, you won't be able to put the .env file anywhere in the project because it will get overwritten every time new code is pushed.
So to fix that you need to specify the correct .env file from somewhere else on the server. Do that by specifying ENV_PATH.
https://django-environ.readthedocs.io/en/latest/
Under the section Multiple env files
Another resource that was helpful:
https://github.com/joke2k/django-environ/issues/143
I set up my settings file like how they did there.
I put my .env file in a proj directory I made in the virtualenvironment folder for the project.
I don't know if it's a good place to put it but that's how I did it. I didn't find much great info online for this stuff. Had to figure out a lot on my own.
Make sure the user which is running the github action has permissions to read the .env file.
Also like .env file, if you have the static files being collected into the base directory of your project you might have an issue with github actions overwriting those files every time new code is pushed. If you have a media directory where the user uploads files to then that will really be an issue because those files won't get overwritten. They'll just disappear.
Now if this was an issue it shouldn't cause github actions to just get stuck on the collect static command. It would just cause files to get overwritten every time the workflow runs and the media files will disappear.
If you do change the directory of where the static and media files are located as stated before, make sure all the variables for the paths are correct in the settings file and the .env file.
You will also need to update the nginx config file for the static and media root directories if you used nginx. Not sure about how apache does this.
You can do that with this command:
sudo nano /etc/nginx/sites-available/myproject
Don't forget to restart the nginx server after doing that.
If you are writing static and media files at a different location from the base project directory on the server, also check permissions on those directories. Make sure the user running the github action has permissions to write to those directories. I suspect that might cause it to hang but it very well might just cause an error.
Check all the syntax in the github actions yml file. Make sure everything is correct and it's not hanging cause it had an incomplete command or something like that.
But yeah, that's some things I had to take a look at. Honestly, none of this might be relevant for you. All of these issues should cause an error somewhere for the most part.
I couldn't really offer many external resources for you to look deeper into this because I'm just speaking from personal experience.
Hope I could help.
Heres my github repo for the project I did: https://github.com/pkudlanov/personal-portfolio-django
I hosted it on digitalocean on a linux server using nginx and gunicorn.

How can I get PyCharm to use the Vagrant Directories rather than my development machines?

When I try to run my Django app via PyCharm inside of Vagrant, it sends my Windows Path over SSH which then provides an error in the shell:
> ssh://vagrant#127.0.0.1:2200/usr/bin/python -u C:/Projects/dev_project/dev/manage.py runserver 0.0.0.0:8000
bash: line 0: cd:
C:/Projects/dev_project/dev: No such
file or directory /usr/bin/python: can't open file
'C:/Projects/dev_project/dev/manage.py':
[Errno 2] No such file or directory
I created a Python Project in PyCharm (and instantiated a Django Project in a sub-folder) in order to incorporate Vagrant.
dev_project (PyCharm project root)
|--.vagrant
|--dev (Django project root)
|--dev
|--app
|--manage.py
|--Vagrantfile
In Settings
I enabled Django Support (providing the Windows Paths as there is no other option) in Settings > Languages & Frameworks > Django.
The Vagrant Python Interpreter is selected as the Project Interpreter (and Django Console & Python Console)
In Run Configurations
My host is 0.0.0.0 and port is 8000
My Python Interpreter is the Vagrant Environment. I am also adding the Content Roots & Source Roots to the Python Path.
The bizarre problem is it was working fine, and then I exited out and it broke again. Also, I do not have 'Working Directory' explicitly defined anywhere.
This blog article showed me my issue, as I did not have my Path Mappings setup within my Run Configuration.
From the Article (Configure Your Project to Use the Correct Interpreter):
Select Run->Edit Configurations…
Select the configuration on the right PyCharm created for us. In my case it was called “session_tracker”.
Change “Python interpreter” to our newly created one.
A new field will appear. Click the button next to “Path mappings”. A new window will appear to let you create your mappings.
Vagrant shares a local directory with the VM. My Vagrant file is configured so a folder is called “django_shared” in our [VagrantFolder] locally, and “django_shared” in the home directory on the VM are the same. You need to enter the full paths of each of those in the “Edit Path Mappings” window. - Click the + button to create a new mapping, then enter the values for the mapping on each side.
When no other option works I do the following:
Create seperate virtualenv in my local directory
Reconfigure PyCharm to use the interpreter in that venv
Restart PyCharm
Maybe not a very good solution, but works.

What is the entry point that Apache mod_wsgi on OpenShift Online is looking for?

I've got an OpenShift Python 2.7 application that I am guessing uses mod_wsgi.
Is it possible to ssh in to OpenShift Online and view the .conf files located somewhere like:
/etc/apache2/sites-available/
I am wanting to see which .wsgi file Apache looks for as defined in WSGIScriptAlias.
Perhaps it just looks for /wsgi/application?
A few posts indicate that changes have been made recently to the structure of Python applications, but they may not effect my older version:
How to change or override openshift.conf in Python 3.3 cartridge
https://blog.openshift.com/openshift-online-march-2014-release-blog/
WSGI Application not found on OpenShift
Ideally, I'm trying to comprehend the order in which these files are executed and their functions:
/wsgi/application
/wsgi/my-bottle-application
setup.py
setup.pyc
setup.pyo
UPDATE
This indicates the entry point is wsgi/application:
https://github.com/openshift/origin-server/search?utf8=%E2%9C%93&q=OPENSHIFT_PYTHON_WSGI_APPLICATION
I'd still be interested to know the order of execution of the above files and exactly what setup.py does and how it is executed - ie there are no references to it in application so how is it 'called'?.
According to this you can set your entry point: https://blog.openshift.com/openshift-online-march-2014-release-blog/
Python
For python app’s we’ve made some similar changes:
We got rid of wsgi/, wsgi/static/, data/ and libs/ directories.
You can use wsgi.py instead of wsgi/application as the default WSGI entry-point.
We’ve discarded the README.md file that can often conflict with an upstream file of the same name.
New OPENSHIFT_PYTHON_WSGI_APPLICATION to set an alternative WSGI entry-point.
wsgi.py WSGI entry-point (configurable by $OPENSHIFT_PYTHON_WSGI_APPLICATION)
setup.py Standard setup.py, specify deps here
.openshift/ Location for OpenShift specific files
action_hooks/ See the Action Hooks documentation
markers/ See the Markers section below
For more information on environment variables on OpenShift Online: https://developers.openshift.com/en/managing-environment-variables.html
Example that worked for me using the OPENSHIFT_PYTHON_WSGI_APPLICATION variable:
rhc env-set OPENSHIFT_PYTHON_WSGI_APPLICATION="${OPENSHIFT_REPO_DIR}/server/wsgi.py" --app MyApp

Django and live server, on rewrite, the old sources files are still used

I'm using Django 1.3 on an Apache server and mod_wsgi(daemon mode), with Nginx for serving static file. The database is on a separate server. The wsgi daemon runs on 2 threads with a maximum requests of 100.
I get in trouble when I override old .py files ... Not .pyc ... I'm also overriding the .wsgi config file (http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode). Sometimes, some server requests uses the old code, and therefore an error is generated (HTTP ERROR 500). Is there a server side cache that needs be emptied?
Can this be generated by the .pyc files? Do I need to restart the Apache server or the wsgi daemon?
If you remove the .pyc files and touch your wsgi files it should reload the wsgi daemon when it gets a chance, and you should be good.
On occasion I have had to restart apache in order to get my changes take affect.
Setup up ownership/permissions so that the user that code runs as under Apache cannot change code files nor create .pyc files. The user application runs as should only have ability to write to data or upload directories that it really needs to as is safer anyway.
The most reliable deployment method would be to install new version into a completely new directory hierarchy, with WSGI script file outside that tree. Then replace WSGI script file with new one referring to new directory. The WSGI script file in doing this should not be edited in place however, but a new file moved into place so filesystem does an atomic replace of the whole file and no risk of on the fly edit being picked up.

Django tutorial on remote server: how to view in my browser?

I'm getting started with a Django tutorial, and I've run into a snag. Having created the sample "mysite" on my usual domain, I want to be able to display it in my browser. The tutorial points me to http://127.0.0.1:8000. However, that's not going to work, as I'm doing this remotely.
[background information]
What I have done, apparently successfully, is
django-admin.py startproject mysite
(created mysite directory containing four files)
python manage.py runserver
(Validating models... 0 errors found, etc.)
The absolute path is
/home/toewsweb/public_html/pythonlab/mysite
What URL should I be able to use to bring this up in my browser?
I also put mysite at
/home/toewsweb/mysite (since it's not supposed to go in a publicly accessible directory)
What URL should I be able to use in this case?
This is a virtual private server, so I have access to httpd.conf. I have downloaded and installed mod_wsgi and have added it to the Apache configuration. I actually did set a subdomain with a DocumentRoot of /home/toewsweb/public_html/pythonlab/mysite; however, when I point the browser to that subdomain, I just get the directory listing.
[/background information]
Right now, I just want to know how to view what I'm working on in my browser.
Thanks!
For development purposes, there's no need to mess about with configuring WSGI (although it's useful to know, as you will need to do it for production). Just start the dev server so that it listens to an external port:
./manage.py runserver 0:8000
This binds to the external IP address, so now you can access your Django site via port 8000 on that server:
http://whatever.my.ip.is:8000
You need to setup the apache WSGIScriptAlias directive in your VirtualHost to properly load python and your site. Django's docs have a great explanation on what you need to do.
Basic configuration
Once you’ve got mod_wsgi installed and activated, edit your httpd.conf file and add:
WSGIScriptAlias / /path/to/mysite/apache/django.wsgi
The first bit above is the url you want to be serving your application at (/ indicates the root url), and the second is the location of a "WSGI file" -- see below -- on your system, usually inside of your project. This tells Apache to serve any request below the given URL using the WSGI application defined by that file.
Next we'll need to actually create this WSGI application, so create the file mentioned in the second part of WSGIScriptAlias and add:
import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
If your project is not on your PYTHONPATH by default you can add:
path = '/path/to/mysite'
if path not in sys.path:
sys.path.append(path)
just below the import sys line to place your project on the path. Remember to replace 'mysite.settings' with your correct settings file, and '/path/to/mysite' with your own project's location.
OR
The other option is to run the dev server so it's accesible externally like so:
python manage.py runserver 0.0.0.0:80
though please DO NOT use this in production. The dev server is single-threaded, and has not been auditing for security.